From a872bc70357dc154d987b34556986a2ca37559b5 Mon Sep 17 00:00:00 2001 From: Kam Date: Sat, 16 Mar 2024 16:21:44 -0400 Subject: [PATCH] Fix Item Slot not registering changes properly --- .../gui/custom/components/CustomGuiSlot.java | 26 ++++++++ .../npcs/containers/ContainerCustomGui.java | 61 ++++++++++++------- .../noppes/npcs/scripted/gui/ScriptGui.java | 3 +- 3 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiSlot.java b/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiSlot.java index 34748b77c..1659720ad 100644 --- a/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiSlot.java +++ b/src/main/java/noppes/npcs/client/gui/custom/components/CustomGuiSlot.java @@ -8,7 +8,11 @@ package noppes.npcs.client.gui.custom.components; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; +import noppes.npcs.EventHooks; +import noppes.npcs.api.entity.IPlayer; import noppes.npcs.api.gui.IItemSlot; +import noppes.npcs.containers.ContainerCustomGui; +import noppes.npcs.scripted.NpcAPI; public class CustomGuiSlot extends Slot { public boolean clientSide; @@ -23,4 +27,26 @@ public class CustomGuiSlot extends Slot { this.slot = slot; this.player = player; } + + @Override + public void onSlotChanged() { + if(!player.worldObj.isRemote){ + boolean changed; + if (getStack() != null && slot.getStack() != null) { + changed = !getStack().equals(slot.getStack().getMCItemStack()); + } else { + // Handle the case when either getStack() or slot.getStack() is null (indicating an empty slot) + changed = getStack() != null || slot.getStack() != null; + } + if(changed) { + slot.setStack(NpcAPI.Instance().getIItemStack(getStack())); + if (player.openContainer instanceof ContainerCustomGui) { + EventHooks.onCustomGuiSlot((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, + getSlotIndex(), getStack(), slot); + } + } + } + super.onSlotChanged(); + } + } diff --git a/src/main/java/noppes/npcs/containers/ContainerCustomGui.java b/src/main/java/noppes/npcs/containers/ContainerCustomGui.java index 895c9dcaa..c42b6f041 100644 --- a/src/main/java/noppes/npcs/containers/ContainerCustomGui.java +++ b/src/main/java/noppes/npcs/containers/ContainerCustomGui.java @@ -38,7 +38,7 @@ public class ContainerCustomGui extends Container { } slotCount=0; - inventorySlots.clear(); + // inventorySlots.clear(); for (IItemSlot slot : this.customGui.getSlots()) { if (slot.hasStack()) { this.addSlot(player, slot.getPosX(), slot.getPosY(), slot, slot.getStack().getMCItemStack(), player.worldObj.isRemote); @@ -106,40 +106,59 @@ public class ContainerCustomGui extends Container { if(slotId < 0) return super.slotClick(slotId, dragType, clickTypeIn, player); - if (slotId >= this.inventorySlots.size()) { - return null; - } - - Slot mcSlot = (Slot)this.inventorySlots.get(slotId); - if(!player.worldObj.isRemote && mcSlot != null) { + if(!player.worldObj.isRemote) { IItemSlot slot = null; if (this.getSlot(slotId) instanceof CustomGuiSlot) { slot = ((CustomGuiSlot)this.getSlot(slotId)).slot; } if(!EventHooks.onCustomGuiSlotClicked((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, slotId, slot, dragType, clickTypeIn)) { - ItemStack prevStack = mcSlot.getStack(); - if (slot != null) { - prevStack = slot.getStack() == null ? null : slot.getStack().getMCItemStack(); - } ItemStack item = super.slotClick(slotId, dragType, clickTypeIn, player); - if (!ItemStack.areItemStacksEqual(prevStack,item)) { - if (slot != null) { - slot.setStack(NpcAPI.Instance().getIItemStack(mcSlot.getStack())); - } - if (player.openContainer instanceof ContainerCustomGui) { - EventHooks.onCustomGuiSlot((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, - mcSlot.slotNumber, prevStack, slot); - } - } EntityPlayerMP p = (EntityPlayerMP) player; p.sendContainerToPlayer(this); return item; } } - return null; + +// if(slotId < 0) +// return super.slotClick(slotId, dragType, clickTypeIn, player); +// +// if (slotId >= this.inventorySlots.size()) { +// return null; +// } +// +// Slot mcSlot = (Slot)this.inventorySlots.get(slotId); +// if(!player.worldObj.isRemote && mcSlot != null) { +// IItemSlot slot = null; +// if (this.getSlot(slotId) instanceof CustomGuiSlot) { +// slot = ((CustomGuiSlot)this.getSlot(slotId)).slot; +// } +// if(!EventHooks.onCustomGuiSlotClicked((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, slotId, slot, dragType, clickTypeIn)) { +// ItemStack prevStack = mcSlot.getStack(); +// if (slot != null) { +// prevStack = slot.getStack() == null ? null : slot.getStack().getMCItemStack(); +// } +// ItemStack item = super.slotClick(slotId, dragType, clickTypeIn, player); +// +// if (!ItemStack.areItemStacksEqual(prevStack,item)) { +// if (slot != null) { +// slot.setStack(NpcAPI.Instance().getIItemStack(mcSlot.getStack())); +// } +// if (player.openContainer instanceof ContainerCustomGui) { +// EventHooks.onCustomGuiSlot((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, +// mcSlot.slotNumber, prevStack, slot); +// } +// } +// +// EntityPlayerMP p = (EntityPlayerMP) player; +// p.sendContainerToPlayer(this); +// return item; +// } +// } +// +// return null; } public boolean canDragIntoSlot(Slot p_94531_1_) { return true; } diff --git a/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java b/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java index 782321582..035bf1028 100644 --- a/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java +++ b/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java @@ -256,7 +256,7 @@ public class ScriptGui implements ICustomGui { for(int i = 0; i < list.tagCount(); i++){ NBTBase b = list.getCompoundTagAt(i); ScriptGuiComponent component = ScriptGuiComponent.createFromNBT((NBTTagCompound)b); - if (component != null) { + if (component != null && !(component instanceof ScriptGuiItemSlot)) { components.put(component.getID(), component); } } @@ -267,6 +267,7 @@ public class ScriptGui implements ICustomGui { for(int i = 0; i < list.tagCount(); i++){ NBTBase b = list.getCompoundTagAt(i); ScriptGuiItemSlot component = (ScriptGuiItemSlot)ScriptGuiComponent.createFromNBT((NBTTagCompound)b); + components.put(component.getID(), component); slots.add(component); }