Fix flask crash on server

This commit is contained in:
Dimach 2018-12-13 12:22:51 +02:00
parent cd841ad39e
commit db7c85e641
3 changed files with 46 additions and 25 deletions

View file

@ -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<Side, FMLEmbeddedChannel> 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<Object> aOutput)

View file

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

View file

@ -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<MessageSetFlaskCapacity, IMessage> {
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;
}
}
}