From 14003341e38f9da8df9a9ab719ff8c3fbe19d988 Mon Sep 17 00:00:00 2001 From: SpaceToad Date: Mon, 13 Jan 2014 20:26:16 +0100 Subject: [PATCH] progress made in MC 1.7 network support --- common/buildcraft/BuildCraftBuilders.java | 11 ++- common/buildcraft/BuildCraftCore.java | 19 ++-- common/buildcraft/BuildCraftEnergy.java | 5 +- common/buildcraft/BuildCraftFactory.java | 13 ++- common/buildcraft/BuildCraftMod.java | 34 +++++++ common/buildcraft/BuildCraftSilicon.java | 11 ++- common/buildcraft/BuildCraftTransport.java | 10 +- .../network/PacketHandlerBuilders.java | 37 +++++--- .../builders/network/PacketLibraryAction.java | 6 +- common/buildcraft/core/TileBuildCraft.java | 2 +- .../network/BuildCraftChannelHandler.java | 21 +++++ .../core/network/BuildCraftPacket.java | 30 +----- .../core/network/ISynchronizedTile.java | 2 +- .../core/network/PacketCoordinates.java | 6 +- .../core/network/PacketGuiReturn.java | 6 +- .../core/network/PacketGuiWidget.java | 7 +- .../core/network/PacketHandler.java | 39 ++++---- common/buildcraft/core/network/PacketNBT.java | 7 +- .../core/network/PacketPayload.java | 6 +- .../core/network/PacketPayloadArrays.java | 10 +- .../core/network/PacketPayloadStream.java | 10 +- .../core/network/PacketSlotChange.java | 7 +- .../core/network/PacketTileState.java | 6 +- .../buildcraft/core/network/PacketUpdate.java | 6 +- common/buildcraft/core/proxy/CoreProxy.java | 37 ++++---- .../core/proxy/CoreProxyClient.java | 12 ++- common/buildcraft/core/utils/BlockUtil.java | 2 +- common/buildcraft/core/utils/Utils.java | 26 ++++++ common/buildcraft/factory/TileQuarry.java | 2 +- .../factory/network/PacketHandlerFactory.java | 32 ++++--- .../silicon/network/PacketHandlerSilicon.java | 48 +++++----- .../buildcraft/transport/TileGenericPipe.java | 2 +- .../transport/gui/ContainerGateInterface.java | 2 +- .../transport/network/PacketFluidUpdate.java | 7 +- .../network/PacketGateExpansionMap.java | 13 ++- .../network/PacketHandlerTransport.java | 91 +++++++++---------- .../network/PacketPipeTransportItemStack.java | 9 +- .../PacketPipeTransportItemStackRequest.java | 6 +- .../network/PacketPipeTransportTraveler.java | 7 +- .../transport/network/PacketPowerUpdate.java | 6 +- .../network/TransportConnectionHandler.java | 2 +- 41 files changed, 384 insertions(+), 231 deletions(-) create mode 100755 common/buildcraft/BuildCraftMod.java create mode 100755 common/buildcraft/core/network/BuildCraftChannelHandler.java diff --git a/common/buildcraft/BuildCraftBuilders.java b/common/buildcraft/BuildCraftBuilders.java index 075a92eb..70d2bcbd 100644 --- a/common/buildcraft/BuildCraftBuilders.java +++ b/common/buildcraft/BuildCraftBuilders.java @@ -64,6 +64,7 @@ import buildcraft.core.InterModComms; import buildcraft.core.Version; import buildcraft.core.blueprints.BptPlayerIndex; import buildcraft.core.blueprints.BptRootIndex; +import buildcraft.core.network.PacketHandler; import buildcraft.core.proxy.CoreProxy; import buildcraft.core.utils.BCLog; import cpw.mods.fml.common.Mod; @@ -79,9 +80,11 @@ import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.common.registry.LanguageRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; + import java.io.IOException; import java.util.LinkedList; import java.util.TreeMap; + import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -93,8 +96,7 @@ import net.minecraftforge.common.config.Property; import net.minecraftforge.event.ForgeSubscribe; @Mod(name = "BuildCraft Builders", version = Version.VERSION, useMetadata = false, modid = "BuildCraft|Builders", dependencies = DefaultProps.DEPENDENCY_CORE) -@NetworkMod(channels = {DefaultProps.NET_CHANNEL_NAME}, packetHandler = PacketHandlerBuilders.class, clientSideRequired = true, serverSideRequired = true) -public class BuildCraftBuilders { +public class BuildCraftBuilders extends BuildCraftMod { public static final int LIBRARY_PAGE_SIZE = 12; public static final int MAX_BLUEPRINTS_NAME_SIZE = 14; @@ -118,8 +120,11 @@ public class BuildCraftBuilders { @EventHandler public void init(FMLInitializationEvent evt) { + channels = NetworkRegistry.INSTANCE.newChannel + (DefaultProps.NET_CHANNEL_NAME + "-BUILDERS", new PacketHandlerBuilders()); + // Register gui handler - NetworkRegistry.instance().registerGuiHandler(instance, new GuiHandler()); + NetworkRegistry.INSTANCE.registerGuiHandler(instance, new GuiHandler()); // Register save handler MinecraftForge.EVENT_BUS.register(new EventHandlerBuilders()); diff --git a/common/buildcraft/BuildCraftCore.java b/common/buildcraft/BuildCraftCore.java index b1ba8145..ca644063 100644 --- a/common/buildcraft/BuildCraftCore.java +++ b/common/buildcraft/BuildCraftCore.java @@ -8,15 +8,21 @@ package buildcraft; import static buildcraft.BuildCraftEnergy.spawnOilSprings; + import java.io.File; +import java.util.EnumMap; import java.util.TreeMap; import net.minecraft.block.Block; import net.minecraft.block.BlockFluid; import net.minecraft.entity.EntityList; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.network.Packet; import net.minecraft.util.IIcon; +import net.minecraft.world.World; import net.minecraftforge.client.event.TextureStitchEvent; import net.minecraftforge.common.config.Configuration; import net.minecraftforge.common.IPlantable; @@ -47,6 +53,7 @@ import buildcraft.core.SpringPopulate; import buildcraft.core.TickHandlerCoreClient; import buildcraft.core.Version; import buildcraft.core.blueprints.BptItem; +import buildcraft.core.network.BuildCraftPacket; import buildcraft.core.network.EntityIds; import buildcraft.core.network.PacketHandler; import buildcraft.core.network.PacketUpdate; @@ -76,7 +83,8 @@ import cpw.mods.fml.common.event.FMLInterModComms; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.event.FMLServerStartingEvent; -import cpw.mods.fml.common.network.NetworkMod; +import cpw.mods.fml.common.network.FMLEmbeddedChannel; +import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.EntityRegistry; import cpw.mods.fml.common.registry.LanguageRegistry; import cpw.mods.fml.common.registry.TickRegistry; @@ -84,11 +92,8 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @Mod(name = "BuildCraft", version = Version.VERSION, useMetadata = false, modid = "BuildCraft|Core", acceptedMinecraftVersions = "[1.6.4,1.7)", dependencies = "required-after:Forge@[9.11.1.953,)") -@NetworkMod(channels = {DefaultProps.NET_CHANNEL_NAME}, packetHandler = PacketHandler.class, clientSideRequired = true, serverSideRequired = true) -public class BuildCraftCore { - +public class BuildCraftCore extends BuildCraftMod { public static enum RenderMode { - Full, NoDynamic }; public static RenderMode render = RenderMode.Full; @@ -264,7 +269,9 @@ public class BuildCraftCore { @EventHandler public void initialize(FMLInitializationEvent evt) { - // MinecraftForge.registerConnectionHandler(new ConnectionHandler()); + channels = NetworkRegistry.INSTANCE.newChannel + (DefaultProps.NET_CHANNEL_NAME + "-CORE", new PacketHandler()); + ActionManager.registerTriggerProvider(new DefaultTriggerProvider()); ActionManager.registerActionProvider(new DefaultActionProvider()); diff --git a/common/buildcraft/BuildCraftEnergy.java b/common/buildcraft/BuildCraftEnergy.java index 5c7630d9..0ece1f6c 100644 --- a/common/buildcraft/BuildCraftEnergy.java +++ b/common/buildcraft/BuildCraftEnergy.java @@ -62,8 +62,7 @@ import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidStack; @Mod(name = "BuildCraft Energy", version = Version.VERSION, useMetadata = false, modid = "BuildCraft|Energy", dependencies = DefaultProps.DEPENDENCY_CORE) -@NetworkMod(channels = {DefaultProps.NET_CHANNEL_NAME}, clientSideRequired = true, serverSideRequired = true) -public class BuildCraftEnergy { +public class BuildCraftEnergy extends BuildCraftMod { public final static int ENERGY_REMOVE_BLOCK = 25; public final static int ENERGY_EXTRACT_ITEM = 2; @@ -222,7 +221,7 @@ public class BuildCraftEnergy { @EventHandler public void init(FMLInitializationEvent evt) { - NetworkRegistry.instance().registerGuiHandler(instance, new GuiHandler()); + NetworkRegistry.INSTANCE.registerGuiHandler(instance, new GuiHandler()); new BptBlockEngine(engineBlock.blockID); diff --git a/common/buildcraft/BuildCraftFactory.java b/common/buildcraft/BuildCraftFactory.java index 0137787a..62cf52ec 100644 --- a/common/buildcraft/BuildCraftFactory.java +++ b/common/buildcraft/BuildCraftFactory.java @@ -10,6 +10,7 @@ package buildcraft; import buildcraft.core.DefaultProps; import buildcraft.core.InterModComms; import buildcraft.core.Version; +import buildcraft.core.network.PacketHandler; import buildcraft.core.proxy.CoreProxy; import buildcraft.core.utils.ConfigUtils; import buildcraft.factory.BlockAutoWorkbench; @@ -39,7 +40,9 @@ import buildcraft.factory.TileQuarry; import buildcraft.factory.TileRefinery; import buildcraft.factory.TileTank; import buildcraft.factory.network.PacketHandlerFactory; + import com.google.common.collect.Lists; + import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Instance; @@ -51,7 +54,9 @@ import cpw.mods.fml.common.network.NetworkMod; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; + import java.util.List; + import net.minecraft.block.Block; import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.item.Item; @@ -66,8 +71,7 @@ import net.minecraftforge.common.config.Property; import net.minecraftforge.event.ForgeSubscribe; @Mod(name = "BuildCraft Factory", version = Version.VERSION, useMetadata = false, modid = "BuildCraft|Factory", dependencies = DefaultProps.DEPENDENCY_CORE) -@NetworkMod(channels = {DefaultProps.NET_CHANNEL_NAME}, packetHandler = PacketHandlerFactory.class, clientSideRequired = true, serverSideRequired = true) -public class BuildCraftFactory { +public class BuildCraftFactory extends BuildCraftMod { public static final int MINING_MJ_COST_PER_BLOCK = 64; public static BlockQuarry quarryBlock; @@ -127,7 +131,7 @@ public class BuildCraftFactory { @EventHandler public void load(FMLInitializationEvent evt) { - NetworkRegistry.instance().registerGuiHandler(instance, new GuiHandler()); + NetworkRegistry.INSTANCE.registerGuiHandler(instance, new GuiHandler()); // EntityRegistry.registerModEntity(EntityMechanicalArm.class, "bcMechanicalArm", EntityIds.MECHANICAL_ARM, instance, 50, 1, true); @@ -154,6 +158,9 @@ public class BuildCraftFactory { @EventHandler public void initialize(FMLPreInitializationEvent evt) { + channels = NetworkRegistry.INSTANCE.newChannel + (DefaultProps.NET_CHANNEL_NAME + "-FACTORY", new PacketHandlerFactory()); + ConfigUtils genCat = new ConfigUtils(BuildCraftCore.mainConfiguration, Configuration.CATEGORY_GENERAL); allowMining = genCat.get("mining.enabled", true, "disables the recipes for automated mining machines"); diff --git a/common/buildcraft/BuildCraftMod.java b/common/buildcraft/BuildCraftMod.java new file mode 100755 index 00000000..2064175a --- /dev/null +++ b/common/buildcraft/BuildCraftMod.java @@ -0,0 +1,34 @@ +package buildcraft; + +import java.util.EnumMap; + +import cpw.mods.fml.common.network.FMLEmbeddedChannel; +import cpw.mods.fml.common.network.FMLOutboundHandler; +import cpw.mods.fml.common.network.FMLOutboundHandler.OutboundTarget; +import cpw.mods.fml.relauncher.Side; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.network.Packet; +import net.minecraft.world.World; +import buildcraft.core.network.BuildCraftPacket; + +public class BuildCraftMod { + public EnumMap channels; + + public void sendToPlayers(BuildCraftPacket packet, World world, int x, int y, int z, int maxDistance) { + channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL); + channels.get(Side.SERVER).writeOutbound(packet); + } + + public void sendToPlayer(EntityPlayer entityplayer, BuildCraftPacket packet) { + channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER); + channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(entityplayer); + channels.get(Side.SERVER).writeOutbound(packet); + } + + public void sendToServer(BuildCraftPacket packet) { + channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(OutboundTarget.TOSERVER); + channels.get(Side.CLIENT).writeOutbound(packet); + } + +} \ No newline at end of file diff --git a/common/buildcraft/BuildCraftSilicon.java b/common/buildcraft/BuildCraftSilicon.java index 6bb2e70d..ae72053e 100644 --- a/common/buildcraft/BuildCraftSilicon.java +++ b/common/buildcraft/BuildCraftSilicon.java @@ -16,6 +16,7 @@ import buildcraft.core.InterModComms; import buildcraft.silicon.ItemRedstoneChipset; import buildcraft.silicon.ItemRedstoneChipset.Chipset; import buildcraft.core.Version; +import buildcraft.core.network.PacketHandler; import buildcraft.core.proxy.CoreProxy; import buildcraft.silicon.BlockLaser; import buildcraft.silicon.BlockLaserTable; @@ -44,17 +45,18 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkMod; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.LanguageRegistry; + import java.util.ArrayList; import java.util.EnumSet; import java.util.List; + import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraftforge.common.config.Property; @Mod(name = "BuildCraft Silicon", version = Version.VERSION, useMetadata = false, modid = "BuildCraft|Silicon", dependencies = DefaultProps.DEPENDENCY_TRANSPORT) -@NetworkMod(channels = {DefaultProps.NET_CHANNEL_NAME}, packetHandler = PacketHandlerSilicon.class, clientSideRequired = true, serverSideRequired = true) -public class BuildCraftSilicon { +public class BuildCraftSilicon extends BuildCraftMod { public static ItemRedstoneChipset redstoneChipset; public static BlockLaser laserBlock; @@ -91,7 +93,10 @@ public class BuildCraftSilicon { @EventHandler public void init(FMLInitializationEvent evt) { - NetworkRegistry.instance().registerGuiHandler(instance, new GuiHandler()); + channels = NetworkRegistry.INSTANCE.newChannel + (DefaultProps.NET_CHANNEL_NAME + "-SILICON", new PacketHandlerSilicon()); + + NetworkRegistry.INSTANCE.registerGuiHandler(instance, new GuiHandler()); CoreProxy.proxy.registerTileEntity(TileLaser.class, "net.minecraft.src.buildcraft.factory.TileLaser"); CoreProxy.proxy.registerTileEntity(TileAssemblyTable.class, "net.minecraft.src.buildcraft.factory.TileAssemblyTable"); CoreProxy.proxy.registerTileEntity(TileAdvancedCraftingTable.class, "net.minecraft.src.buildcraft.factory.TileAssemblyAdvancedWorkbench"); diff --git a/common/buildcraft/BuildCraftTransport.java b/common/buildcraft/BuildCraftTransport.java index a01d9f48..2491280f 100644 --- a/common/buildcraft/BuildCraftTransport.java +++ b/common/buildcraft/BuildCraftTransport.java @@ -16,6 +16,7 @@ import buildcraft.core.DefaultProps; import buildcraft.core.InterModComms; import buildcraft.core.ItemBuildCraft; import buildcraft.core.Version; +import buildcraft.core.network.PacketHandler; import buildcraft.core.proxy.CoreProxy; import buildcraft.core.triggers.BCAction; import buildcraft.core.triggers.BCTrigger; @@ -101,7 +102,9 @@ import cpw.mods.fml.common.network.NetworkMod; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.common.registry.LanguageRegistry; + import java.util.LinkedList; + import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; @@ -113,7 +116,7 @@ import net.minecraftforge.common.config.Property; @Mod(version = Version.VERSION, modid = "BuildCraft|Transport", name = "Buildcraft Transport", dependencies = DefaultProps.DEPENDENCY_CORE) @NetworkMod(channels = {DefaultProps.NET_CHANNEL_NAME}, packetHandler = PacketHandlerTransport.class, connectionHandler = TransportConnectionHandler.class) -public class BuildCraftTransport { +public class BuildCraftTransport extends BuildCraftMod { public static BlockGenericPipe genericPipeBlock; public static float pipeDurability; @@ -344,6 +347,9 @@ public class BuildCraftTransport { @EventHandler public void init(FMLInitializationEvent evt) { + channels = NetworkRegistry.INSTANCE.newChannel + (DefaultProps.NET_CHANNEL_NAME + "-TRANSPORT", new PacketHandlerTransport()); + // Register connection handler // MinecraftForge.registerConnectionHandler(new ConnectionHandler()); @@ -377,7 +383,7 @@ public class BuildCraftTransport { } TransportProxy.proxy.registerRenderers(); - NetworkRegistry.instance().registerGuiHandler(instance, new GuiHandler()); + NetworkRegistry.INSTANCE.registerGuiHandler(instance, new GuiHandler()); } @EventHandler diff --git a/common/buildcraft/builders/network/PacketHandlerBuilders.java b/common/buildcraft/builders/network/PacketHandlerBuilders.java index 56576f7b..84eb18c3 100644 --- a/common/buildcraft/builders/network/PacketHandlerBuilders.java +++ b/common/buildcraft/builders/network/PacketHandlerBuilders.java @@ -2,41 +2,50 @@ package buildcraft.builders.network; import buildcraft.builders.TileArchitect; import buildcraft.builders.TileBlueprintLibrary; +import buildcraft.core.network.BuildCraftChannelHandler; +import buildcraft.core.network.BuildCraftPacket; import buildcraft.core.network.PacketIds; import buildcraft.core.network.PacketPayloadArrays; import buildcraft.core.network.PacketUpdate; +import buildcraft.core.proxy.CoreProxy; import cpw.mods.fml.common.network.IPacketHandler; +import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.Player; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; + import java.io.ByteArrayInputStream; import java.io.DataInputStream; + import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.network.INetHandler; import net.minecraft.network.INetworkManager; -import net.minecraft.network.packet.Packet250CustomPayload; +import net.minecraft.network.Packet250CustomPayload; import net.minecraft.tileentity.TileEntity; -public class PacketHandlerBuilders implements IPacketHandler { +public class PacketHandlerBuilders extends BuildCraftChannelHandler { @Override - public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { + public void decodeInto(ChannelHandlerContext ctx, ByteBuf data, BuildCraftPacket packet) { + super.decodeInto(ctx, data, packet); - DataInputStream data = new DataInputStream(new ByteArrayInputStream(packet.data)); try { - int packetID = data.read(); + INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); + + EntityPlayer player = + CoreProxy.proxy.getPlayerFromNetHandler(netHandler); + + int packetID = packet.getID(); + switch (packetID) { case PacketIds.ARCHITECT_NAME: - PacketUpdate packetA = new PacketUpdate(); - packetA.readData(data); - onArchitectName((EntityPlayer) player, packetA); + onArchitectName(player, (PacketUpdate) packet); break; case PacketIds.LIBRARY_ACTION: - PacketLibraryAction packetB = new PacketLibraryAction(); - packetB.readData(data); - onLibraryAction((EntityPlayer) player, packetB); + onLibraryAction(player, (PacketLibraryAction) packet); break; case PacketIds.LIBRARY_SELECT: - PacketLibraryAction packetC = new PacketLibraryAction(); - packetC.readData(data); - onLibrarySelect((EntityPlayer) player, packetC); + onLibrarySelect(player, (PacketLibraryAction) packet); break; } } catch (Exception ex) { diff --git a/common/buildcraft/builders/network/PacketLibraryAction.java b/common/buildcraft/builders/network/PacketLibraryAction.java index 5aa18491..4fcb5f28 100644 --- a/common/buildcraft/builders/network/PacketLibraryAction.java +++ b/common/buildcraft/builders/network/PacketLibraryAction.java @@ -1,6 +1,8 @@ package buildcraft.builders.network; import buildcraft.core.network.PacketCoordinates; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -17,13 +19,13 @@ public class PacketLibraryAction extends PacketCoordinates { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { data.writeInt(actionId); super.writeData(data); } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { actionId = data.readInt(); super.readData(data); } diff --git a/common/buildcraft/core/TileBuildCraft.java b/common/buildcraft/core/TileBuildCraft.java index 773f46b7..aece4119 100644 --- a/common/buildcraft/core/TileBuildCraft.java +++ b/common/buildcraft/core/TileBuildCraft.java @@ -23,7 +23,7 @@ import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.network.packet.Packet; +import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; diff --git a/common/buildcraft/core/network/BuildCraftChannelHandler.java b/common/buildcraft/core/network/BuildCraftChannelHandler.java new file mode 100755 index 00000000..9de015aa --- /dev/null +++ b/common/buildcraft/core/network/BuildCraftChannelHandler.java @@ -0,0 +1,21 @@ +package buildcraft.core.network; + +import cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; + +public class BuildCraftChannelHandler extends FMLIndexedMessageToMessageCodec { + + public BuildCraftChannelHandler() { + } + + @Override + public void encodeInto(ChannelHandlerContext ctx, BuildCraftPacket packet, ByteBuf data) throws Exception { + packet.writeData(data); + } + + @Override + public void decodeInto(ChannelHandlerContext ctx, ByteBuf data, BuildCraftPacket packet) { + packet.readData(data); + } +} diff --git a/common/buildcraft/core/network/BuildCraftPacket.java b/common/buildcraft/core/network/BuildCraftPacket.java index e1712054..70a946f4 100644 --- a/common/buildcraft/core/network/BuildCraftPacket.java +++ b/common/buildcraft/core/network/BuildCraftPacket.java @@ -1,39 +1,15 @@ package buildcraft.core.network; import buildcraft.core.DefaultProps; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import net.minecraft.network.packet.Packet; -import net.minecraft.network.packet.Packet250CustomPayload; +import io.netty.buffer.ByteBuf; public abstract class BuildCraftPacket { protected boolean isChunkDataPacket = false; - protected String channel = DefaultProps.NET_CHANNEL_NAME; public abstract int getID(); - public Packet getPacket() { + public abstract void readData(ByteBuf data); - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - DataOutputStream data = new DataOutputStream(bytes); - try { - data.writeByte(getID()); - writeData(data); - } catch (IOException e) { - e.printStackTrace(); - } - Packet250CustomPayload packet = new Packet250CustomPayload(); - packet.channel = channel; - packet.data = bytes.toByteArray(); - packet.length = packet.data.length; - packet.isChunkDataPacket = this.isChunkDataPacket; - return packet; - } - - public abstract void readData(DataInputStream data) throws IOException; - - public abstract void writeData(DataOutputStream data) throws IOException; + public abstract void writeData(ByteBuf data); } diff --git a/common/buildcraft/core/network/ISynchronizedTile.java b/common/buildcraft/core/network/ISynchronizedTile.java index a72e0643..4e5c2a44 100644 --- a/common/buildcraft/core/network/ISynchronizedTile.java +++ b/common/buildcraft/core/network/ISynchronizedTile.java @@ -8,7 +8,7 @@ package buildcraft.core.network; import java.io.IOException; -import net.minecraft.network.packet.Packet; +import net.minecraft.network.Packet; public interface ISynchronizedTile { diff --git a/common/buildcraft/core/network/PacketCoordinates.java b/common/buildcraft/core/network/PacketCoordinates.java index c73a9640..3b9bb47f 100644 --- a/common/buildcraft/core/network/PacketCoordinates.java +++ b/common/buildcraft/core/network/PacketCoordinates.java @@ -1,5 +1,7 @@ package buildcraft.core.network; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -25,7 +27,7 @@ public class PacketCoordinates extends BuildCraftPacket { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { data.writeInt(posX); data.writeInt(posY); @@ -33,7 +35,7 @@ public class PacketCoordinates extends BuildCraftPacket { } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { posX = data.readInt(); posY = data.readInt(); diff --git a/common/buildcraft/core/network/PacketGuiReturn.java b/common/buildcraft/core/network/PacketGuiReturn.java index e559b24b..ce4abd21 100644 --- a/common/buildcraft/core/network/PacketGuiReturn.java +++ b/common/buildcraft/core/network/PacketGuiReturn.java @@ -1,5 +1,7 @@ package buildcraft.core.network; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -35,7 +37,7 @@ public class PacketGuiReturn extends BuildCraftPacket { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { data.writeInt(obj.getWorld().provider.dimensionId); if (obj instanceof TileEntity) { TileEntity tile = (TileEntity) obj; @@ -55,7 +57,7 @@ public class PacketGuiReturn extends BuildCraftPacket { } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { int dim = data.readInt(); World world = DimensionManager.getWorld(dim); boolean tileReturn = data.readBoolean(); diff --git a/common/buildcraft/core/network/PacketGuiWidget.java b/common/buildcraft/core/network/PacketGuiWidget.java index e8a78843..bdc1c4c8 100644 --- a/common/buildcraft/core/network/PacketGuiWidget.java +++ b/common/buildcraft/core/network/PacketGuiWidget.java @@ -10,9 +10,12 @@ package buildcraft.core.network; import buildcraft.core.gui.BuildCraftContainer; import cpw.mods.fml.client.FMLClientHandler; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; + import net.minecraft.client.entity.EntityClientPlayerMP; /** * @@ -34,14 +37,14 @@ public class PacketGuiWidget extends BuildCraftPacket { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { data.writeByte(windowId); data.writeByte(widgetId); data.write(payload); } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { windowId = data.readByte(); widgetId = data.readByte(); diff --git a/common/buildcraft/core/network/PacketHandler.java b/common/buildcraft/core/network/PacketHandler.java index 8c0feb94..eaf2b61d 100644 --- a/common/buildcraft/core/network/PacketHandler.java +++ b/common/buildcraft/core/network/PacketHandler.java @@ -1,18 +1,20 @@ package buildcraft.core.network; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; + import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; +import buildcraft.core.proxy.CoreProxy; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.INetworkManager; -import net.minecraft.network.packet.Packet250CustomPayload; +import net.minecraft.network.INetHandler; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; -import cpw.mods.fml.common.network.IPacketHandler; -import cpw.mods.fml.common.network.Player; +import cpw.mods.fml.common.network.NetworkRegistry; -public class PacketHandler implements IPacketHandler { +public class PacketHandler extends BuildCraftChannelHandler { private void onTileUpdate(EntityPlayer player, PacketTileUpdate packet) throws IOException { World world = player.worldObj; @@ -30,23 +32,28 @@ public class PacketHandler implements IPacketHandler { } @Override - public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { - DataInputStream data = new DataInputStream(new ByteArrayInputStream(packet.data)); + public void decodeInto(ChannelHandlerContext ctx, ByteBuf data, BuildCraftPacket packet) { + super.decodeInto(ctx, data, packet); + try { - int packetID = data.read(); + INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); + + EntityPlayer player = + CoreProxy.proxy.getPlayerFromNetHandler(netHandler); + + int packetID = packet.getID(); + switch (packetID) { case PacketIds.TILE_UPDATE: { - PacketTileUpdate pkt = new PacketTileUpdate(); - pkt.readData(data); - onTileUpdate((EntityPlayer) player, pkt); + onTileUpdate(player, (PacketTileUpdate) packet); break; } case PacketIds.STATE_UPDATE: { - PacketTileState pkt = new PacketTileState(); + PacketTileState pkt = (PacketTileState) packet; pkt.readData(data); - World world = ((EntityPlayer) player).worldObj; + World world = player.worldObj; TileEntity tile = world.getBlockTileEntity(pkt.posX, pkt.posY, pkt.posZ); if (tile instanceof ISyncedTile) { pkt.applyStates(data, (ISyncedTile) tile); @@ -55,14 +62,12 @@ public class PacketHandler implements IPacketHandler { } case PacketIds.GUI_RETURN: { - PacketGuiReturn pkt = new PacketGuiReturn((EntityPlayer) player); - pkt.readData(data); + // action will have happened already at read time break; } case PacketIds.GUI_WIDGET: { - PacketGuiWidget pkt = new PacketGuiWidget(); - pkt.readData(data); + // action will have happened already at read time break; } } diff --git a/common/buildcraft/core/network/PacketNBT.java b/common/buildcraft/core/network/PacketNBT.java index 0cf5a19a..92a1a929 100644 --- a/common/buildcraft/core/network/PacketNBT.java +++ b/common/buildcraft/core/network/PacketNBT.java @@ -1,8 +1,11 @@ package buildcraft.core.network; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; + import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; @@ -19,7 +22,7 @@ public class PacketNBT extends PacketCoordinates { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { super.writeData(data); @@ -29,7 +32,7 @@ public class PacketNBT extends PacketCoordinates { } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { super.readData(data); diff --git a/common/buildcraft/core/network/PacketPayload.java b/common/buildcraft/core/network/PacketPayload.java index 98eaa9df..973c6ef8 100644 --- a/common/buildcraft/core/network/PacketPayload.java +++ b/common/buildcraft/core/network/PacketPayload.java @@ -2,6 +2,8 @@ package buildcraft.core.network; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -21,9 +23,9 @@ public abstract class PacketPayload { return null; } - public abstract void writeData(DataOutputStream data) throws IOException; + public abstract void writeData(ByteBuf data); - public abstract void readData(DataInputStream data) throws IOException; + public abstract void readData(ByteBuf data); public abstract Type getType(); } diff --git a/common/buildcraft/core/network/PacketPayloadArrays.java b/common/buildcraft/core/network/PacketPayloadArrays.java index 09710642..429cd480 100644 --- a/common/buildcraft/core/network/PacketPayloadArrays.java +++ b/common/buildcraft/core/network/PacketPayloadArrays.java @@ -9,6 +9,8 @@ package buildcraft.core.network; import buildcraft.core.utils.Utils; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -71,7 +73,7 @@ public class PacketPayloadArrays extends PacketPayload { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { data.writeInt(intPayload.length); data.writeInt(floatPayload.length); data.writeInt(stringPayload.length); @@ -83,12 +85,12 @@ public class PacketPayloadArrays extends PacketPayload { data.writeFloat(floatData); } for (String stringData : stringPayload) { - data.writeUTF(stringData); + Utils.writeUTF(data, stringData); } } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { intPayload = new int[data.readInt()]; floatPayload = new float[data.readInt()]; stringPayload = new String[data.readInt()]; @@ -100,7 +102,7 @@ public class PacketPayloadArrays extends PacketPayload { floatPayload[i] = data.readFloat(); } for (int i = 0; i < stringPayload.length; i++) { - stringPayload[i] = data.readUTF(); + stringPayload[i] = Utils.readUTF(data); } } diff --git a/common/buildcraft/core/network/PacketPayloadStream.java b/common/buildcraft/core/network/PacketPayloadStream.java index 651d5b2f..9c164088 100644 --- a/common/buildcraft/core/network/PacketPayloadStream.java +++ b/common/buildcraft/core/network/PacketPayloadStream.java @@ -8,6 +8,8 @@ */ package buildcraft.core.network; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -25,10 +27,10 @@ public class PacketPayloadStream extends PacketPayload { public static interface StreamWriter { - public void writeData(DataOutputStream data) throws IOException; + public void writeData(ByteBuf data); } private StreamWriter handler; - public DataInputStream stream; + public ByteBuf stream; public PacketPayloadStream() { } @@ -38,12 +40,12 @@ public class PacketPayloadStream extends PacketPayload { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { handler.writeData(data); } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { stream = data; } diff --git a/common/buildcraft/core/network/PacketSlotChange.java b/common/buildcraft/core/network/PacketSlotChange.java index 43aa772b..3b276020 100644 --- a/common/buildcraft/core/network/PacketSlotChange.java +++ b/common/buildcraft/core/network/PacketSlotChange.java @@ -1,8 +1,11 @@ package buildcraft.core.network; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; + import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompressedStreamTools; @@ -21,7 +24,7 @@ public class PacketSlotChange extends PacketCoordinates { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { super.writeData(data); @@ -45,7 +48,7 @@ public class PacketSlotChange extends PacketCoordinates { } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { super.readData(data); diff --git a/common/buildcraft/core/network/PacketTileState.java b/common/buildcraft/core/network/PacketTileState.java index fd4c2581..df9ccce4 100644 --- a/common/buildcraft/core/network/PacketTileState.java +++ b/common/buildcraft/core/network/PacketTileState.java @@ -1,5 +1,7 @@ package buildcraft.core.network; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -43,7 +45,7 @@ public class PacketTileState extends PacketCoordinates { } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { super.readData(data); } @@ -61,7 +63,7 @@ public class PacketTileState extends PacketCoordinates { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { super.writeData(data); data.writeByte(stateList.size()); for (StateWithId stateWithId : stateList) { diff --git a/common/buildcraft/core/network/PacketUpdate.java b/common/buildcraft/core/network/PacketUpdate.java index afd82b80..35d6b53d 100644 --- a/common/buildcraft/core/network/PacketUpdate.java +++ b/common/buildcraft/core/network/PacketUpdate.java @@ -1,5 +1,7 @@ package buildcraft.core.network; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -36,7 +38,7 @@ public class PacketUpdate extends BuildCraftPacket { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { data.writeInt(posX); data.writeInt(posY); @@ -51,7 +53,7 @@ public class PacketUpdate extends BuildCraftPacket { } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { posX = data.readInt(); posY = data.readInt(); diff --git a/common/buildcraft/core/proxy/CoreProxy.java b/common/buildcraft/core/proxy/CoreProxy.java index 14ea71c5..2b9d0de4 100644 --- a/common/buildcraft/core/proxy/CoreProxy.java +++ b/common/buildcraft/core/proxy/CoreProxy.java @@ -14,9 +14,11 @@ import buildcraft.core.network.BuildCraftPacket; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.registry.GameRegistry; + import java.io.File; import java.util.List; import java.util.Random; + import net.minecraft.block.Block; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; @@ -28,7 +30,9 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; -import net.minecraft.network.packet.Packet; +import net.minecraft.network.INetHandler; +import net.minecraft.network.NetHandlerPlayServer; +import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ChatMessageComponent; import net.minecraft.util.ChunkCoordinates; @@ -131,26 +135,6 @@ public class CoreProxy { //GameRegistry.addShapelessRecipe(result, recipe); } - public void sendToPlayers(Packet packet, World world, int x, int y, int z, int maxDistance) { - if (packet != null) { - for (int j = 0; j < world.playerEntities.size(); j++) { - EntityPlayerMP player = (EntityPlayerMP) world.playerEntities.get(j); - - if (Math.abs(player.posX - x) <= maxDistance && Math.abs(player.posY - y) <= maxDistance && Math.abs(player.posZ - z) <= maxDistance) { - player.playerNetServerHandler.sendPacketToPlayer(packet); - } - } - } - } - - public void sendToPlayer(EntityPlayer entityplayer, BuildCraftPacket packet) { - EntityPlayerMP player = (EntityPlayerMP) entityplayer; - player.playerNetServerHandler.sendPacketToPlayer(packet.getPacket()); - } - - public void sendToServer(Packet packet) { - } - public int addCustomTexture(String pathToTexture) { return 0; } @@ -237,4 +221,15 @@ public class CoreProxy { public EntityBlock newEntityBlock(World world, double i, double j, double k, double iSize, double jSize, double kSize, LaserKind laserKind) { return new EntityBlock(world, i, j, k, iSize, jSize, kSize); } + + /** + * This function returns either the player from the handler if it's on the + * server, or directly from the minecraft instance if it's the client. + * + * TODO: This is a bit kludgy, probably better to separate the client and + * server channels instead. + */ + public EntityPlayer getPlayerFromNetHandler (INetHandler hander) { + return ((NetHandlerPlayServer) hander).field_147369_b; + } } diff --git a/common/buildcraft/core/proxy/CoreProxyClient.java b/common/buildcraft/core/proxy/CoreProxyClient.java index ebbde727..222e9a36 100644 --- a/common/buildcraft/core/proxy/CoreProxyClient.java +++ b/common/buildcraft/core/proxy/CoreProxyClient.java @@ -26,18 +26,23 @@ import buildcraft.transport.render.TileEntityPickupFX; import cpw.mods.fml.client.FMLClientHandler; import cpw.mods.fml.client.registry.RenderingRegistry; import cpw.mods.fml.common.registry.LanguageRegistry; + import java.io.File; import java.util.List; + import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.WorldClient; +import net.minecraft.client.network.NetHandlerPlayClient; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.network.packet.Packet; +import net.minecraft.network.INetHandler; +import net.minecraft.network.NetHandlerPlayServer; +import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ChatMessageComponent; import net.minecraft.util.ChunkCoordinates; @@ -185,4 +190,9 @@ public class CoreProxyClient extends CoreProxy { } return eb; } + + @Override + public EntityPlayer getPlayerFromNetHandler (INetHandler hander) { + return Minecraft.getMinecraft().thePlayer; + } } diff --git a/common/buildcraft/core/utils/BlockUtil.java b/common/buildcraft/core/utils/BlockUtil.java index 358bee7a..37c1cffe 100644 --- a/common/buildcraft/core/utils/BlockUtil.java +++ b/common/buildcraft/core/utils/BlockUtil.java @@ -22,7 +22,7 @@ import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; -import net.minecraft.network.packet.Packet60Explosion; +import net.minecraft.network.Packet60Explosion; import net.minecraft.world.ChunkPosition; import net.minecraft.world.Explosion; import net.minecraft.world.World; diff --git a/common/buildcraft/core/utils/Utils.java b/common/buildcraft/core/utils/Utils.java index d40d96b1..154834ec 100644 --- a/common/buildcraft/core/utils/Utils.java +++ b/common/buildcraft/core/utils/Utils.java @@ -25,8 +25,11 @@ import buildcraft.core.network.ISynchronizedTile; import buildcraft.core.network.PacketUpdate; import buildcraft.core.proxy.CoreProxy; import buildcraft.energy.TileEngine; +import io.netty.buffer.ByteBuf; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -397,4 +400,27 @@ public class Utils { } return slots; } + + public static void writeUTF (ByteBuf data, String str) { + try { + byte [] b = str.getBytes("UTF-8"); + data.writeInt (b.length); + data.writeBytes(b); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + data.writeInt (0); + } + } + + public static String readUTF (ByteBuf data) { + try { + int len = data.readInt(); + byte [] b = new byte [len]; + data.readBytes(b); + return new String (b, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return null; + } + } } diff --git a/common/buildcraft/factory/TileQuarry.java b/common/buildcraft/factory/TileQuarry.java index ce668878..ed500438 100644 --- a/common/buildcraft/factory/TileQuarry.java +++ b/common/buildcraft/factory/TileQuarry.java @@ -15,7 +15,7 @@ import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.network.packet.Packet3Chat; +import net.minecraft.network.Packet3Chat; import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.ChatMessageComponent; import net.minecraft.world.ChunkCoordIntPair; diff --git a/common/buildcraft/factory/network/PacketHandlerFactory.java b/common/buildcraft/factory/network/PacketHandlerFactory.java index bf9d7dd0..6a5b305f 100644 --- a/common/buildcraft/factory/network/PacketHandlerFactory.java +++ b/common/buildcraft/factory/network/PacketHandlerFactory.java @@ -1,37 +1,45 @@ package buildcraft.factory.network; +import buildcraft.core.network.BuildCraftChannelHandler; +import buildcraft.core.network.BuildCraftPacket; import buildcraft.core.network.PacketIds; import buildcraft.core.network.PacketPayload; import buildcraft.core.network.PacketPayloadStream; import buildcraft.core.network.PacketUpdate; +import buildcraft.core.proxy.CoreProxy; import buildcraft.factory.TileRefinery; -import cpw.mods.fml.common.network.IPacketHandler; -import cpw.mods.fml.common.network.Player; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; + import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; + +import cpw.mods.fml.common.network.NetworkRegistry; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.network.INetworkManager; -import net.minecraft.network.packet.Packet250CustomPayload; +import net.minecraft.network.INetHandler; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.fluids.FluidRegistry; -public class PacketHandlerFactory implements IPacketHandler { +public class PacketHandlerFactory extends BuildCraftChannelHandler { @Override - public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { - - DataInputStream data = new DataInputStream(new ByteArrayInputStream(packet.data)); + public void decodeInto(ChannelHandlerContext ctx, ByteBuf data, BuildCraftPacket packet) { + super.decodeInto(ctx, data, packet); + try { - int packetID = data.read(); - PacketUpdate packetU = new PacketUpdate(); + INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); + + EntityPlayer player = + CoreProxy.proxy.getPlayerFromNetHandler(netHandler); + + int packetID = packet.getID(); switch (packetID) { case PacketIds.REFINERY_FILTER_SET: - packetU.readData(data); - onRefinerySelect((EntityPlayer) player, packetU); + onRefinerySelect(player, (PacketUpdate) packet); break; } diff --git a/common/buildcraft/silicon/network/PacketHandlerSilicon.java b/common/buildcraft/silicon/network/PacketHandlerSilicon.java index 6faa57db..6dbd3c23 100644 --- a/common/buildcraft/silicon/network/PacketHandlerSilicon.java +++ b/common/buildcraft/silicon/network/PacketHandlerSilicon.java @@ -1,53 +1,55 @@ package buildcraft.silicon.network; +import buildcraft.core.network.BuildCraftChannelHandler; +import buildcraft.core.network.BuildCraftPacket; import buildcraft.core.network.PacketCoordinates; import buildcraft.core.network.PacketIds; import buildcraft.core.network.PacketNBT; import buildcraft.core.network.PacketSlotChange; +import buildcraft.core.proxy.CoreProxy; import buildcraft.silicon.TileAdvancedCraftingTable; import buildcraft.silicon.TileAssemblyTable; import buildcraft.silicon.TileAssemblyTable.SelectionMessage; import buildcraft.silicon.gui.ContainerAssemblyTable; -import cpw.mods.fml.common.network.IPacketHandler; -import cpw.mods.fml.common.network.Player; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; + import java.io.ByteArrayInputStream; import java.io.DataInputStream; + +import cpw.mods.fml.common.network.NetworkRegistry; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; -import net.minecraft.network.INetworkManager; -import net.minecraft.network.packet.Packet250CustomPayload; +import net.minecraft.network.INetHandler; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; -public class PacketHandlerSilicon implements IPacketHandler { +public class PacketHandlerSilicon extends BuildCraftChannelHandler { @Override - public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { - - DataInputStream data = new DataInputStream(new ByteArrayInputStream(packet.data)); + public void decodeInto(ChannelHandlerContext ctx, ByteBuf data, BuildCraftPacket packet) { + super.decodeInto(ctx, data, packet); + try { - int packetID = data.read(); + INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); + + EntityPlayer player = + CoreProxy.proxy.getPlayerFromNetHandler(netHandler); + + int packetID = packet.getID(); + switch (packetID) { - case PacketIds.SELECTION_ASSEMBLY_SEND: - PacketNBT packetT = new PacketNBT(); - packetT.readData(data); - onSelectionUpdate((EntityPlayer) player, packetT); + case PacketIds.SELECTION_ASSEMBLY_SEND: + onSelectionUpdate(player, (PacketNBT) packet); break; - case PacketIds.SELECTION_ASSEMBLY: - PacketNBT packetA = new PacketNBT(); - packetA.readData(data); - onAssemblySelect((EntityPlayer) player, packetA); + onAssemblySelect(player, (PacketNBT) packet); break; case PacketIds.SELECTION_ASSEMBLY_GET: - PacketCoordinates packetC = new PacketCoordinates(); - packetC.readData(data); - onAssemblyGetSelection((EntityPlayer) player, packetC); + onAssemblyGetSelection(player, (PacketCoordinates) packet); break; case PacketIds.ADVANCED_WORKBENCH_SETSLOT: - PacketSlotChange packet1 = new PacketSlotChange(); - packet1.readData(data); - onAdvancedWorkbenchSet((EntityPlayer) player, packet1); + onAdvancedWorkbenchSet(player, (PacketSlotChange) packet); break; } diff --git a/common/buildcraft/transport/TileGenericPipe.java b/common/buildcraft/transport/TileGenericPipe.java index 4699b2a9..51fa6cb7 100644 --- a/common/buildcraft/transport/TileGenericPipe.java +++ b/common/buildcraft/transport/TileGenericPipe.java @@ -18,7 +18,7 @@ import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.network.packet.Packet; +import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; diff --git a/common/buildcraft/transport/gui/ContainerGateInterface.java b/common/buildcraft/transport/gui/ContainerGateInterface.java index 048c15f2..0f0f988e 100644 --- a/common/buildcraft/transport/gui/ContainerGateInterface.java +++ b/common/buildcraft/transport/gui/ContainerGateInterface.java @@ -38,7 +38,7 @@ import net.minecraft.inventory.ICrafting; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; -import net.minecraft.network.packet.Packet; +import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.util.ForgeDirection; diff --git a/common/buildcraft/transport/network/PacketFluidUpdate.java b/common/buildcraft/transport/network/PacketFluidUpdate.java index d1dae319..e90f6d0a 100644 --- a/common/buildcraft/transport/network/PacketFluidUpdate.java +++ b/common/buildcraft/transport/network/PacketFluidUpdate.java @@ -5,10 +5,13 @@ import buildcraft.core.network.PacketIds; import buildcraft.core.proxy.CoreProxy; import buildcraft.transport.PipeTransportFluids; import buildcraft.transport.TileGenericPipe; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.BitSet; + import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; @@ -37,7 +40,7 @@ public class PacketFluidUpdate extends PacketCoordinates { } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { super.readData(data); World world = CoreProxy.proxy.getClientWorld(); @@ -82,7 +85,7 @@ public class PacketFluidUpdate extends PacketCoordinates { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { super.writeData(data); byte[] dBytes = toByteArray(delta); diff --git a/common/buildcraft/transport/network/PacketGateExpansionMap.java b/common/buildcraft/transport/network/PacketGateExpansionMap.java index 4f390f6d..efacbc5c 100644 --- a/common/buildcraft/transport/network/PacketGateExpansionMap.java +++ b/common/buildcraft/transport/network/PacketGateExpansionMap.java @@ -3,8 +3,13 @@ package buildcraft.transport.network; import buildcraft.api.gates.GateExpansions; import buildcraft.core.network.BuildCraftPacket; import buildcraft.core.network.PacketIds; +import buildcraft.core.utils.Utils; + import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; + +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -16,22 +21,22 @@ public class PacketGateExpansionMap extends BuildCraftPacket { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { BiMap map = GateExpansions.getServerMap(); data.writeByte(map.size()); for (Map.Entry entry : map.entrySet()) { data.writeByte(entry.getKey()); - data.writeUTF(entry.getValue()); + Utils.writeUTF(data, entry.getValue()); } } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { int numEntries = data.readByte(); BiMap map = HashBiMap.create(numEntries); for (int i = 0; i < numEntries; i++) { byte id = data.readByte(); - String identifier = data.readUTF(); + String identifier = Utils.readUTF(data); map.put(id, identifier); } GateExpansions.setClientMap(map); diff --git a/common/buildcraft/transport/network/PacketHandlerTransport.java b/common/buildcraft/transport/network/PacketHandlerTransport.java index 75e3d47e..dd50c5cb 100644 --- a/common/buildcraft/transport/network/PacketHandlerTransport.java +++ b/common/buildcraft/transport/network/PacketHandlerTransport.java @@ -1,115 +1,110 @@ package buildcraft.transport.network; +import buildcraft.core.network.BuildCraftChannelHandler; +import buildcraft.core.network.BuildCraftPacket; import buildcraft.core.network.PacketCoordinates; import buildcraft.core.network.PacketIds; import buildcraft.core.network.PacketSlotChange; import buildcraft.core.network.PacketUpdate; +import buildcraft.core.proxy.CoreProxy; import buildcraft.transport.PipeTransportItems; import buildcraft.transport.PipeTransportPower; import buildcraft.transport.TileGenericPipe; import buildcraft.transport.gui.ContainerGateInterface; import buildcraft.transport.pipes.PipeItemsDiamond; import buildcraft.transport.pipes.PipeItemsEmerald; -import cpw.mods.fml.common.network.IPacketHandler; -import cpw.mods.fml.common.network.Player; +import cpw.mods.fml.common.network.NetworkRegistry; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; + import java.io.ByteArrayInputStream; import java.io.DataInputStream; + +import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.inventory.Container; -import net.minecraft.network.INetworkManager; -import net.minecraft.network.packet.Packet250CustomPayload; +import net.minecraft.network.INetHandler; +import net.minecraft.network.NetHandlerPlayServer; +import net.minecraft.network.play.INetHandlerPlayServer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; -public class PacketHandlerTransport implements IPacketHandler { +public class PacketHandlerTransport extends BuildCraftChannelHandler { + /** + * TODO: A lot of this is based on the player to retrieve the world. + * Passing a dimension id would be more appropriate. More generally, it + * seems like a lot of these packets could be replaced with tile-based + * RPCs. + */ @Override - public void onPacketData(INetworkManager manager, Packet250CustomPayload packet2, Player player) { - DataInputStream data = new DataInputStream(new ByteArrayInputStream(packet2.data)); + public void decodeInto(ChannelHandlerContext ctx, ByteBuf data, BuildCraftPacket packet) { + super.decodeInto(ctx, data, packet); try { - // NetClientHandler net = (NetClientHandler) network.getNetHandler(); + INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); + + EntityPlayer player = + CoreProxy.proxy.getPlayerFromNetHandler(netHandler); - int packetID = data.read(); + int packetID = packet.getID(); - PacketUpdate packet = new PacketUpdate(); switch (packetID) { case PacketIds.PIPE_POWER: - PacketPowerUpdate packetPower = new PacketPowerUpdate(); - packetPower.readData(data); - onPacketPower((EntityPlayer) player, packetPower); + onPacketPower(player, (PacketPowerUpdate) packet); break; case PacketIds.PIPE_LIQUID: - PacketFluidUpdate packetFluid = new PacketFluidUpdate(); - packetFluid.readData(data); + // action will have happened already at read time break; case PacketIds.PIPE_TRAVELER: { - PacketPipeTransportTraveler pkt = new PacketPipeTransportTraveler(); - pkt.readData(data); - onPipeTravelerUpdate((EntityPlayer) player, pkt); + onPipeTravelerUpdate(player, (PacketPipeTransportTraveler) packet); break; } case PacketIds.GATE_ACTIONS: - packet.readData(data); - onGateActions((EntityPlayer) player, packet); + onGateActions(player, (PacketUpdate) packet); break; case PacketIds.GATE_TRIGGERS: - packet.readData(data); - onGateTriggers((EntityPlayer) player, packet); + onGateTriggers(player, (PacketUpdate) packet); break; case PacketIds.GATE_SELECTION: - packet.readData(data); - onGateSelection((EntityPlayer) player, packet); + onGateSelection(player, (PacketUpdate) packet); break; case PacketIds.PIPE_ITEMSTACK: { - PacketPipeTransportItemStack pkt = new PacketPipeTransportItemStack(); - pkt.readData(data); + // action will have happened already at read time break; } case PacketIds.PIPE_GATE_EXPANSION_MAP: { - PacketGateExpansionMap pkt = new PacketGateExpansionMap(); - pkt.readData(data); + // action will have happened already at read time break; } /** * SERVER SIDE * */ - case PacketIds.DIAMOND_PIPE_SELECT: { - PacketSlotChange packet1 = new PacketSlotChange(); - packet1.readData(data); - onDiamondPipeSelect((EntityPlayer) player, packet1); + case PacketIds.DIAMOND_PIPE_SELECT: { + onDiamondPipeSelect((EntityPlayer) player, (PacketSlotChange) packet); break; } - case PacketIds.EMERALD_PIPE_SELECT: { - PacketSlotChange packet1 = new PacketSlotChange(); - packet1.readData(data); - onEmeraldPipeSelect((EntityPlayer) player, packet1); + case PacketIds.EMERALD_PIPE_SELECT: { + onEmeraldPipeSelect((EntityPlayer) player, (PacketSlotChange) packet); break; } - case PacketIds.GATE_REQUEST_INIT: - PacketCoordinates packetU = new PacketCoordinates(); - packetU.readData(data); - onGateInitRequest((EntityPlayer) player, packetU); + case PacketIds.GATE_REQUEST_INIT: + onGateInitRequest((EntityPlayer) player, (PacketCoordinates) packet); break; case PacketIds.GATE_REQUEST_SELECTION: - PacketCoordinates packetS = new PacketCoordinates(); - packetS.readData(data); - onGateSelectionRequest((EntityPlayerMP) player, packetS); + onGateSelectionRequest((EntityPlayer) player, (PacketCoordinates) packet); break; case PacketIds.GATE_SELECTION_CHANGE: - PacketUpdate packet3 = new PacketUpdate(); - packet3.readData(data); - onGateSelectionChange((EntityPlayerMP) player, packet3); + onGateSelectionChange((EntityPlayer) player, (PacketUpdate) packet); break; case PacketIds.PIPE_ITEMSTACK_REQUEST: { - PacketPipeTransportItemStackRequest pkt = new PacketPipeTransportItemStackRequest(player); - pkt.readData(data); + // action will have happened already at read time break; } } diff --git a/common/buildcraft/transport/network/PacketPipeTransportItemStack.java b/common/buildcraft/transport/network/PacketPipeTransportItemStack.java index b8217be3..d7f76db4 100644 --- a/common/buildcraft/transport/network/PacketPipeTransportItemStack.java +++ b/common/buildcraft/transport/network/PacketPipeTransportItemStack.java @@ -3,11 +3,14 @@ package buildcraft.transport.network; import buildcraft.core.network.BuildCraftPacket; import buildcraft.core.network.PacketIds; import buildcraft.transport.TravelingItem; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; + import net.minecraft.item.ItemStack; -import net.minecraft.network.packet.Packet; +import net.minecraft.network.Packet; public class PacketPipeTransportItemStack extends BuildCraftPacket { @@ -23,13 +26,13 @@ public class PacketPipeTransportItemStack extends BuildCraftPacket { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { data.writeInt(entityId); Packet.writeItemStack(stack, data); } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { this.entityId = data.readInt(); stack = Packet.readItemStack(data); TravelingItem item = TravelingItem.clientCache.get(entityId); diff --git a/common/buildcraft/transport/network/PacketPipeTransportItemStackRequest.java b/common/buildcraft/transport/network/PacketPipeTransportItemStackRequest.java index c4b12f95..16f4a879 100644 --- a/common/buildcraft/transport/network/PacketPipeTransportItemStackRequest.java +++ b/common/buildcraft/transport/network/PacketPipeTransportItemStackRequest.java @@ -5,6 +5,8 @@ import buildcraft.core.network.PacketIds; import buildcraft.transport.TravelingItem; import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.network.Player; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -23,12 +25,12 @@ public class PacketPipeTransportItemStackRequest extends BuildCraftPacket { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { data.writeShort(travelerID); } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { travelerID = data.readShort(); TravelingItem.TravelingItemCache cache = TravelingItem.serverCache; TravelingItem item = cache.get(travelerID); diff --git a/common/buildcraft/transport/network/PacketPipeTransportTraveler.java b/common/buildcraft/transport/network/PacketPipeTransportTraveler.java index f6497967..b8429d62 100644 --- a/common/buildcraft/transport/network/PacketPipeTransportTraveler.java +++ b/common/buildcraft/transport/network/PacketPipeTransportTraveler.java @@ -4,9 +4,12 @@ import buildcraft.core.network.BuildCraftPacket; import buildcraft.core.network.PacketIds; import buildcraft.core.utils.EnumColor; import buildcraft.transport.TravelingItem; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; + import net.minecraft.util.MathHelper; import net.minecraftforge.common.util.ForgeDirection; @@ -35,7 +38,7 @@ public class PacketPipeTransportTraveler extends BuildCraftPacket { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { data.writeFloat((float) item.xCoord); data.writeFloat((float) item.yCoord); data.writeFloat((float) item.zCoord); @@ -53,7 +56,7 @@ public class PacketPipeTransportTraveler extends BuildCraftPacket { } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { this.itemX = data.readFloat(); this.itemY = data.readFloat(); this.itemZ = data.readFloat(); diff --git a/common/buildcraft/transport/network/PacketPowerUpdate.java b/common/buildcraft/transport/network/PacketPowerUpdate.java index 64f86e35..bb45d617 100644 --- a/common/buildcraft/transport/network/PacketPowerUpdate.java +++ b/common/buildcraft/transport/network/PacketPowerUpdate.java @@ -2,6 +2,8 @@ package buildcraft.transport.network; import buildcraft.core.network.PacketCoordinates; import buildcraft.core.network.PacketIds; +import io.netty.buffer.ByteBuf; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -19,7 +21,7 @@ public class PacketPowerUpdate extends PacketCoordinates { } @Override - public void readData(DataInputStream data) throws IOException { + public void readData(ByteBuf data) { displayPower = new short[] { 0, 0, 0, 0, 0, 0 }; super.readData(data); overload = data.readBoolean(); @@ -29,7 +31,7 @@ public class PacketPowerUpdate extends PacketCoordinates { } @Override - public void writeData(DataOutputStream data) throws IOException { + public void writeData(ByteBuf data) { super.writeData(data); data.writeBoolean(overload); for (int i = 0; i < displayPower.length; i++) { diff --git a/common/buildcraft/transport/network/TransportConnectionHandler.java b/common/buildcraft/transport/network/TransportConnectionHandler.java index 65ec35f0..0e796a5e 100644 --- a/common/buildcraft/transport/network/TransportConnectionHandler.java +++ b/common/buildcraft/transport/network/TransportConnectionHandler.java @@ -14,7 +14,7 @@ import cpw.mods.fml.common.network.Player; import net.minecraft.network.INetworkManager; import net.minecraft.network.NetLoginHandler; import net.minecraft.network.packet.NetHandler; -import net.minecraft.network.packet.Packet1Login; +import net.minecraft.network.Packet1Login; import net.minecraft.server.MinecraftServer; /**