Added getMushroomBlockType() / setMushroomBlockType() APIs to Red/BrownMushroomBlock

This commit is contained in:
Dylan K. Taylor 2021-07-22 19:13:26 +01:00
parent 41d9bf8a2e
commit 83016a97bd
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 160 additions and 10 deletions

View File

@ -23,32 +23,45 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\InvalidBlockStateException;
use pocketmine\block\utils\MushroomBlockType;
use pocketmine\data\bedrock\MushroomBlockTypeIdMap;
use pocketmine\item\Item;
use function mt_rand;
class RedMushroomBlock extends Opaque{
/**
* @var int
* In PC they have blockstate properties for each of the sides (pores/not pores). Unfortunately, we can't support
* that because we can't serialize 2^6 combinations into a 4-bit metadata value, so this has to stick with storing
* the legacy crap for now.
* TODO: change this once proper blockstates are implemented
*/
protected int $rotationData = 0;
protected MushroomBlockType $mushroomBlockType;
public function __construct(BlockIdentifier $idInfo, string $name, BlockBreakInfo $breakInfo){
$this->mushroomBlockType = MushroomBlockType::PORES();
parent::__construct($idInfo, $name, $breakInfo);
}
protected function writeStateToMeta() : int{
return $this->rotationData;
return MushroomBlockTypeIdMap::getInstance()->toId($this->mushroomBlockType);
}
public function readStateFromData(int $id, int $stateMeta) : void{
$this->rotationData = $stateMeta;
$type = MushroomBlockTypeIdMap::getInstance()->fromId($stateMeta);
if($type === null){
throw new InvalidBlockStateException("No such mushroom variant $stateMeta");
}
$this->mushroomBlockType = $type;
}
public function getStateBitmask() : int{
return 0b1111;
}
public function getMushroomBlockType() : MushroomBlockType{ return $this->mushroomBlockType; }
/** @return $this */
public function setMushroomBlockType(MushroomBlockType $mushroomBlockType) : self{
$this->mushroomBlockType = $mushroomBlockType;
return $this;
}
public function getDropsForCompatibleTool(Item $item) : array{
return [
VanillaBlocks::RED_MUSHROOM()->asItem()->setCount(mt_rand(0, 2))

View File

@ -0,0 +1,63 @@
<?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;
use pocketmine\utils\EnumTrait;
/**
* This doc-block is generated automatically, do not modify it manually.
* This must be regenerated whenever registry members are added, removed or changed.
* @see \pocketmine\utils\RegistryUtils::_generateMethodAnnotations()
*
* @method static MushroomBlockType ALL_CAP()
* @method static MushroomBlockType CAP_EAST()
* @method static MushroomBlockType CAP_MIDDLE()
* @method static MushroomBlockType CAP_NORTH()
* @method static MushroomBlockType CAP_NORTHEAST()
* @method static MushroomBlockType CAP_NORTHWEST()
* @method static MushroomBlockType CAP_SOUTH()
* @method static MushroomBlockType CAP_SOUTHEAST()
* @method static MushroomBlockType CAP_SOUTHWEST()
* @method static MushroomBlockType CAP_WEST()
* @method static MushroomBlockType PORES()
*/
final class MushroomBlockType{
use EnumTrait;
protected static function setup() : void{
self::registerAll(
new self("PORES"),
new self("CAP_NORTHWEST"),
new self("CAP_NORTH"),
new self("CAP_NORTHEAST"),
new self("CAP_WEST"),
new self("CAP_MIDDLE"),
new self("CAP_EAST"),
new self("CAP_SOUTHWEST"),
new self("CAP_SOUTH"),
new self("CAP_SOUTHEAST"),
new self("ALL_CAP")
);
}
}

View File

@ -0,0 +1,74 @@
<?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\data\bedrock;
use pocketmine\block\BlockLegacyMetadata as LegacyMeta;
use pocketmine\block\utils\MushroomBlockType;
use pocketmine\utils\SingletonTrait;
use function array_key_exists;
final class MushroomBlockTypeIdMap{
use SingletonTrait;
/**
* @var MushroomBlockType[]
* @phpstan-var array<int, MushroomBlockType>
*/
private array $idToEnum = [];
/**
* @var int[]
* @phpstan-var array<int, int>
*/
private array $enumToId = [];
public function __construct(){
$this->register(LegacyMeta::MUSHROOM_BLOCK_ALL_PORES, MushroomBlockType::PORES());
$this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_NORTHWEST_CORNER, MushroomBlockType::CAP_NORTHWEST());
$this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_NORTH_SIDE, MushroomBlockType::CAP_NORTH());
$this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_NORTHEAST_CORNER, MushroomBlockType::CAP_NORTHEAST());
$this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_WEST_SIDE, MushroomBlockType::CAP_WEST());
$this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_TOP_ONLY, MushroomBlockType::CAP_MIDDLE());
$this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_EAST_SIDE, MushroomBlockType::CAP_EAST());
$this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_SOUTHWEST_CORNER, MushroomBlockType::CAP_SOUTHWEST());
$this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_SOUTH_SIDE, MushroomBlockType::CAP_SOUTH());
$this->register(LegacyMeta::MUSHROOM_BLOCK_CAP_SOUTHEAST_CORNER, MushroomBlockType::CAP_SOUTHEAST());
$this->register(LegacyMeta::MUSHROOM_BLOCK_ALL_CAP, MushroomBlockType::ALL_CAP());
}
public function register(int $id, MushroomBlockType $type) : void{
$this->idToEnum[$id] = $type;
$this->enumToId[$type->id()] = $id;
}
public function fromId(int $id) : ?MushroomBlockType{
return $this->idToEnum[$id] ?? null;
}
public function toId(MushroomBlockType $type) : int{
if(!array_key_exists($type->id(), $this->enumToId)){
throw new \InvalidArgumentException("Mushroom block type does not have a mapped ID"); //this should never happen
}
return $this->enumToId[$type->id()];
}
}