From e3405d22e9220bcb55ce16a0147d5af5e4adf679 Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Sat, 18 Mar 2023 03:47:24 -0500 Subject: [PATCH] Fixed null pointer exceptions from new custom gui slot commit. --- src/main/java/noppes/npcs/EventHooks.java | 5 +++-- .../noppes/npcs/containers/ContainerCustomGui.java | 9 ++++++--- .../noppes/npcs/scripted/event/CustomGuiEvent.java | 14 +++++++++++--- .../java/noppes/npcs/scripted/gui/ScriptGui.java | 7 +++++-- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/java/noppes/npcs/EventHooks.java b/src/main/java/noppes/npcs/EventHooks.java index 0214fcf7e..86948f307 100644 --- a/src/main/java/noppes/npcs/EventHooks.java +++ b/src/main/java/noppes/npcs/EventHooks.java @@ -26,6 +26,7 @@ import noppes.npcs.api.entity.IProjectile; import noppes.npcs.api.gui.ICustomGui; import noppes.npcs.api.item.IItemCustom; import noppes.npcs.api.item.IItemStack; +import noppes.npcs.client.gui.custom.components.CustomGuiSlot; import noppes.npcs.constants.EnumScriptType; import noppes.npcs.controllers.CustomGuiController; import noppes.npcs.controllers.ScriptContainer; @@ -560,8 +561,8 @@ public class EventHooks { CustomGuiController.onSlotChange(event); } - public static boolean onCustomGuiSlotClicked(IPlayer player, ICustomGui gui, int slotId, int guiSlotId, int dragType, int clickType) { - CustomGuiEvent.SlotClickEvent event = new CustomGuiEvent.SlotClickEvent(player, gui, slotId, guiSlotId, player.getOpenContainer().getSlot(slotId), dragType, clickType); + public static boolean onCustomGuiSlotClicked(IPlayer player, ICustomGui gui, int slotId, CustomGuiSlot guiSlot, int dragType, int clickType) { + CustomGuiEvent.SlotClickEvent event = new CustomGuiEvent.SlotClickEvent(player, gui, slotId, guiSlot, player.getOpenContainer().getSlot(slotId), dragType, clickType); return CustomGuiController.onSlotClick(event); } diff --git a/src/main/java/noppes/npcs/containers/ContainerCustomGui.java b/src/main/java/noppes/npcs/containers/ContainerCustomGui.java index df398327d..d937b895c 100644 --- a/src/main/java/noppes/npcs/containers/ContainerCustomGui.java +++ b/src/main/java/noppes/npcs/containers/ContainerCustomGui.java @@ -108,9 +108,12 @@ public class ContainerCustomGui extends Container { public ItemStack slotClick(int slotId, int dragType, int clickTypeIn, EntityPlayer player) { if(slotId < 0) return super.slotClick(slotId, dragType, clickTypeIn, player); - if(!player.worldObj.isRemote && this.getSlot(slotId) instanceof CustomGuiSlot) { - CustomGuiSlot guiSlot = (CustomGuiSlot) this.getSlot(slotId); - if(!EventHooks.onCustomGuiSlotClicked((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, slotId, guiSlot.guiSlotId, dragType, clickTypeIn)) { + if(!player.worldObj.isRemote) { + CustomGuiSlot guiSlot = null; + if (this.getSlot(slotId) instanceof CustomGuiSlot) { + guiSlot = (CustomGuiSlot) this.getSlot(slotId); + } + if(!EventHooks.onCustomGuiSlotClicked((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, slotId, guiSlot, dragType, clickTypeIn)) { ItemStack item = super.slotClick(slotId, dragType, clickTypeIn, player); EntityPlayerMP p = (EntityPlayerMP) player; p.sendContainerToPlayer(this); diff --git a/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java b/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java index 6e5a6989f..571d699c6 100644 --- a/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java +++ b/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java @@ -123,7 +123,11 @@ public class CustomGuiEvent extends CustomNPCsEvent implements ICustomGuiEvent { super(player, gui); this.slotId = slotId; this.stack = stack; - this.slot = (IItemSlot) gui.getComponent(((CustomGuiSlot)(((EntityPlayerMP)player.getMCEntity()).openContainer.getSlot(slotId))).guiSlotId); + if ((((EntityPlayerMP)player.getMCEntity()).openContainer.getSlot(slotId) instanceof CustomGuiSlot)) { + this.slot = (IItemSlot) gui.getComponent(((CustomGuiSlot) (((EntityPlayerMP) player.getMCEntity()).openContainer.getSlot(slotId))).guiSlotId); + } else { + this.slot = null; + } } public String getHookName() { @@ -150,13 +154,17 @@ public class CustomGuiEvent extends CustomNPCsEvent implements ICustomGuiEvent { */ public final int clickType; - public SlotClickEvent(IPlayer player, ICustomGui gui, int slotId, int guiSlotId, IItemStack stack, int dragType, int clickType) { + public SlotClickEvent(IPlayer player, ICustomGui gui, int slotId, CustomGuiSlot guiSlot, IItemStack stack, int dragType, int clickType) { super(player,gui); this.slotId = slotId; this.stack = stack; this.dragType = dragType; this.clickType = clickType; - this.slot = (IItemSlot) gui.getComponent(guiSlotId); + if (guiSlot != null) { + this.slot = (IItemSlot) gui.getComponent(guiSlot.guiSlotId); + } else { + this.slot = null; + } } public String getHookName() { diff --git a/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java b/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java index 4541ed496..f4456ce31 100644 --- a/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java +++ b/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java @@ -143,13 +143,13 @@ public class ScriptGui implements ICustomGui { } public IItemSlot addItemSlot(int x, int y) { - ScriptGuiItemSlot slot = new ScriptGuiItemSlot(this.getMaxId() + 1, x, y); + ScriptGuiItemSlot slot = new ScriptGuiItemSlot(-1, x, y); this.slots.add(slot); return (IItemSlot)this.slots.get(this.slots.size() - 1); } public IItemSlot addItemSlot(int x, int y, IItemStack stack) { - ScriptGuiItemSlot slot = new ScriptGuiItemSlot(this.getMaxId() + 1, x, y, stack); + ScriptGuiItemSlot slot = new ScriptGuiItemSlot(-1, x, y, stack); this.slots.add(slot); return (IItemSlot)this.slots.get(this.slots.size() - 1); } @@ -308,6 +308,9 @@ public class ScriptGui implements ICustomGui { while(var3.hasNext()) { c = (ICustomGuiComponent)var3.next(); + if (c.getID() == -1) { + c.setID(this.getMaxId()+1); + } list.appendTag(((ScriptGuiComponent)c).toNBT(new NBTTagCompound())); }