HaniiPuppy

Packet-sending code.

Mar 17th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. /**
  2.  * Packet sent to players upon joining the server, for sending registry information as loaded or otherwise held by the
  3.  * server.
  4.  */
  5. public class PlayerJoinPacket implements IMessage
  6. {
  7.     /**
  8.      * The packet handler, for copying config information from the server into the client.
  9.      */
  10.     public static class Handler implements IMessageHandler<PlayerJoinPacket, IMessage>
  11.     {
  12.         @Override
  13.         public IMessage onMessage(PlayerJoinPacket message, MessageContext ctx)
  14.         {
  15.             ArcaneBooks.instance.registries.spellEffects.loadFromString(message.unparsedSpellEffects);
  16.             return null;
  17.         }
  18.        
  19.         /*
  20.        
  21.         Version of the above method for Minecraft v1.8.
  22.        
  23.         @Override
  24.         public IMessage onMessage(PlayerJoinPacket message, MessageContext ctx)
  25.         {
  26.             IThreadListener mainThread = Minecraft.getMinecraft();
  27.            
  28.             mainThread.addScheduledTask(new Runnable()
  29.             {
  30.                 @Override
  31.                 public void run()
  32.                 { ArcaneBooks.instance.registries.spellEffects.fillFromString(message.unparsedSpellEffects); }
  33.             });
  34.             return null;
  35.         }
  36.        
  37.         */
  38.     }
  39.    
  40.     String unparsedSpellEffects = null;
  41.    
  42.     @Override
  43.     public void fromBytes(ByteBuf buf)
  44.     { unparsedSpellEffects = ByteBufUtils.readUTF8String(buf); }
  45.  
  46.     @Override
  47.     public void toBytes(ByteBuf buf)
  48.     { ByteBufUtils.writeUTF8String(buf, ArcaneBooks.instance.registries.spellEffects.toString()); }
  49. }
  50.  
  51.  
  52.  
  53. -----------------------------------------------------------------------------------------------------------
  54.  
  55. // In the main mod class.
  56.  
  57.     @Mod.EventHandler
  58.     public void PreInitializationEvent(FMLPreInitializationEvent event)
  59.     {
  60.         packetChannel = NetworkRegistry.INSTANCE.newSimpleChannel("ArcaneBooks");
  61.         packetChannel.registerMessage(PlayerJoinPacket.Handler.class, PlayerJoinPacket.class, 1, Side.CLIENT);
  62.         FMLCommonHandler.instance().bus().register(new PlayerJoinServerListener());
  63.     }
  64.  
  65.  
  66.  
  67. ------------------------------------------------------------------------------------------------------------
  68.  
  69.  
  70.  
  71. /**
  72.  * Listens to players logging into the server, so that the server can send the player a packet containing registry
  73.  * information/contents as loaded or otherwise held by the server.
  74.  */
  75. public class PlayerJoinServerListener
  76. {
  77.     @SubscribeEvent
  78.     public void onPlayerJoin(PlayerLoggedInEvent event)
  79.     {
  80.         boolean runningOnServer = !DimensionManager.getWorld(0).isRemote;
  81.        
  82.         if(runningOnServer)
  83.             ArcaneBooks.instance.packetChannel.sendTo(new PlayerJoinPacket(), (EntityPlayerMP)event.player);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment