Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flask crash on server #1506

Merged
merged 1 commit into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/main/java/gregtech/common/GT_Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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.*;
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
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;
Expand Down Expand Up @@ -115,7 +115,7 @@ protected void keyTyped(char character, int key) {
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();
}

Expand Down
59 changes: 42 additions & 17 deletions src/main/java/gregtech/common/net/MessageSetFlaskCapacity.java
Original file line number Diff line number Diff line change
@@ -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 MessageSetFlaskCapacity(int capacity, EntityPlayer p) {
super(false);
this.capacity = capacity;
this.dimID = p.worldObj.provider.dimensionId;
this.playerID = p.getEntityId();
}

@Override
public byte getPacketID() {
return 5;
}

public void fromBytes(ByteBuf buf) {
this.capacity = buf.readInt();
@Override
public byte[] encode() {
ByteArrayDataOutput tOut = ByteStreams.newDataOutput(10);
tOut.writeInt(capacity);
tOut.writeInt(dimID);
tOut.writeInt(playerID);
return tOut.toByteArray();
}

public void toBytes(ByteBuf buf) {
buf.writeInt(this.capacity);
@Override
public GT_Packet decode(ByteArrayDataInput aData) {
return new MessageSetFlaskCapacity(aData.readInt(), aData.readInt(), aData.readInt());
}

public static final class Handler
implements IMessageHandler<MessageSetFlaskCapacity, IMessage> {
public IMessage onMessage(MessageSetFlaskCapacity message, MessageContext ctx) {
ItemStack stack = ctx.getServerHandler().playerEntity.getHeldItem();
@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;
}
}
}