Guest User

PacketPipeline

a guest
Jul 24th, 2016
81
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package fr.shyfe.dcedo.common;
  2.  
  3. import cpw.mods.fml.common.network.FMLEmbeddedChannel;
  4. import cpw.mods.fml.common.network.FMLOutboundHandler;
  5. import cpw.mods.fml.common.network.FMLOutboundHandler.OutboundTarget;
  6. import cpw.mods.fml.common.network.NetworkRegistry;
  7. import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
  8. import cpw.mods.fml.common.network.internal.FMLProxyPacket;
  9. import cpw.mods.fml.relauncher.Side;
  10. import cpw.mods.fml.relauncher.SideOnly;
  11. import io.netty.buffer.ByteBuf;
  12. import io.netty.buffer.Unpooled;
  13. import io.netty.channel.Channel;
  14. import io.netty.channel.ChannelHandler;
  15. import io.netty.channel.ChannelHandler.Sharable;
  16. import io.netty.channel.ChannelHandlerContext;
  17. import io.netty.handler.codec.MessageToMessageCodec;
  18. import io.netty.util.Attribute;
  19. import java.util.Collections;
  20. import java.util.Comparator;
  21. import java.util.EnumMap;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. import net.minecraft.client.Minecraft;
  25. import net.minecraft.entity.player.EntityPlayer;
  26. import net.minecraft.entity.player.EntityPlayerMP;
  27. import net.minecraft.network.INetHandler;
  28. import net.minecraft.network.NetHandlerPlayServer;
  29.  
  30. public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, AbstractPacket>
  31. {
  32. private EnumMap<Side, FMLEmbeddedChannel> channels;
  33. private LinkedList<Class<? extends AbstractPacket>> packets = new LinkedList();
  34. private boolean isPostInitialised = false;
  35.  
  36. public boolean registerPacket(Class<? extends AbstractPacket> clazz)
  37. {
  38. if (this.packets.size() > 256) {
  39. return false;
  40. }
  41. if (this.packets.contains(clazz)) {
  42. return false;
  43. }
  44. if (this.isPostInitialised) {
  45. return false;
  46. }
  47. this.packets.add(clazz);
  48. return true;
  49. }
  50.  
  51. protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out)
  52. throws Exception
  53. {
  54. ByteBuf buffer = Unpooled.buffer();
  55. Class<? extends AbstractPacket> clazz = msg.getClass();
  56. if (!this.packets.contains(msg.getClass())) {
  57. throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName());
  58. }
  59. byte discriminator = (byte)this.packets.indexOf(clazz);
  60. buffer.writeByte(discriminator);
  61. msg.encodeInto(ctx, buffer);
  62. FMLProxyPacket proxyPacket = new FMLProxyPacket(buffer.copy(), (String)ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());
  63. out.add(proxyPacket);
  64. }
  65.  
  66. protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out)
  67. throws Exception
  68. {
  69. ByteBuf payload = msg.payload();
  70. byte discriminator = payload.readByte();
  71. Class<? extends AbstractPacket> clazz = (Class)this.packets.get(discriminator);
  72. if (clazz == null) {
  73. throw new NullPointerException("No packet registered for discriminator: " + discriminator);
  74. }
  75. AbstractPacket pkt = (AbstractPacket)clazz.newInstance();
  76. pkt.decodeInto(ctx, payload.slice());
  77. switch (cpw.mods.fml.common.FMLCommonHandler.instance().getEffectiveSide())
  78. {
  79. case CLIENT:
  80. EntityPlayer player = getClientPlayer();
  81. pkt.handleClientSide(player);
  82. break;
  83. case SERVER:
  84. INetHandler netHandler = (INetHandler)ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
  85. EntityPlayer player1 = ((NetHandlerPlayServer)netHandler).playerEntity;
  86. pkt.handleServerSide(player1);
  87. break;
  88. }
  89. out.add(pkt);
  90. }
  91.  
  92. public void initialise()
  93. {
  94. this.channels = NetworkRegistry.INSTANCE.newChannel("DCEDOInventory", new ChannelHandler[] { this });
  95. registerPackets();
  96. }
  97.  
  98. public void registerPackets()
  99. {
  100. registerPacket(OpenGuiPacket.class);
  101. registerPacket(SyncPlayerPropsPacket.class);
  102. }
  103.  
  104. public void postInitialise()
  105. {
  106. if (this.isPostInitialised) {
  107. return;
  108. }
  109. this.isPostInitialised = true;
  110. Collections.sort(this.packets, new Comparator()
  111. {
  112. public int compare(Class<? extends AbstractPacket> clazz1, Class<? extends AbstractPacket> clazz2)
  113. {
  114. int com = String.CASE_INSENSITIVE_ORDER.compare(clazz1.getCanonicalName(), clazz2.getCanonicalName());
  115. if (com == 0) {
  116. com = clazz1.getCanonicalName().compareTo(clazz2.getCanonicalName());
  117. }
  118. return com;
  119. }
  120.  
  121. @Override
  122. public int compare(Object o1, Object o2)
  123. {
  124. // TODO Auto-generated method stub
  125. return 0;
  126. }
  127. });
  128. }
  129.  
  130. @SideOnly(Side.CLIENT)
  131. private EntityPlayer getClientPlayer()
  132. {
  133. return Minecraft.getMinecraft().thePlayer;
  134. }
  135.  
  136. public void sendToAll(AbstractPacket message)
  137. {
  138. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
  139. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).writeAndFlush(message);
  140. }
  141.  
  142. public void sendTo(AbstractPacket message, EntityPlayerMP player)
  143. {
  144. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER);
  145. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player);
  146. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).writeAndFlush(message);
  147. }
  148.  
  149. public void sendToAllAround(AbstractPacket message, NetworkRegistry.TargetPoint point)
  150. {
  151. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT);
  152. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point);
  153. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).writeAndFlush(message);
  154. }
  155.  
  156. public void sendToDimension(AbstractPacket message, int dimensionId)
  157. {
  158. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION);
  159. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(Integer.valueOf(dimensionId));
  160. ((FMLEmbeddedChannel)this.channels.get(Side.SERVER)).writeAndFlush(message);
  161. }
  162.  
  163. public void sendToServer(AbstractPacket message)
  164. {
  165. ((FMLEmbeddedChannel)this.channels.get(Side.CLIENT)).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
  166. ((FMLEmbeddedChannel)this.channels.get(Side.CLIENT)).writeAndFlush(message);
  167. }
  168. }
RAW Paste Data