mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-22 04:52:25 +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,7 +1,6 @@
|
||||
package mekanism.common.network;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -9,6 +8,10 @@ import mekanism.api.Coord4D;
|
||||
import mekanism.common.IInvConfiguration;
|
||||
import mekanism.common.ITileNetwork;
|
||||
import mekanism.common.Mekanism;
|
||||
import mekanism.common.PacketHandler;
|
||||
import mekanism.common.network.PacketConfigurationUpdate.ConfigurationUpdateMessage;
|
||||
import mekanism.common.network.PacketConfigurationUpdate.ConfigurationUpdateMessage.ConfigurationPacket;
|
||||
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
|
||||
import mekanism.common.tile.TileEntityBasicBlock;
|
||||
import mekanism.common.util.MekanismUtils;
|
||||
import mekanism.common.util.TransporterUtils;
|
||||
@@ -16,172 +19,179 @@ import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
||||
|
||||
public class PacketConfigurationUpdate extends MekanismPacket
|
||||
public class PacketConfigurationUpdate implements IMessageHandler<ConfigurationUpdateMessage, IMessage>
|
||||
{
|
||||
public Coord4D coord4D;
|
||||
|
||||
public int configIndex;
|
||||
|
||||
public int inputSide;
|
||||
|
||||
public int clickType;
|
||||
|
||||
public ConfigurationPacket packetType;
|
||||
|
||||
public PacketConfigurationUpdate() {}
|
||||
|
||||
public PacketConfigurationUpdate(ConfigurationPacket type, Coord4D coord, int click, int extra)
|
||||
{
|
||||
packetType = type;
|
||||
|
||||
coord4D = coord;
|
||||
|
||||
if(packetType == ConfigurationPacket.EJECT_COLOR)
|
||||
{
|
||||
clickType = click;
|
||||
}
|
||||
|
||||
if(packetType == ConfigurationPacket.SIDE_DATA)
|
||||
{
|
||||
clickType = click;
|
||||
configIndex = extra;
|
||||
}
|
||||
|
||||
if(packetType == ConfigurationPacket.INPUT_COLOR)
|
||||
{
|
||||
clickType = click;
|
||||
inputSide = extra;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, ByteBuf dataStream)
|
||||
public IMessage onMessage(ConfigurationUpdateMessage message, MessageContext context)
|
||||
{
|
||||
dataStream.writeInt(packetType.ordinal());
|
||||
|
||||
dataStream.writeInt(coord4D.xCoord);
|
||||
dataStream.writeInt(coord4D.yCoord);
|
||||
dataStream.writeInt(coord4D.zCoord);
|
||||
|
||||
dataStream.writeInt(coord4D.dimensionId);
|
||||
|
||||
if(packetType != ConfigurationPacket.EJECT && packetType != ConfigurationPacket.STRICT_INPUT)
|
||||
{
|
||||
dataStream.writeInt(clickType);
|
||||
}
|
||||
|
||||
if(packetType == ConfigurationPacket.SIDE_DATA)
|
||||
{
|
||||
dataStream.writeInt(configIndex);
|
||||
}
|
||||
|
||||
if(packetType == ConfigurationPacket.INPUT_COLOR)
|
||||
{
|
||||
dataStream.writeInt(inputSide);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(ChannelHandlerContext ctx, EntityPlayer player, ByteBuf dataStream)
|
||||
{
|
||||
packetType = ConfigurationPacket.values()[dataStream.readInt()];
|
||||
|
||||
coord4D = new Coord4D(dataStream.readInt(), dataStream.readInt(), dataStream.readInt(), dataStream.readInt());
|
||||
|
||||
TileEntity tile = coord4D.getTileEntity(player.worldObj);
|
||||
|
||||
TileEntity tile = message.coord4D.getTileEntity(PacketHandler.getPlayer(context).worldObj);
|
||||
|
||||
if(tile instanceof IInvConfiguration)
|
||||
{
|
||||
IInvConfiguration config = (IInvConfiguration)tile;
|
||||
|
||||
if(packetType == ConfigurationPacket.EJECT)
|
||||
if(message.packetType == ConfigurationPacket.EJECT)
|
||||
{
|
||||
config.getEjector().setEjecting(!config.getEjector().isEjecting());
|
||||
}
|
||||
else if(packetType == ConfigurationPacket.SIDE_DATA)
|
||||
else if(message.packetType == ConfigurationPacket.SIDE_DATA)
|
||||
{
|
||||
clickType = dataStream.readInt();
|
||||
configIndex = dataStream.readInt();
|
||||
|
||||
if(clickType == 0)
|
||||
if(message.clickType == 0)
|
||||
{
|
||||
MekanismUtils.incrementOutput((IInvConfiguration)tile, configIndex);
|
||||
MekanismUtils.incrementOutput((IInvConfiguration)tile, message.configIndex);
|
||||
}
|
||||
else if(clickType == 1)
|
||||
else if(message.clickType == 1)
|
||||
{
|
||||
MekanismUtils.decrementOutput((IInvConfiguration)tile, configIndex);
|
||||
MekanismUtils.decrementOutput((IInvConfiguration)tile, message.configIndex);
|
||||
}
|
||||
else if(clickType == 2)
|
||||
else if(message.clickType == 2)
|
||||
{
|
||||
((IInvConfiguration)tile).getConfiguration()[configIndex] = 0;
|
||||
((IInvConfiguration)tile).getConfiguration()[message.configIndex] = 0;
|
||||
}
|
||||
|
||||
Mekanism.packetPipeline.sendToAllAround(new PacketTileEntity(coord4D, ((ITileNetwork) tile).getNetworkedData(new ArrayList())), coord4D.getTargetPoint(50D));
|
||||
Mekanism.packetHandler.sendToAllAround(new TileEntityMessage(message.coord4D, ((ITileNetwork)tile).getNetworkedData(new ArrayList())), message.coord4D.getTargetPoint(50D));
|
||||
}
|
||||
else if(packetType == ConfigurationPacket.EJECT_COLOR)
|
||||
else if(message.packetType == ConfigurationPacket.EJECT_COLOR)
|
||||
{
|
||||
clickType = dataStream.readInt();
|
||||
|
||||
if(clickType == 0)
|
||||
if(message.clickType == 0)
|
||||
{
|
||||
config.getEjector().setOutputColor(TransporterUtils.increment(config.getEjector().getOutputColor()));
|
||||
}
|
||||
else if(clickType == 1)
|
||||
else if(message.clickType == 1)
|
||||
{
|
||||
config.getEjector().setOutputColor(TransporterUtils.decrement(config.getEjector().getOutputColor()));
|
||||
}
|
||||
else if(clickType == 2)
|
||||
else if(message.clickType == 2)
|
||||
{
|
||||
config.getEjector().setOutputColor(null);
|
||||
}
|
||||
}
|
||||
else if(packetType == ConfigurationPacket.INPUT_COLOR)
|
||||
else if(message.packetType == ConfigurationPacket.INPUT_COLOR)
|
||||
{
|
||||
clickType = dataStream.readInt();
|
||||
inputSide = dataStream.readInt();
|
||||
ForgeDirection side = ForgeDirection.getOrientation(message.inputSide);
|
||||
|
||||
ForgeDirection side = ForgeDirection.getOrientation(inputSide);
|
||||
|
||||
if(clickType == 0)
|
||||
if(message.clickType == 0)
|
||||
{
|
||||
config.getEjector().setInputColor(side, TransporterUtils.increment(config.getEjector().getInputColor(side)));
|
||||
}
|
||||
else if(clickType == 1)
|
||||
else if(message.clickType == 1)
|
||||
{
|
||||
config.getEjector().setInputColor(side, TransporterUtils.decrement(config.getEjector().getInputColor(side)));
|
||||
}
|
||||
else if(clickType == 2)
|
||||
else if(message.clickType == 2)
|
||||
{
|
||||
config.getEjector().setInputColor(side, null);
|
||||
}
|
||||
}
|
||||
else if(packetType == ConfigurationPacket.STRICT_INPUT)
|
||||
else if(message.packetType == ConfigurationPacket.STRICT_INPUT)
|
||||
{
|
||||
config.getEjector().setStrictInput(!config.getEjector().hasStrictInput());
|
||||
}
|
||||
|
||||
for(EntityPlayer p : ((TileEntityBasicBlock)config).playersUsing)
|
||||
{
|
||||
Mekanism.packetPipeline.sendTo(new PacketTileEntity(coord4D, ((ITileNetwork) tile).getNetworkedData(new ArrayList())), (EntityPlayerMP)p);
|
||||
Mekanism.packetHandler.sendTo(new TileEntityMessage(message.coord4D, ((ITileNetwork)tile).getNetworkedData(new ArrayList())), (EntityPlayerMP)p);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleClientSide(EntityPlayer player)
|
||||
|
||||
public static class ConfigurationUpdateMessage implements IMessage
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleServerSide(EntityPlayer player)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static enum ConfigurationPacket
|
||||
{
|
||||
EJECT, SIDE_DATA, EJECT_COLOR, INPUT_COLOR, STRICT_INPUT
|
||||
public Coord4D coord4D;
|
||||
|
||||
public int configIndex;
|
||||
|
||||
public int inputSide;
|
||||
|
||||
public int clickType;
|
||||
|
||||
public ConfigurationPacket packetType;
|
||||
|
||||
public ConfigurationUpdateMessage() {}
|
||||
|
||||
public ConfigurationUpdateMessage(ConfigurationPacket type, Coord4D coord, int click, int extra)
|
||||
{
|
||||
packetType = type;
|
||||
|
||||
coord4D = coord;
|
||||
|
||||
if(packetType == ConfigurationPacket.EJECT_COLOR)
|
||||
{
|
||||
clickType = click;
|
||||
}
|
||||
|
||||
if(packetType == ConfigurationPacket.SIDE_DATA)
|
||||
{
|
||||
clickType = click;
|
||||
configIndex = extra;
|
||||
}
|
||||
|
||||
if(packetType == ConfigurationPacket.INPUT_COLOR)
|
||||
{
|
||||
clickType = click;
|
||||
inputSide = extra;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf dataStream)
|
||||
{
|
||||
dataStream.writeInt(packetType.ordinal());
|
||||
|
||||
dataStream.writeInt(coord4D.xCoord);
|
||||
dataStream.writeInt(coord4D.yCoord);
|
||||
dataStream.writeInt(coord4D.zCoord);
|
||||
|
||||
dataStream.writeInt(coord4D.dimensionId);
|
||||
|
||||
if(packetType != ConfigurationPacket.EJECT && packetType != ConfigurationPacket.STRICT_INPUT)
|
||||
{
|
||||
dataStream.writeInt(clickType);
|
||||
}
|
||||
|
||||
if(packetType == ConfigurationPacket.SIDE_DATA)
|
||||
{
|
||||
dataStream.writeInt(configIndex);
|
||||
}
|
||||
|
||||
if(packetType == ConfigurationPacket.INPUT_COLOR)
|
||||
{
|
||||
dataStream.writeInt(inputSide);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf dataStream)
|
||||
{
|
||||
packetType = ConfigurationPacket.values()[dataStream.readInt()];
|
||||
|
||||
coord4D = new Coord4D(dataStream.readInt(), dataStream.readInt(), dataStream.readInt(), dataStream.readInt());
|
||||
|
||||
if(packetType == ConfigurationPacket.SIDE_DATA)
|
||||
{
|
||||
clickType = dataStream.readInt();
|
||||
configIndex = dataStream.readInt();
|
||||
}
|
||||
else if(packetType == ConfigurationPacket.EJECT_COLOR)
|
||||
{
|
||||
clickType = dataStream.readInt();
|
||||
}
|
||||
else if(packetType == ConfigurationPacket.INPUT_COLOR)
|
||||
{
|
||||
clickType = dataStream.readInt();
|
||||
inputSide = dataStream.readInt();
|
||||
}
|
||||
}
|
||||
|
||||
public static enum ConfigurationPacket
|
||||
{
|
||||
EJECT, SIDE_DATA, EJECT_COLOR, INPUT_COLOR, STRICT_INPUT
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user