Guest User

Untitled

a guest
Jul 13th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package halestormxv.eAngelus.network.packets;
  2.  
  3. import halestormxv.eAngelus.capabilities.Interfaces.IMorality;
  4. import halestormxv.eAngelus.capabilities.MoralityCapability.moralityProvider;
  5. import halestormxv.eAngelus.main.Utils;
  6. import halestormxv.eAngelus.network.eAngelusPacketHandler;
  7. import io.netty.buffer.ByteBuf;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.entity.player.EntityPlayerMP;
  10. import net.minecraftforge.fml.common.FMLCommonHandler;
  11. import net.minecraftforge.fml.common.network.ByteBufUtils;
  12. import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
  13. import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
  14. import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
  15. import net.minecraftforge.fml.relauncher.Side;
  16.  
  17. /**
  18.  * Created by Blaze on 7/12/2017.
  19.  */
  20. public class SyncMorality implements IMessage
  21. {
  22.     private boolean messageValid;
  23.     private int moralityValue;
  24.     private String className;
  25.     private String moralityValueFieldName;
  26.  
  27.     public SyncMorality(){ this.messageValid = false; }
  28.  
  29.     public SyncMorality(int moralityValue, String className, String moralityValueFieldName)
  30.     {
  31.         this.moralityValue = moralityValue;
  32.         this.className = className;
  33.         this.moralityValueFieldName = moralityValueFieldName;
  34.         this.messageValid = true;
  35.     }
  36.  
  37.     @Override
  38.     public void fromBytes(ByteBuf buf)
  39.     {
  40.         try{
  41.             this.moralityValue = buf.readInt();
  42.             this.className = ByteBufUtils.readUTF8String(buf);
  43.             this.moralityValueFieldName = ByteBufUtils.readUTF8String(buf);
  44.         } catch (IndexOutOfBoundsException ioe) { Utils.getLogger().catching(ioe); return; }
  45.         this.messageValid = true;
  46.     }
  47.  
  48.     @Override
  49.     public void toBytes(ByteBuf buf)
  50.     {
  51.         if (!this.messageValid)
  52.             return;
  53.         buf.writeInt(moralityValue);
  54.         ByteBufUtils.writeUTF8String(buf, this.className);
  55.         ByteBufUtils.writeUTF8String(buf, this.moralityValueFieldName);
  56.         //ByteBufUtils.writeTag(); Might be needed to sync data?
  57.     }
  58.  
  59.     public static class Handler implements IMessageHandler<SyncMorality, IMessage>
  60.     {
  61.         @Override
  62.         public IMessage onMessage(SyncMorality message, MessageContext ctx)
  63.         {
  64.             if(!message.messageValid && ctx.side != Side.SERVER)
  65.                 return null;
  66.             FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> processMessage(message, ctx));
  67.             return null;
  68.         }
  69.         void processMessage(SyncMorality message, MessageContext ctx)
  70.         {
  71.             EntityPlayerMP player = ctx.getServerHandler().playerEntity;
  72.             if(player == null)
  73.                 return;
  74.             if(!player.hasCapability(moralityProvider.MORALITY_CAP, null))
  75.                 return;
  76.             IMorality morality = player.getCapability(moralityProvider.MORALITY_CAP, null);
  77.             eAngelusPacketHandler.INSTANCE.sendTo(new SyncMoralityReturn(morality.getMorality(), message.className, message.moralityValueFieldName),
  78.                     ctx.getServerHandler().playerEntity); //ctx.getServerHandler().playerEntity) = Player Who Sent Packet
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment