예제 #1
0
  @Override
  protected void decode(
      ChannelHandlerContext channelHandlerContext, FMLProxyPacket fmlProxyPacket, List<Object> out)
      throws Exception {
    ByteBuf payload = fmlProxyPacket.payload();
    byte discriminator = payload.readByte();
    Class<? extends AbstractPacketOld> clazz = this.packets.get(discriminator);
    if (clazz == null) {
      throw new NullPointerException("No packet registered for discriminator: " + discriminator);
    }

    AbstractPacketOld packet = clazz.newInstance();
    packet.decodeInto(channelHandlerContext, payload.slice());

    EntityPlayer player;
    switch (FMLCommonHandler.instance().getEffectiveSide()) {
      case CLIENT:
        player = PacketHandlerOld.getClientPlayer();
        packet.handleClientSide(player);
        break;

      case SERVER:
        INetHandler netHandler =
            channelHandlerContext.channel().attr(NetworkRegistry.NET_HANDLER).get();
        player = ((NetHandlerPlayServer) netHandler).playerEntity;
        packet.handleServerSide(player);
        break;

      default:
    }
    out.add(packet);
  }
예제 #2
0
 @Override
 protected void encode(
     ChannelHandlerContext channelHandlerContext,
     AbstractPacketOld abstractPacketOld,
     List<Object> out)
     throws Exception {
   ByteBuf buffer = Unpooled.buffer();
   Class<? extends AbstractPacketOld> clazz = abstractPacketOld.getClass();
   if (!this.packets.contains(abstractPacketOld.getClass())) {
     throw new NullPointerException(
         "No Packet Registered for: " + abstractPacketOld.getClass().getCanonicalName());
   }
   byte discriminator = (byte) this.packets.indexOf(clazz);
   buffer.writeByte(discriminator);
   abstractPacketOld.encodeInto(channelHandlerContext, buffer);
   FMLProxyPacket proxyPacket =
       new FMLProxyPacket(
           new PacketBuffer(buffer.copy()),
           channelHandlerContext.channel().attr(NetworkRegistry.FML_CHANNEL).get());
   out.add(proxyPacket);
 }