From 972e930b31646142d1adcbdf4e229873fa5dbc4e Mon Sep 17 00:00:00 2001 From: "Luis C.M." <42545778+c0508383@users.noreply.github.com> Date: Mon, 17 Feb 2025 21:39:18 -0600 Subject: [PATCH] Implemented anvil repair and item destroyed events for custom items and linked items --- src/main/java/noppes/npcs/EventHooks.java | 19 ++++++ .../noppes/npcs/ScriptItemEventHandler.java | 24 ++++++- .../noppes/npcs/constants/EnumScriptType.java | 2 + .../noppes/npcs/scripted/event/ItemEvent.java | 66 +++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) diff --git a/src/main/java/noppes/npcs/EventHooks.java b/src/main/java/noppes/npcs/EventHooks.java index d51d71627..913a2a5a0 100644 --- a/src/main/java/noppes/npcs/EventHooks.java +++ b/src/main/java/noppes/npcs/EventHooks.java @@ -17,6 +17,7 @@ import net.minecraftforge.event.world.WorldEvent; import noppes.npcs.api.IWorld; import noppes.npcs.api.entity.*; import noppes.npcs.api.event.IAnimationEvent; +import noppes.npcs.api.event.IItemEvent; import noppes.npcs.api.gui.ICustomGui; import noppes.npcs.api.gui.IItemSlot; import noppes.npcs.api.handler.data.IAnimation; @@ -147,6 +148,24 @@ public class EventHooks { return NpcAPI.EVENT_BUS.post(event); } + public static void onRepairCustomItem(IItemCustomizable item, IPlayer player, IItemStack left, IItemStack right, float anvilBreakChance) { + INpcScriptHandler handler = (INpcScriptHandler) item.getScriptHandler(); + ItemEvent.RepairItem event = new ItemEvent.RepairItem(item, player, left, right, anvilBreakChance); + if (handler != null) { + handler.callScript(EnumScriptType.REPAIR_ITEM, event); + } + NpcAPI.EVENT_BUS.post(event); + } + + public static void onBreakCustomItem(IItemCustomizable item, IPlayer player) { + INpcScriptHandler handler = (INpcScriptHandler) item.getScriptHandler(); + ItemEvent.BreakItem event = new ItemEvent.BreakItem(item, player); + if (handler != null) { + handler.callScript(EnumScriptType.BREAK_ITEM, event); + } + NpcAPI.EVENT_BUS.post(event); + } + public static void onLinkedItemVersionChange(IItemLinked item, int version, int prevVersion) { INpcScriptHandler handler = (INpcScriptHandler) item.getScriptHandler(); LinkedItemEvent.VersionChangeEvent event = new LinkedItemEvent.VersionChangeEvent(item, version, prevVersion); diff --git a/src/main/java/noppes/npcs/ScriptItemEventHandler.java b/src/main/java/noppes/npcs/ScriptItemEventHandler.java index b2b1f95ac..b6416aaef 100644 --- a/src/main/java/noppes/npcs/ScriptItemEventHandler.java +++ b/src/main/java/noppes/npcs/ScriptItemEventHandler.java @@ -9,9 +9,7 @@ import net.minecraft.world.WorldServer; import net.minecraftforge.event.entity.EntityJoinWorldEvent; import net.minecraftforge.event.entity.item.ItemTossEvent; import net.minecraftforge.event.entity.living.LivingEvent; -import net.minecraftforge.event.entity.player.EntityInteractEvent; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; -import net.minecraftforge.event.entity.player.PlayerUseItemEvent; +import net.minecraftforge.event.entity.player.*; import noppes.npcs.api.IWorld; import noppes.npcs.api.entity.IPlayer; import noppes.npcs.api.item.IItemCustomizable; @@ -224,4 +222,24 @@ public class ScriptItemEventHandler { } } } + @SubscribeEvent + public void invoke(AnvilRepairEvent repairEvent) { + IItemStack output = NpcAPI.Instance().getIItemStack(repairEvent.output); + if (output instanceof IItemCustomizable) { + IPlayer player = (IPlayer) NpcAPI.Instance().getIEntity(repairEvent.entityPlayer); + IItemStack left = NpcAPI.Instance().getIItemStack(repairEvent.left); + IItemStack right = NpcAPI.Instance().getIItemStack(repairEvent.right); + float breakChance = repairEvent.breakChance; + EventHooks.onRepairCustomItem((IItemCustomizable) output, player, left, right, breakChance); + } + } + + @SubscribeEvent + public void invoke(PlayerDestroyItemEvent destroyItemEvent) { + IItemStack itemStack = NpcAPI.Instance().getIItemStack(destroyItemEvent.original); + if (itemStack instanceof IItemCustomizable) { + IPlayer player = (IPlayer) NpcAPI.Instance().getIEntity(destroyItemEvent.entityPlayer); + EventHooks.onBreakCustomItem((IItemCustomizable) itemStack, player); + } + } } diff --git a/src/main/java/noppes/npcs/constants/EnumScriptType.java b/src/main/java/noppes/npcs/constants/EnumScriptType.java index cf2a87ba8..f0270d443 100644 --- a/src/main/java/noppes/npcs/constants/EnumScriptType.java +++ b/src/main/java/noppes/npcs/constants/EnumScriptType.java @@ -72,6 +72,8 @@ public enum EnumScriptType { USING_ITEM("usingItem"), STOP_USING_ITEM("stopItem"), FINISH_USING_ITEM("finishItem"), + REPAIR_ITEM("repairItem"), + BREAK_ITEM("breakItem"), USE_HOE("useHoe"), SLEEP("sleep"), WAKE_UP("wakeUp"), diff --git a/src/main/java/noppes/npcs/scripted/event/ItemEvent.java b/src/main/java/noppes/npcs/scripted/event/ItemEvent.java index 92307ff41..31d620583 100644 --- a/src/main/java/noppes/npcs/scripted/event/ItemEvent.java +++ b/src/main/java/noppes/npcs/scripted/event/ItemEvent.java @@ -5,6 +5,7 @@ import noppes.npcs.api.entity.IEntity; import noppes.npcs.api.entity.IPlayer; import noppes.npcs.api.event.IItemEvent; import noppes.npcs.api.item.IItemCustomizable; +import noppes.npcs.api.item.IItemStack; import noppes.npcs.constants.EnumScriptType; public class ItemEvent extends CustomNPCsEvent implements IItemEvent { @@ -297,4 +298,69 @@ public class ItemEvent extends CustomNPCsEvent implements IItemEvent { return duration; } } + + public static class BreakItem extends ItemEvent implements IItemEvent.BreakItem { + public final IPlayer player; + + public BreakItem(IItemCustomizable item, IPlayer player) { + super(item); + this.player = player; + } + + public String getHookName() { + return EnumScriptType.BREAK_ITEM.function; + } + + @Override + public IItemStack getBrokenStack() { + return this.item; + } + + @Override + public IPlayer getPlayer() { + return this.player; + } + } + + public static class RepairItem extends ItemEvent implements IItemEvent.RepairItem { + public final IPlayer player; + public final IItemStack left, right; + public final float anvilBreakChance; + + public RepairItem(IItemCustomizable item, IPlayer player, IItemStack left, IItemStack right, float anvilBreakChance) { + super(item); + this.player = player; + this.left = left; + this.right = right; + this.anvilBreakChance = anvilBreakChance; + } + + public String getHookName() { + return EnumScriptType.REPAIR_ITEM.function; + } + + public IPlayer getPlayer() { + return this.player; + } + + @Override + public IItemStack getLeft() { + return this.left; + } + + @Override + public IItemStack getRight() { + return this.right; + } + + @Override + public IItemStack getOutput() { + return this.item; + } + + @Override + public float getAnvilBreakChance() { + return this.anvilBreakChance; + } + } }