Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. public class MessageTrafficBox implements IMessage, IMessageHandler<MessageTrafficBox, IMessage> {
  2.  
  3.     public int x,y,z;
  4.     private int timeBetweenEachSequence, timeForEachSequence;
  5.     private List<Sequence> sequences;
  6.     private List<BlockPos> lights;
  7.  
  8.     public MessageTrafficBox() {
  9.     }
  10.  
  11.     public MessageTrafficBox(int x, int y, int z, int timeBetweenEachSequence, int timeForEachSequence, List<BlockPos> lights, List<Sequence> sequences) {
  12.         this.x = x;
  13.         this.y = y;
  14.         this.z = z;
  15.         this.timeBetweenEachSequence = timeBetweenEachSequence;
  16.         this.timeForEachSequence = timeForEachSequence;
  17.         this.lights = lights;
  18.         this.sequences = sequences;
  19.  
  20.     }
  21.  
  22.     @Override
  23.     public void toBytes(ByteBuf buf) {
  24.         buf.writeInt(x);
  25.         buf.writeInt(y);
  26.         buf.writeInt(z);
  27.  
  28.         buf.writeInt(timeBetweenEachSequence);
  29.         buf.writeInt(timeForEachSequence);
  30.  
  31.         NBTTagCompound compound = new NBTTagCompound();
  32.  
  33.         NBTTagList tagList = new NBTTagList();
  34.         lights.forEach(blockPos -> tagList.appendTag(new NBTTagLong(blockPos.toLong())));
  35.         compound.setTag("lights", tagList);
  36.  
  37.         NBTTagList tagList2 = new NBTTagList();
  38.         sequences.forEach(sequence -> tagList2.appendTag(new NBTTagString(sequence.toString())));
  39.         compound.setTag("sequences", tagList2);
  40.  
  41.         ByteBufUtils.writeTag(buf, compound);
  42.     }
  43.  
  44.     @Override
  45.     public void fromBytes(ByteBuf buf) {
  46.         this.x = buf.readInt();
  47.         this.y = buf.readInt();
  48.         this.z = buf.readInt();
  49.  
  50.         this.timeBetweenEachSequence = buf.readInt();
  51.         this.timeForEachSequence = buf.readInt();
  52.  
  53.         NBTTagCompound compound = ByteBufUtils.readTag(buf);
  54.         sequences = new ArrayList<>();
  55.         lights = new ArrayList<>();
  56.  
  57.         if(compound.hasKey("lights",Constants.NBT.TAG_LIST)){
  58.             lights.clear();
  59.             NBTTagList tagList = compound.getTagList("lights", Constants.NBT.TAG_LONG);
  60.             for (NBTBase nbtBase : tagList) {
  61.                 lights.add(BlockPos.fromLong(((NBTTagLong) nbtBase).getLong()));
  62.             }
  63.         }
  64.  
  65.         if(compound.hasKey("sequences",Constants.NBT.TAG_LIST)){
  66.             sequences.clear();
  67.             NBTTagList tagList = compound.getTagList("sequences", Constants.NBT.TAG_STRING);
  68.             for (NBTBase nbtBase : tagList) {
  69.                 sequences.add(Sequence.fromString(((NBTTagString) nbtBase).getString(), lights));
  70.             }
  71.         }
  72.  
  73.     }
  74.  
  75.     @Override
  76.     public IMessage onMessage(MessageTrafficBox message, MessageContext ctx) {
  77.         World world = ctx.getServerHandler().player.world;
  78.         BlockPos pos = new BlockPos(message.x, message.y, message.z);
  79.         TileEntity tileEntity = world.getTileEntity(pos);
  80.         if(tileEntity instanceof TileEntityTrafficBox) {
  81.             TileEntityTrafficBox trafficBox = (TileEntityTrafficBox) tileEntity;
  82.             trafficBox.setLights(message.lights);
  83.             trafficBox.setSequences(message.sequences);
  84.             trafficBox.setTimeBetweenEachSequence(message.timeBetweenEachSequence);
  85.             trafficBox.setTimeForEachSequence(message.timeForEachSequence);
  86.             TileEntityUtil.markBlockForUpdate(world, pos);
  87.         }
  88.         return null;
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement