Merge and fix

This commit is contained in:
paulevsGitch 2020-11-08 15:07:19 +03:00
parent b91c85529d
commit ce9f4add97
548 changed files with 17517 additions and 16862 deletions

View file

@ -1,273 +1,273 @@
package ru.betterend.recipe.builders;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.registry.EndBlocks;
import ru.betterend.util.RecipeHelper;
public class AlloyingRecipe implements Recipe<Inventory> {
public final static String GROUP = "alloying";
public final static RecipeType<AlloyingRecipe> TYPE = EndRecipeManager.registerType(GROUP);
public final static Serializer SERIALIZER = EndRecipeManager.registerSerializer(GROUP, new Serializer());
public final static Identifier ID = BetterEnd.makeID(GROUP);
protected final RecipeType<?> type;
protected final Identifier id;
protected final Ingredient primaryInput;
protected final Ingredient secondaryInput;
protected final ItemStack output;
protected final String group;
protected final float experience;
protected final int smeltTime;
public AlloyingRecipe(Identifier id, String group, Ingredient primaryInput, Ingredient secondaryInput, ItemStack output, float experience, int smeltTime) {
this.group = group;
this.id = id;
this.primaryInput = primaryInput;
this.secondaryInput = secondaryInput;
this.output = output;
this.experience = experience;
this.smeltTime = smeltTime;
this.type = TYPE;
}
public float getExperience() {
return this.experience;
}
public int getSmeltTime() {
return this.smeltTime;
}
@Override
public DefaultedList<Ingredient> getPreviewInputs() {
DefaultedList<Ingredient> defaultedList = DefaultedList.of();
defaultedList.add(primaryInput);
defaultedList.add(secondaryInput);
return defaultedList;
}
@Override
public boolean matches(Inventory inv, World world) {
return this.primaryInput.test(inv.getStack(0)) && this.secondaryInput.test(inv.getStack(1)) ||
this.primaryInput.test(inv.getStack(1)) && this.secondaryInput.test(inv.getStack(0));
}
@Override
public ItemStack craft(Inventory inv) {
return this.output.copy();
}
@Override
public boolean fits(int width, int height) {
return true;
}
@Override
public ItemStack getOutput() {
return this.output;
}
@Override
public Identifier getId() {
return this.id;
}
@Override
public RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}
@Override
public RecipeType<?> getType() {
return this.type;
}
@Override
@Environment(EnvType.CLIENT)
public String getGroup() {
return this.group;
}
@Environment(EnvType.CLIENT)
public ItemStack getRecipeKindIcon() {
return new ItemStack(EndBlocks.END_STONE_SMELTER);
}
public static class Builder {
private final static Builder INSTANCE = new Builder();
public static Builder create(Identifier id) {
INSTANCE.id = id;
INSTANCE.group = String.format("%s_%s", GROUP, id);
INSTANCE.primaryInput = null;
INSTANCE.secondaryInput = null;
INSTANCE.output = null;
INSTANCE.experience = 0.0F;
INSTANCE.smeltTime = 350;
return INSTANCE;
}
public static Builder create(String id) {
return create(BetterEnd.makeID(id));
}
private Identifier id;
private Ingredient primaryInput;
private Ingredient secondaryInput;
private ItemStack output;
private String group;
private float experience;
private int smeltTime;
private boolean alright = true;
private Builder() {}
public Builder setGroup(String group) {
this.group = group;
return this;
}
public Builder setPrimaryInput(ItemConvertible... inputs) {
for (ItemConvertible item : inputs) {
this.alright &= RecipeHelper.exists(item);
}
this.primaryInput = Ingredient.ofItems(inputs);
return this;
}
public Builder setSecondaryInput(ItemConvertible... inputs) {
for (ItemConvertible item : inputs) {
this.alright &= RecipeHelper.exists(item);
}
this.secondaryInput = Ingredient.ofItems(inputs);
return this;
}
public Builder setPrimaryInput(Tag<Item> input) {
this.primaryInput = Ingredient.fromTag(input);
return this;
}
public Builder setSecondaryInput(Tag<Item> input) {
this.secondaryInput = Ingredient.fromTag(input);
return this;
}
public Builder setInput(ItemConvertible primaryInput, ItemConvertible secondaryInput) {
this.setPrimaryInput(primaryInput);
this.setSecondaryInput(secondaryInput);
return this;
}
public Builder setInput(Tag<Item> primaryInput, Tag<Item> secondaryInput) {
this.setPrimaryInput(primaryInput);
this.setSecondaryInput(secondaryInput);
return this;
}
public Builder setOutput(ItemConvertible output, int amount) {
this.alright &= RecipeHelper.exists(output);
this.output = new ItemStack(output, amount);
return this;
}
public Builder setExpiriense(float amount) {
this.experience = amount;
return this;
}
public Builder setSmeltTime(int time) {
this.smeltTime = time;
return this;
}
public void build() {
if (primaryInput == null) {
BetterEnd.LOGGER.warning("Primary input for Alloying recipe can't be 'null', recipe {} will be ignored!", id);
return;
}
if(secondaryInput == null) {
BetterEnd.LOGGER.warning("Secondary input for Alloying can't be 'null', recipe {} will be ignored!", id);
return;
}
if(output == null) {
BetterEnd.LOGGER.warning("Output for Alloying can't be 'null', recipe {} will be ignored!", id);
return;
}
if (EndRecipeManager.getRecipe(TYPE, id) != null) {
BetterEnd.LOGGER.warning("Can't add Alloying recipe! Id {} already exists!", id);
return;
}
if (!alright) {
BetterEnd.LOGGER.debug("Can't add Alloying recipe {}! Ingeredient or output not exists.", id);
return;
}
EndRecipeManager.addRecipe(TYPE, new AlloyingRecipe(id, group, primaryInput, secondaryInput, output, experience, smeltTime));
}
}
public static class Serializer implements RecipeSerializer<AlloyingRecipe> {
@Override
public AlloyingRecipe read(Identifier id, JsonObject json) {
JsonArray ingredients = JsonHelper.getArray(json, "ingredients");
Ingredient primaryInput = Ingredient.fromJson(ingredients.get(0));
Ingredient secondaryInput = Ingredient.fromJson(ingredients.get(1));
String resultStr = JsonHelper.getString(json, "result");
String group = JsonHelper.getString(json, "group", "");
Identifier resultId = new Identifier(resultStr);
ItemStack output = new ItemStack(Registry.ITEM.getOrEmpty(resultId).orElseThrow(() -> {
return new IllegalStateException("Item: " + resultStr + " does not exists!");
}));
float experience = JsonHelper.getFloat(json, "experience", 0.0F);
int smeltTime = JsonHelper.getInt(json, "smelttime", 350);
return new AlloyingRecipe(id, group, primaryInput, secondaryInput, output, experience, smeltTime);
}
@Override
public AlloyingRecipe read(Identifier id, PacketByteBuf packetBuffer) {
String group = packetBuffer.readString(32767);
Ingredient primary = Ingredient.fromPacket(packetBuffer);
Ingredient secondary = Ingredient.fromPacket(packetBuffer);
ItemStack output = packetBuffer.readItemStack();
float experience = packetBuffer.readFloat();
int smeltTime = packetBuffer.readVarInt();
return new AlloyingRecipe(id, group, primary, secondary, output, experience, smeltTime);
}
@Override
public void write(PacketByteBuf packetBuffer, AlloyingRecipe recipe) {
packetBuffer.writeString(recipe.group);
recipe.primaryInput.write(packetBuffer);
recipe.secondaryInput.write(packetBuffer);
packetBuffer.writeItemStack(recipe.output);
packetBuffer.writeFloat(recipe.experience);
packetBuffer.writeVarInt(recipe.smeltTime);
}
}
}
package ru.betterend.recipe.builders;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.registry.EndBlocks;
import ru.betterend.util.RecipeHelper;
public class AlloyingRecipe implements Recipe<Inventory> {
public final static String GROUP = "alloying";
public final static RecipeType<AlloyingRecipe> TYPE = EndRecipeManager.registerType(GROUP);
public final static Serializer SERIALIZER = EndRecipeManager.registerSerializer(GROUP, new Serializer());
public final static Identifier ID = BetterEnd.makeID(GROUP);
protected final RecipeType<?> type;
protected final Identifier id;
protected final Ingredient primaryInput;
protected final Ingredient secondaryInput;
protected final ItemStack output;
protected final String group;
protected final float experience;
protected final int smeltTime;
public AlloyingRecipe(Identifier id, String group, Ingredient primaryInput, Ingredient secondaryInput, ItemStack output, float experience, int smeltTime) {
this.group = group;
this.id = id;
this.primaryInput = primaryInput;
this.secondaryInput = secondaryInput;
this.output = output;
this.experience = experience;
this.smeltTime = smeltTime;
this.type = TYPE;
}
public float getExperience() {
return this.experience;
}
public int getSmeltTime() {
return this.smeltTime;
}
@Override
public DefaultedList<Ingredient> getPreviewInputs() {
DefaultedList<Ingredient> defaultedList = DefaultedList.of();
defaultedList.add(primaryInput);
defaultedList.add(secondaryInput);
return defaultedList;
}
@Override
public boolean matches(Inventory inv, World world) {
return this.primaryInput.test(inv.getStack(0)) && this.secondaryInput.test(inv.getStack(1)) ||
this.primaryInput.test(inv.getStack(1)) && this.secondaryInput.test(inv.getStack(0));
}
@Override
public ItemStack craft(Inventory inv) {
return this.output.copy();
}
@Override
public boolean fits(int width, int height) {
return true;
}
@Override
public ItemStack getOutput() {
return this.output;
}
@Override
public Identifier getId() {
return this.id;
}
@Override
public RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}
@Override
public RecipeType<?> getType() {
return this.type;
}
@Override
@Environment(EnvType.CLIENT)
public String getGroup() {
return this.group;
}
@Environment(EnvType.CLIENT)
public ItemStack getRecipeKindIcon() {
return new ItemStack(EndBlocks.END_STONE_SMELTER);
}
public static class Builder {
private final static Builder INSTANCE = new Builder();
public static Builder create(Identifier id) {
INSTANCE.id = id;
INSTANCE.group = String.format("%s_%s", GROUP, id);
INSTANCE.primaryInput = null;
INSTANCE.secondaryInput = null;
INSTANCE.output = null;
INSTANCE.experience = 0.0F;
INSTANCE.smeltTime = 350;
return INSTANCE;
}
public static Builder create(String id) {
return create(BetterEnd.makeID(id));
}
private Identifier id;
private Ingredient primaryInput;
private Ingredient secondaryInput;
private ItemStack output;
private String group;
private float experience;
private int smeltTime;
private boolean alright = true;
private Builder() {}
public Builder setGroup(String group) {
this.group = group;
return this;
}
public Builder setPrimaryInput(ItemConvertible... inputs) {
for (ItemConvertible item : inputs) {
this.alright &= RecipeHelper.exists(item);
}
this.primaryInput = Ingredient.ofItems(inputs);
return this;
}
public Builder setSecondaryInput(ItemConvertible... inputs) {
for (ItemConvertible item : inputs) {
this.alright &= RecipeHelper.exists(item);
}
this.secondaryInput = Ingredient.ofItems(inputs);
return this;
}
public Builder setPrimaryInput(Tag<Item> input) {
this.primaryInput = Ingredient.fromTag(input);
return this;
}
public Builder setSecondaryInput(Tag<Item> input) {
this.secondaryInput = Ingredient.fromTag(input);
return this;
}
public Builder setInput(ItemConvertible primaryInput, ItemConvertible secondaryInput) {
this.setPrimaryInput(primaryInput);
this.setSecondaryInput(secondaryInput);
return this;
}
public Builder setInput(Tag<Item> primaryInput, Tag<Item> secondaryInput) {
this.setPrimaryInput(primaryInput);
this.setSecondaryInput(secondaryInput);
return this;
}
public Builder setOutput(ItemConvertible output, int amount) {
this.alright &= RecipeHelper.exists(output);
this.output = new ItemStack(output, amount);
return this;
}
public Builder setExpiriense(float amount) {
this.experience = amount;
return this;
}
public Builder setSmeltTime(int time) {
this.smeltTime = time;
return this;
}
public void build() {
if (primaryInput == null) {
BetterEnd.LOGGER.warning("Primary input for Alloying recipe can't be 'null', recipe {} will be ignored!", id);
return;
}
if(secondaryInput == null) {
BetterEnd.LOGGER.warning("Secondary input for Alloying can't be 'null', recipe {} will be ignored!", id);
return;
}
if(output == null) {
BetterEnd.LOGGER.warning("Output for Alloying can't be 'null', recipe {} will be ignored!", id);
return;
}
if (EndRecipeManager.getRecipe(TYPE, id) != null) {
BetterEnd.LOGGER.warning("Can't add Alloying recipe! Id {} already exists!", id);
return;
}
if (!alright) {
BetterEnd.LOGGER.debug("Can't add Alloying recipe {}! Ingeredient or output not exists.", id);
return;
}
EndRecipeManager.addRecipe(TYPE, new AlloyingRecipe(id, group, primaryInput, secondaryInput, output, experience, smeltTime));
}
}
public static class Serializer implements RecipeSerializer<AlloyingRecipe> {
@Override
public AlloyingRecipe read(Identifier id, JsonObject json) {
JsonArray ingredients = JsonHelper.getArray(json, "ingredients");
Ingredient primaryInput = Ingredient.fromJson(ingredients.get(0));
Ingredient secondaryInput = Ingredient.fromJson(ingredients.get(1));
String resultStr = JsonHelper.getString(json, "result");
String group = JsonHelper.getString(json, "group", "");
Identifier resultId = new Identifier(resultStr);
ItemStack output = new ItemStack(Registry.ITEM.getOrEmpty(resultId).orElseThrow(() -> {
return new IllegalStateException("Item: " + resultStr + " does not exists!");
}));
float experience = JsonHelper.getFloat(json, "experience", 0.0F);
int smeltTime = JsonHelper.getInt(json, "smelttime", 350);
return new AlloyingRecipe(id, group, primaryInput, secondaryInput, output, experience, smeltTime);
}
@Override
public AlloyingRecipe read(Identifier id, PacketByteBuf packetBuffer) {
String group = packetBuffer.readString(32767);
Ingredient primary = Ingredient.fromPacket(packetBuffer);
Ingredient secondary = Ingredient.fromPacket(packetBuffer);
ItemStack output = packetBuffer.readItemStack();
float experience = packetBuffer.readFloat();
int smeltTime = packetBuffer.readVarInt();
return new AlloyingRecipe(id, group, primary, secondary, output, experience, smeltTime);
}
@Override
public void write(PacketByteBuf packetBuffer, AlloyingRecipe recipe) {
packetBuffer.writeString(recipe.group);
recipe.primaryInput.write(packetBuffer);
recipe.secondaryInput.write(packetBuffer);
packetBuffer.writeItemStack(recipe.output);
packetBuffer.writeFloat(recipe.experience);
packetBuffer.writeVarInt(recipe.smeltTime);
}
}
}

