Guest User

Untitled

a guest
Sep 7th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. package com.halestormxv.Main.handler.network.packets;
  2.  
  3. import com.halestormxv.Main.handler.ExtendedPlayer;
  4. import com.halestormxv.Main.handler.network.AbstractClientMessageHandler;
  5.  
  6. import cpw.mods.fml.common.network.ByteBufUtils;
  7. import cpw.mods.fml.common.network.simpleimpl.IMessage;
  8. import cpw.mods.fml.common.network.simpleimpl.MessageContext;
  9. import io.netty.buffer.ByteBuf;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.nbt.NBTTagCompound;
  12.  
  13. // I send this packet whenever the player joins the world, specifically from ExtendedPlayer#loadProxyData:
  14. //PacketDispatcher.sendTo(new SyncPlayerPropsMessage(player), (EntityPlayerMP) player);
  15.  
  16. /**
  17.  *
  18.  * A packet to send ALL data stored in your extended properties to the client.
  19.  * This is handy if you only need to send your data once per game session or
  20.  * all of your data needs to be synchronized together; it's also handy while
  21.  * first starting, since you only need one packet for everything - however,
  22.  * you should NOT use such a packet in your final product!!!
  23.  *
  24.  * Each packet should handle one thing and one thing only, in order to minimize
  25.  * network traffic as much as possible. There is no point sending 20+ fields'
  26.  * worth of data when you just need the current mana amount; conversely, it's
  27.  * foolish to send 20 packets for all the data when the player first loads, when
  28.  * you could send it all in one single packet.
  29.  *
  30.  * TL;DR - make separate packets for each piece of data, and one big packet for
  31.  * those times when you need to send everything.
  32.  *
  33.  */
  34. public class SyncPlayerPropsMessage implements IMessage
  35. //remember - the IMessageHandler will be implemented as a static inner class
  36. {
  37.     // Previously, we've been writing each field in our properties one at a time,
  38.     // but that is really annoying, and we've already done it in the save and load
  39.     // NBT methods anyway, so here's a slick way to efficiently send all of your
  40.     // extended data, and no matter how much you add or remove, you'll never have
  41.     // to change the packet / synchronization of your data.
  42.  
  43.     // this will store our ExtendedPlayer data, allowing us to easily read and write
  44.     private NBTTagCompound data;
  45.  
  46.     // The basic, no-argument constructor MUST be included to use the new automated handling
  47.     public SyncPlayerPropsMessage() {}
  48.  
  49.     // We need to initialize our data, so provide a suitable constructor:
  50.     public SyncPlayerPropsMessage(EntityPlayer player) {
  51.         // create a new tag compound
  52.         data = new NBTTagCompound();
  53.         // and save our player's data into it
  54.         ExtendedPlayer.get(player).saveNBTData(data);
  55.     }
  56.  
  57.     @Override
  58.     public void fromBytes(ByteBuf buffer) {
  59.         // luckily, ByteBufUtils provides an easy way to read the NBT
  60.         data = ByteBufUtils.readTag(buffer);
  61.     }
  62.  
  63.     @Override
  64.     public void toBytes(ByteBuf buffer) {
  65.         // ByteBufUtils provides a convenient method for writing the compound
  66.         ByteBufUtils.writeTag(buffer, data);
  67.     }
  68.  
  69.     /**
  70.      * Remember: this class MUST be static or you will crash
  71.      *
  72.      * Don't forget to add as well - it is required
  73.      */
  74.     public static class Handler extends AbstractClientMessageHandler<SyncPlayerPropsMessage> {
  75.         // the fruits of our labor: we immediately know from the method name that we are handling
  76.         // a message on the client side, and we have our EntityPlayer right there ready for use. Awesome.
  77.         @Override
  78.         public IMessage handleClientMessage(EntityPlayer player, SyncPlayerPropsMessage message, MessageContext ctx) {
  79.             // now we can just load the NBTTagCompound data directly; one and done, folks
  80.             ExtendedPlayer.get(player).loadNBTData(message.data);
  81.             return null;
  82.         }
  83.  
  84.         // Note here that we don't (and can't) implement the handleServerMessage method
  85.         // since we extended AbstractClientMessage. This is exactly what we want.
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment