mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-22 21:09:45 +00:00
Rewrite networking code for the 100th time to fix memory leak
This commit is contained in:
1 parent
be3883195c
commit
e5f76907ce
103 files changed
+2683
-2759
No files matched your search
@@ -1,99 +1,102 @@
|
||||
package mekanism.common.network;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import mekanism.api.Coord4D;
|
||||
import mekanism.common.Mekanism;
|
||||
import mekanism.common.PacketHandler;
|
||||
import mekanism.common.network.PacketSimpleGui.SimpleGuiMessage;
|
||||
import mekanism.common.tile.TileEntityBasicBlock;
|
||||
import net.minecraft.client.gui.GuiScreen;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.world.World;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
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 cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
public class PacketSimpleGui extends MekanismPacket
|
||||
public class PacketSimpleGui implements IMessageHandler<SimpleGuiMessage, IMessage>
|
||||
{
|
||||
public Coord4D coord4D;
|
||||
|
||||
public int guiId;
|
||||
|
||||
public PacketSimpleGui() {}
|
||||
|
||||
public PacketSimpleGui(Coord4D coord, int gui)
|
||||
{
|
||||
coord4D = coord;
|
||||
guiId = gui;
|
||||
}
|
||||
|
||||
public static void openServerGui(int id, EntityPlayerMP playerMP, World world, Coord4D obj)
|
||||
{
|
||||
playerMP.closeContainer();
|
||||
playerMP.getNextWindowId();
|
||||
|
||||
int window = playerMP.currentWindowId;
|
||||
|
||||
Mekanism.packetPipeline.sendTo(new PacketSimpleGui(obj, id), playerMP);
|
||||
|
||||
playerMP.openContainer = Mekanism.proxy.getServerGui(id, playerMP, world, obj.xCoord, obj.yCoord, obj.zCoord);
|
||||
playerMP.openContainer.windowId = window;
|
||||
playerMP.openContainer.addCraftingToCrafters(playerMP);
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public GuiScreen getGui(int id, EntityPlayer player, World world, Coord4D obj)
|
||||
{
|
||||
return (GuiScreen)Mekanism.proxy.getClientGui(id, player, world, obj.xCoord, obj.yCoord, obj.zCoord);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, ByteBuf dataStream)
|
||||
public IMessage onMessage(SimpleGuiMessage message, MessageContext context)
|
||||
{
|
||||
dataStream.writeInt(coord4D.xCoord);
|
||||
dataStream.writeInt(coord4D.yCoord);
|
||||
dataStream.writeInt(coord4D.zCoord);
|
||||
|
||||
dataStream.writeInt(coord4D.dimensionId);
|
||||
|
||||
dataStream.writeInt(guiId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(ChannelHandlerContext ctx, EntityPlayer player, ByteBuf dataStream)
|
||||
{
|
||||
coord4D = new Coord4D(dataStream.readInt(), dataStream.readInt(), dataStream.readInt(), dataStream.readInt());
|
||||
|
||||
guiId = dataStream.readInt();
|
||||
|
||||
EntityPlayer player = PacketHandler.getPlayer(context);
|
||||
|
||||
if(!player.worldObj.isRemote)
|
||||
{
|
||||
World worldServer = FMLCommonHandler.instance().getMinecraftServerInstance().worldServerForDimension(coord4D.dimensionId);
|
||||
World worldServer = FMLCommonHandler.instance().getMinecraftServerInstance().worldServerForDimension(message.coord4D.dimensionId);
|
||||
|
||||
if(worldServer != null && coord4D.getTileEntity(worldServer) instanceof TileEntityBasicBlock)
|
||||
if(worldServer != null && message.coord4D.getTileEntity(worldServer) instanceof TileEntityBasicBlock)
|
||||
{
|
||||
if(guiId == -1)
|
||||
if(message.guiId == -1)
|
||||
{
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
openServerGui(guiId, (EntityPlayerMP)player, player.worldObj, coord4D);
|
||||
SimpleGuiMessage.openServerGui(message.guiId, (EntityPlayerMP)player, player.worldObj, message.coord4D);
|
||||
}
|
||||
}
|
||||
else {
|
||||
FMLCommonHandler.instance().showGuiScreen(getGui(guiId, player, player.worldObj, coord4D));
|
||||
FMLCommonHandler.instance().showGuiScreen(SimpleGuiMessage.getGui(message.guiId, player, player.worldObj, message.coord4D));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class SimpleGuiMessage implements IMessage
|
||||
{
|
||||
public Coord4D coord4D;
|
||||
|
||||
public int guiId;
|
||||
|
||||
public SimpleGuiMessage() {}
|
||||
|
||||
public SimpleGuiMessage(Coord4D coord, int gui)
|
||||
{
|
||||
coord4D = coord;
|
||||
guiId = gui;
|
||||
}
|
||||
|
||||
public static void openServerGui(int id, EntityPlayerMP playerMP, World world, Coord4D obj)
|
||||
{
|
||||
playerMP.closeContainer();
|
||||
playerMP.getNextWindowId();
|
||||
|
||||
int window = playerMP.currentWindowId;
|
||||
|
||||
Mekanism.packetHandler.sendTo(new SimpleGuiMessage(obj, id), playerMP);
|
||||
|
||||
playerMP.openContainer = Mekanism.proxy.getServerGui(id, playerMP, world, obj.xCoord, obj.yCoord, obj.zCoord);
|
||||
playerMP.openContainer.windowId = window;
|
||||
playerMP.openContainer.addCraftingToCrafters(playerMP);
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static GuiScreen getGui(int id, EntityPlayer player, World world, Coord4D obj)
|
||||
{
|
||||
return (GuiScreen)Mekanism.proxy.getClientGui(id, player, world, obj.xCoord, obj.yCoord, obj.zCoord);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf dataStream)
|
||||
{
|
||||
dataStream.writeInt(coord4D.xCoord);
|
||||
dataStream.writeInt(coord4D.yCoord);
|
||||
dataStream.writeInt(coord4D.zCoord);
|
||||
|
||||
dataStream.writeInt(coord4D.dimensionId);
|
||||
|
||||
dataStream.writeInt(guiId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf dataStream)
|
||||
{
|
||||
coord4D = new Coord4D(dataStream.readInt(), dataStream.readInt(), dataStream.readInt(), dataStream.readInt());
|
||||
|
||||
guiId = dataStream.readInt();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleClientSide(EntityPlayer player)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleServerSide(EntityPlayer player)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user