View file

@ -1,241 +1,241 @@
package ru.betterend.recipe.builders;
import com.google.gson.JsonObject;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ToolItem;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.registry.EndTags;
import ru.betterend.util.RecipeHelper;
public class AnvilSmithingRecipe implements Recipe<Inventory> {
public final static String GROUP = "smithing";
public final static RecipeType<AnvilSmithingRecipe> TYPE = EndRecipeManager.registerType(GROUP);
public final static Serializer SERIALIZER = EndRecipeManager.registerSerializer(GROUP, new Serializer());
public final static Identifier ID = BetterEnd.makeID(GROUP);
private final Identifier id;
private final Ingredient input;
private final ItemStack output;
private final int damage;
private final int level;
public AnvilSmithingRecipe(Identifier identifier, Ingredient input, ItemStack output, int level, int damage) {
this.id = identifier;
this.input = input;
this.output = output;
this.level = level;
this.damage = damage;
}
@Override
public RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}
@Override
public ItemStack getOutput() {
return this.output;
}
@Override
public boolean matches(Inventory craftingInventory, World world) {
return this.matches(craftingInventory);
}
@Override
public ItemStack craft(Inventory craftingInventory) {
return this.output.copy();
}
public ItemStack craft(Inventory craftingInventory, PlayerEntity player) {
if (!player.isCreative()) {
ItemStack hammer = craftingInventory.getStack(0);
int damage = hammer.getDamage() + this.damage;
if (damage >= hammer.getMaxDamage()) return ItemStack.EMPTY;
hammer.damage(this.damage, player, entity -> {
entity.sendEquipmentBreakStatus(null);
});
}
return this.craft(craftingInventory);
}
public boolean matches(Inventory craftingInventory) {
ItemStack hammer = craftingInventory.getStack(0);
if (hammer.isEmpty() || !EndTags.HAMMERS.contains(hammer.getItem())) {
return false;
}
int level = ((ToolItem) hammer.getItem()).getMaterial().getMiningLevel();
return level >= this.level && this.input.test(craftingInventory.getStack(1));
}
public int getDamage() {
return this.damage;
}
@Override
public DefaultedList<Ingredient> getPreviewInputs() {
DefaultedList<Ingredient> defaultedList = DefaultedList.of();
defaultedList.add(Ingredient.ofStacks(EndTags.HAMMERS.values().stream().filter(hammer -> {
return ((ToolItem) hammer).getMaterial().getMiningLevel() >= level;
}).map(ItemStack::new)));
defaultedList.add(input);
return defaultedList;
}
@Override
@Environment(EnvType.CLIENT)
public boolean fits(int width, int height) {
return true;
}
@Override
public Identifier getId() {
return this.id;
}
@Override
public RecipeType<?> getType() {
return TYPE;
}
@Override
public boolean isIgnoredInRecipeBook() {
return true;
}
public static class Builder {
private final static Builder INSTANCE = new Builder();
public static Builder create(String id) {
return create(BetterEnd.makeID(id));
}
public static Builder create(Identifier id) {
INSTANCE.id = id;
INSTANCE.input = null;
INSTANCE.output = null;
INSTANCE.level = 1;
INSTANCE.damage = 1;
return INSTANCE;
}
private Identifier id;
private Ingredient input;
private ItemStack output;
private int level = 1;
private int damage = 1;
private boolean alright = true;
private Builder() {}
public Builder setInput(ItemConvertible... inputItems) {
for (ItemConvertible item : inputItems) {
this.alright &= RecipeHelper.exists(item);
}
this.setInput(Ingredient.ofItems(inputItems));
return this;
}
public Builder setInput(Tag<Item> inputTag) {
this.setInput(Ingredient.fromTag(inputTag));
return this;
}
public Builder setInput(Ingredient ingredient) {
this.input = ingredient;
return this;
}
public Builder setOutput(ItemConvertible output, int amount) {
this.alright &= RecipeHelper.exists(output);
this.output = new ItemStack(output, amount);
return this;
}
public Builder setLevel(int level) {
this.level = level;
return this;
}
public Builder setDamage(int damage) {
this.damage = damage;
return this;
}
public void build() {
if (input == null) {
BetterEnd.LOGGER.warning("Input for Smithing recipe can't be 'null', recipe {} will be ignored!", id);
return;
}
if(output == null) {
BetterEnd.LOGGER.warning("Output for Smithing recipe can't be 'null', recipe {} will be ignored!", id);
return;
}
if (EndRecipeManager.getRecipe(TYPE, id) != null) {
BetterEnd.LOGGER.warning("Can't add Smithing recipe! Id {} already exists!", id);
return;
}
if (!alright) {
BetterEnd.LOGGER.debug("Can't add Smithing recipe {}! Ingeredient or output not exists.", id);
return;
}
EndRecipeManager.addRecipe(TYPE, new AnvilSmithingRecipe(id, input, output, level, damage));
}
}
public static class Serializer implements RecipeSerializer<AnvilSmithingRecipe> {
@Override
public AnvilSmithingRecipe read(Identifier id, JsonObject json) {
Ingredient input = Ingredient.fromJson(JsonHelper.getObject(json, "input"));
String resultStr = JsonHelper.getString(json, "result");
Identifier resultId = new Identifier(resultStr);
ItemStack output = new ItemStack(Registry.ITEM.getOrEmpty(resultId).orElseThrow(() -> {
return new IllegalStateException("Item: " + resultStr + " does not exists!");
}));
int level = JsonHelper.getInt(json, "level", 1);
int damage = JsonHelper.getInt(json, "damage", 1);
return new AnvilSmithingRecipe(id, input, output, level, damage);
}
@Override
public AnvilSmithingRecipe read(Identifier id, PacketByteBuf packetBuffer) {
Ingredient input = Ingredient.fromPacket(packetBuffer);
ItemStack output = packetBuffer.readItemStack();
int level = packetBuffer.readVarInt();
int damage = packetBuffer.readVarInt();
return new AnvilSmithingRecipe(id, input, output, level, damage);
}
@Override
public void write(PacketByteBuf packetBuffer, AnvilSmithingRecipe recipe) {
recipe.input.write(packetBuffer);
packetBuffer.writeItemStack(recipe.output);
packetBuffer.writeVarInt(recipe.level);
packetBuffer.writeVarInt(recipe.damage);
}
}
}
package ru.betterend.recipe.builders;
import com.google.gson.JsonObject;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ToolItem;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.registry.EndTags;
import ru.betterend.util.RecipeHelper;
public class AnvilSmithingRecipe implements Recipe<Inventory> {
public final static String GROUP = "smithing";
public final static RecipeType<AnvilSmithingRecipe> TYPE = EndRecipeManager.registerType(GROUP);
public final static Serializer SERIALIZER = EndRecipeManager.registerSerializer(GROUP, new Serializer());
public final static Identifier ID = BetterEnd.makeID(GROUP);
private final Identifier id;
private final Ingredient input;
private final ItemStack output;
private final int damage;
private final int level;
public AnvilSmithingRecipe(Identifier identifier, Ingredient input, ItemStack output, int level, int damage) {
this.id = identifier;
this.input = input;
this.output = output;
this.level = level;
this.damage = damage;
}
@Override
public RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}
@Override
public ItemStack getOutput() {
return this.output;
}
@Override
public boolean matches(Inventory craftingInventory, World world) {
return this.matches(craftingInventory);
}
@Override
public ItemStack craft(Inventory craftingInventory) {
return this.output.copy();
}
public ItemStack craft(Inventory craftingInventory, PlayerEntity player) {
if (!player.isCreative()) {
ItemStack hammer = craftingInventory.getStack(0);
int damage = hammer.getDamage() + this.damage;
if (damage >= hammer.getMaxDamage()) return ItemStack.EMPTY;
hammer.damage(this.damage, player, entity -> {
entity.sendEquipmentBreakStatus(null);
});
}
return this.craft(craftingInventory);
}
public boolean matches(Inventory craftingInventory) {
ItemStack hammer = craftingInventory.getStack(0);
if (hammer.isEmpty() || !EndTags.HAMMERS.contains(hammer.getItem())) {
return false;
}
int level = ((ToolItem) hammer.getItem()).getMaterial().getMiningLevel();
return level >= this.level && this.input.test(craftingInventory.getStack(1));
}
public int getDamage() {
return this.damage;
}
@Override
public DefaultedList<Ingredient> getPreviewInputs() {
DefaultedList<Ingredient> defaultedList = DefaultedList.of();
defaultedList.add(Ingredient.ofStacks(EndTags.HAMMERS.values().stream().filter(hammer -> {
return ((ToolItem) hammer).getMaterial().getMiningLevel() >= level;
}).map(ItemStack::new)));
defaultedList.add(input);
return defaultedList;
}
@Override
@Environment(EnvType.CLIENT)
public boolean fits(int width, int height) {
return true;
}
@Override
public Identifier getId() {
return this.id;
}
@Override
public RecipeType<?> getType() {
return TYPE;
}
@Override
public boolean isIgnoredInRecipeBook() {
return true;
}
public static class Builder {
private final static Builder INSTANCE = new Builder();
public static Builder create(String id) {
return create(BetterEnd.makeID(id));
}
public static Builder create(Identifier id) {
INSTANCE.id = id;
INSTANCE.input = null;
INSTANCE.output = null;
INSTANCE.level = 1;
INSTANCE.damage = 1;
return INSTANCE;
}
private Identifier id;
private Ingredient input;
private ItemStack output;
private int level = 1;
private int damage = 1;
private boolean alright = true;
private Builder() {}
public Builder setInput(ItemConvertible... inputItems) {
for (ItemConvertible item : inputItems) {
this.alright &= RecipeHelper.exists(item);
}
this.setInput(Ingredient.ofItems(inputItems));
return this;
}
public Builder setInput(Tag<Item> inputTag) {
this.setInput(Ingredient.fromTag(inputTag));
return this;
}
public Builder setInput(Ingredient ingredient) {
this.input = ingredient;
return this;
}
public Builder setOutput(ItemConvertible output, int amount) {
this.alright &= RecipeHelper.exists(output);
this.output = new ItemStack(output, amount);
return this;
}
public Builder setLevel(int level) {
this.level = level;
return this;
}
public Builder setDamage(int damage) {
this.damage = damage;
return this;
}
public void build() {
if (input == null) {
BetterEnd.LOGGER.warning("Input for Smithing recipe can't be 'null', recipe {} will be ignored!", id);
return;
}
if(output == null) {
BetterEnd.LOGGER.warning("Output for Smithing recipe can't be 'null', recipe {} will be ignored!", id);
return;
}
if (EndRecipeManager.getRecipe(TYPE, id) != null) {
BetterEnd.LOGGER.warning("Can't add Smithing recipe! Id {} already exists!", id);
return;
}
if (!alright) {
BetterEnd.LOGGER.debug("Can't add Smithing recipe {}! Ingeredient or output not exists.", id);
return;
}
EndRecipeManager.addRecipe(TYPE, new AnvilSmithingRecipe(id, input, output, level, damage));
}
}
public static class Serializer implements RecipeSerializer<AnvilSmithingRecipe> {
@Override
public AnvilSmithingRecipe read(Identifier id, JsonObject json) {
Ingredient input = Ingredient.fromJson(JsonHelper.getObject(json, "input"));
String resultStr = JsonHelper.getString(json, "result");
Identifier resultId = new Identifier(resultStr);
ItemStack output = new ItemStack(Registry.ITEM.getOrEmpty(resultId).orElseThrow(() -> {
return new IllegalStateException("Item: " + resultStr + " does not exists!");
}));
int level = JsonHelper.getInt(json, "level", 1);
int damage = JsonHelper.getInt(json, "damage", 1);
return new AnvilSmithingRecipe(id, input, output, level, damage);
}
@Override
public AnvilSmithingRecipe read(Identifier id, PacketByteBuf packetBuffer) {
Ingredient input = Ingredient.fromPacket(packetBuffer);
ItemStack output = packetBuffer.readItemStack();
int level = packetBuffer.readVarInt();
int damage = packetBuffer.readVarInt();
return new AnvilSmithingRecipe(id, input, output, level, damage);
}
@Override
public void write(PacketByteBuf packetBuffer, AnvilSmithingRecipe recipe) {
recipe.input.write(packetBuffer);
packetBuffer.writeItemStack(recipe.output);
packetBuffer.writeVarInt(recipe.level);
packetBuffer.writeVarInt(recipe.damage);
}
}
}

View file

@ -1,253 +1,253 @@
package ru.betterend.recipe.builders;
import java.util.Arrays;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import ru.betterend.BetterEnd;
import ru.betterend.interfaces.CompoundSerializer;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.rituals.InfusionRitual;
public class InfusionRecipe implements Recipe<InfusionRitual> {
public final static String GROUP = "infusion";
public final static RecipeType<InfusionRecipe> TYPE = EndRecipeManager.registerType(GROUP);
public final static Serializer SERIALIZER = EndRecipeManager.registerSerializer(GROUP, new Serializer());
public final static Identifier ID = BetterEnd.makeID(GROUP);
private final Identifier id;
private Ingredient input;
private ItemStack output;
private int time = 1;
private Ingredient[] catalysts = new Ingredient[8];
private InfusionRecipe(Identifier id) {
this(id, null, null);
}
private InfusionRecipe(Identifier id, Ingredient input, ItemStack output) {
this.id = id;
this.input = input;
this.output = output;
Arrays.fill(catalysts, Ingredient.EMPTY);
}
public int getInfusionTime() {
return this.time;
}
@Override
public boolean matches(InfusionRitual inv, World world) {
boolean valid = this.input.test(inv.getStack(0));
if (!valid) return false;
for (int i = 1; i < 9; i++) {
valid &= this.catalysts[i].test(inv.getStack(i));
}
return valid;
}
@Override
public ItemStack craft(InfusionRitual ritual) {
return this.output.copy();
}
@Override
public boolean fits(int width, int height) {
return true;
}
@Override
public ItemStack getOutput() {
return this.output;
}
@Override
public Identifier getId() {
return this.id;
}
@Override
public RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}
@Override
public RecipeType<?> getType() {
return TYPE;
}
public InfusionRecipe fromTag(CompoundTag tag) {
return SERIALIZER.fromTag(tag);
}
public CompoundTag toTag(CompoundTag tag) {
return SERIALIZER.toTag(this, tag);
}
public static class Builder {
private final static Builder INSTANCE = new Builder();
public static Builder create(String id) {
return create(BetterEnd.makeID(id));
}
public static Builder create(Identifier id) {
INSTANCE.id = id;
INSTANCE.input = null;
INSTANCE.output = null;
INSTANCE.time = 1;
Arrays.fill(INSTANCE.catalysts, Ingredient.EMPTY);
return INSTANCE;
}
private Identifier id;
private Ingredient input;
private ItemStack output;
private int time = 1;
private Ingredient[] catalysts = new Ingredient[8];
private Builder() {
Arrays.fill(catalysts, Ingredient.EMPTY);
}
public Builder setInput(ItemConvertible input) {
this.input = Ingredient.ofItems(input);
return this;
}
public Builder setOutput(ItemStack output) {
this.output = output;
this.output.setCount(1);
return this;
}
public Builder setTime(int time) {
this.time = time;
return this;
}
public Builder addCatalyst(int slot, ItemConvertible item) {
if (slot > 7) return this;
this.catalysts[slot] = Ingredient.ofItems(item);
return this;
}
public void build() {
if (input == null) {
BetterEnd.LOGGER.warning("Input for Infusion recipe can't be 'null', recipe {} will be ignored!", id);
return;
}
if (output == null) {
BetterEnd.LOGGER.warning("Output for Infusion recipe can't be 'null', recipe {} will be ignored!", id);
return;
}
InfusionRecipe recipe = new InfusionRecipe(id, input, output);
recipe.time = time;
int empty = 0;
for (int i = 0; i < catalysts.length; i++) {
if (catalysts[i].isEmpty()) empty++;
else recipe.catalysts[i] = catalysts[i];
}
if (empty == catalysts.length) {
BetterEnd.LOGGER.warning("At least one catalyst must be non empty, recipe {} will be ignored!", id);
return;
}
EndRecipeManager.addRecipe(TYPE, recipe);
}
}
public static class Serializer implements RecipeSerializer<InfusionRecipe> {
@Override
public InfusionRecipe read(Identifier id, JsonObject json) {
InfusionRecipe recipe = new InfusionRecipe(id);
recipe.input = Ingredient.fromJson(json.get("input"));
Identifier outId = new Identifier(JsonHelper.getString(json, "output"));
recipe.output = new ItemStack(Registry.ITEM.getOrEmpty(outId).orElseThrow(() -> {
return new IllegalStateException("Item: " + outId + " does not exists!");
}));
recipe.time = JsonHelper.getInt(json, "time", 1);
JsonArray catalysts = JsonHelper.asArray(json, "catalysts");
for (int i = 0; i < catalysts.size(); i++) {
ItemStack stack = new ItemStack(Registry.ITEM.getOrEmpty(outId).orElse(null));
recipe.catalysts[i] = Ingredient.ofStacks(
Arrays.stream(new ItemStack[] { stack }));
}
return recipe;
}
@Override
public InfusionRecipe read(Identifier id, PacketByteBuf buffer) {
InfusionRecipe recipe = new InfusionRecipe(id);
recipe.input = Ingredient.fromPacket(buffer);
recipe.output = buffer.readItemStack();
recipe.time = buffer.readVarInt();
for (int i = 0; i < 9; i++) {
recipe.catalysts[i] = Ingredient.fromPacket(buffer);
}
return recipe;
}
@Override
public void write(PacketByteBuf buffer, InfusionRecipe recipe) {
recipe.input.write(buffer);
buffer.writeItemStack(recipe.output);
buffer.writeVarInt(recipe.time);
for (int i = 0; i < 9; i++) {
recipe.catalysts[i].write(buffer);
}
}
public InfusionRecipe fromTag(CompoundTag tag) {
Identifier id = new Identifier(tag.getString("id"));
InfusionRecipe recipe = new InfusionRecipe(id);
CompoundSerializer<Ingredient> inputSerializer = this.toSerializer(recipe.input);
recipe.input = inputSerializer.fromTag(tag.getCompound("input"));
recipe.output = ItemStack.fromTag(tag.getCompound("output"));
recipe.time = tag.getInt("time");
CompoundTag catalysts = tag.getCompound("catalysts");
for(int i = 0; i < recipe.catalysts.length; i++) {
String key = Integer.toString(i);
CompoundSerializer<Ingredient> cataSerializer = this.toSerializer(recipe.catalysts[i]);
recipe.catalysts[i] = cataSerializer.fromTag(catalysts.getCompound(key));
}
return recipe;
}
public CompoundTag toTag(InfusionRecipe recipe, CompoundTag tag) {
CompoundSerializer<?> inputSerializer = this.toSerializer(recipe.input);
tag.put("input", inputSerializer.toTag(new CompoundTag()));
tag.put("output", recipe.output.toTag(new CompoundTag()));
tag.putInt("time", recipe.time);
CompoundTag catalysts = new CompoundTag();
for(int i = 0; i < recipe.catalysts.length; i++) {
String key = Integer.toString(i);
CompoundSerializer<?> cataSerializer = this.toSerializer(recipe.catalysts[i]);
catalysts.put(key, cataSerializer.toTag(new CompoundTag()));
}
tag.put("catalysts", catalysts);
return tag;
}
@SuppressWarnings("unchecked")
private CompoundSerializer<Ingredient> toSerializer(Ingredient ingredient) {
return CompoundSerializer.class.cast(ingredient);
}
}
}
package ru.betterend.recipe.builders;
import java.util.Arrays;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import ru.betterend.BetterEnd;
import ru.betterend.interfaces.CompoundSerializer;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.rituals.InfusionRitual;
public class InfusionRecipe implements Recipe<InfusionRitual> {
public final static String GROUP = "infusion";
public final static RecipeType<InfusionRecipe> TYPE = EndRecipeManager.registerType(GROUP);
public final static Serializer SERIALIZER = EndRecipeManager.registerSerializer(GROUP, new Serializer());
public final static Identifier ID = BetterEnd.makeID(GROUP);
private final Identifier id;
private Ingredient input;
private ItemStack output;
private int time = 1;
private Ingredient[] catalysts = new Ingredient[8];
private InfusionRecipe(Identifier id) {
this(id, null, null);
}
private InfusionRecipe(Identifier id, Ingredient input, ItemStack output) {
this.id = id;
this.input = input;
this.output = output;
Arrays.fill(catalysts, Ingredient.EMPTY);
}
public int getInfusionTime() {
return this.time;
}
@Override
public boolean matches(InfusionRitual inv, World world) {
boolean valid = this.input.test(inv.getStack(0));
if (!valid) return false;
for (int i = 1; i < 9; i++) {
valid &= this.catalysts[i].test(inv.getStack(i));
}
return valid;
}
@Override
public ItemStack craft(InfusionRitual ritual) {
return this.output.copy();
}
@Override
public boolean fits(int width, int height) {
return true;
}
@Override
public ItemStack getOutput() {
return this.output;
}
@Override
public Identifier getId() {
return this.id;
}
@Override
public RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}
@Override
public RecipeType<?> getType() {
return TYPE;
}
public InfusionRecipe fromTag(CompoundTag tag) {
return SERIALIZER.fromTag(tag);
}
public CompoundTag toTag(CompoundTag tag) {
return SERIALIZER.toTag(this, tag);
}
public static class Builder {
private final static Builder INSTANCE = new Builder();
public static Builder create(String id) {
return create(BetterEnd.makeID(id));
}
public static Builder create(Identifier id) {
INSTANCE.id = id;
INSTANCE.input = null;
INSTANCE.output = null;
INSTANCE.time = 1;
Arrays.fill(INSTANCE.catalysts, Ingredient.EMPTY);
return INSTANCE;
}
private Identifier id;
private Ingredient input;
private ItemStack output;
private int time = 1;
private Ingredient[] catalysts = new Ingredient[8];
private Builder() {
Arrays.fill(catalysts, Ingredient.EMPTY);
}
public Builder setInput(ItemConvertible input) {
this.input = Ingredient.ofItems(input);
return this;
}
public Builder setOutput(ItemStack output) {
this.output = output;
this.output.setCount(1);
return this;
}
public Builder setTime(int time) {
this.time = time;
return this;
}
public Builder addCatalyst(int slot, ItemConvertible item) {
if (slot > 7) return this;
this.catalysts[slot] = Ingredient.ofItems(item);
return this;
}
public void build() {
if (input == null) {
BetterEnd.LOGGER.warning("Input for Infusion recipe can't be 'null', recipe {} will be ignored!", id);
return;
}
if (output == null) {
BetterEnd.LOGGER.warning("Output for Infusion recipe can't be 'null', recipe {} will be ignored!", id);
return;
}
InfusionRecipe recipe = new InfusionRecipe(id, input, output);
recipe.time = time;
int empty = 0;
for (int i = 0; i < catalysts.length; i++) {
if (catalysts[i].isEmpty()) empty++;
else recipe.catalysts[i] = catalysts[i];
}
if (empty == catalysts.length) {
BetterEnd.LOGGER.warning("At least one catalyst must be non empty, recipe {} will be ignored!", id);
return;
}
EndRecipeManager.addRecipe(TYPE, recipe);
}
}
public static class Serializer implements RecipeSerializer<InfusionRecipe> {
@Override
public InfusionRecipe read(Identifier id, JsonObject json) {
InfusionRecipe recipe = new InfusionRecipe(id);
recipe.input = Ingredient.fromJson(json.get("input"));
Identifier outId = new Identifier(JsonHelper.getString(json, "output"));
recipe.output = new ItemStack(Registry.ITEM.getOrEmpty(outId).orElseThrow(() -> {
return new IllegalStateException("Item: " + outId + " does not exists!");
}));
recipe.time = JsonHelper.getInt(json, "time", 1);
JsonArray catalysts = JsonHelper.asArray(json, "catalysts");
for (int i = 0; i < catalysts.size(); i++) {
ItemStack stack = new ItemStack(Registry.ITEM.getOrEmpty(outId).orElse(null));
recipe.catalysts[i] = Ingredient.ofStacks(
Arrays.stream(new ItemStack[] { stack }));
}
return recipe;
}
@Override
public InfusionRecipe read(Identifier id, PacketByteBuf buffer) {
InfusionRecipe recipe = new InfusionRecipe(id);
recipe.input = Ingredient.fromPacket(buffer);
recipe.output = buffer.readItemStack();
recipe.time = buffer.readVarInt();
for (int i = 0; i < 9; i++) {
recipe.catalysts[i] = Ingredient.fromPacket(buffer);
}
return recipe;
}
@Override
public void write(PacketByteBuf buffer, InfusionRecipe recipe) {
recipe.input.write(buffer);
buffer.writeItemStack(recipe.output);
buffer.writeVarInt(recipe.time);
for (int i = 0; i < 9; i++) {
recipe.catalysts[i].write(buffer);
}
}
public InfusionRecipe fromTag(CompoundTag tag) {
Identifier id = new Identifier(tag.getString("id"));
InfusionRecipe recipe = new InfusionRecipe(id);
CompoundSerializer<Ingredient> inputSerializer = this.toSerializer(recipe.input);
recipe.input = inputSerializer.fromTag(tag.getCompound("input"));
recipe.output = ItemStack.fromTag(tag.getCompound("output"));
recipe.time = tag.getInt("time");
CompoundTag catalysts = tag.getCompound("catalysts");
for(int i = 0; i < recipe.catalysts.length; i++) {
String key = Integer.toString(i);
CompoundSerializer<Ingredient> cataSerializer = this.toSerializer(recipe.catalysts[i]);
recipe.catalysts[i] = cataSerializer.fromTag(catalysts.getCompound(key));
}
return recipe;
}
public CompoundTag toTag(InfusionRecipe recipe, CompoundTag tag) {
CompoundSerializer<?> inputSerializer = this.toSerializer(recipe.input);
tag.put("input", inputSerializer.toTag(new CompoundTag()));
tag.put("output", recipe.output.toTag(new CompoundTag()));
tag.putInt("time", recipe.time);
CompoundTag catalysts = new CompoundTag();
for(int i = 0; i < recipe.catalysts.length; i++) {
String key = Integer.toString(i);
CompoundSerializer<?> cataSerializer = this.toSerializer(recipe.catalysts[i]);
catalysts.put(key, cataSerializer.toTag(new CompoundTag()));
}
tag.put("catalysts", catalysts);
return tag;
}
@SuppressWarnings("unchecked")
private CompoundSerializer<Ingredient> toSerializer(Ingredient ingredient) {
return CompoundSerializer.class.cast(ingredient);
}
}
}