diff --git a/src/main/java/noppes/npcs/EventHooks.java b/src/main/java/noppes/npcs/EventHooks.java index 86948f307..d5ff8c41a 100644 --- a/src/main/java/noppes/npcs/EventHooks.java +++ b/src/main/java/noppes/npcs/EventHooks.java @@ -24,6 +24,7 @@ import noppes.npcs.api.entity.IEntity; import noppes.npcs.api.entity.IPlayer; import noppes.npcs.api.entity.IProjectile; import noppes.npcs.api.gui.ICustomGui; +import noppes.npcs.api.gui.IItemSlot; import noppes.npcs.api.item.IItemCustom; import noppes.npcs.api.item.IItemStack; import noppes.npcs.client.gui.custom.components.CustomGuiSlot; @@ -556,13 +557,13 @@ public class EventHooks { CustomGuiController.onButton(event); } - public static void onCustomGuiSlot(IPlayer player, ICustomGui gui, int slotId) { - CustomGuiEvent.SlotEvent event = new CustomGuiEvent.SlotEvent(player, gui, slotId, player.getOpenContainer().getSlot(slotId)); + public static void onCustomGuiSlot(IPlayer player, ICustomGui gui, int slotId, ItemStack stack, IItemSlot slot) { + CustomGuiEvent.SlotEvent event = new CustomGuiEvent.SlotEvent(player, gui, slotId, stack, slot); CustomGuiController.onSlotChange(event); } - 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); + public static boolean onCustomGuiSlotClicked(IPlayer player, ICustomGui gui, int slotId, IItemSlot slot, int dragType, int clickType) { + CustomGuiEvent.SlotClickEvent event = new CustomGuiEvent.SlotClickEvent(player, gui, slotId, slot, player.getOpenContainer().getSlot(slotId), dragType, clickType); return CustomGuiController.onSlotClick(event); } diff --git a/src/main/java/noppes/npcs/PacketHandlerServer.java b/src/main/java/noppes/npcs/PacketHandlerServer.java index 0e9e2b65e..18957d7c0 100644 --- a/src/main/java/noppes/npcs/PacketHandlerServer.java +++ b/src/main/java/noppes/npcs/PacketHandlerServer.java @@ -74,10 +74,6 @@ public class PacketHandlerServer{ ((ContainerCustomGui) player.openContainer).customGui.fromNBT(Server.readNBT(buffer)); EventHooks.onCustomGuiButton((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui) player.openContainer).customGui, buffer.readInt()); return; - } else if (type == EnumPacketServer.CustomGuiSlotChange && player.openContainer instanceof ContainerCustomGui) { - ((ContainerCustomGui) player.openContainer).customGui.fromNBT(Server.readNBT(buffer)); - EventHooks.onCustomGuiSlot((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui) player.openContainer).customGui, buffer.readInt()); - return; } else if (type == EnumPacketServer.CustomGuiUnfocused && player.openContainer instanceof ContainerCustomGui) { ((ContainerCustomGui) player.openContainer).customGui.fromNBT(Server.readNBT(buffer)); EventHooks.onCustomGuiUnfocused((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui) player.openContainer).customGui, buffer.readInt()); diff --git a/src/main/java/noppes/npcs/client/gui/custom/GuiCustom.java b/src/main/java/noppes/npcs/client/gui/custom/GuiCustom.java index f85078f3b..217c85816 100644 --- a/src/main/java/noppes/npcs/client/gui/custom/GuiCustom.java +++ b/src/main/java/noppes/npcs/client/gui/custom/GuiCustom.java @@ -454,10 +454,6 @@ public class GuiCustom extends GuiScreen implements ICustomScrollListener, IGuiD Client.sendData(EnumPacketServer.CustomGuiButton, new Object[]{this.updateGui().toNBT(), button.id}); } - public void slotChange(CustomGuiSlot slot) { - Client.sendData(EnumPacketServer.CustomGuiSlotChange, new Object[]{this.updateGui().toNBT(), slot.slotNumber}); - } - public String prevScrollClicked = null; public void customScrollClicked(int i, int j, int k, GuiCustomScroll scroll) { Client.sendData(EnumPacketServer.CustomGuiScrollClick, new Object[]{this.updateGui().toNBT(), scroll.id, scroll.selected, this.getScrollSelection((CustomGuiScrollComponent)scroll), false}); @@ -636,7 +632,7 @@ public class GuiCustom extends GuiScreen implements ICustomScrollListener, IGuiD } public boolean doesGuiPauseGame() { - return this.gui == null || this.gui.getDoesPauseGame(); + return this.gui == null || this.gui.doesPauseGame(); } public void addDataHolder(IDataHolder component) { 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 1d3d57b6c..34748b77c 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 @@ -5,28 +5,22 @@ package noppes.npcs.client.gui.custom.components; -import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; -import noppes.npcs.client.gui.custom.GuiCustom; +import noppes.npcs.api.gui.IItemSlot; public class CustomGuiSlot extends Slot { public boolean clientSide; public final int slotNumber; - public final int guiSlotId; + public final IItemSlot slot; + public final EntityPlayer player; - public CustomGuiSlot(IInventory inventoryIn, int index, int guiSlotId, int xPosition, int yPosition, boolean clientSide) { + public CustomGuiSlot(EntityPlayer player, IInventory inventoryIn, int index, IItemSlot slot, int xPosition, int yPosition, boolean clientSide) { super(inventoryIn, index, xPosition, yPosition); this.clientSide = clientSide; this.slotNumber = index; - this.guiSlotId = guiSlotId; - } - - public void onSlotChanged() { - if (this.clientSide) { - ((GuiCustom)Minecraft.getMinecraft().currentScreen).slotChange(this); - } - - super.onSlotChanged(); + this.slot = slot; + this.player = player; } } diff --git a/src/main/java/noppes/npcs/constants/EnumPacketServer.java b/src/main/java/noppes/npcs/constants/EnumPacketServer.java index 0001a6117..194837038 100644 --- a/src/main/java/noppes/npcs/constants/EnumPacketServer.java +++ b/src/main/java/noppes/npcs/constants/EnumPacketServer.java @@ -93,7 +93,7 @@ public enum EnumPacketServer { NaturalSpawnGetAll, MailOpenSetup, DimensionsGet, DimensionTeleport, GetTileEntity, Gui, IsGuiOpen, - CustomGuiButton,CustomGuiSlotChange,CustomGuiScrollClick,CustomGuiClose,CustomGuiUnfocused, + CustomGuiButton,CustomGuiScrollClick,CustomGuiClose,CustomGuiUnfocused, ServerUpdateSkinOverlays; diff --git a/src/main/java/noppes/npcs/containers/ContainerCustomGui.java b/src/main/java/noppes/npcs/containers/ContainerCustomGui.java index d937b895c..56e74f101 100644 --- a/src/main/java/noppes/npcs/containers/ContainerCustomGui.java +++ b/src/main/java/noppes/npcs/containers/ContainerCustomGui.java @@ -7,9 +7,11 @@ package noppes.npcs.containers; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import noppes.npcs.EventHooks; import noppes.npcs.api.entity.IPlayer; @@ -44,9 +46,9 @@ public class ContainerCustomGui extends Container { while(var3.hasNext()) { IItemSlot slot = (IItemSlot)var3.next(); if (slot.hasStack()) { - this.addSlot(slot.getPosX(), slot.getPosY(), slot.getID(), slot.getStack().getMCItemStack(), player.worldObj.isRemote); + this.addSlot(player, slot.getPosX(), slot.getPosY(), slot, slot.getStack().getMCItemStack(), player.worldObj.isRemote); } else { - this.addSlot(slot.getPosX(), slot.getPosY(), slot.getID(), player.worldObj.isRemote); + this.addSlot(player, slot.getPosX(), slot.getPosY(), slot, player.worldObj.isRemote); } } } @@ -75,13 +77,13 @@ public class ContainerCustomGui extends Container { return itemstack; } - void addSlot(int x, int y, int guiSlotId, boolean clientSide) { - this.addSlotToContainer(new CustomGuiSlot(this.guiInventory, this.slotCount++, guiSlotId, x, y, clientSide)); + void addSlot(EntityPlayer player, int x, int y, IItemSlot slot, boolean clientSide) { + this.addSlotToContainer(new CustomGuiSlot(player, this.guiInventory, this.slotCount++, slot, x, y, clientSide)); } - void addSlot(int x, int y, int guiSlotId, ItemStack itemStack, boolean clientSide) { + void addSlot(EntityPlayer player, int x, int y, IItemSlot slot, ItemStack itemStack, boolean clientSide) { this.guiInventory.setInventorySlotContents(this.slotCount, itemStack); - this.addSlotToContainer(new CustomGuiSlot(this.guiInventory, this.slotCount++, guiSlotId, x, y, clientSide)); + this.addSlotToContainer(new CustomGuiSlot(player, this.guiInventory, this.slotCount++, slot, x, y, clientSide)); } void addPlayerInventory(EntityPlayer player, int x, int y) { @@ -108,20 +110,36 @@ 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) { - CustomGuiSlot guiSlot = null; + Slot mcSlot = (Slot)this.inventorySlots.get(slotId); + if(!player.worldObj.isRemote && mcSlot != null) { + IItemSlot slot = null; if (this.getSlot(slotId) instanceof CustomGuiSlot) { - guiSlot = (CustomGuiSlot) this.getSlot(slotId); + slot = ((CustomGuiSlot)this.getSlot(slotId)).slot; } - if(!EventHooks.onCustomGuiSlotClicked((IPlayer) NpcAPI.Instance().getIEntity(player), ((ContainerCustomGui)player.openContainer).customGui, slotId, guiSlot, dragType, clickTypeIn)) { + 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; } diff --git a/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java b/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java index 571d699c6..c34357c16 100644 --- a/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java +++ b/src/main/java/noppes/npcs/scripted/event/CustomGuiEvent.java @@ -2,6 +2,7 @@ package noppes.npcs.scripted.event; import cpw.mods.fml.common.eventhandler.Cancelable; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.ItemStack; import noppes.npcs.api.entity.IPlayer; import noppes.npcs.api.event.ICustomGuiEvent; import noppes.npcs.api.gui.ICustomGui; @@ -9,6 +10,7 @@ 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; +import noppes.npcs.scripted.NpcAPI; public class CustomGuiEvent extends CustomNPCsEvent implements ICustomGuiEvent { public final IPlayer player; @@ -119,15 +121,11 @@ public class CustomGuiEvent extends CustomNPCsEvent implements ICustomGuiEvent { public final IItemStack stack; public final IItemSlot slot; - public SlotEvent(IPlayer player, ICustomGui gui, int slotId, IItemStack stack) { + public SlotEvent(IPlayer player, ICustomGui gui, int slotId, ItemStack stack, IItemSlot slot) { super(player, gui); this.slotId = slotId; - this.stack = stack; - 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; - } + this.stack = NpcAPI.Instance().getIItemStack(stack); + this.slot = slot; } public String getHookName() { @@ -154,17 +152,13 @@ public class CustomGuiEvent extends CustomNPCsEvent implements ICustomGuiEvent { */ public final int clickType; - public SlotClickEvent(IPlayer player, ICustomGui gui, int slotId, CustomGuiSlot guiSlot, IItemStack stack, int dragType, int clickType) { + public SlotClickEvent(IPlayer player, ICustomGui gui, int slotId, IItemSlot slot, IItemStack stack, int dragType, int clickType) { super(player,gui); this.slotId = slotId; this.stack = stack; this.dragType = dragType; this.clickType = clickType; - if (guiSlot != null) { - this.slot = (IItemSlot) gui.getComponent(guiSlot.guiSlotId); - } else { - this.slot = null; - } + this.slot = slot; } 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 f4456ce31..4c228d7f4 100644 --- a/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java +++ b/src/main/java/noppes/npcs/scripted/gui/ScriptGui.java @@ -14,8 +14,10 @@ import noppes.npcs.api.item.IItemStack; import noppes.npcs.controllers.CustomGuiController; import noppes.npcs.controllers.ScriptController; import noppes.npcs.controllers.data.PlayerDataScript; +import scala.actors.threadpool.Arrays; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -28,8 +30,7 @@ public class ScriptGui implements ICustomGui { boolean pauseGame; boolean showPlayerInv; String backgroundTexture = ""; - //ScriptContainer scriptHandler; - List components = new ArrayList(); + HashMap components = new HashMap<>(); List slots = new ArrayList(); public ScriptGui() { @@ -56,7 +57,7 @@ public class ScriptGui implements ICustomGui { } public List getComponents() { - return this.components; + return Arrays.asList(this.components.values().toArray()); } public List getSlots() { @@ -76,7 +77,7 @@ public class ScriptGui implements ICustomGui { this.pauseGame = pauseGame; } - public boolean getDoesPauseGame() { + public boolean doesPauseGame() { return this.pauseGame; } @@ -90,98 +91,94 @@ public class ScriptGui implements ICustomGui { public IButton addButton(int id, String label, int x, int y) { ScriptGuiButton component = new ScriptGuiButton(id, label, x, y); - this.components.add(component); - return (IButton)this.components.get(this.components.size() - 1); + this.updateComponent(component); + return (IButton)this.getComponent(id); } public IButton addButton(int id, String label, int x, int y, int width, int height) { ScriptGuiButton component = new ScriptGuiButton(id, label, x, y, width, height); - this.components.add(component); - return (IButton)this.components.get(this.components.size() - 1); + this.updateComponent(component); + return (IButton)this.getComponent(id); } public IButton addTexturedButton(int id, String label, int x, int y, int width, int height, String texture) { ScriptGuiButton component = new ScriptGuiButton(id, label, x, y, width, height, texture); - this.components.add(component); - return (IButton)this.components.get(this.components.size() - 1); + this.updateComponent(component); + return (IButton)this.getComponent(id); } public IButton addTexturedButton(int id, String label, int x, int y, int width, int height, String texture, int textureX, int textureY) { ScriptGuiButton component = new ScriptGuiButton(id, label, x, y, width, height, texture, textureX, textureY); - this.components.add(component); - return (IButton)this.components.get(this.components.size() - 1); + this.updateComponent(component); + return (IButton)this.getComponent(id); } public ILabel addLabel(int id, String label, int x, int y, int width, int height) { ScriptGuiLabel component = new ScriptGuiLabel(id, label, x, y, width, height); - this.components.add(component); - return (ILabel)this.components.get(this.components.size() - 1); + this.updateComponent(component); + return (ILabel)this.getComponent(id); } public ILabel addLabel(int id, String label, int x, int y, int width, int height, int color) { ScriptGuiLabel component = new ScriptGuiLabel(id, label, x, y, width, height, color); - this.components.add(component); - return (ILabel)this.components.get(this.components.size() - 1); + this.updateComponent(component); + return (ILabel)this.getComponent(id); } public ITextField addTextField(int id, int x, int y, int width, int height) { ScriptGuiTextField component = new ScriptGuiTextField(id, x, y, width, height); - this.components.add(component); - return (ITextField)this.components.get(this.components.size() - 1); + this.updateComponent(component); + return (ITextField)this.getComponent(id); } public ITexturedRect addTexturedRect(int id, String texture, int x, int y, int width, int height) { ScriptGuiTexturedRect component = new ScriptGuiTexturedRect(id, texture, x, y, width, height); - this.components.add(component); - return (ITexturedRect)this.components.get(this.components.size() - 1); + this.updateComponent(component); + return (ITexturedRect)this.getComponent(id); } public ITexturedRect addTexturedRect(int id, String texture, int x, int y, int width, int height, int textureX, int textureY) { ScriptGuiTexturedRect component = new ScriptGuiTexturedRect(id, texture, x, y, width, height, textureX, textureY); - this.components.add(component); - return (ITexturedRect)this.components.get(this.components.size() - 1); - } - - public IItemSlot addItemSlot(int x, int 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(-1, x, y, stack); - this.slots.add(slot); - return (IItemSlot)this.slots.get(this.slots.size() - 1); + this.updateComponent(component); + return (ITexturedRect)this.getComponent(id); } 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); + this.updateComponent(slot); + return (IItemSlot)this.getComponent(id); } 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); + this.updateComponent(slot); + return (IItemSlot)this.getComponent(id); + } + + public IItemSlot addItemSlot(int x, int y) { + return this.addItemSlot(-1, x, y); + } + + public IItemSlot addItemSlot(int x, int y, IItemStack stack) { + return this.addItemSlot(-1, x, y, stack); } public IScroll addScroll(int id, int x, int y, int width, int height, String[] list) { ScriptGuiScroll component = new ScriptGuiScroll(id, x, y, width, height, list); - this.components.add(component); - return (IScroll)this.components.get(this.components.size() - 1); + this.updateComponent(component); + return (IScroll)this.getComponent(id); } public ILine addLine(int id, int x1, int y1, int x2, int y2, int color, int thickness) { - ScriptGuiLine line = new ScriptGuiLine(id, x1, y1, x2, y2, color, thickness); - this.components.add(line); - return (ILine) this.components.get(this.components.size() - 1); + ScriptGuiLine component = new ScriptGuiLine(id, x1, y1, x2, y2, color, thickness); + this.updateComponent(component); + return (ILine) this.getComponent(id); } public ILine addLine(int id, int x1, int y1, int x2, int y2) { - ScriptGuiLine line = new ScriptGuiLine(id, x1, y1, x2, y2); - this.components.add(line); - return (ILine) this.components.get(this.components.size() - 1); + ScriptGuiLine component = new ScriptGuiLine(id, x1, y1, x2, y2); + this.updateComponent(component); + return (ILine) this.getComponent(id); } public void showPlayerInventory(int x, int y) { @@ -191,39 +188,32 @@ public class ScriptGui implements ICustomGui { } public ICustomGuiComponent getComponent(int componentID) { - Iterator var2 = this.components.iterator(); - - ICustomGuiComponent component; - do { - if (!var2.hasNext()) { - return null; - } - - component = (ICustomGuiComponent)var2.next(); - } while(component.getID() != componentID); - - return component; + return this.components.get(componentID); } public void removeComponent(int componentID) { - for(int i = 0; i < this.components.size(); ++i) { - if (((ICustomGuiComponent)this.components.get(i)).getID() == componentID) { - this.components.remove(i); - return; + if (this.components.containsKey(componentID)) { + ICustomGuiComponent component = this.getComponent(componentID); + if (component instanceof IItemSlot) { + for (IItemSlot slot : this.slots) { + if (slot.getID() == componentID) { + this.slots.remove(slot); + break; + } + } } + this.components.remove(componentID); } - } public void updateComponent(ICustomGuiComponent component) { - for(int i = 0; i < this.components.size(); ++i) { - ICustomGuiComponent c = (ICustomGuiComponent)this.components.get(i); - if (c.getID() == component.getID()) { - this.components.set(i, component); - return; + if (component != null) { + this.removeComponent(component.getID()); + this.components.put(component.getID(), component); + if (component instanceof IItemSlot) { + this.slots.add((IItemSlot) component); } } - } public void update(IPlayer player) { @@ -258,16 +248,16 @@ public class ScriptGui implements ICustomGui { this.height = tag.getIntArray("size")[1]; this.pauseGame = tag.getBoolean("pause"); this.backgroundTexture = tag.getString("bgTexture"); - List components = new ArrayList(); NBTTagList list = tag.getTagList("components", 10); for(int i = 0; i < list.tagCount(); i++){ NBTBase b = list.getCompoundTagAt(i); ScriptGuiComponent component = ScriptGuiComponent.createFromNBT((NBTTagCompound)b); - components.add(component); + if (component != null) { + components.put(component.getID(), component); + } } - this.components = components; List slots = new ArrayList(); list = tag.getTagList("slots", 10); @@ -294,7 +284,7 @@ public class ScriptGui implements ICustomGui { tag.setBoolean("pause", this.pauseGame); tag.setString("bgTexture", this.backgroundTexture); NBTTagList list = new NBTTagList(); - Iterator var3 = this.components.iterator(); + Iterator var3 = this.components.values().iterator(); ICustomGuiComponent c; while(var3.hasNext()) {