HyCodeYourTale
classpublicPriority 3

WeatherParticle

com.hypixel.hytale.protocol.WeatherParticle

6

Methods

6

Public Methods

5

Fields

3

Constructors

Constants

intFIXED_BLOCK_SIZE= 13
intMAX_SIZE= 16384018
intNULLABLE_BIT_FIELD_SIZE= 1
intVARIABLE_BLOCK_START= 13
intVARIABLE_FIELD_COUNT= 1

Constructors

public
WeatherParticle()
public
WeatherParticle(String systemId, Color color, float scale, boolean isOvergroundOnly, float positionOffsetMultiplier)
public
WeatherParticle(WeatherParticle other)

Methods

Public Methods (6)

publicstatic
int computeBytesConsumed(ByteBuf buf, int offset)
public
int computeSize()
public
boolean equals(Object obj)
@Override
public
int hashCode()
@Override
public
void serialize(ByteBuf buf)
publicstatic
ValidationResult validateStructure(ByteBuf buffer, int offset)

Fields

Public Fields (5)

publicColor color
publicboolean isOvergroundOnly
publicfloat positionOffsetMultiplier
publicfloat scale
publicString systemId

Related Classes

Source Code

package com.hypixel.hytale.protocol;

import com.hypixel.hytale.protocol.io.PacketIO;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.ValidationResult;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class WeatherParticle {
   public static final int NULLABLE_BIT_FIELD_SIZE = 1;
   public static final int FIXED_BLOCK_SIZE = 13;
   public static final int VARIABLE_FIELD_COUNT = 1;
   public static final int VARIABLE_BLOCK_START = 13;
   public static final int MAX_SIZE = 16384018;
   @Nullable
   public String systemId;
   @Nullable
   public Color color;
   public float scale;
   public boolean isOvergroundOnly;
   public float positionOffsetMultiplier;

   public WeatherParticle() {
   }

   public WeatherParticle(@Nullable String systemId, @Nullable Color color, float scale, boolean isOvergroundOnly, float positionOffsetMultiplier) {
      this.systemId = systemId;
      this.color = color;
      this.scale = scale;
      this.isOvergroundOnly = isOvergroundOnly;
      this.positionOffsetMultiplier = positionOffsetMultiplier;
   }

   public WeatherParticle(@Nonnull WeatherParticle other) {
      this.systemId = other.systemId;
      this.color = other.color;
      this.scale = other.scale;
      this.isOvergroundOnly = other.isOvergroundOnly;
      this.positionOffsetMultiplier = other.positionOffsetMultiplier;
   }

   @Nonnull
   public static WeatherParticle deserialize(@Nonnull ByteBuf buf, int offset) {
      WeatherParticle obj = new WeatherParticle();
      byte nullBits = buf.getByte(offset);
      if ((nullBits & 2) != 0) {
         obj.color = Color.deserialize(buf, offset + 1);
      }

      obj.scale = buf.getFloatLE(offset + 4);
      obj.isOvergroundOnly = buf.getByte(offset + 8) != 0;
      obj.positionOffsetMultiplier = buf.getFloatLE(offset + 9);
      int pos = offset + 13;
      if ((nullBits & 1) != 0) {
         int systemIdLen = VarInt.peek(buf, pos);
         if (systemIdLen < 0) {
            throw ProtocolException.negativeLength("SystemId", systemIdLen);
         }

         if (systemIdLen > 4096000) {
            throw ProtocolException.stringTooLong("SystemId", systemIdLen, 4096000);
         }

         int systemIdVarLen = VarInt.length(buf, pos);
         obj.systemId = PacketIO.readVarString(buf, pos, PacketIO.UTF8);
         pos += systemIdVarLen + systemIdLen;
      }

      return obj;
   }

   public static int computeBytesConsumed(@Nonnull ByteBuf buf, int offset) {
      byte nullBits = buf.getByte(offset);
      int pos = offset + 13;
      if ((nullBits & 1) != 0) {
         int sl = VarInt.peek(buf, pos);
         pos += VarInt.length(buf, pos) + sl;
      }

      return pos - offset;
   }

   public void serialize(@Nonnull ByteBuf buf) {
      byte nullBits = 0;
      if (this.systemId != null) {
         nullBits = (byte)(nullBits | 1);
      }

      if (this.color != null) {
         nullBits = (byte)(nullBits | 2);
      }

      buf.writeByte(nullBits);
      if (this.color != null) {
         this.color.serialize(buf);
      } else {
         buf.writeZero(3);
      }

      buf.writeFloatLE(this.scale);
      buf.writeByte(this.isOvergroundOnly ? 1 : 0);
      buf.writeFloatLE(this.positionOffsetMultiplier);
      if (this.systemId != null) {
         PacketIO.writeVarString(buf, this.systemId, 4096000);
      }
   }

   public int computeSize() {
      int size = 13;
      if (this.systemId != null) {
         size += PacketIO.stringSize(this.systemId);
      }

      return size;
   }

   public static ValidationResult validateStructure(@Nonnull ByteBuf buffer, int offset) {
      if (buffer.readableBytes() - offset < 13) {
         return ValidationResult.error("Buffer too small: expected at least 13 bytes");
      } else {
         byte nullBits = buffer.getByte(offset);
         int pos = offset + 13;
         if ((nullBits & 1) != 0) {
            int systemIdLen = VarInt.peek(buffer, pos);
            if (systemIdLen < 0) {
               return ValidationResult.error("Invalid string length for SystemId");
            }

            if (systemIdLen > 4096000) {
               return ValidationResult.error("SystemId exceeds max length 4096000");
            }

            pos += VarInt.length(buffer, pos);
            pos += systemIdLen;
            if (pos > buffer.writerIndex()) {
               return ValidationResult.error("Buffer overflow reading SystemId");
            }
         }

         return ValidationResult.OK;
      }
   }

   public WeatherParticle clone() {
      WeatherParticle copy = new WeatherParticle();
      copy.systemId = this.systemId;
      copy.color = this.color != null ? this.color.clone() : null;
      copy.scale = this.scale;
      copy.isOvergroundOnly = this.isOvergroundOnly;
      copy.positionOffsetMultiplier = this.positionOffsetMultiplier;
      return copy;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj) {
         return true;
      } else {
         return !(obj instanceof WeatherParticle other)
            ? false
            : Objects.equals(this.systemId, other.systemId)
               && Objects.equals(this.color, other.color)
               && this.scale == other.scale
               && this.isOvergroundOnly == other.isOvergroundOnly
               && this.positionOffsetMultiplier == other.positionOffsetMultiplier;
      }
   }

   @Override
   public int hashCode() {
      return Objects.hash(this.systemId, this.color, this.scale, this.isOvergroundOnly, this.positionOffsetMultiplier);
   }
}