271 lines
7.7 KiB
Java
271 lines
7.7 KiB
Java
package ru.betterend.recipe.builders;
|
|
|
|
import java.util.Objects;
|
|
|
|
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.world.World;
|
|
import ru.betterend.BetterEnd;
|
|
import ru.betterend.config.Configs;
|
|
import ru.betterend.interfaces.BetterEndRecipe;
|
|
import ru.betterend.recipe.EndRecipeManager;
|
|
import ru.betterend.registry.EndTags;
|
|
import ru.betterend.util.ItemUtil;
|
|
import ru.betterend.util.RecipeHelper;
|
|
|
|
public class AnvilRecipe implements Recipe<Inventory>, BetterEndRecipe {
|
|
|
|
public final static String GROUP = "smithing";
|
|
public final static RecipeType<AnvilRecipe> 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 AnvilRecipe(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()) {
|
|
if (!checkHammerDurability(craftingInventory, player)) return ItemStack.EMPTY;
|
|
ItemStack hammer = craftingInventory.getStack(1);
|
|
hammer.damage(this.damage, player, entity -> {
|
|
entity.sendEquipmentBreakStatus(null);
|
|
});
|
|
}
|
|
return this.craft(craftingInventory);
|
|
}
|
|
|
|
public boolean checkHammerDurability(Inventory craftingInventory, PlayerEntity player) {
|
|
if (player.isCreative()) return true;
|
|
ItemStack hammer = craftingInventory.getStack(1);
|
|
int damage = hammer.getDamage() + this.damage;
|
|
return damage < hammer.getMaxDamage();
|
|
}
|
|
|
|
public boolean matches(Inventory craftingInventory) {
|
|
ItemStack hammer = craftingInventory.getStack(1);
|
|
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(0));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (this == o) return true;
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
AnvilRecipe that = (AnvilRecipe) o;
|
|
return damage == that.damage && level == that.level && id.equals(that.id) && input.equals(that.input) && output.equals(that.output);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(id, input, output, damage, level);
|
|
}
|
|
|
|
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;
|
|
INSTANCE.alright = true;
|
|
|
|
return INSTANCE;
|
|
}
|
|
|
|
private Identifier id;
|
|
private Ingredient input;
|
|
private ItemStack output;
|
|
private int level = 1;
|
|
private int damage = 1;
|
|
private boolean alright;
|
|
|
|
private Builder() {}
|
|
|
|
public Builder setInput(ItemConvertible... inputItems) {
|
|
this.alright &= RecipeHelper.exists(inputItems);
|
|
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) {
|
|
return this.setOutput(output, 1);
|
|
}
|
|
|
|
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 (Configs.RECIPE_CONFIG.getBoolean("anvil", id.getPath(), true)) {
|
|
if (input == null) {
|
|
BetterEnd.LOGGER.warning("Input for Anvil recipe can't be 'null', recipe {} will be ignored!", id);
|
|
return;
|
|
}
|
|
if(output == null) {
|
|
BetterEnd.LOGGER.warning("Output for Anvil recipe can't be 'null', recipe {} will be ignored!", id);
|
|
return;
|
|
}
|
|
if (EndRecipeManager.getRecipe(TYPE, id) != null) {
|
|
BetterEnd.LOGGER.warning("Can't add Anvil recipe! Id {} already exists!", id);
|
|
return;
|
|
}
|
|
if (!alright) {
|
|
BetterEnd.LOGGER.debug("Can't add Anvil recipe {}! Ingeredient or output not exists.", id);
|
|
return;
|
|
}
|
|
EndRecipeManager.addRecipe(TYPE, new AnvilRecipe(id, input, output, level, damage));
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class Serializer implements RecipeSerializer<AnvilRecipe> {
|
|
@Override
|
|
public AnvilRecipe read(Identifier id, JsonObject json) {
|
|
Ingredient input = Ingredient.fromJson(json.get("input"));
|
|
JsonObject result = JsonHelper.getObject(json, "result");
|
|
ItemStack output = ItemUtil.fromJsonRecipe(result);
|
|
if (output == null) {
|
|
throw new IllegalStateException("Output item does not exists!");
|
|
}
|
|
int level = JsonHelper.getInt(json, "level", 1);
|
|
int damage = JsonHelper.getInt(json, "damage", 1);
|
|
|
|
return new AnvilRecipe(id, input, output, level, damage);
|
|
}
|
|
|
|
@Override
|
|
public AnvilRecipe read(Identifier id, PacketByteBuf packetBuffer) {
|
|
Ingredient input = Ingredient.fromPacket(packetBuffer);
|
|
ItemStack output = packetBuffer.readItemStack();
|
|
int level = packetBuffer.readVarInt();
|
|
int damage = packetBuffer.readVarInt();
|
|
|
|
return new AnvilRecipe(id, input, output, level, damage);
|
|
}
|
|
|
|
@Override
|
|
public void write(PacketByteBuf packetBuffer, AnvilRecipe recipe) {
|
|
recipe.input.write(packetBuffer);
|
|
packetBuffer.writeItemStack(recipe.output);
|
|
packetBuffer.writeVarInt(recipe.level);
|
|
packetBuffer.writeVarInt(recipe.damage);
|
|
}
|
|
|
|
|
|
}
|
|
}
|