From f2ef8e4818b14283e7ad10d7205b3e4d1c93c0b7 Mon Sep 17 00:00:00 2001 From: ProfMobius Date: Mon, 23 Jun 2014 23:19:27 +0200 Subject: [PATCH] Fixed MW dep prb Fixed Opis Network derp. --- src/main/java/mapwriter/forge/MwForge.java | 2 +- src/main/java/mcp/mobius/opis/modOpis.java | 3 +- .../mobius/opis/network/PacketManager.java | 217 +++++++++++------- 3 files changed, 131 insertions(+), 91 deletions(-) diff --git a/src/main/java/mapwriter/forge/MwForge.java b/src/main/java/mapwriter/forge/MwForge.java index 5763409..4616a06 100644 --- a/src/main/java/mapwriter/forge/MwForge.java +++ b/src/main/java/mapwriter/forge/MwForge.java @@ -21,7 +21,7 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.network.FMLNetworkEvent; -@Mod(modid="MapWriter", name="MapWriter", version="2.1.1") +@Mod(modid="MapWriter", name="MapWriter", version="2.1.1", acceptableRemoteVersions="*") public class MwForge { @Instance("MapWriter") diff --git a/src/main/java/mcp/mobius/opis/modOpis.java b/src/main/java/mcp/mobius/opis/modOpis.java index 1ce6811..3a18799 100644 --- a/src/main/java/mcp/mobius/opis/modOpis.java +++ b/src/main/java/mcp/mobius/opis/modOpis.java @@ -62,7 +62,8 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.event.FMLServerStartingEvent; import cpw.mods.fml.common.registry.GameRegistry; -@Mod(modid="Opis", name="Opis", version="1.2.2", dependencies="required-after:MobiusCore@[1.2.2]", acceptableRemoteVersions="*") +//@Mod(modid="Opis", name="Opis", version="1.2.2", dependencies="required-after:MobiusCore@[1.2.2]", acceptableRemoteVersions="*") +@Mod(modid="Opis", name="Opis", version="1.2.2") public class modOpis { diff --git a/src/main/java/mcp/mobius/opis/network/PacketManager.java b/src/main/java/mcp/mobius/opis/network/PacketManager.java index 5bbf0f7..c61bdb0 100644 --- a/src/main/java/mcp/mobius/opis/network/PacketManager.java +++ b/src/main/java/mcp/mobius/opis/network/PacketManager.java @@ -6,6 +6,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.SimpleChannelInboundHandler; import java.io.IOException; import java.lang.reflect.Constructor; @@ -70,10 +71,11 @@ import net.minecraft.network.play.server.S02PacketChat; import net.minecraft.util.ChatComponentText; @Sharable -public class PacketManager extends FMLIndexedMessageToMessageCodec +//public class PacketManager extends FMLIndexedMessageToMessageCodec +public class PacketManager { - private static final PacketManager INSTANCE = new PacketManager(); - private static final Logger LOGGER = LogManager.getLogger(); + //private static final PacketManager INSTANCE = new PacketManager(); + //private static final Logger LOGGER = LogManager.getLogger(); private static final EnumMap channels = Maps.newEnumMap(Side.class); public static void init() @@ -81,55 +83,137 @@ public class PacketManager extends FMLIndexedMessageToMessageCodec if (!channels.isEmpty()) // avoid duplicate inits.. return; - INSTANCE.addDiscriminator(0, PacketReqChunks.class); - INSTANCE.addDiscriminator(1, PacketReqData.class); - INSTANCE.addDiscriminator(2, NetDataCommand.class); - INSTANCE.addDiscriminator(3, NetDataList.class); - INSTANCE.addDiscriminator(4, NetDataValue.class); - INSTANCE.addDiscriminator(5, PacketChunks.class); + Codec codec = new Codec(); + + codec.addDiscriminator(0, PacketReqChunks.class); + codec.addDiscriminator(1, PacketReqData.class); + codec.addDiscriminator(2, NetDataCommand.class); + codec.addDiscriminator(3, NetDataList.class); + codec.addDiscriminator(4, NetDataValue.class); + codec.addDiscriminator(5, PacketChunks.class); - channels.putAll(NetworkRegistry.INSTANCE.newChannel("Opis", INSTANCE)); - } - - // IO METHODS - - @Override - public void encodeInto(ChannelHandlerContext ctx, PacketBase packet, ByteBuf target) throws Exception - { - ByteArrayDataOutput output = ByteStreams.newDataOutput(); - packet.encode(output); - target.writeBytes(output.toByteArray()); - } - - @Override - public void decodeInto(ChannelHandlerContext ctx, ByteBuf source, PacketBase packet) - { - ByteArrayDataInput input = ByteStreams.newDataInput(source.array()); - input.skipBytes(1); // skip the packet identifier byte - packet.decode(input); - - if (FMLCommonHandler.instance().getEffectiveSide().isClient()) + channels.putAll(NetworkRegistry.INSTANCE.newChannel("Opis", codec)); + + // add handlers + if (FMLCommonHandler.instance().getSide().isClient()) { - actionClient(packet); + // for the client + FMLEmbeddedChannel channel = channels.get(Side.CLIENT); + String codecName = channel.findChannelHandlerNameForType(Codec.class); + channel.pipeline().addAfter(codecName, "ClientHandler", new HandlerClient()); } else { - actionServer(ctx, packet); + // for the server + FMLEmbeddedChannel channel = channels.get(Side.SERVER); + String codecName = channel.findChannelHandlerNameForType(Codec.class); + channel.pipeline().addAfter(codecName, "ServerHandler", new HandlerServer()); + } + + } + + /** + * @author abrarsyed + * This class converts FMLMessage to PacketBase for my system to handle. + */ + private static final class Codec extends FMLIndexedMessageToMessageCodec + { + @Override + public void encodeInto(ChannelHandlerContext ctx, PacketBase packet, ByteBuf target) throws Exception + { + ByteArrayDataOutput output = ByteStreams.newDataOutput(); + packet.encode(output); + target.writeBytes(output.toByteArray()); + } + + @Override + public void decodeInto(ChannelHandlerContext ctx, ByteBuf source, PacketBase packet) + { + ByteArrayDataInput input = ByteStreams.newDataInput(source.array()); + input.skipBytes(1); // skip the packet identifier byte + packet.decode(input); + + if (FMLCommonHandler.instance().getEffectiveSide().isClient()) + { + actionClient(packet); + } + else + { + actionServer(ctx, packet); + } + } + + @SideOnly(Side.CLIENT) + private void actionClient(PacketBase packet) + { + Minecraft mc = Minecraft.getMinecraft(); + packet.actionClient(mc.theWorld, mc.thePlayer); + } + + private void actionServer(ChannelHandlerContext ctx, PacketBase packet) + { + EntityPlayerMP player = ((NetHandlerPlayServer) ctx.channel().attr(NetworkRegistry.NET_HANDLER).get()).playerEntity; + packet.actionServer(player.worldObj, player); + } + + @Override + public FMLIndexedMessageToMessageCodec addDiscriminator(int discriminator, Class type) + { + // double check it has an empty constructor. or fail early. + // This is to guard myself against my own stupidity. + + if (!hasEmptyContructor(type)) + { + LogManager.getLogger().log(Level.FATAL, type.getName() + "does not have an empty constructor!"); + } + + return super.addDiscriminator(discriminator, type); + } + + @SuppressWarnings("rawtypes") + private static boolean hasEmptyContructor(Class type) + { + try + { + for (Constructor c : type.getConstructors()) + { + if (c.getParameterTypes().length == 0) + { + return true; + } + } + } + catch (SecurityException e) + { + // really? + } + + return false; + } + } + + @Sharable + @SideOnly(Side.CLIENT) + private static final class HandlerClient extends SimpleChannelInboundHandler + { + @Override + protected void channelRead0(ChannelHandlerContext ctx, PacketBase packet) throws Exception + { + Minecraft mc = Minecraft.getMinecraft(); + packet.actionClient(mc.theWorld, mc.thePlayer); } } - - @SideOnly(Side.CLIENT) - private void actionClient(PacketBase packet) + + @Sharable + private static final class HandlerServer extends SimpleChannelInboundHandler { - Minecraft mc = Minecraft.getMinecraft(); - packet.actionClient(mc.theWorld, mc.thePlayer); - } - - private void actionServer(ChannelHandlerContext ctx, PacketBase packet) - { - EntityPlayerMP player = ((NetHandlerPlayServer)ctx.channel().attr(NetworkRegistry.NET_HANDLER).get()).playerEntity; - packet.actionServer(player.worldObj, player); - } + @Override + protected void channelRead0(ChannelHandlerContext ctx, PacketBase packet) throws Exception + { + EntityPlayerMP player = ((NetHandlerPlayServer) ctx.channel().attr(NetworkRegistry.NET_HANDLER).get()).playerEntity; + packet.actionServer(player.worldObj, player); + } + } // UTIL SENDING METHODS @@ -216,51 +300,6 @@ public class PacketManager extends FMLIndexedMessageToMessageCodec } } - // OTHER METHODS - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception - { - LOGGER.log(Level.ERROR, "Error in packet handling!", cause); - ctx.fireExceptionCaught(cause); // for the other handlers. - } - - @Override - public FMLIndexedMessageToMessageCodec addDiscriminator(int discriminator, Class type) - { - // double check it has an empty constructor. or fail early. - // This is to gaurd myself against my own stupidity. - - if (!hasEmptyContructor(type)) - { - LOGGER.log(Level.FATAL, type.getName() + "does not have an empty constructor!"); - } - - return super.addDiscriminator(discriminator, type); - } - - @SuppressWarnings("rawtypes") - private static boolean hasEmptyContructor(Class type) - { - try - { - for (Constructor c : type.getConstructors()) - { - if (c.getParameterTypes().length == 0) - { - return true; - } - } - } - catch (SecurityException e) - { - // really? - } - - return false; - } - - public static void sendFullUpdate(EntityPlayerMP player){ ArrayList timingEntities = EntityManager.INSTANCE.getWorses(100); ArrayList timingTileEnts = TileEntityManager.INSTANCE.getWorses(100);