From 196cb258992c3cca8b3b5d0181dace30040811a2 Mon Sep 17 00:00:00 2001 From: Pahimar Date: Thu, 2 Apr 2015 19:30:02 -0400 Subject: [PATCH] Inventory sync when the search term is updated (client and server) and ensure that only people using the specified tablet process the inventory update packet --- .../gui/element/ElementSearchField.java | 4 +- .../ContainerTransmutationTablet.java | 14 ++-- .../MessageTransmutationKnowledgeUpdate.java | 73 ++++++++++++++++--- 3 files changed, 73 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/pahimar/ee3/client/gui/element/ElementSearchField.java b/src/main/java/com/pahimar/ee3/client/gui/element/ElementSearchField.java index ea1ebe01..7f210224 100644 --- a/src/main/java/com/pahimar/ee3/client/gui/element/ElementSearchField.java +++ b/src/main/java/com/pahimar/ee3/client/gui/element/ElementSearchField.java @@ -1,5 +1,6 @@ package com.pahimar.ee3.client.gui.element; +import com.pahimar.ee3.inventory.element.IElementTextFieldHandler; import com.pahimar.ee3.network.PacketHandler; import com.pahimar.ee3.network.message.MessageGuiElementTextFieldUpdate; import com.pahimar.repackage.cofh.lib.gui.GuiBase; @@ -15,8 +16,9 @@ public class ElementSearchField extends ElementTextField @Override protected void onCharacterEntered(boolean success) { - if (success) + if (success && this.gui.inventorySlots instanceof IElementTextFieldHandler) { + ((IElementTextFieldHandler) this.gui.inventorySlots).handleElementTextFieldUpdate(this.name, this.getText()); PacketHandler.INSTANCE.sendToServer(new MessageGuiElementTextFieldUpdate(this)); } } diff --git a/src/main/java/com/pahimar/ee3/inventory/ContainerTransmutationTablet.java b/src/main/java/com/pahimar/ee3/inventory/ContainerTransmutationTablet.java index aa3939e4..354a6fa0 100644 --- a/src/main/java/com/pahimar/ee3/inventory/ContainerTransmutationTablet.java +++ b/src/main/java/com/pahimar/ee3/inventory/ContainerTransmutationTablet.java @@ -30,7 +30,7 @@ import java.util.*; public class ContainerTransmutationTablet extends ContainerEE implements IElementTextFieldHandler, IElementSliderHandler { private InventoryTransmutationTablet inventoryTransmutationTablet; - private TileEntityTransmutationTablet tileEntityTransmutationTablet; + public final TileEntityTransmutationTablet tileEntityTransmutationTablet; private float energyValue; private String searchTerm; private boolean requiresUpdate = false; @@ -287,7 +287,7 @@ public class ContainerTransmutationTablet extends ContainerEE implements IElemen if (!this.tileEntityTransmutationTablet.getWorldObj().isRemote && itemStack != null && itemStack.getItem() instanceof ItemAlchemicalTome && ItemHelper.hasOwnerUUID(itemStack)) { - PacketHandler.INSTANCE.sendToAllAround(new MessageTransmutationKnowledgeUpdate(), new NetworkRegistry.TargetPoint(this.tileEntityTransmutationTablet.getWorldObj().provider.dimensionId, (double) this.tileEntityTransmutationTablet.xCoord, (double) this.tileEntityTransmutationTablet.yCoord, (double) this.tileEntityTransmutationTablet.zCoord, 5d)); + PacketHandler.INSTANCE.sendToAllAround(new MessageTransmutationKnowledgeUpdate(this.containerTransmutationTablet.tileEntityTransmutationTablet, null), new NetworkRegistry.TargetPoint(this.tileEntityTransmutationTablet.getWorldObj().provider.dimensionId, (double) this.tileEntityTransmutationTablet.xCoord, (double) this.tileEntityTransmutationTablet.yCoord, (double) this.tileEntityTransmutationTablet.zCoord, 5d)); } } @@ -295,12 +295,14 @@ public class ContainerTransmutationTablet extends ContainerEE implements IElemen public void putStack(ItemStack itemStack) { super.putStack(itemStack); + + Set knownTransmutations = TransmutationKnowledgeRegistry.getInstance().getPlayersKnownTransmutations(ItemHelper.getOwnerUUID(itemStack)); + this.containerTransmutationTablet.inventoryTransmutationTablet = new InventoryTransmutationTablet(knownTransmutations); + this.containerTransmutationTablet.updateInventory(); + if (!this.tileEntityTransmutationTablet.getWorldObj().isRemote && itemStack != null && itemStack.getItem() instanceof ItemAlchemicalTome && ItemHelper.hasOwnerUUID(itemStack)) { - Set knownTransmutations = TransmutationKnowledgeRegistry.getInstance().getPlayersKnownTransmutations(ItemHelper.getOwnerUUID(itemStack)); - this.containerTransmutationTablet.inventoryTransmutationTablet = new InventoryTransmutationTablet(knownTransmutations); - this.containerTransmutationTablet.updateInventory(); - PacketHandler.INSTANCE.sendToAllAround(new MessageTransmutationKnowledgeUpdate(knownTransmutations), new NetworkRegistry.TargetPoint(this.tileEntityTransmutationTablet.getWorldObj().provider.dimensionId, (double) this.tileEntityTransmutationTablet.xCoord, (double) this.tileEntityTransmutationTablet.yCoord, (double) this.tileEntityTransmutationTablet.zCoord, 5d)); + PacketHandler.INSTANCE.sendToAllAround(new MessageTransmutationKnowledgeUpdate(this.containerTransmutationTablet.tileEntityTransmutationTablet, knownTransmutations), new NetworkRegistry.TargetPoint(this.tileEntityTransmutationTablet.getWorldObj().provider.dimensionId, (double) this.tileEntityTransmutationTablet.xCoord, (double) this.tileEntityTransmutationTablet.yCoord, (double) this.tileEntityTransmutationTablet.zCoord, 5d)); } } } diff --git a/src/main/java/com/pahimar/ee3/network/message/MessageTransmutationKnowledgeUpdate.java b/src/main/java/com/pahimar/ee3/network/message/MessageTransmutationKnowledgeUpdate.java index 953894fe..0a49939e 100644 --- a/src/main/java/com/pahimar/ee3/network/message/MessageTransmutationKnowledgeUpdate.java +++ b/src/main/java/com/pahimar/ee3/network/message/MessageTransmutationKnowledgeUpdate.java @@ -2,8 +2,8 @@ package com.pahimar.ee3.network.message; import com.pahimar.ee3.inventory.ContainerTransmutationTablet; import com.pahimar.ee3.knowledge.TransmutationKnowledge; +import com.pahimar.ee3.tileentity.TileEntityTransmutationTablet; import com.pahimar.ee3.util.CompressionHelper; -import com.pahimar.ee3.util.LogHelper; import cpw.mods.fml.client.FMLClientHandler; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; @@ -16,26 +16,65 @@ import java.util.Collection; public class MessageTransmutationKnowledgeUpdate implements IMessage, IMessageHandler { + public int xCoord, yCoord, zCoord; public TransmutationKnowledge transmutationKnowledge; public MessageTransmutationKnowledgeUpdate() { - this.transmutationKnowledge = new TransmutationKnowledge(); + } - public MessageTransmutationKnowledgeUpdate(Collection knownTransmutationsCollection) + public MessageTransmutationKnowledgeUpdate(TileEntityTransmutationTablet tileEntityTransmutationTablet, Collection knownTransmutationsCollection) { - this.transmutationKnowledge = new TransmutationKnowledge(knownTransmutationsCollection); + if (tileEntityTransmutationTablet != null) + { + this.xCoord = tileEntityTransmutationTablet.xCoord; + this.yCoord = tileEntityTransmutationTablet.yCoord; + this.zCoord = tileEntityTransmutationTablet.zCoord; + } + else + { + this.xCoord = 0; + this.yCoord = Integer.MIN_VALUE; + this.zCoord = 0; + } + + if (knownTransmutationsCollection != null) + { + this.transmutationKnowledge = new TransmutationKnowledge(knownTransmutationsCollection); + } + else + { + this.transmutationKnowledge = new TransmutationKnowledge(); + } + } + + public MessageTransmutationKnowledgeUpdate(int xCoord, int yCoord, int zCoord, Collection knownTransmutationsCollection) + { + this.xCoord = xCoord; + this.yCoord = yCoord; + this.zCoord = zCoord; + + if (knownTransmutationsCollection != null) + { + this.transmutationKnowledge = new TransmutationKnowledge(knownTransmutationsCollection); + } + else + { + this.transmutationKnowledge = new TransmutationKnowledge(); + } } @Override public void fromBytes(ByteBuf buf) { + this.xCoord = buf.readInt(); + this.yCoord = buf.readInt(); + this.zCoord = buf.readInt(); + byte[] compressedString = null; int readableBytes = buf.readInt(); - LogHelper.info(readableBytes); - if (readableBytes > 0) { compressedString = buf.readBytes(readableBytes).array(); @@ -51,6 +90,10 @@ public class MessageTransmutationKnowledgeUpdate implements IMessage, IMessageHa @Override public void toBytes(ByteBuf buf) { + buf.writeInt(xCoord); + buf.writeInt(yCoord); + buf.writeInt(zCoord); + byte[] compressedString = null; if (transmutationKnowledge != null) @@ -72,13 +115,21 @@ public class MessageTransmutationKnowledgeUpdate implements IMessage, IMessageHa @Override public IMessage onMessage(MessageTransmutationKnowledgeUpdate message, MessageContext ctx) { - if (FMLClientHandler.instance().getClient().currentScreen instanceof GuiContainer) + if (message.yCoord != Integer.MIN_VALUE) { - GuiContainer guiContainer = (GuiContainer) FMLClientHandler.instance().getClient().currentScreen; - - if (guiContainer.inventorySlots instanceof ContainerTransmutationTablet) + if (FMLClientHandler.instance().getClient().currentScreen instanceof GuiContainer) { - ((ContainerTransmutationTablet) guiContainer.inventorySlots).handleTransmutationKnowledgeUpdate(message.transmutationKnowledge); + GuiContainer guiContainer = (GuiContainer) FMLClientHandler.instance().getClient().currentScreen; + + if (guiContainer.inventorySlots instanceof ContainerTransmutationTablet) + { + TileEntityTransmutationTablet tileEntityTransmutationTablet = ((ContainerTransmutationTablet) guiContainer.inventorySlots).tileEntityTransmutationTablet; + + if (tileEntityTransmutationTablet.xCoord == message.xCoord && tileEntityTransmutationTablet.yCoord == message.yCoord && tileEntityTransmutationTablet.zCoord == message.zCoord) + { + ((ContainerTransmutationTablet) guiContainer.inventorySlots).handleTransmutationKnowledgeUpdate(message.transmutationKnowledge); + } + } } }