Arctic_Wolfy

PlayerSync

Aug 6th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. public class StatSyncMessage implements IMessage {
  2.  
  3.     private NBTTagCompound tag;
  4.     public StatSyncMessage(){}
  5.     public StatSyncMessage(NBTTagCompound tag) {this.tag = tag;}
  6.  
  7.     @Override public void fromBytes(ByteBuf buf) {tag = ByteBufUtils.readTag(buf);}
  8.     @Override public void toBytes(ByteBuf buf) {ByteBufUtils.writeTag(buf, tag);}
  9.  
  10.     public static class StatMessageHandler implements IMessageHandler<StatSyncMessage,IMessage> {
  11.  
  12.         @Override
  13.         public IMessage onMessage(StatSyncMessage message, MessageContext ctx) {
  14.             final NBTTagCompound tag = message.tag;
  15.             Minecraft.getMinecraft().addScheduledTask(new Callable<Object>() {
  16.                 @Override
  17.                 public Object call() throws Exception {
  18.                     Minecraft mc = Minecraft.getMinecraft();
  19.                     WorldClient world = mc.theWorld;
  20.                     List<EntityPlayer> players = world.playerEntities;
  21.  
  22.                     for (EntityPlayer player : players) {
  23.                         if (tag.getString("name").equals(player.getName())) {
  24.  
  25.                             IFurredPlayer furred = player.getCapability(FurryX.FURRED_PLAYER_CAPABILITY, null);
  26.                             furred.deserializeNBT(tag);
  27.                             furred.setPlayer(player);
  28.  
  29.                             player.refreshDisplayName();
  30.                             break;
  31.                         }
  32.                     }
  33.                     return null;
  34.                 }
  35.             });
  36.             return null;
  37.         }
  38.     }
  39. }
  40.  
  41. public class EventHandler {
  42.     @SubscribeEvent
  43.     public void playerLoggedOn(net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent event) {
  44.         EntityPlayer playerA = event.player;
  45.         World world = playerA.worldObj;
  46.  
  47.         if (!world.isRemote){
  48.             IFurredPlayer furred = playerA.getCapability(FURRED_PLAYER_CAPABILITY, null);
  49.  
  50.             FXPackerHandler.INSTANCE.sendToAll(new StatSyncMessage(furred.serializeNBT()));
  51.  
  52.             List<EntityPlayer> players = world.playerEntities;
  53.             for (EntityPlayer playerB : players) {
  54.                 IFurredPlayer furredB = playerB.getCapability(FURRED_PLAYER_CAPABILITY, null);
  55.  
  56.                 if (playerA instanceof EntityPlayerMP)
  57.                     FXPackerHandler.INSTANCE.sendTo(new StatSyncMessage(furredB.serializeNBT()), (EntityPlayerMP) playerA);
  58.             }
  59.         }
  60.  
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment