Guest User

Untitled

a guest
Jul 7th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. package flaxbeard.cyberware.common.network;
  2.  
  3. import flaxbeard.cyberware.api.CyberwareAPI;
  4. import io.netty.buffer.ByteBuf;
  5.  
  6. import java.util.concurrent.Callable;
  7.  
  8. import net.minecraft.client.Minecraft;
  9. import net.minecraft.entity.Entity;
  10. import net.minecraft.nbt.NBTTagCompound;
  11. import net.minecraftforge.common.DimensionManager;
  12. import net.minecraftforge.fml.common.network.ByteBufUtils;
  13. import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
  14. import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
  15. import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
  16.  
  17. public class CyberwareSyncPacket implements IMessage
  18. {
  19.     public CyberwareSyncPacket() {}
  20.    
  21.     private NBTTagCompound data;
  22.     private int entityId;
  23.     private int dimensionId;
  24.  
  25.     public CyberwareSyncPacket(NBTTagCompound data, int dimensionId, int entityId)
  26.     {
  27.         this.data = data;
  28.         this.entityId = entityId;
  29.         this.dimensionId = dimensionId;
  30.     }
  31.  
  32.     @Override
  33.     public void toBytes(ByteBuf buf)
  34.     {
  35.         buf.writeInt(dimensionId);
  36.         buf.writeInt(entityId);
  37.         ByteBufUtils.writeTag(buf, data);
  38.     }
  39.    
  40.     @Override
  41.     public void fromBytes(ByteBuf buf)
  42.     {
  43.         dimensionId = buf.readInt();
  44.         entityId = buf.readInt();
  45.         data = ByteBufUtils.readTag(buf);
  46.     }
  47.    
  48.     public static class CyberwareSyncPacketHandler implements IMessageHandler<CyberwareSyncPacket, IMessage>
  49.     {
  50.  
  51.         @Override
  52.         public IMessage onMessage(CyberwareSyncPacket message, MessageContext ctx)
  53.         {
  54.             Minecraft.getMinecraft().addScheduledTask(new DoSync(message.dimensionId, message.entityId, message.data));
  55.  
  56.             return null;
  57.         }
  58.        
  59.     }
  60.    
  61.     private static class DoSync implements Callable<Void>
  62.     {
  63.         private int dimensionId;
  64.         private int entityId;
  65.         private NBTTagCompound data;
  66.        
  67.         public DoSync(int dimensionId, int entityId, NBTTagCompound data)
  68.         {
  69.             this.dimensionId = dimensionId;
  70.             this.entityId = entityId;
  71.             this.data = data;
  72.         }
  73.        
  74.         @Override
  75.         public Void call() throws Exception
  76.         {
  77.             Entity targetEntity = DimensionManager.getWorld(dimensionId).getEntityByID(entityId);
  78.            
  79.             if (targetEntity != null && CyberwareAPI.hasCapability(targetEntity))
  80.             {
  81.                 CyberwareAPI.getCapability(targetEntity).deserializeNBT(data);
  82.             }
  83.             else
  84.             {
  85.                 System.out.println("C// " + entityId);
  86.             }
  87.            
  88.             return null;
  89.         }
  90.        
  91.     }
  92. }
  93.  
  94.  
  95. // EVENT HANDLER CODE
  96.     @SubscribeEvent
  97.     public void syncCyberwareData(EntityJoinWorldEvent event)
  98.     {
  99.         Entity e = event.getEntity();
  100.         if (CyberwareAPI.hasCapability(e))
  101.         {
  102.             NBTTagCompound nbt = CyberwareAPI.getCapability(e).serializeNBT();
  103.             CyberwarePacketHandler.INSTANCE.sendToAll(new CyberwareSyncPacket(nbt, e.worldObj.provider.getDimension(), e.getEntityId()));
  104.             System.out.println("S// " + e.getEntityId());
  105.  
  106.         }
  107.     }
Add Comment
Please, Sign In to add comment