feat: allow scripts to modify anvil cost

This commit is contained in:
Kam committed 2025-07-09 22:43:06 -04:00
1 parent 2cd06193c2
commit e73e785fbf
2 files changed
+35 -6

No files matched your search

@@ -120,6 +120,7 @@ public class ContainerAnvilRepair extends Container {
ItemStack output = null;
boolean canPickup = true;
RecipeScriptEvent.Pre pre = null;
if (matchingRecipe != null) {
if (!matchingRecipe.availability.isAvailable(player)) {
return;
@@ -157,20 +158,28 @@ public class ContainerAnvilRepair extends Container {
int xpCost = baseXpCost * materialsUsed;
calcItem.setItemDamage(currentDamage);
ItemStack[] items = new ItemStack[]{input0, input1};
pre = EventHooks.onRecipeScriptPre(player, matchingRecipe.getScriptHandler(), matchingRecipe, items);
int scriptXp = pre.getXpCost();
int scriptMat = pre.getMaterialUsage();
xpCost += scriptXp;
materialsUsed += scriptMat;
pre.setXpCost(xpCost);
pre.setMaterialUsage(materialsUsed);
canPickup = !pre.isCanceled();
if (player.experienceTotal >= xpCost) {
output = calcItem;
}
repairMaterialConsumed = materialsUsed;
repairCost = xpCost;
output = EventHooks.onRecipeScriptPost(player, matchingRecipe.getScriptHandler(), matchingRecipe, items, output);
} else {
repairCost = 0;
}
if (matchingRecipe != null) {
ItemStack[] items = new ItemStack[]{input0, input1};
RecipeScriptEvent.Pre pre = EventHooks.onRecipeScriptPre(player, matchingRecipe.getScriptHandler(), matchingRecipe, items);
canPickup = !pre.isCanceled();
output = EventHooks.onRecipeScriptPost(player, matchingRecipe.getScriptHandler(), matchingRecipe, items, output);
}
anvilResult.setInventorySlotContents(0, output);
this.resultCanPickup = canPickup;
@@ -36,6 +36,8 @@ public class RecipeScriptEvent extends PlayerEvent {
@Cancelable
public static class Pre extends RecipeScriptEvent {
private String message = "";
private int xpCost = 0;
private int materialUsage = 0;
public Pre(IPlayer player, Object recipe, boolean isAnvil, IItemStack[] items) {
super(player, recipe, isAnvil, items);
@@ -49,6 +51,24 @@ public class RecipeScriptEvent extends PlayerEvent {
return message;
}
public int getXpCost() {
return xpCost;
}
public void setXpCost(int xpCost) {
if (this.isAnvil)
this.xpCost = xpCost;
}
public int getMaterialUsage() {
return materialUsage;
}
public void setMaterialUsage(int materialUsage) {
if (this.isAnvil)
this.materialUsage = materialUsage;
}
public String getHookName() {
return RecipeScript.ScriptType.PRE.function;
}