From db7c85e6412306a15fb948cd0354a2a6d8d0a297 Mon Sep 17 00:00:00 2001 From: Dimach Date: Thu, 13 Dec 2018 12:22:51 +0200 Subject: [PATCH] Fix flask crash on server --- src/main/java/gregtech/common/GT_Network.java | 6 +- .../gui/GT_GUIContainerVolumetricFlask.java | 4 +- .../common/net/MessageSetFlaskCapacity.java | 61 +++++++++++++------ 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/main/java/gregtech/common/GT_Network.java b/src/main/java/gregtech/common/GT_Network.java index 8f6266e6..45b419f1 100644 --- a/src/main/java/gregtech/common/GT_Network.java +++ b/src/main/java/gregtech/common/GT_Network.java @@ -6,7 +6,6 @@ import cpw.mods.fml.common.network.FMLEmbeddedChannel; import cpw.mods.fml.common.network.FMLOutboundHandler; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.internal.FMLProxyPacket; -import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; import cpw.mods.fml.relauncher.Side; import gregtech.api.enums.GT_Values; import gregtech.api.net.*; @@ -31,13 +30,10 @@ public class GT_Network implements IGT_NetworkHandler { private final EnumMap mChannel; private final GT_Packet[] mSubChannels; - public final SimpleNetworkWrapper networkWrapper; public GT_Network() { - networkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel("gregtech_network_wrapper"); - networkWrapper.registerMessage(MessageSetFlaskCapacity.Handler.class, MessageSetFlaskCapacity.class, 0, Side.SERVER); this.mChannel = NetworkRegistry.INSTANCE.newChannel("GregTech", new ChannelHandler[]{this, new HandlerShared()}); - this.mSubChannels = new GT_Packet[]{new GT_Packet_TileEntity(), new GT_Packet_Sound(), new GT_Packet_Block_Event(), new GT_Packet_Ores(), new GT_Packet_Pollution()}; + this.mSubChannels = new GT_Packet[]{new GT_Packet_TileEntity(), new GT_Packet_Sound(), new GT_Packet_Block_Event(), new GT_Packet_Ores(), new GT_Packet_Pollution(), new MessageSetFlaskCapacity()}; } protected void encode(ChannelHandlerContext aContext, GT_Packet aPacket, List aOutput) diff --git a/src/main/java/gregtech/common/gui/GT_GUIContainerVolumetricFlask.java b/src/main/java/gregtech/common/gui/GT_GUIContainerVolumetricFlask.java index 65b06765..779fced4 100644 --- a/src/main/java/gregtech/common/gui/GT_GUIContainerVolumetricFlask.java +++ b/src/main/java/gregtech/common/gui/GT_GUIContainerVolumetricFlask.java @@ -4,9 +4,9 @@ package gregtech.common.gui; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import gregtech.api.enums.GT_Values; -import gregtech.common.GT_Network; import gregtech.common.items.GT_VolumetricFlask; import gregtech.common.net.MessageSetFlaskCapacity; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiTextField; @@ -115,7 +115,7 @@ public final class GT_GUIContainerVolumetricFlask extends GuiContainer { protected void actionPerformed(GuiButton btn) { try { if (btn == apply) { - ((GT_Network) GT_Values.NW).networkWrapper.sendToServer(new MessageSetFlaskCapacity(Integer.parseInt(amount.getText()))); + GT_Values.NW.sendToServer(new MessageSetFlaskCapacity(Integer.parseInt(amount.getText()), Minecraft.getMinecraft().thePlayer)); mc.thePlayer.closeScreen(); } diff --git a/src/main/java/gregtech/common/net/MessageSetFlaskCapacity.java b/src/main/java/gregtech/common/net/MessageSetFlaskCapacity.java index c20b4002..ee869a2b 100644 --- a/src/main/java/gregtech/common/net/MessageSetFlaskCapacity.java +++ b/src/main/java/gregtech/common/net/MessageSetFlaskCapacity.java @@ -1,42 +1,67 @@ package gregtech.common.net; -import cpw.mods.fml.common.network.simpleimpl.IMessage; -import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; -import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import gregtech.api.net.GT_Packet; import gregtech.common.items.GT_VolumetricFlask; -import io.netty.buffer.ByteBuf; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; +import net.minecraftforge.common.DimensionManager; -public final class MessageSetFlaskCapacity implements IMessage { - private int capacity; +public final class MessageSetFlaskCapacity extends GT_Packet { + private int capacity, dimID, playerID; public MessageSetFlaskCapacity() { + super(true); } - public MessageSetFlaskCapacity(int capacity) { + public MessageSetFlaskCapacity(int capacity, int dimID, int playerID) { + super(false); this.capacity = capacity; + this.dimID = dimID; + this.playerID = playerID; } - - public void fromBytes(ByteBuf buf) { - this.capacity = buf.readInt(); + public MessageSetFlaskCapacity(int capacity, EntityPlayer p) { + super(false); + this.capacity = capacity; + this.dimID = p.worldObj.provider.dimensionId; + this.playerID = p.getEntityId(); } - public void toBytes(ByteBuf buf) { - buf.writeInt(this.capacity); + @Override + public byte getPacketID() { + return 5; } - public static final class Handler - implements IMessageHandler { - public IMessage onMessage(MessageSetFlaskCapacity message, MessageContext ctx) { - ItemStack stack = ctx.getServerHandler().playerEntity.getHeldItem(); + @Override + public byte[] encode() { + ByteArrayDataOutput tOut = ByteStreams.newDataOutput(10); + tOut.writeInt(capacity); + tOut.writeInt(dimID); + tOut.writeInt(playerID); + return tOut.toByteArray(); + } + + @Override + public GT_Packet decode(ByteArrayDataInput aData) { + return new MessageSetFlaskCapacity(aData.readInt(), aData.readInt(), aData.readInt()); + } + + @Override + public void process(IBlockAccess aWorld) { + World w = DimensionManager.getWorld(dimID); + if (w != null && w.getEntityByID(playerID) instanceof EntityPlayer) { + ItemStack stack = ((EntityPlayer) w.getEntityByID(playerID)).getHeldItem(); if ((stack != null) && (stack.stackSize > 0)) { Item item = stack.getItem(); if ((item instanceof GT_VolumetricFlask)) - ((GT_VolumetricFlask) item).setCapacity(stack, message.capacity); + ((GT_VolumetricFlask) item).setCapacity(stack, capacity); } - return null; } } } \ No newline at end of file