Implemented anvil repair and item destroyed events for custom items and linked items

This commit is contained in:
Luis C.M. committed 2025-02-17 21:39:18 -06:00
1 parent 90bd8aeb02
commit 972e930b31
4 files changed
+108 -3

No files matched your search

+19
View File
@@ -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);
@@ -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);
}
}
}
@@ -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"),
@@ -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;
}
}
}