Block: Replace Color and WoodType magic numbers with type-safe objects

this provides automatic type safety without the need for magic number value checking everywhere.
This commit is contained in:
Dylan K. Taylor
2019-02-12 13:52:59 +00:00
parent 18440f612f
commit 7b3993730a
25 changed files with 422 additions and 250 deletions

View File

@ -24,7 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataValidator;
use pocketmine\block\utils\Color;
use pocketmine\block\utils\DyeColor;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\lang\TranslationContainer;
@ -52,11 +52,11 @@ class Bed extends Transparent{
protected $occupied = false;
/** @var bool */
protected $head = false;
/** @var int */
protected $color = Color::RED;
/** @var DyeColor */
protected $color;
public function __construct(){
$this->color = DyeColor::$RED;
}
protected function writeStateToMeta() : int{
@ -183,7 +183,7 @@ class Bed extends Transparent{
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{
$this->color = $item->getDamage(); //TODO: replace this with a proper colour getter
$this->color = DyeColor::fromMagicNumber($item->getDamage()); //TODO: replace this with a proper colour getter
$down = $this->getSide(Facing::DOWN);
if(!$down->isTransparent()){
$this->facing = $player !== null ? $player->getHorizontalFacing() : Facing::NORTH;
@ -211,7 +211,7 @@ class Bed extends Transparent{
}
public function getItem() : Item{
return ItemFactory::get($this->getItemId(), $this->color);
return ItemFactory::get($this->getItemId(), $this->color->getMagicNumber());
}
public function isAffectedBySilkTouch() : bool{

View File

@ -23,10 +23,10 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\Color;
use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\InvalidBlockStateException;
use pocketmine\block\utils\PillarRotationTrait;
use pocketmine\block\utils\WoodType;
use pocketmine\block\utils\TreeType;
use pocketmine\item\Item;
use pocketmine\level\Position;
use function array_fill;
@ -93,17 +93,17 @@ class BlockFactory{
self::registerBlock(new Cobblestone());
foreach(WoodType::ALL as $type){
self::registerBlock(new Planks(Block::PLANKS, $type, WoodType::NAMES[$type] . " Planks"));
self::registerBlock(new Sapling(Block::SAPLING, $type, WoodType::NAMES[$type] . " Sapling"));
self::registerBlock(new WoodenFence(Block::FENCE, $type, WoodType::NAMES[$type] . " Fence"));
}
foreach(TreeType::getAll() as $treeType){
$magicNumber = $treeType->getMagicNumber();
$name = $treeType->getDisplayName();
self::registerBlock(new Planks(Block::PLANKS, $magicNumber, $name . " Planks"));
self::registerBlock(new Sapling(Block::SAPLING, $magicNumber, $treeType, $name . " Sapling"));
self::registerBlock(new WoodenFence(Block::FENCE, $magicNumber, $name . " Fence"));
foreach(WoodType::ALL as $type){
//TODO: find a better way to deal with this split
self::registerBlock(new Log($type >= 4 ? Block::WOOD2 : Block::WOOD, $type & 0x03, WoodType::NAMES[$type] . " Log"));
self::registerBlock(new Wood($type >= 4 ? Block::WOOD2 : Block::WOOD, ($type & 0x03) | 0b1100, WoodType::NAMES[$type] . " Wood"));
self::registerBlock(new Leaves($type >= 4 ? Block::LEAVES2 : Block::LEAVES, $type & 0x03, $type, WoodType::NAMES[$type] . " Leaves"));
self::registerBlock(new Log($magicNumber >= 4 ? Block::WOOD2 : Block::WOOD, $magicNumber & 0x03, $treeType, $name . " Log"));
self::registerBlock(new Wood($magicNumber >= 4 ? Block::WOOD2 : Block::WOOD, ($magicNumber & 0x03) | 0b1100, $treeType, $name . " Wood"));
self::registerBlock(new Leaves($magicNumber >= 4 ? Block::LEAVES2 : Block::LEAVES, $magicNumber & 0x03, $treeType, $name . " Leaves"));
}
self::registerBlock(new Bedrock());
@ -151,14 +151,14 @@ class BlockFactory{
//TODO: PISTON
//TODO: PISTONARMCOLLISION
foreach(Color::ALL as $color){
self::registerBlock(new Wool(Block::WOOL, $color, Color::NAMES[$color] . " Wool"));
self::registerBlock(new HardenedClay(Block::STAINED_CLAY, $color, Color::NAMES[$color] . " Stained Clay"));
self::registerBlock(new Glass(Block::STAINED_GLASS, $color, Color::NAMES[$color] . " Stained Glass"));
self::registerBlock(new GlassPane(Block::STAINED_GLASS_PANE, $color, Color::NAMES[$color] . " Stained Glass Pane"));
self::registerBlock(new Carpet(Block::CARPET, $color, Color::NAMES[$color] . " Carpet"));
self::registerBlock(new Concrete(Block::CONCRETE, $color, Color::NAMES[$color] . " Concrete"));
self::registerBlock(new ConcretePowder(Block::CONCRETE_POWDER, $color, Color::NAMES[$color] . " Concrete Powder"));
foreach(DyeColor::getAll() as $color){
self::registerBlock(new Wool(Block::WOOL, $color->getMagicNumber(), $color->getDisplayName() . " Wool"));
self::registerBlock(new HardenedClay(Block::STAINED_CLAY, $color->getMagicNumber(), $color->getDisplayName() . " Stained Clay"));
self::registerBlock(new Glass(Block::STAINED_GLASS, $color->getMagicNumber(), $color->getDisplayName() . " Stained Glass"));
self::registerBlock(new GlassPane(Block::STAINED_GLASS_PANE, $color->getMagicNumber(), $color->getDisplayName() . " Stained Glass Pane"));
self::registerBlock(new Carpet(Block::CARPET, $color->getMagicNumber(), $color->getDisplayName() . " Carpet"));
self::registerBlock(new Concrete(Block::CONCRETE, $color->getMagicNumber(), $color->getDisplayName() . " Concrete"));
self::registerBlock(new ConcretePowder(Block::CONCRETE_POWDER, $color->getMagicNumber(), $color->getDisplayName() . " Concrete Powder"));
}
self::registerBlock(new Dandelion());
@ -197,8 +197,8 @@ class BlockFactory{
new StoneSlab(Block::STONE_SLAB2, Block::DOUBLE_STONE_SLAB2, 6, "Smooth Sandstone"),
new StoneSlab(Block::STONE_SLAB2, Block::DOUBLE_STONE_SLAB2, 7, "Red Nether Brick")
];
foreach(WoodType::ALL as $woodType){
$slabTypes[] = new WoodenSlab($woodType);
foreach(TreeType::getAll() as $woodType){
$slabTypes[] = new WoodenSlab(Block::WOODEN_SLAB, Block::DOUBLE_WOODEN_SLAB, $woodType->getMagicNumber(), $woodType->getDisplayName());
}
foreach($slabTypes as $type){
self::registerBlock($type);

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataValidator;
use pocketmine\block\utils\TreeType;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\math\AxisAlignedBB;
@ -85,7 +86,7 @@ class CocoaBlock extends Transparent{
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{
if(Facing::axis($face) !== Facing::AXIS_Y and $blockClicked->getId() === Block::LOG and $blockClicked->getVariant() === Wood::JUNGLE){
if(Facing::axis($face) !== Facing::AXIS_Y and $blockClicked instanceof Wood and $blockClicked->getTreeType() === TreeType::$JUNGLE){
$this->facing = $face;
return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}
@ -103,7 +104,7 @@ class CocoaBlock extends Transparent{
public function onNearbyBlockChange() : void{
$side = $this->getSide(Facing::opposite($this->facing));
if($side->getId() !== Block::LOG or $side->getVariant() !== Wood::JUNGLE){
if(!($side instanceof Wood) or $side->getTreeType() !== TreeType::$JUNGLE){
$this->level->useBreakOn($this);
}
}

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\WoodType;
use pocketmine\block\utils\TreeType;
use pocketmine\event\block\LeavesDecayEvent;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
@ -34,17 +34,17 @@ use pocketmine\Player;
use function mt_rand;
class Leaves extends Transparent{
/** @var int */
protected $woodType;
/** @var TreeType */
protected $treeType;
/** @var bool */
protected $noDecay = false;
/** @var bool */
protected $checkDecay = false;
public function __construct(int $id, int $variant, int $woodType, ?string $name = null){
public function __construct(int $id, int $variant, TreeType $treeType, ?string $name = null){
parent::__construct($id, $variant, $name);
$this->woodType = $woodType;
$this->treeType = $treeType;
}
protected function writeStateToMeta() : int{
@ -132,9 +132,9 @@ class Leaves extends Transparent{
$drops = [];
if(mt_rand(1, 20) === 1){ //Saplings
$drops[] = ItemFactory::get(Item::SAPLING, $this->woodType);
$drops[] = ItemFactory::get(Item::SAPLING, $this->treeType->getMagicNumber());
}
if(($this->woodType === WoodType::OAK or $this->woodType === WoodType::DARK_OAK) and mt_rand(1, 200) === 1){ //Apples
if(($this->treeType === TreeType::$OAK or $this->treeType === TreeType::$DARK_OAK) and mt_rand(1, 200) === 1){ //Apples
$drops[] = ItemFactory::get(Item::APPLE);
}

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\TreeType;
use pocketmine\item\Item;
use pocketmine\level\generator\object\Tree;
use pocketmine\math\Facing;
@ -35,6 +36,13 @@ class Sapling extends Flowable{
/** @var bool */
protected $ready = false;
/** @var TreeType */
private $treeType;
public function __construct(int $id, int $variant, TreeType $treeType, ?string $name = null, int $itemId = null){
parent::__construct($id, $variant, $name, $itemId);
$this->treeType = $treeType;
}
protected function writeStateToMeta() : int{
return ($this->ready ? 0x08 : 0);
@ -60,7 +68,7 @@ class Sapling extends Flowable{
public function onActivate(Item $item, Player $player = null) : bool{
if($item->getId() === Item::DYE and $item->getDamage() === 0x0F){ //Bonemeal
//TODO: change log type
Tree::growTree($this->getLevel(), $this->x, $this->y, $this->z, new Random(mt_rand()), $this->getVariant());
Tree::growTree($this->getLevel(), $this->x, $this->y, $this->z, new Random(mt_rand()), $this->treeType);
$item->pop();
@ -83,7 +91,7 @@ class Sapling extends Flowable{
public function onRandomTick() : void{
if($this->level->getFullLightAt($this->x, $this->y, $this->z) >= 8 and mt_rand(1, 7) === 1){
if($this->ready){
Tree::growTree($this->getLevel(), $this->x, $this->y, $this->z, new Random(mt_rand()), $this->getVariant());
Tree::growTree($this->getLevel(), $this->x, $this->y, $this->z, new Random(mt_rand()), $this->treeType);
}else{
$this->ready = true;
$this->getLevel()->setBlock($this, $this);

View File

@ -23,11 +23,25 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\TreeType;
class Wood extends Solid{
public const OAK = 0;
public const SPRUCE = 1;
public const BIRCH = 2;
public const JUNGLE = 3;
/** @var TreeType */
private $treeType;
public function __construct(int $id, int $variant, TreeType $treeType, ?string $name = null, int $itemId = null){
parent::__construct($id, $variant, $name, $itemId);
$this->treeType = $treeType;
}
/**
* TODO: this is ad hoc, but add an interface for this to all tree-related blocks
* @return TreeType
*/
public function getTreeType() : TreeType{
return $this->treeType;
}
public function getHardness() : float{
return 2;

View File

@ -23,16 +23,10 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\WoodType;
class WoodenSlab extends Slab{
protected $id = self::WOODEN_SLAB;
public function __construct(int $variant = 0){
parent::__construct(self::WOODEN_SLAB, self::DOUBLE_WOODEN_SLAB, $variant, WoodType::NAMES[$variant]);
}
public function getHardness() : float{
return 2;
}

View File

@ -1,81 +0,0 @@
<?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 Color{
public const WHITE = 0;
public const ORANGE = 1;
public const MAGENTA = 2;
public const LIGHT_BLUE = 3;
public const YELLOW = 4;
public const LIME = 5;
public const PINK = 6;
public const GRAY = 7;
public const LIGHT_GRAY = 8;
public const CYAN = 9;
public const PURPLE = 10;
public const BLUE = 11;
public const BROWN = 12;
public const GREEN = 13;
public const RED = 14;
public const BLACK = 15;
public const ALL = [
self::WHITE,
self::ORANGE,
self::MAGENTA,
self::LIGHT_BLUE,
self::YELLOW,
self::LIME,
self::PINK,
self::GRAY,
self::LIGHT_GRAY,
self::CYAN,
self::PURPLE,
self::BLUE,
self::BROWN,
self::GREEN,
self::RED,
self::BLACK
];
public const NAMES = [
self::WHITE => "White",
self::ORANGE => "Orange",
self::MAGENTA => "Magenta",
self::LIGHT_BLUE => "Light Blue",
self::YELLOW => "Yellow",
self::LIME => "Lime",
self::PINK => "Pink",
self::GRAY => "Gray",
self::LIGHT_GRAY => "Light Gray",
self::CYAN => "Cyan",
self::PURPLE => "Purple",
self::BLUE => "Blue",
self::BROWN => "Brown",
self::GREEN => "Green",
self::RED => "Red",
self::BLACK => "Black"
];
}

View File

@ -0,0 +1,151 @@
<?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;
final class DyeColor{
/** @var DyeColor */
public static $WHITE;
/** @var DyeColor */
public static $ORANGE;
/** @var DyeColor */
public static $MAGENTA;
/** @var DyeColor */
public static $LIGHT_BLUE;
/** @var DyeColor */
public static $YELLOW;
/** @var DyeColor */
public static $LIME;
/** @var DyeColor */
public static $PINK;
/** @var DyeColor */
public static $GRAY;
/** @var DyeColor */
public static $LIGHT_GRAY;
/** @var DyeColor */
public static $CYAN;
/** @var DyeColor */
public static $PURPLE;
/** @var DyeColor */
public static $BLUE;
/** @var DyeColor */
public static $BROWN;
/** @var DyeColor */
public static $GREEN;
/** @var DyeColor */
public static $RED;
/** @var DyeColor */
public static $BLACK;
/** @var DyeColor[] */
private static $numericIdMap = [];
/** @var DyeColor[] separate mapping that doesn't depend on magic numbers */
private static $all = [];
/**
* @internal
*/
public static function _init() : void{
self::register(self::$WHITE = new DyeColor("White", 0));
self::register(self::$ORANGE = new DyeColor("Orange", 1));
self::register(self::$MAGENTA = new DyeColor("Magenta", 2));
self::register(self::$LIGHT_BLUE = new DyeColor("Light Blue", 3));
self::register(self::$YELLOW = new DyeColor("Yellow", 4));
self::register(self::$LIME = new DyeColor("Lime", 5));
self::register(self::$PINK = new DyeColor("Pink", 6));
self::register(self::$GRAY = new DyeColor("Gray", 7));
self::register(self::$LIGHT_GRAY = new DyeColor("Light Gray", 8));
self::register(self::$CYAN = new DyeColor("Cyan", 9));
self::register(self::$PURPLE = new DyeColor("Purple", 10));
self::register(self::$BLUE = new DyeColor("Blue", 11));
self::register(self::$BROWN = new DyeColor("Brown", 12));
self::register(self::$GREEN = new DyeColor("Green", 13));
self::register(self::$RED = new DyeColor("Red", 14));
self::register(self::$BLACK = new DyeColor("Black", 15));
}
private static function register(DyeColor $color) : void{
self::$numericIdMap[$color->getMagicNumber()] = $color;
self::$all[] = $color;
}
/**
* Returns a set of all known dye colours.
*
* @return DyeColor[]
*/
public static function getAll() : array{
return self::$all;
}
/**
* Returns a DyeColor object matching the given magic number
* @internal
*
* @param int $magicNumber
* @param bool $inverted Invert the ID before using it (useful for actual dye magic IDs)
*
* @return DyeColor
* @throws \InvalidArgumentException
*/
public static function fromMagicNumber(int $magicNumber, bool $inverted = false) : DyeColor{
$real = $inverted ? ~$magicNumber & 0xf : $magicNumber;
if(!isset(self::$numericIdMap[$real])){
throw new \InvalidArgumentException("Unknown dye colour magic number $magicNumber");
}
return self::$numericIdMap[$real];
}
/** @var string */
private $displayName;
/** @var int */
private $magicNumber;
private function __construct(string $displayName, int $magicNumber){
$this->displayName = $displayName;
$this->magicNumber = $magicNumber;
}
/**
* @return string
*/
public function getDisplayName() : string{
return $this->displayName;
}
/**
* @return int
*/
public function getMagicNumber() : int{
return $this->magicNumber;
}
/**
* @return int
*/
public function getInvertedMagicNumber() : int{
return ~$this->magicNumber & 0xf;
}
}
DyeColor::_init();

View File

@ -0,0 +1,112 @@
<?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;
final class TreeType{
/** @var TreeType */
public static $OAK;
/** @var TreeType */
public static $SPRUCE;
/** @var TreeType */
public static $BIRCH;
/** @var TreeType */
public static $JUNGLE;
/** @var TreeType */
public static $ACACIA;
/** @var TreeType */
public static $DARK_OAK;
/** @var TreeType[] */
private static $numericIdMap = [];
/** @var TreeType[] */
private static $all = [];
/**
* @internal
*/
public static function _init() : void{
self::register(self::$OAK = new TreeType("Oak", 0));
self::register(self::$SPRUCE = new TreeType("Spruce", 1));
self::register(self::$BIRCH = new TreeType("Birch", 2));
self::register(self::$JUNGLE = new TreeType("Jungle", 3));
self::register(self::$ACACIA = new TreeType("Acacia", 4));
self::register(self::$DARK_OAK = new TreeType("Dark Oak", 5));
}
private static function register(TreeType $type) : void{
self::$numericIdMap[$type->getMagicNumber()] = $type;
self::$all[] = $type;
}
/**
* @return TreeType[]
*/
public static function getAll() : array{
return self::$all;
}
/**
* @internal
* @param int $magicNumber
*
* @return TreeType
* @throws \InvalidArgumentException
*/
public static function fromMagicNumber(int $magicNumber) : TreeType{
if(!isset(self::$numericIdMap[$magicNumber])){
throw new \InvalidArgumentException("Unknown tree type magic number $magicNumber");
}
return self::$numericIdMap[$magicNumber];
}
/** @var string */
private $displayName;
/** @var int */
private $magicNumber;
/**
* @param string $displayName
* @param int $magicNumber
*/
private function __construct(string $displayName, int $magicNumber){
$this->displayName = $displayName;
$this->magicNumber = $magicNumber;
}
/**
* @return string
*/
public function getDisplayName() : string{
return $this->displayName;
}
/**
* @return int
*/
public function getMagicNumber() : int{
return $this->magicNumber;
}
}
TreeType::_init();

View File

@ -1,51 +0,0 @@
<?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 WoodType{
public const OAK = 0;
public const SPRUCE = 1;
public const BIRCH = 2;
public const JUNGLE = 3;
public const ACACIA = 4;
public const DARK_OAK = 5;
public const ALL = [
self::OAK,
self::SPRUCE,
self::BIRCH,
self::JUNGLE,
self::ACACIA,
self::DARK_OAK
];
public const NAMES = [
self::OAK => "Oak",
self::SPRUCE => "Spruce",
self::BIRCH => "Birch",
self::JUNGLE => "Jungle",
self::ACACIA => "Acacia",
self::DARK_OAK => "Dark Oak"
];
}