Add message banner support for recipe pre-events

This commit is contained in:
Kam committed 2025-07-09 18:17:32 -04:00
1 parent 6a65168f4a
commit 01e9122700
4 files changed
+65 -85

No files matched your search

+9 -2
View File
@@ -35,6 +35,7 @@ import noppes.npcs.scripted.event.*;
import noppes.npcs.scripted.event.player.*;
import noppes.npcs.scripted.event.player.PlayerEvent.*;
import noppes.npcs.scripted.item.ScriptItemStack;
import kamkeel.npcs.network.packets.data.AchievementPacket;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
@@ -167,7 +168,7 @@ public class EventHooks {
}
public static boolean onRecipeScriptPre(EntityPlayer player, RecipeScript script, Object recipe, ItemStack[] items) {
public static RecipeScriptEvent.Pre onRecipeScriptPre(EntityPlayer player, RecipeScript script, Object recipe, ItemStack[] items) {
IItemStack[] iitems = new IItemStack[items.length];
for (int i = 0; i < items.length; i++) {
iitems[i] = items[i] == null ? null : NpcAPI.Instance().getIItemStack(items[i]);
@@ -176,7 +177,13 @@ public class EventHooks {
if (script != null) {
script.callScript(RecipeScript.ScriptType.PRE.function, event);
}
return NpcAPI.EVENT_BUS.post(event);
NpcAPI.EVENT_BUS.post(event);
if (!player.worldObj.isRemote) {
String msg = event.getMessage();
if (msg == null) msg = "";
AchievementPacket.sendAchievement((EntityPlayerMP) player, false, "", msg);
}
return event;
}
public static ItemStack onRecipeScriptPost(EntityPlayer player, RecipeScript script, Object recipe, ItemStack[] items, ItemStack result) {
@@ -10,6 +10,7 @@ import noppes.npcs.EventHooks;
import noppes.npcs.api.handler.data.IAnvilRecipe;
import noppes.npcs.controllers.RecipeController;
import noppes.npcs.controllers.data.RecipeAnvil;
import noppes.npcs.scripted.event.RecipeScriptEvent;
public class ContainerAnvilRepair extends Container {
// A 2-slot crafting matrix: slot 0 = damaged item, slot 1 = repair material.
@@ -27,6 +28,7 @@ public class ContainerAnvilRepair extends Container {
public int repairMaterialConsumed = 0;
private RecipeAnvil currentRecipe;
private SlotAnvilOutput resultSlot;
public ContainerAnvilRepair(InventoryPlayer playerInv, World world, int x, int y, int z) {
this.worldObj = world;
@@ -36,7 +38,8 @@ public class ContainerAnvilRepair extends Container {
this.player = playerInv.player;
// Output slot (index 0) using our custom SlotAnvilOutput.
this.addSlotToContainer(new SlotAnvilOutput(this, anvilResult, 0, 133, 47));
this.resultSlot = new SlotAnvilOutput(this, anvilResult, 0, 133, 47);
this.addSlotToContainer(this.resultSlot);
// Input slot 0: damaged item. We restrict its stack size to 1.
this.addSlotToContainer(new Slot(anvilMatrix, 0, 26, 47) {
@@ -115,6 +118,7 @@ public class ContainerAnvilRepair extends Container {
currentRecipe = matchingRecipe;
ItemStack output = null;
boolean canPickup = true;
if (matchingRecipe != null) {
if (!matchingRecipe.availability.isAvailable(player)) {
return;
@@ -160,8 +164,17 @@ public class ContainerAnvilRepair extends Container {
} 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);
if (this.resultSlot != null) {
this.resultSlot.setCanPickup(canPickup);
}
}
}
@@ -218,7 +231,7 @@ public class ContainerAnvilRepair extends Container {
copy = stack.copy();
if (index == 0) { // output slot
SlotAnvilOutput resultSlot = (SlotAnvilOutput) slot;
if (!resultSlot.preEvent(player)) {
if (!resultSlot.canPickup()) {
return null;
}
if (player.experienceTotal < this.repairCost) {
@@ -263,7 +276,6 @@ public class ContainerAnvilRepair extends Container {
// subtracts one from the damaged item stack.
public class SlotAnvilOutput extends Slot {
private final ContainerAnvilRepair container;
private boolean preChecked = false;
private boolean canPickup = true;
public SlotAnvilOutput(ContainerAnvilRepair container, IInventory inventory, int index, int x, int y) {
@@ -271,25 +283,12 @@ public class ContainerAnvilRepair extends Container {
this.container = container;
}
public boolean preEvent(EntityPlayer player) {
preChecked = true;
if (!container.worldObj.isRemote) {
RecipeAnvil recipe = container.currentRecipe;
if (recipe != null) {
ItemStack[] items = new ItemStack[]{
container.anvilMatrix.getStackInRowAndColumn(0, 0),
container.anvilMatrix.getStackInRowAndColumn(1, 0)
};
canPickup = !EventHooks.onRecipeScriptPre(player, recipe.getScriptHandler(), recipe, items);
if (!canPickup) {
container.updateRepairResult();
preChecked = false; // reset for next attempt
}
} else {
canPickup = true;
}
}
return canPickup;
public void setCanPickup(boolean value) {
this.canPickup = value;
}
public boolean canPickup() {
return this.canPickup;
}
@Override
@@ -299,31 +298,14 @@ public class ContainerAnvilRepair extends Container {
@Override
public boolean canTakeStack(EntityPlayer player) {
if (!preChecked) {
preEvent(player);
}
return canPickup && player.experienceTotal >= container.repairCost;
}
@Override
public void onPickupFromSlot(EntityPlayer player, ItemStack stack) {
if (!preChecked) {
if (!preEvent(player))
return;
}
if (!canPickup) {
return;
}
RecipeAnvil recipe = container.currentRecipe;
ItemStack[] items = new ItemStack[]{
container.anvilMatrix.getStackInRowAndColumn(0, 0),
container.anvilMatrix.getStackInRowAndColumn(1, 0)
};
if (!container.worldObj.isRemote && recipe != null) {
container.updateRepairResult();
stack = EventHooks.onRecipeScriptPost(player, recipe.getScriptHandler(), recipe, items, stack);
}
if (player.experienceTotal < container.repairCost) {
container.updateRepairResult();
return;
@@ -353,7 +335,6 @@ public class ContainerAnvilRepair extends Container {
}
container.repairCost = 0;
container.updateRepairResult();
preChecked = false;
canPickup = true;
super.onPickupFromSlot(player, stack);
}
@@ -9,6 +9,7 @@ import net.minecraft.network.play.server.S2FPacketSetSlot;
import net.minecraft.world.World;
import noppes.npcs.CustomItems;
import noppes.npcs.EventHooks;
import noppes.npcs.scripted.event.RecipeScriptEvent;
import noppes.npcs.controllers.RecipeController;
import noppes.npcs.controllers.data.RecipeCarpentry;
@@ -22,6 +23,7 @@ public class ContainerCarpentryBench extends Container {
private int posZ;
private RecipeCarpentry currentRecipe;
private SlotCarpentryResult resultSlot;
public ContainerCarpentryBench(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5) {
this.worldObj = par2World;
@@ -29,7 +31,8 @@ public class ContainerCarpentryBench extends Container {
this.posY = par4;
this.posZ = par5;
this.player = par1InventoryPlayer.player;
this.addSlotToContainer(new SlotCarpentryResult(this, par1InventoryPlayer.player, this.craftMatrix, this.craftResult, 0, 133, 41));
this.resultSlot = new SlotCarpentryResult(this, par1InventoryPlayer.player, this.craftMatrix, this.craftResult, 0, 133, 41);
this.addSlotToContainer(this.resultSlot);
int var6;
int var7;
@@ -66,11 +69,24 @@ public class ContainerCarpentryBench extends Container {
this.currentRecipe = recipe;
ItemStack item = null;
boolean canPickup = true;
if (recipe != null && recipe.availability.isAvailable(player)) {
item = recipe.getCraftingResult(this.craftMatrix);
ItemStack[] items = new ItemStack[this.craftMatrix.getSizeInventory()];
for (int i = 0; i < items.length; i++) {
items[i] = this.craftMatrix.getStackInSlot(i);
}
RecipeScriptEvent.Pre pre = EventHooks.onRecipeScriptPre(player, recipe.getScriptHandler(), recipe, items);
canPickup = !pre.isCanceled();
item = EventHooks.onRecipeScriptPost(player, recipe.getScriptHandler(), recipe, items, item);
}
this.craftResult.setInventorySlotContents(0, item);
if (this.resultSlot != null) {
this.resultSlot.setCanPickup(canPickup);
}
EntityPlayerMP plmp = (EntityPlayerMP) player;
plmp.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(this.windowId, 0, item));
}
@@ -113,7 +129,7 @@ public class ContainerCarpentryBench extends Container {
if (par1 == 0) {
SlotCarpentryResult resultSlot = (SlotCarpentryResult) var3;
if (!resultSlot.preEvent(par1EntityPlayer)) {
if (!resultSlot.canPickup()) {
return null;
}
if (!this.mergeItemStack(var4, 17, 53, true)) {
@@ -162,65 +178,30 @@ public class ContainerCarpentryBench extends Container {
// Custom result slot to handle recipe scripts
private class SlotCarpentryResult extends SlotCrafting {
private final ContainerCarpentryBench container;
private final InventoryCrafting matrix;
private boolean preChecked = false;
private boolean canPickup = true;
public SlotCarpentryResult(ContainerCarpentryBench container, EntityPlayer player, InventoryCrafting matrix, IInventory result, int index, int x, int y) {
super(player, matrix, result, index, x, y);
this.container = container;
this.matrix = matrix;
}
public boolean preEvent(EntityPlayer player) {
preChecked = true;
if (!container.worldObj.isRemote) {
RecipeCarpentry recipe = container.currentRecipe;
if (recipe != null) {
ItemStack[] items = new ItemStack[matrix.getSizeInventory()];
for (int i = 0; i < items.length; i++) {
items[i] = matrix.getStackInSlot(i);
}
canPickup = !EventHooks.onRecipeScriptPre(player, recipe.getScriptHandler(), recipe, items);
if (!canPickup) {
container.onCraftMatrixChanged(matrix);
preChecked = false; // reset for next attempt
}
} else {
canPickup = true;
}
}
return canPickup;
public void setCanPickup(boolean value) {
this.canPickup = value;
}
public boolean canPickup() {
return this.canPickup;
}
@Override
public boolean canTakeStack(EntityPlayer player) {
if (!preChecked) {
preEvent(player);
}
return canPickup && super.canTakeStack(player);
}
@Override
public void onPickupFromSlot(EntityPlayer player, ItemStack stack) {
if (!preChecked) {
if (!preEvent(player))
return;
}
if (!canPickup)
return;
if (!container.worldObj.isRemote) {
RecipeCarpentry recipe = container.currentRecipe;
if (recipe != null) {
ItemStack[] items = new ItemStack[matrix.getSizeInventory()];
for (int i = 0; i < items.length; i++) {
items[i] = matrix.getStackInSlot(i);
}
stack = EventHooks.onRecipeScriptPost(player, recipe.getScriptHandler(), recipe, items, stack);
}
}
preChecked = false;
canPickup = true;
super.onPickupFromSlot(player, stack);
}
}
@@ -35,9 +35,20 @@ public class RecipeScriptEvent extends PlayerEvent {
@Cancelable
public static class Pre extends RecipeScriptEvent {
private String message = "";
public Pre(IPlayer player, Object recipe, boolean isAnvil, IItemStack[] items) {
super(player, recipe, isAnvil, items);
}
public void setMessage(String message) {
this.message = message == null ? "" : message;
}
public String getMessage() {
return message;
}
public String getHookName() {
return RecipeScript.ScriptType.PRE.function;
}