package WayofTime.alchemicalWizardry.common; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import java.util.EnumMap; import net.minecraft.client.Minecraft; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.network.Packet; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; import WayofTime.alchemicalWizardry.AlchemicalWizardry; import WayofTime.alchemicalWizardry.common.tileEntity.TEAltar; import WayofTime.alchemicalWizardry.common.tileEntity.TEOrientable; import WayofTime.alchemicalWizardry.common.tileEntity.TEPedestal; import WayofTime.alchemicalWizardry.common.tileEntity.TEPlinth; import WayofTime.alchemicalWizardry.common.tileEntity.TESocket; import WayofTime.alchemicalWizardry.common.tileEntity.TETeleposer; import WayofTime.alchemicalWizardry.common.tileEntity.TEWritingTable; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.network.FMLEmbeddedChannel; import cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec; import cpw.mods.fml.common.network.FMLOutboundHandler; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; /** * Handles the packet wrangling for IronChest * @author cpw * */ public enum NewPacketHandler { INSTANCE; /** * Our channel "pair" from {@link NetworkRegistry} */ private EnumMap channels; /** * Make our packet handler, and add an {@link IronChestCodec} always */ private NewPacketHandler() { // request a channel pair for IronChest from the network registry // Add the IronChestCodec as a member of both channel pipelines this.channels = NetworkRegistry.INSTANCE.newChannel("BloodMagic", new TEAltarCodec()); if (FMLCommonHandler.instance().getSide() == Side.CLIENT) { addClientHandler(); } } /** * This is only called on the client side - it adds an * {@link IronChestMessageHandler} to the client side pipeline, since the * only place we expect to handle messages is on the client. */ @SideOnly(Side.CLIENT) private void addClientHandler() { FMLEmbeddedChannel clientChannel = this.channels.get(Side.CLIENT); String tileAltarCodec = clientChannel.findChannelHandlerNameForType(TEAltarCodec.class); clientChannel.pipeline().addAfter(tileAltarCodec, "TEAltarHandler", new TEAltarMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "TEOrientableHandler", new TEOrientableMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "TEPedestalHandler", new TEPedestalMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "TEPlinthHandler", new TEPlinthMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "TESocketHandler", new TESocketMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "TETeleposerHandler", new TETeleposerMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "TEWritingTableHandler", new TEWritingTableMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "ParticleHandler", new ParticleMessageHandler()); clientChannel.pipeline().addAfter(tileAltarCodec, "VelocityHandler", new VelocityMessageHandler()); } /** * This class simply handles the {@link IronChestMessage} when it's received * at the client side It can contain client only code, because it's only run * on the client. * * @author cpw * */ private static class TEAltarMessageHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, TEAltarMessage msg) throws Exception { World world = AlchemicalWizardry.proxy.getClientWorld(); TileEntity te = world.getTileEntity(msg.x, msg.y, msg.z); if (te instanceof TEAltar) { TEAltar altar = (TEAltar) te; altar.handlePacketData(msg.items, msg.fluids, msg.capacity); } } } private static class TEOrientableMessageHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, TEOrientableMessage msg) throws Exception { World world = AlchemicalWizardry.proxy.getClientWorld(); TileEntity te = world.getTileEntity(msg.x, msg.y, msg.z); if (te instanceof TEOrientable) { TEOrientable tile = (TEOrientable)te; ((TEOrientable) te).setInputDirection(ForgeDirection.getOrientation(msg.input)); ((TEOrientable) te).setOutputDirection(ForgeDirection.getOrientation(msg.output)); } } } private static class TEPedestalMessageHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, TEPedestalMessage msg) throws Exception { World world = AlchemicalWizardry.proxy.getClientWorld(); TileEntity te = world.getTileEntity(msg.x, msg.y, msg.z); if (te instanceof TEPedestal) { TEPedestal pedestal = (TEPedestal) te; pedestal.handlePacketData(msg.items); } } } private static class TEPlinthMessageHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, TEPlinthMessage msg) throws Exception { World world = AlchemicalWizardry.proxy.getClientWorld(); TileEntity te = world.getTileEntity(msg.x, msg.y, msg.z); if (te instanceof TEPlinth) { TEPlinth Plinth = (TEPlinth) te; Plinth.handlePacketData(msg.items); } } } private static class TESocketMessageHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, TESocketMessage msg) throws Exception { World world = AlchemicalWizardry.proxy.getClientWorld(); TileEntity te = world.getTileEntity(msg.x, msg.y, msg.z); if (te instanceof TESocket) { TESocket Socket = (TESocket) te; Socket.handlePacketData(msg.items); } } } private static class TETeleposerMessageHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, TETeleposerMessage msg) throws Exception { World world = AlchemicalWizardry.proxy.getClientWorld(); TileEntity te = world.getTileEntity(msg.x, msg.y, msg.z); if (te instanceof TETeleposer) { TETeleposer Teleposer = (TETeleposer) te; Teleposer.handlePacketData(msg.items); } } } private static class TEWritingTableMessageHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, TEWritingTableMessage msg) throws Exception { World world = AlchemicalWizardry.proxy.getClientWorld(); TileEntity te = world.getTileEntity(msg.x, msg.y, msg.z); if (te instanceof TEWritingTable) { TEWritingTable WritingTable = (TEWritingTable) te; WritingTable.handlePacketData(msg.items); } } } private static class ParticleMessageHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, ParticleMessage msg) throws Exception { World world = AlchemicalWizardry.proxy.getClientWorld(); world.spawnParticle(msg.particle, msg.xCoord, msg.yCoord, msg.zCoord, msg.xVel, msg.yVel, msg.zVel); } } private static class VelocityMessageHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, VelocityMessage msg) throws Exception { EntityPlayer player = Minecraft.getMinecraft().thePlayer; if(player!=null) { player.motionX = msg.xVel; player.motionY = msg.yVel; player.motionZ = msg.zVel; } } } public static class BMMessage { int index; } public static class TEAltarMessage extends BMMessage { int x; int y; int z; int[] items; int[] fluids; int capacity; } public static class TEOrientableMessage extends BMMessage { int x; int y; int z; int input; int output; } public static class TEPedestalMessage extends BMMessage { int x; int y; int z; int[] items; } public static class TEPlinthMessage extends BMMessage { int x; int y; int z; int[] items; } public static class TESocketMessage extends BMMessage { int x; int y; int z; int[] items; } public static class TETeleposerMessage extends BMMessage { int x; int y; int z; int[] items; } public static class TEWritingTableMessage extends BMMessage { int x; int y; int z; int[] items; } public static class ParticleMessage extends BMMessage { String particle; double xCoord; double yCoord; double zCoord; double xVel; double yVel; double zVel; } public static class VelocityMessage extends BMMessage { double xVel; double yVel; double zVel; } private class TEAltarCodec extends FMLIndexedMessageToMessageCodec { public TEAltarCodec() { addDiscriminator(0, TEAltarMessage.class); addDiscriminator(1, TEOrientableMessage.class); addDiscriminator(2, TEPedestalMessage.class); addDiscriminator(3, TEPlinthMessage.class); addDiscriminator(4, TESocketMessage.class); addDiscriminator(5, TETeleposerMessage.class); addDiscriminator(6, TEWritingTableMessage.class); addDiscriminator(7, ParticleMessage.class); addDiscriminator(8, VelocityMessage.class); } @Override public void encodeInto(ChannelHandlerContext ctx, BMMessage msg, ByteBuf target) throws Exception { target.writeInt(msg.index); switch(msg.index) { case 0: target.writeInt(((TEAltarMessage)msg).x); target.writeInt(((TEAltarMessage)msg).y); target.writeInt(((TEAltarMessage)msg).z); target.writeBoolean(((TEAltarMessage)msg).items != null); if (((TEAltarMessage)msg).items != null) { int[] items = ((TEAltarMessage)msg).items; for (int j = 0; j < items.length; j++) { int i = items[j]; target.writeInt(i); } } target.writeBoolean(((TEAltarMessage)msg).fluids != null); if(((TEAltarMessage)msg).fluids != null) { int[] fluids = ((TEAltarMessage)msg).fluids; for (int j = 0; j < fluids.length; j++) { int i = fluids[j]; target.writeInt(i); } } target.writeInt(((TEAltarMessage)msg).capacity); break; case 1: target.writeInt(((TEOrientableMessage)msg).x); target.writeInt(((TEOrientableMessage)msg).y); target.writeInt(((TEOrientableMessage)msg).z); target.writeInt(((TEOrientableMessage)msg).input); target.writeInt(((TEOrientableMessage)msg).output); break; case 2: target.writeInt(((TEPedestalMessage)msg).x); target.writeInt(((TEPedestalMessage)msg).y); target.writeInt(((TEPedestalMessage)msg).z); target.writeBoolean(((TEPedestalMessage)msg).items != null); if (((TEPedestalMessage)msg).items != null) { int[] items = ((TEPedestalMessage)msg).items; for (int j = 0; j < items.length; j++) { int i = items[j]; target.writeInt(i); } } break; case 3: target.writeInt(((TEPlinthMessage)msg).x); target.writeInt(((TEPlinthMessage)msg).y); target.writeInt(((TEPlinthMessage)msg).z); target.writeBoolean(((TEPlinthMessage)msg).items != null); if (((TEPlinthMessage)msg).items != null) { int[] items = ((TEPlinthMessage)msg).items; for (int j = 0; j < items.length; j++) { int i = items[j]; target.writeInt(i); } } break; case 4: target.writeInt(((TESocketMessage)msg).x); target.writeInt(((TESocketMessage)msg).y); target.writeInt(((TESocketMessage)msg).z); target.writeBoolean(((TESocketMessage)msg).items != null); if (((TESocketMessage)msg).items != null) { int[] items = ((TESocketMessage)msg).items; for (int j = 0; j < items.length; j++) { int i = items[j]; target.writeInt(i); } } break; case 5: target.writeInt(((TETeleposerMessage)msg).x); target.writeInt(((TETeleposerMessage)msg).y); target.writeInt(((TETeleposerMessage)msg).z); target.writeBoolean(((TETeleposerMessage)msg).items != null); if (((TETeleposerMessage)msg).items != null) { int[] items = ((TETeleposerMessage)msg).items; for (int j = 0; j < items.length; j++) { int i = items[j]; target.writeInt(i); } } break; case 6: target.writeInt(((TEWritingTableMessage)msg).x); target.writeInt(((TEWritingTableMessage)msg).y); target.writeInt(((TEWritingTableMessage)msg).z); target.writeBoolean(((TEWritingTableMessage)msg).items != null); if (((TEWritingTableMessage)msg).items != null) { int[] items = ((TEWritingTableMessage)msg).items; for (int j = 0; j < items.length; j++) { int i = items[j]; target.writeInt(i); } } break; case 7: String str = ((ParticleMessage)msg).particle; target.writeInt(str.length()); for(int i=0; i