Nuke Block->meta, split into variant and state properties, lots of cleanup

This is a major change to the way block metadata is handled within the PM core. This separates variant metadata (which really ought to be part of the ID) from state metadata, and in a couple of cases flattens separate states of blocks together.

The result of this is that invalid variants can be much more easily detected, and additionally state handling is much cleaner since meta is only needed at the serialize layer instead of throughout the code.
This commit is contained in:
Dylan K. Taylor
2018-09-21 19:28:10 +01:00
parent a55ab54ddb
commit 56d9943b0d
178 changed files with 2012 additions and 2120 deletions

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\WoodType;
use pocketmine\event\block\LeavesDecayEvent;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
@ -32,17 +33,30 @@ use pocketmine\math\Vector3;
use pocketmine\Player;
class Leaves extends Transparent{
public const OAK = 0;
public const SPRUCE = 1;
public const BIRCH = 2;
public const JUNGLE = 3;
public const ACACIA = 0;
public const DARK_OAK = 1;
/** @var int */
protected $woodType;
protected $id = self::LEAVES;
/** @var bool */
protected $noDecay = false;
/** @var bool */
protected $checkDecay = false;
public function __construct(int $meta = 0){
$this->setDamage($meta);
public function __construct(int $id, int $variant, int $woodType, ?string $name = null){
parent::__construct($id, $variant, $name);
$this->woodType = $woodType;
}
protected function writeStateToMeta() : int{
return ($this->noDecay ? 0x04 : 0) | ($this->checkDecay ? 0x08 : 0);
}
public function readStateFromMeta(int $meta) : void{
$this->noDecay = ($meta & 0x04) !== 0;
$this->checkDecay = ($meta & 0x08) !== 0;
}
public function getStateBitmask() : int{
return 0b1100;
}
public function getHardness() : float{
@ -53,16 +67,6 @@ class Leaves extends Transparent{
return BlockToolType::TYPE_SHEARS;
}
public function getName() : string{
static $names = [
self::OAK => "Oak Leaves",
self::SPRUCE => "Spruce Leaves",
self::BIRCH => "Birch Leaves",
self::JUNGLE => "Jungle Leaves"
];
return $names[$this->getVariant()];
}
public function diffusesSkyLight() : bool{
return true;
}
@ -80,7 +84,7 @@ class Leaves extends Transparent{
return true;
}
if($pos->getId() === $this->id and $distance <= 4){
if($pos->getId() === $this->getId() and $distance <= 4){
foreach(Facing::ALL as $side){
if($this->findLog($pos->getSide($side), $visited, $distance + 1)){
return true;
@ -92,8 +96,8 @@ class Leaves extends Transparent{
}
public function onNearbyBlockChange() : void{
if(($this->meta & 0b00001100) === 0){
$this->meta |= 0x08;
if(!$this->noDecay and !$this->checkDecay){
$this->checkDecay = true;
$this->getLevel()->setBlock($this, $this, true, false);
}
}
@ -103,9 +107,7 @@ class Leaves extends Transparent{
}
public function onRandomTick() : void{
if(($this->meta & 0b00001100) === 0x08){
$this->meta &= 0x03;
if(!$this->noDecay and $this->checkDecay){
$this->getLevel()->getServer()->getPluginManager()->callEvent($ev = new LeavesDecayEvent($this));
if($ev->isCancelled() or $this->findLog($this)){
@ -117,14 +119,10 @@ class Leaves extends Transparent{
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{
$this->meta |= 0x04;
$this->noDecay = true; //artificial leaves don't decay
return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}
public function getVariantBitmask() : int{
return 0x03;
}
public function getDrops(Item $item) : array{
if($item->getBlockToolType() & BlockToolType::TYPE_SHEARS){
return $this->getDropsForCompatibleTool($item);
@ -132,23 +130,15 @@ class Leaves extends Transparent{
$drops = [];
if(mt_rand(1, 20) === 1){ //Saplings
$drops[] = $this->getSaplingItem();
$drops[] = ItemFactory::get(Item::SAPLING, $this->woodType);
}
if($this->canDropApples() and mt_rand(1, 200) === 1){ //Apples
if(($this->woodType === WoodType::OAK or $this->woodType === WoodType::DARK_OAK) and mt_rand(1, 200) === 1){ //Apples
$drops[] = ItemFactory::get(Item::APPLE);
}
return $drops;
}
public function getSaplingItem() : Item{
return ItemFactory::get(Item::SAPLING, $this->getVariant());
}
public function canDropApples() : bool{
return $this->meta === self::OAK;
}
public function getFlameEncouragement() : int{
return 30;
}