Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/data/RecipeAnvil.java
T

152 lines
5.5 KiB
Java
Raw Normal View History

2025-02-09 20:49:45 -05:00
package noppes.npcs.controllers.data;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.NoppesUtilPlayer;
import noppes.npcs.NoppesUtilServer;
2025-02-10 02:44:28 -05:00
import noppes.npcs.api.handler.data.IAnvilRecipe;
2025-02-09 20:49:45 -05:00
public class RecipeAnvil implements IAnvilRecipe {
public int id = -1;
public String name = "";
public Availability availability = new Availability();
2025-02-10 02:44:28 -05:00
public boolean ignoreRepairItemNBT = false;
public boolean ignoreRepairMaterialNBT = false;
public boolean ignoreRepairMaterialDamage = false;
2025-02-09 20:49:45 -05:00
public ItemStack itemToRepair;
public ItemStack repairMaterial;
2025-02-10 02:44:28 -05:00
public int xpCost;
public float repairPercentage;
2025-02-09 20:49:45 -05:00
private boolean isAnvil = true;
2025-03-13 17:14:45 -04:00
public RecipeAnvil() {
}
2025-02-09 20:49:45 -05:00
public RecipeAnvil(String name, ItemStack itemToRepair, ItemStack repairMaterial, int xpCost, float repairPercentage) {
this.name = name;
this.itemToRepair = itemToRepair;
this.repairMaterial = repairMaterial;
this.xpCost = xpCost;
this.repairPercentage = repairPercentage;
}
public static RecipeAnvil read(NBTTagCompound compound) {
RecipeAnvil recipe = new RecipeAnvil();
recipe.id = compound.getInteger("ID");
recipe.name = compound.getString("Name");
recipe.availability.readFromNBT(compound.getCompoundTag("Availability"));
recipe.xpCost = compound.getInteger("XPCost");
recipe.repairPercentage = compound.getFloat("RepairPercentage");
recipe.itemToRepair = NoppesUtilServer.readItem(compound.getCompoundTag("ItemToRepair"));
recipe.repairMaterial = NoppesUtilServer.readItem(compound.getCompoundTag("RepairMaterial"));
2025-02-10 02:44:28 -05:00
recipe.ignoreRepairMaterialNBT = compound.getBoolean("IgnoreRepairMatNBT");
recipe.ignoreRepairItemNBT = compound.getBoolean("IgnoreRepairItemNBT");
recipe.ignoreRepairMaterialDamage = compound.getBoolean("IgnoreRepairMatDamage");
2025-02-09 20:49:45 -05:00
recipe.isAnvil = true;
return recipe;
}
public NBTTagCompound writeNBT() {
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("ID", id);
compound.setString("Name", name);
compound.setTag("Availability", availability.writeToNBT(new NBTTagCompound()));
compound.setInteger("XPCost", xpCost);
compound.setFloat("RepairPercentage", repairPercentage);
2025-03-13 17:14:45 -04:00
if (itemToRepair != null) {
2025-02-10 02:44:28 -05:00
compound.setTag("ItemToRepair", NoppesUtilServer.writeItem(itemToRepair, new NBTTagCompound()));
}
2025-03-13 17:14:45 -04:00
if (repairMaterial != null) {
2025-02-10 02:44:28 -05:00
compound.setTag("RepairMaterial", NoppesUtilServer.writeItem(repairMaterial, new NBTTagCompound()));
}
compound.setBoolean("IgnoreRepairMatNBT", ignoreRepairMaterialNBT);
compound.setBoolean("IgnoreRepairItemNBT", ignoreRepairItemNBT);
compound.setBoolean("IgnoreRepairMatDamage", ignoreRepairMaterialDamage);
2025-02-09 20:49:45 -05:00
compound.setBoolean("IsAnvil", true);
return compound;
}
2025-02-10 02:44:28 -05:00
@Override
2025-02-09 20:49:45 -05:00
public int getXpCost() {
return xpCost;
}
@Override
public float getRepairPercentage() {
return repairPercentage;
}
@Override
public boolean matches(ItemStack inputItem, ItemStack inputRepairMaterial) {
if (inputItem == null || inputRepairMaterial == null) return false;
2025-02-10 02:44:28 -05:00
if (!NoppesUtilPlayer.compareItems(this.itemToRepair, inputItem, true, this.ignoreRepairItemNBT)) {
2025-02-09 20:49:45 -05:00
return false;
}
2025-02-10 02:44:28 -05:00
if (!NoppesUtilPlayer.compareItems(this.repairMaterial, inputRepairMaterial, this.ignoreRepairMaterialDamage, this.ignoreRepairMaterialNBT)) {
2025-02-09 20:49:45 -05:00
return false;
}
return true;
}
@Override
public ItemStack getResult(ItemStack inputItem) {
if (inputItem == null)
return null;
ItemStack result = inputItem.copy();
if (!result.isItemStackDamageable())
return result;
int maxDamage = result.getMaxDamage();
int repairAmount = (int) (maxDamage * repairPercentage);
int currentDamage = result.getItemDamage();
int newDamage = currentDamage - repairAmount;
if (newDamage < 0) newDamage = 0;
result.setItemDamage(newDamage);
return result;
}
@Override
public String getName() {
return name;
}
@Override
public int getID() {
return this.id;
}
2025-02-10 02:44:28 -05:00
public static RecipeAnvil saveRecipe(RecipeAnvil recipe, ItemStack output, ItemStack repairMaterial) {
if (output != null) {
recipe.itemToRepair = output.copy();
if (recipe.itemToRepair.isItemStackDamageable()) {
recipe.itemToRepair.setItemDamage(0);
}
} else {
recipe.itemToRepair = null;
}
recipe.repairMaterial = (repairMaterial == null ? null : repairMaterial.copy());
return recipe;
}
public boolean isValid() {
return itemToRepair != null && repairMaterial != null;
}
/**
* Creates and returns a deep copy of this RecipeAnvil.
*/
public void copy(RecipeAnvil recipe) {
this.id = recipe.id;
this.name = recipe.name;
this.availability = recipe.availability;
this.ignoreRepairMaterialDamage = recipe.ignoreRepairMaterialDamage;
this.ignoreRepairItemNBT = recipe.ignoreRepairItemNBT;
this.ignoreRepairMaterialNBT = recipe.ignoreRepairMaterialNBT;
2025-03-13 17:14:45 -04:00
this.repairPercentage = recipe.repairPercentage;
;
2025-02-10 02:44:28 -05:00
this.xpCost = recipe.xpCost;
}
2025-02-09 20:49:45 -05:00
}