From 586c451f0621811cca5d831f830aa87b91c0c68a Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Sat, 18 Mar 2023 03:04:46 -0500 Subject: [PATCH] ICustomGui: -Added IDs to custom GUI slots. --- src/api | 2 +- src/main/java/noppes/npcs/EventHooks.java | 4 +-- .../gui/custom/components/CustomGuiSlot.java | 6 +++-- .../npcs/containers/ContainerCustomGui.java | 17 ++++++------ .../npcs/scripted/event/CustomGuiEvent.java | 9 ++++++- .../noppes/npcs/scripted/gui/ScriptGui.java | 26 +++++++++++++++++-- .../npcs/scripted/gui/ScriptGuiItemSlot.java | 7 ++--- 7 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/api b/src/api index 97e163ec3..c24047127 160000 --- a/src/api +++ b/src/api @@ -1 +1 @@ -Subproject commit 97e163ec3c658374a4553c42cf566ea544ada309 +Subproject commit c2404712713b7c4745082f8f11681e80f817ea95 diff --git a/src/main/java/noppes/npcs/EventHooks.java b/src/main/java/noppes/npcs/EventHooks.java index b16fc4323..0214fcf7e 100644 --- a/src/main/java/noppes/npcs/EventHooks.java +++ b/src/main/java/noppes/npcs/EventHooks.java @@ -560,8 +560,8 @@ public class EventHooks { CustomGuiController.onSlotChange(event); } - public static boolean onCustomGuiSlotClicked(IPlayer player, ICustomGui gui, int slotId, int dragType, int clickType) { - CustomGuiEvent.SlotClickEvent event = new CustomGuiEvent.SlotClickEvent(player, gui, slotId, player.getOpenContainer().getSlot(slotId), dragType, clickType); + 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); return CustomGuiController.onSlotClick(event); } 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 61c31ce4c..1d3d57b6c 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 @@ -12,12 +12,14 @@ import noppes.npcs.client.gui.custom.GuiCustom; public class CustomGuiSlot extends Slot { public boolean clientSide; - public int slotNumber; + public final int slotNumber; + public final int guiSlotId; - public CustomGuiSlot(IInventory inventoryIn, int index, int xPosition, int yPosition, boolean clientSide) { + public CustomGuiSlot(IInventory inventoryIn, int index, int guiSlotId, int xPosition, int yPosition, boolean clientSide) { super(inventoryIn, index, xPosition, yPosition); this.clientSide = clientSide; this.slotNumber = index; + this.guiSlotId = guiSlotId; } public void onSlotChanged() { diff --git a/src/main/java/noppes/npcs/containers/ContainerCustomGui.java b/src/main/java/noppes/npcs/containers/ContainerCustomGui.java index b241cd803..df398327d 100644 --- a/src/main/java/noppes/npcs/containers/ContainerCustomGui.java +++ b/src/main/java/noppes/npcs/containers/ContainerCustomGui.java @@ -44,9 +44,9 @@ public class ContainerCustomGui extends Container { while(var3.hasNext()) { IItemSlot slot = (IItemSlot)var3.next(); if (slot.hasStack()) { - this.addSlot(slot.getPosX(), slot.getPosY(), slot.getStack().getMCItemStack(), player.worldObj.isRemote); + this.addSlot(slot.getPosX(), slot.getPosY(), slot.getID(), slot.getStack().getMCItemStack(), player.worldObj.isRemote); } else { - this.addSlot(slot.getPosX(), slot.getPosY(), player.worldObj.isRemote); + this.addSlot(slot.getPosX(), slot.getPosY(), slot.getID(), player.worldObj.isRemote); } } } @@ -75,13 +75,13 @@ public class ContainerCustomGui extends Container { return itemstack; } - void addSlot(int x, int y, boolean clientSide) { - this.addSlotToContainer(new CustomGuiSlot(this.guiInventory, this.slotCount++, x, y, clientSide)); + void addSlot(int x, int y, int guiSlotId, boolean clientSide) { + this.addSlotToContainer(new CustomGuiSlot(this.guiInventory, this.slotCount++, guiSlotId, x, y, clientSide)); } - void addSlot(int x, int y, ItemStack itemStack, boolean clientSide) { + void addSlot(int x, int y, int guiSlotId, ItemStack itemStack, boolean clientSide) { this.guiInventory.setInventorySlotContents(this.slotCount, itemStack); - this.addSlotToContainer(new CustomGuiSlot(this.guiInventory, this.slotCount++, x, y, clientSide)); + this.addSlotToContainer(new CustomGuiSlot(this.guiInventory, this.slotCount++, guiSlotId, x, y, clientSide)); } void addPlayerInventory(EntityPlayer player, int x, int y) { @@ -108,8 +108,9 @@ 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) { - if(!EventHooks.onCustomGuiSlotClicked((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, slotId, dragType, clickTypeIn)) { + 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)) { 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 7dcf7fecd..6e5a6989f 100644 --- a/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java +++ b/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java @@ -1,10 +1,13 @@ package noppes.npcs.scripted.event; import cpw.mods.fml.common.eventhandler.Cancelable; +import net.minecraft.entity.player.EntityPlayerMP; import noppes.npcs.api.entity.IPlayer; import noppes.npcs.api.event.ICustomGuiEvent; import noppes.npcs.api.gui.ICustomGui; +import noppes.npcs.api.gui.IItemSlot; import noppes.npcs.api.item.IItemStack; +import noppes.npcs.client.gui.custom.components.CustomGuiSlot; import noppes.npcs.constants.EnumScriptType; public class CustomGuiEvent extends CustomNPCsEvent implements ICustomGuiEvent { @@ -114,11 +117,13 @@ public class CustomGuiEvent extends CustomNPCsEvent implements ICustomGuiEvent { public static class SlotEvent extends CustomGuiEvent implements ICustomGuiEvent.SlotEvent { public final int slotId; public final IItemStack stack; + public final IItemSlot slot; public SlotEvent(IPlayer player, ICustomGui gui, int slotId, IItemStack stack) { super(player, gui); this.slotId = slotId; this.stack = stack; + this.slot = (IItemSlot) gui.getComponent(((CustomGuiSlot)(((EntityPlayerMP)player.getMCEntity()).openContainer.getSlot(slotId))).guiSlotId); } public String getHookName() { @@ -139,17 +144,19 @@ public class CustomGuiEvent extends CustomNPCsEvent implements ICustomGuiEvent { public final int slotId; public final IItemStack stack; public final int dragType; + public final IItemSlot slot; /** * */ public final int clickType; - public SlotClickEvent(IPlayer player, ICustomGui gui, int slotId, IItemStack stack, int dragType, int clickType) { + public SlotClickEvent(IPlayer player, ICustomGui gui, int slotId, int guiSlotId, 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); } 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 0a34c85d4..4541ed496 100644 --- a/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java +++ b/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java @@ -143,13 +143,25 @@ public class ScriptGui implements ICustomGui { } public IItemSlot addItemSlot(int x, int y) { - ScriptGuiItemSlot slot = new ScriptGuiItemSlot(x, y); + ScriptGuiItemSlot slot = new ScriptGuiItemSlot(this.getMaxId() + 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(x, y, stack); + ScriptGuiItemSlot slot = new ScriptGuiItemSlot(this.getMaxId() + 1, x, y, stack); + this.slots.add(slot); + return (IItemSlot)this.slots.get(this.slots.size() - 1); + } + + public IItemSlot addItemSlot(int id, int x, int y) { + ScriptGuiItemSlot slot = new ScriptGuiItemSlot(id, x, y); + this.slots.add(slot); + return (IItemSlot)this.slots.get(this.slots.size() - 1); + } + + public IItemSlot addItemSlot(int id, int x, int y, IItemStack stack) { + ScriptGuiItemSlot slot = new ScriptGuiItemSlot(id, x, y, stack); this.slots.add(slot); return (IItemSlot)this.slots.get(this.slots.size() - 1); } @@ -230,6 +242,16 @@ public class ScriptGui implements ICustomGui { return this.playerInvY; } + private int getMaxId() { + int max = 0; + for (ICustomGuiComponent component : this.getComponents()) { + if (component.getID() >= max) { + max = component.getID(); + } + } + return max; + } + public ICustomGui fromNBT(NBTTagCompound tag) { this.id = tag.getInteger("id"); this.width = tag.getIntArray("size")[0]; diff --git a/src/main/java/noppes/npcs/scripted/gui/ScriptGuiItemSlot.java b/src/main/java/noppes/npcs/scripted/gui/ScriptGuiItemSlot.java index 9f0a21a41..d7276d1c2 100644 --- a/src/main/java/noppes/npcs/scripted/gui/ScriptGuiItemSlot.java +++ b/src/main/java/noppes/npcs/scripted/gui/ScriptGuiItemSlot.java @@ -13,12 +13,13 @@ public class ScriptGuiItemSlot extends ScriptGuiComponent implements IItemSlot { public ScriptGuiItemSlot() { } - public ScriptGuiItemSlot(int x, int y) { + public ScriptGuiItemSlot(int id, int x, int y) { + this.setID(id); this.setPos(x, y); } - public ScriptGuiItemSlot(int x, int y, IItemStack stack) { - this(x, y); + public ScriptGuiItemSlot(int id, int x, int y, IItemStack stack) { + this(id, x, y); this.setStack(stack); }