Fixed MW dep prb

Fixed Opis Network derp.
This commit is contained in:
ProfMobius committed 2014-06-23 23:19:27 +02:00
1 parent 40a73988fc
commit f2ef8e4818
3 files changed
+131 -91

No files matched your search

@@ -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<PacketBase>
//public class PacketManager extends FMLIndexedMessageToMessageCodec<PacketBase>
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<Side, FMLEmbeddedChannel> channels = Maps.newEnumMap(Side.class);
public static void init()
@@ -81,55 +83,137 @@ public class PacketManager extends FMLIndexedMessageToMessageCodec<PacketBase>
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<PacketBase>
{
@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<PacketBase> addDiscriminator(int discriminator, Class<? extends PacketBase> 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<PacketBase>
{
@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<PacketBase>
{
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<PacketBase>
}
}
// 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<PacketBase> addDiscriminator(int discriminator, Class<? extends PacketBase> 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<DataEntity> timingEntities = EntityManager.INSTANCE.getWorses(100);
ArrayList<DataBlockTileEntity> timingTileEnts = TileEntityManager.INSTANCE.getWorses(100);