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

This commit is contained in:
Pahimar 2015-04-02 19:30:02 -04:00
parent fc2c7b6cde
commit 196cb25899
3 changed files with 73 additions and 18 deletions

View File

@ -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));
}
}

View File

@ -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<ItemStack> 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<ItemStack> 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));
}
}
}

View File

@ -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<MessageTransmutationKnowledgeUpdate, IMessage>
{
public int xCoord, yCoord, zCoord;
public TransmutationKnowledge transmutationKnowledge;
public MessageTransmutationKnowledgeUpdate()
{
this.transmutationKnowledge = new TransmutationKnowledge();
}
public MessageTransmutationKnowledgeUpdate(Collection<ItemStack> knownTransmutationsCollection)
public MessageTransmutationKnowledgeUpdate(TileEntityTransmutationTablet tileEntityTransmutationTablet, Collection<ItemStack> 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<ItemStack> 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);
}
}
}
}