Skip to content

Commit

Permalink
StructureBlock: add types, getters and setters for editor settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Bqleine committed Sep 13, 2023
1 parent 93a0f91 commit 95d7218
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 14 deletions.
230 changes: 216 additions & 14 deletions src/block/tile/StructureBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@

namespace pocketmine\block\tile;

use pocketmine\block\utils\StructureAnimationMode;
use pocketmine\block\utils\StructureAxes;
use pocketmine\block\utils\StructureBlockType;
use pocketmine\block\utils\StructureRotation;
use pocketmine\math\Vector3;
use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\CompoundTag;
Expand Down Expand Up @@ -52,18 +55,18 @@ class StructureBlock extends Spawnable{
public const TAG_Y_STRUCTURE_SIZE = "yStructureSize";
public const TAG_Z_STRUCTURE_SIZE = "zStructureSize";

private int $animationMode = 0; // byte
private StructureAnimationMode $animationMode = StructureAnimationMode::NONE;
private float $animationSeconds = 0;
private StructureBlockType $type = StructureBlockType::SAVE;
private string $dataField = ""; // unused
private bool $ignoreEntities = false;
private float $integrityValue = 100;
private int $integritySeed = 0; // long, 0 means random
private int $integritySeed = 0; // 0 means random
private bool $isPowered = false; // TODO : set by client, should be server side
private int $mirror = 0; // byte
private StructureAxes $mirror;
private int $redstoneSaveMode = 0;
private bool $removeBlocks = false;
private int $rotation = 0; // byte
private StructureRotation $rotation = StructureRotation::_0;
private bool $showBoundingBox = true;
private string $structureName = "";
private Vector3 $structureOffset;
Expand All @@ -77,27 +80,33 @@ class StructureBlock extends Spawnable{
// Vector3 pivot

public function __construct(World $world, Vector3 $pos){
$this->mirror = new StructureAxes(false, false);
$this->structureOffset = new Vector3(0, -1, 0);
$this->structureSize = new Vector3(5, 5, 5);
parent::__construct($world, $pos);
}

// TODO : check values
public function readSaveData(CompoundTag $nbt) : void{
$this->animationMode = $nbt->getByte(self::TAG_ANIMATION_MODE, $this->animationMode);
$this->animationMode = StructureAnimationMode::tryFrom($nbt->getByte(self::TAG_ANIMATION_MODE, $this->animationMode->value))
?? throw new NbtDataException("Invalid StructureAnimationMode value");
$this->type = StructureBlockType::tryFrom($nbt->getInt(self::TAG_DATA, $this->type->value))
?? throw new NbtDataException("Invalid StructureBlockType value");
$this->rotation = StructureRotation::tryFrom($nbt->getByte(self::TAG_ROTATION, $this->rotation->value))
?? throw new NbtDataException("Invalid StructureRotation value");
$this->mirror = StructureAxes::fromInt($nbt->getByte(self::TAG_MIRROR, $this->mirror->toInt()));

$this->animationSeconds = $nbt->getFloat(self::TAG_ANIMATION_SECONDS, $this->animationSeconds);
$this->type = StructureBlockType::tryFrom($nbt->getByte(self::TAG_DATA, $this->type->value)) ?? throw new NbtDataException("Invalid StructureBlockType value");
$this->dataField = $nbt->getString(self::TAG_DATA_FIELD, $this->dataField);
$this->ignoreEntities = (bool) $nbt->getByte(self::TAG_IGNORE_ENTITIES, (int) $this->ignoreEntities);
$this->integrityValue = $nbt->getFloat(self::TAG_INTEGRITY, $this->integrityValue);
$this->isPowered = (bool) $nbt->getByte(self::TAG_IS_POWERED, (int) $this->isPowered);
$this->mirror = $nbt->getByte(self::TAG_MIRROR, $this->mirror);
$this->redstoneSaveMode = $nbt->getByte(self::TAG_REDSTONE_SAVE_MODE, $this->redstoneSaveMode);
$this->removeBlocks = (bool) $nbt->getByte(self::TAG_REMOVE_BLOCKS, (int) $this->removeBlocks);
$this->rotation = $nbt->getByte(self::TAG_ROTATION, $this->rotation);
$this->integritySeed = $nbt->getLong(self::TAG_SEED, $this->integritySeed);
$this->showBoundingBox = (bool) $nbt->getByte(self::TAG_SHOW_BOUNDING_BOX, (int) $this->showBoundingBox);
$this->structureName = $nbt->getString(self::TAG_STRUCTURE_NAME, $this->structureName);

$this->structureOffset = new Vector3(
$nbt->getInt(self::TAG_X_STRUCTURE_OFFSET, $this->structureOffset->getFloorX()),
$nbt->getInt(self::TAG_Y_STRUCTURE_OFFSET, $this->structureOffset->getFloorY()),
Expand All @@ -111,17 +120,17 @@ public function readSaveData(CompoundTag $nbt) : void{
}

protected function writeSaveData(CompoundTag $nbt) : void{
$nbt->setByte(self::TAG_ANIMATION_MODE, $this->animationMode);
$nbt->setByte(self::TAG_ANIMATION_MODE, $this->animationMode->value);
$nbt->setFloat(self::TAG_ANIMATION_SECONDS, $this->animationSeconds);
$nbt->setInt(self::TAG_DATA, $this->type->value);
$nbt->setString(self::TAG_DATA_FIELD, $this->dataField);
$nbt->setByte(self::TAG_IGNORE_ENTITIES, (int) $this->ignoreEntities);
$nbt->setFloat(self::TAG_INTEGRITY, $this->integrityValue);
$nbt->setByte(self::TAG_IS_POWERED, (int) $this->isPowered);
$nbt->setByte(self::TAG_MIRROR, $this->mirror);
$nbt->setByte(self::TAG_MIRROR, $this->mirror->toInt());
$nbt->setByte(self::TAG_REDSTONE_SAVE_MODE, $this->redstoneSaveMode);
$nbt->setByte(self::TAG_REMOVE_BLOCKS, (int) $this->removeBlocks);
$nbt->setByte(self::TAG_ROTATION, $this->rotation);
$nbt->setByte(self::TAG_ROTATION, $this->rotation->value);
$nbt->setLong(self::TAG_SEED, $this->integritySeed);
$nbt->setByte(self::TAG_SHOW_BOUNDING_BOX, (int) $this->showBoundingBox);
$nbt->setString(self::TAG_STRUCTURE_NAME, $this->structureName);
Expand Down Expand Up @@ -161,12 +170,205 @@ public function updateFromPacket(StructureBlockUpdatePacket $packet) : void{
$this->structureSize = new Vector3($settings->dimensions->getX(), $settings->dimensions->getY(), $settings->dimensions->getZ());
$this->structureOffset = new Vector3($settings->offset->getX(), $settings->offset->getY(), $settings->offset->getZ());
//lastTouchedByPlayerId
$this->rotation = $settings->rotation;
$this->mirror = $settings->mirror;
$this->animationMode = $settings->animationMode;
$this->rotation = StructureRotation::from($settings->rotation);
$this->mirror = StructureAxes::fromInt($settings->mirror);
$this->animationMode = StructureAnimationMode::from($settings->animationMode);
$this->animationSeconds = $settings->animationSeconds;
$this->integrityValue = $settings->integrityValue;
$this->integritySeed = $settings->integritySeed;
//pivot
}

public function getAnimationMode() : StructureAnimationMode{
return $this->animationMode;
}

/**
* @return $this
*/
public function setAnimationMode(StructureAnimationMode $mode) : self{
$this->animationMode = $mode;
return $this;
}

public function getAnimationSeconds() : float{
return $this->animationSeconds;
}

/**
* @return $this
*/
public function setAnimationSeconds(float $seconds) : self{
$this->animationSeconds = $seconds;
return $this;
}

public function getType() : StructureBlockType{
return $this->type;
}

/**
* @return $this
*/
public function setType(StructureBlockType $type) : self{
$this->type = $type;
return $this;
}

public function getDataField() : string{
return $this->dataField;
}

/**
* @return $this
*/
public function setDataField(string $dataField) : self{
$this->dataField = $dataField;
return $this;
}

public function getIgnoreEntities() : bool{
return $this->ignoreEntities;
}

/**
* @return $this
*/
public function setIgnoreEntities(bool $ignore) : self{
$this->ignoreEntities = $ignore;
return $this;
}

public function getIntegrity() : float{
return $this->integrityValue;
}

/**
* @return $this
*/
public function setIntegrity(float $value) : self{
$this->integrityValue = $value;
return $this;
}

public function getIntegritySeed() : int{
return $this->integritySeed;
}

/**
* @return $this
*/
public function setIntegritySeed(int $seed) : self{
$this->integritySeed = $seed;
return $this;
}

public function getIsPowered() : bool{
return $this->isPowered;
}

/**
* @return $this
*/
public function setIsPowered(bool $power) : self{
$this->isPowered = $power;
return $this;
}

public function getMirroredAxes() : StructureAxes{
return $this->mirror;
}

/**
* @return $this
*/
public function setMirroredAxes(StructureAxes $axes) : self{
$this->mirror = $axes;
return $this;
}

// TODO : enum / bool ?
public function getRedstoneSaveMode() : int{
return $this->redstoneSaveMode;
}

/**
* @return $this
*/
public function setRedstoneSaveMode(int $saveMode) : self{
$this->redstoneSaveMode = $saveMode;
return $this;
}

public function getRemoveBlocks() : bool{
return $this->removeBlocks;
}

/**
* @return $this
*/
public function setRemoveBlocks(bool $remove) : self{
$this->removeBlocks = $remove;
return $this;
}

public function getRotation() : StructureRotation{
return $this->rotation;
}

/**
* @return $this
*/
public function setRotation(StructureRotation $rotation) : self{
$this->rotation = $rotation;
return $this;
}

public function getShowBoundingBox() : bool{
return $this->showBoundingBox;
}

/**
* @return $this
*/
public function setShowBoundingBox(bool $show) : self{
$this->showBoundingBox = $show;
return $this;
}

public function getStructureName() : string{
return $this->structureName;
}

/**
* @return $this
*/
public function setStructureName(string $name) : self{
$this->structureName = $name;
return $this;
}

public function getStructureOffset() : Vector3{
return $this->structureOffset;
}

/**
* @return $this
*/
public function setStructureOffset(Vector3 $offset) : self{
$this->structureOffset = $offset;
return $this;
}

public function getStructureSize() : Vector3{
return $this->structureSize;
}

/**
* @return $this
*/
public function setStructureSize(Vector3 $size) : self{
$this->structureSize = $size;
return $this;
}
}
30 changes: 30 additions & 0 deletions src/block/utils/StructureAnimationMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\block\utils;

enum StructureAnimationMode : int{
case NONE = 0;
case BY_LAYER = 1;
case BY_BLOCK = 2;
};
41 changes: 41 additions & 0 deletions src/block/utils/StructureAxes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\block\utils;

class StructureAxes{
public function __construct(
public bool $xMirror,
public bool $yMirror
){}

public static function fromInt(int $mirror) : StructureAxes{
$xMirror = ($mirror >> 0) & 1;
$yMirror = ($mirror >> 1) & 1;
return new StructureAxes((bool) $xMirror, (bool) $yMirror);
}

public function toInt() : int{
return $this->xMirror | ($this->yMirror << 1);
}
}
Loading

0 comments on commit 95d7218

Please sign in to comment.