mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
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:
parent
dea0207e4e
commit
8b2d941502
@ -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; }
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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; }
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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; }
|
||||
|
@ -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; }
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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; }
|
||||
|
@ -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; }
|
||||
|
@ -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; }
|
||||
|
@ -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; }
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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; }
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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; }
|
||||
|
@ -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{
|
||||
|
@ -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; }
|
||||
|
@ -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; }
|
||||
|
@ -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; }
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user