Guest User

Untitled

a guest
Jul 13th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. package halestormxv.eAngelus.network.packets;
  2.  
  3. import halestormxv.eAngelus.main.Utils;
  4. import io.netty.buffer.ByteBuf;
  5. import net.minecraft.client.Minecraft;
  6. import net.minecraftforge.fml.common.network.ByteBufUtils;
  7. import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
  8. import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
  9. import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
  10. import net.minecraftforge.fml.relauncher.Side;
  11.  
  12. import java.lang.reflect.Field;
  13.  
  14. /**
  15.  * Created by Blaze on 7/12/2017.
  16.  */
  17. public class SyncMoralityReturn implements IMessage
  18. {
  19.     private boolean messageValid;
  20.     private int moralityValue;
  21.     private String className;
  22.     private String moralityValueFieldName;
  23.  
  24.     public SyncMoralityReturn()
  25.     {
  26.         this.messageValid = false;
  27.     }
  28.  
  29.     public SyncMoralityReturn(int moralityValue, String className, String moralityValueFieldName)
  30.     {
  31.         this.messageValid = true;
  32.         this.moralityValue = moralityValue;
  33.         this.className = className;
  34.         this.moralityValueFieldName = moralityValueFieldName;
  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(this.moralityValue);
  54.         ByteBufUtils.writeUTF8String(buf, this.className);
  55.         ByteBufUtils.writeUTF8String(buf, this.moralityValueFieldName);
  56.     }
  57.  
  58.     public static class Handler implements IMessageHandler<SyncMoralityReturn, IMessage>
  59.     {
  60.         @Override
  61.         public IMessage onMessage(SyncMoralityReturn message, MessageContext ctx)
  62.         {
  63.             if(!message.messageValid && ctx.side != Side.CLIENT)
  64.             return null;
  65.             Minecraft.getMinecraft().addScheduledTask(() -> processMessage(message));
  66.             return null;
  67.         }
  68.  
  69.         void processMessage(SyncMoralityReturn message)
  70.         {
  71.             try{
  72.                 Class clazz = Class.forName(message.className);
  73.                 Field moralityValueFieldName = clazz.getDeclaredField(message.moralityValueFieldName);
  74.                 moralityValueFieldName.setInt(clazz, message.moralityValue);
  75.             } catch (Exception e) {
  76.                 Utils.getLogger().catching(e);
  77.             }
  78.  
  79.         }
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment