Offset integer ranges in runtime block data serialization

this is useful for stuff like snow layers where the range doesn't start at 0.
This commit is contained in:
Dylan K. Taylor 2022-07-13 19:49:30 +01:00
parent dea0207e4e
commit 8b2d941502
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
22 changed files with 34 additions and 25 deletions

View File

@ -54,7 +54,7 @@ class Anvil extends Transparent implements Fallable{
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeInt(2, $this->getDamage());
$w->writeBoundedInt(2, self::UNDAMAGED, self::VERY_DAMAGED, $this->getDamage());
}
public function getRequiredStateDataBits() : int{ return 2; }

View File

@ -65,7 +65,7 @@ class Bamboo extends Transparent{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(2, $this->getLeafSize());
$w->writeBoundedInt(2, self::NO_LEAVES, self::LARGE_LEAVES, $this->getLeafSize());
$w->writeBool($this->isThick());
$w->writeBool($this->isReady());
}

View File

@ -49,7 +49,7 @@ class Cactus extends Transparent{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(4, $this->age);
$w->writeBoundedInt(4, 0, self::MAX_AGE, $this->age);
}
public function getAge() : int{ return $this->age; }

View File

@ -48,7 +48,7 @@ class Cake extends Transparent implements FoodSource{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(3, $this->bites);
$w->writeBoundedInt(3, 0, self::MAX_BITES, $this->bites);
}
/**

View File

@ -56,7 +56,7 @@ class CocoaBlock extends Transparent{
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeInt(2, $this->age);
$w->writeBoundedInt(2, 0, self::MAX_AGE, $this->age);
}
public function getAge() : int{ return $this->age; }

View File

@ -46,7 +46,7 @@ abstract class Crops extends Flowable{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(3, $this->age);
$w->writeBoundedInt(3, 0, self::MAX_AGE, $this->age);
}
public function getAge() : int{ return $this->age; }

View File

@ -50,7 +50,7 @@ class DaylightSensor extends Transparent{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(4, $this->signalStrength);
$w->writeBoundedInt(4, 0, 15, $this->signalStrength);
$w->writeBool($this->inverted);
}

View File

@ -45,7 +45,7 @@ class Farmland extends Transparent{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(3, $this->wetness);
$w->writeBoundedInt(3, 0, self::MAX_WETNESS, $this->wetness);
}
public function getWetness() : int{ return $this->wetness; }

View File

@ -47,7 +47,7 @@ class Fire extends BaseFire{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(4, $this->age);
$w->writeBoundedInt(4, 0, self::MAX_AGE, $this->age);
}
public function getAge() : int{ return $this->age; }

View File

@ -40,7 +40,7 @@ class FrostedIce extends Ice{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(2, $this->age);
$w->writeBoundedInt(2, 0, self::MAX_AGE, $this->age);
}
public function getAge() : int{ return $this->age; }

View File

@ -42,7 +42,7 @@ final class Light extends Flowable{
}
protected function encodeType(RuntimeDataWriter $w) : void{
$w->writeInt(4, $this->level);
$w->writeBoundedInt(4, self::MIN_LIGHT_LEVEL, self::MAX_LIGHT_LEVEL, $this->level);
}
public function getLightLevel() : int{ return $this->level; }

View File

@ -58,7 +58,7 @@ abstract class Liquid extends Transparent{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(3, $this->decay);
$w->writeBoundedInt(3, 0, self::MAX_DECAY, $this->decay);
$w->writeBool($this->falling);
$w->writeBool($this->still);
}

View File

@ -45,7 +45,7 @@ class NetherWartPlant extends Flowable{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(2, $this->age);
$w->writeBoundedInt(2, 0, self::MAX_AGE, $this->age);
}
public function getAge() : int{ return $this->age; }

View File

@ -48,13 +48,13 @@ class RedstoneRepeater extends Flowable{
protected function decodeState(RuntimeDataReader $r) : void{
$this->facing = $r->readHorizontalFacing();
$this->delay = $r->readBoundedInt(2, self::MIN_DELAY - 1, self::MAX_DELAY - 1) + 1;
$this->delay = $r->readBoundedInt(2, self::MIN_DELAY, self::MAX_DELAY);
$this->powered = $r->readBool();
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeHorizontalFacing($this->facing);
$w->writeInt(2, $this->delay - 1);
$w->writeBoundedInt(2, self::MIN_DELAY, self::MAX_DELAY, $this->delay);
$w->writeBool($this->powered);
}

View File

@ -42,12 +42,12 @@ class SeaPickle extends Transparent{
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{
$this->count = $r->readBoundedInt(2, self::MIN_COUNT - 1, self::MAX_COUNT - 1) + 1;
$this->count = $r->readBoundedInt(2, self::MIN_COUNT, self::MAX_COUNT);
$this->underwater = $r->readBool();
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(2, $this->count - 1);
$w->writeBoundedInt(2, self::MIN_COUNT, self::MAX_COUNT, $this->count);
$w->writeBool($this->underwater);
}

View File

@ -50,11 +50,11 @@ class SnowLayer extends Flowable implements Fallable{
public function getRequiredStateDataBits() : int{ return 3; }
protected function decodeState(RuntimeDataReader $r) : void{
$this->layers = $r->readBoundedInt(3, self::MIN_LAYERS - 1, self::MAX_LAYERS - 1) + 1;
$this->layers = $r->readBoundedInt(3, self::MIN_LAYERS, self::MAX_LAYERS);
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(3, $this->layers - 1);
$w->writeBoundedInt(3, self::MIN_LAYERS, self::MAX_LAYERS, $this->layers);
}
public function getLayers() : int{ return $this->layers; }

View File

@ -45,7 +45,7 @@ class Sugarcane extends Flowable{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(4, $this->age);
$w->writeBoundedInt(4, 0, self::MAX_AGE, $this->age);
}
private function grow() : bool{

View File

@ -53,7 +53,7 @@ class SweetBerryBush extends Flowable{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(3, $this->age);
$w->writeBoundedInt(3, self::STAGE_SAPLING, self::STAGE_MATURE, $this->age);
}
public function getAge() : int{ return $this->age; }

View File

@ -36,7 +36,7 @@ trait AnalogRedstoneSignalEmitterTrait{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(4, $this->signalStrength);
$w->writeBoundedInt(4, 0, 15, $this->signalStrength);
}
public function getOutputSignalStrength() : int{ return $this->signalStrength; }

View File

@ -38,7 +38,7 @@ trait SignLikeRotationTrait{
}
protected function encodeState(RuntimeDataWriter $w) : void{
$w->writeInt(4, $this->rotation);
$w->writeBoundedInt(4, 0, 15, $this->rotation);
}
public function getRotation() : int{ return $this->rotation; }

View File

@ -49,7 +49,7 @@ final class RuntimeDataReader{
}
public function readBoundedInt(int $bits, int $min, int $max) : int{
$result = $this->readInt($bits);
$result = $this->readInt($bits) + $min;
if($result < $min || $result > $max){
throw new InvalidSerializedRuntimeDataException("Value is outside the range $min - $max");
}

View File

@ -52,6 +52,15 @@ final class RuntimeDataWriter{
return $this;
}
/** @param int $bits *@return $this */
public function writeBoundedInt(int $bits, int $min, int $max, int $value) : self{
if($value < $min || $value > $max){
throw new \InvalidArgumentException("Value $value is outside the range $min - $max");
}
$this->writeInt($bits, $value - $min);
return $this;
}
/** @return $this */
public function writeBool(bool $value) : self{
return $this->writeInt(1, $value ? 1 : 0);
@ -104,7 +113,7 @@ final class RuntimeDataWriter{
public function writeWallConnections(array $connections) : self{
//TODO: we can pack this into 7 bits instead of 8
foreach(Facing::HORIZONTAL as $facing){
$this->writeInt(2, match($connections[$facing] ?? null){
$this->writeBoundedInt(2, 0, 2, match($connections[$facing] ?? null){
null => 0,
WallConnectionType::SHORT() => 1,
WallConnectionType::TALL() => 2,