Generate methods for surrogate enums, nip stupidity in the bud

this also allows changing the internal implementation later without breaking plugins.
This commit is contained in:
Dylan K. Taylor 2019-02-20 11:09:22 +00:00
parent 88c4b836f0
commit 36e9db4c07
17 changed files with 135 additions and 43 deletions

View File

@ -55,7 +55,7 @@ class Bed extends Transparent{
protected $color; protected $color;
public function __construct(){ public function __construct(){
$this->color = DyeColor::$RED; $this->color = DyeColor::RED();
} }
protected function writeStateToMeta() : int{ protected function writeStateToMeta() : int{

View File

@ -86,7 +86,7 @@ class CocoaBlock extends Transparent{
} }
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{ 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 instanceof Wood and $blockClicked->getTreeType() === TreeType::$JUNGLE){ if(Facing::axis($face) !== Facing::AXIS_Y and $blockClicked instanceof Wood and $blockClicked->getTreeType() === TreeType::JUNGLE()){
$this->facing = $face; $this->facing = $face;
return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player); return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player);
} }
@ -109,7 +109,7 @@ class CocoaBlock extends Transparent{
public function onNearbyBlockChange() : void{ public function onNearbyBlockChange() : void{
$side = $this->getSide(Facing::opposite($this->facing)); $side = $this->getSide(Facing::opposite($this->facing));
if(!($side instanceof Wood) or $side->getTreeType() !== TreeType::$JUNGLE){ if(!($side instanceof Wood) or $side->getTreeType() !== TreeType::JUNGLE()){
$this->level->useBreakOn($this); $this->level->useBreakOn($this);
} }
} }

View File

@ -134,7 +134,7 @@ class Leaves extends Transparent{
if(mt_rand(1, 20) === 1){ //Saplings if(mt_rand(1, 20) === 1){ //Saplings
$drops[] = ItemFactory::get(Item::SAPLING, $this->treeType->getMagicNumber()); $drops[] = ItemFactory::get(Item::SAPLING, $this->treeType->getMagicNumber());
} }
if(($this->treeType === TreeType::$OAK or $this->treeType === TreeType::$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); $drops[] = ItemFactory::get(Item::APPLE);
} }

View File

@ -26,37 +26,103 @@ namespace pocketmine\block\utils;
final class DyeColor{ final class DyeColor{
/** @var DyeColor */ /** @var DyeColor */
public static $WHITE; private static $WHITE;
/** @var DyeColor */ /** @var DyeColor */
public static $ORANGE; private static $ORANGE;
/** @var DyeColor */ /** @var DyeColor */
public static $MAGENTA; private static $MAGENTA;
/** @var DyeColor */ /** @var DyeColor */
public static $LIGHT_BLUE; private static $LIGHT_BLUE;
/** @var DyeColor */ /** @var DyeColor */
public static $YELLOW; private static $YELLOW;
/** @var DyeColor */ /** @var DyeColor */
public static $LIME; private static $LIME;
/** @var DyeColor */ /** @var DyeColor */
public static $PINK; private static $PINK;
/** @var DyeColor */ /** @var DyeColor */
public static $GRAY; private static $GRAY;
/** @var DyeColor */ /** @var DyeColor */
public static $LIGHT_GRAY; private static $LIGHT_GRAY;
/** @var DyeColor */ /** @var DyeColor */
public static $CYAN; private static $CYAN;
/** @var DyeColor */ /** @var DyeColor */
public static $PURPLE; private static $PURPLE;
/** @var DyeColor */ /** @var DyeColor */
public static $BLUE; private static $BLUE;
/** @var DyeColor */ /** @var DyeColor */
public static $BROWN; private static $BROWN;
/** @var DyeColor */ /** @var DyeColor */
public static $GREEN; private static $GREEN;
/** @var DyeColor */ /** @var DyeColor */
public static $RED; private static $RED;
/** @var DyeColor */ /** @var DyeColor */
public static $BLACK; private static $BLACK;
/* auto-generated code */
public static function WHITE() : DyeColor{
return self::$WHITE;
}
public static function ORANGE() : DyeColor{
return self::$ORANGE;
}
public static function MAGENTA() : DyeColor{
return self::$MAGENTA;
}
public static function LIGHT_BLUE() : DyeColor{
return self::$LIGHT_BLUE;
}
public static function YELLOW() : DyeColor{
return self::$YELLOW;
}
public static function LIME() : DyeColor{
return self::$LIME;
}
public static function PINK() : DyeColor{
return self::$PINK;
}
public static function GRAY() : DyeColor{
return self::$GRAY;
}
public static function LIGHT_GRAY() : DyeColor{
return self::$LIGHT_GRAY;
}
public static function CYAN() : DyeColor{
return self::$CYAN;
}
public static function PURPLE() : DyeColor{
return self::$PURPLE;
}
public static function BLUE() : DyeColor{
return self::$BLUE;
}
public static function BROWN() : DyeColor{
return self::$BROWN;
}
public static function GREEN() : DyeColor{
return self::$GREEN;
}
public static function RED() : DyeColor{
return self::$RED;
}
public static function BLACK() : DyeColor{
return self::$BLACK;
}
/** @var DyeColor[] */ /** @var DyeColor[] */
private static $numericIdMap = []; private static $numericIdMap = [];

View File

@ -26,17 +26,43 @@ namespace pocketmine\block\utils;
final class TreeType{ final class TreeType{
/** @var TreeType */ /** @var TreeType */
public static $OAK; private static $OAK;
/** @var TreeType */ /** @var TreeType */
public static $SPRUCE; private static $SPRUCE;
/** @var TreeType */ /** @var TreeType */
public static $BIRCH; private static $BIRCH;
/** @var TreeType */ /** @var TreeType */
public static $JUNGLE; private static $JUNGLE;
/** @var TreeType */ /** @var TreeType */
public static $ACACIA; private static $ACACIA;
/** @var TreeType */ /** @var TreeType */
public static $DARK_OAK; private static $DARK_OAK;
/* auto-generated code */
public static function OAK() : TreeType{
return self::$OAK;
}
public static function SPRUCE() : TreeType{
return self::$SPRUCE;
}
public static function BIRCH() : TreeType{
return self::$BIRCH;
}
public static function JUNGLE() : TreeType{
return self::$JUNGLE;
}
public static function ACACIA() : TreeType{
return self::$ACACIA;
}
public static function DARK_OAK() : TreeType{
return self::$DARK_OAK;
}
/** @var TreeType[] */ /** @var TreeType[] */
private static $numericIdMap = []; private static $numericIdMap = [];

View File

@ -225,7 +225,7 @@ class Banner extends Item{
public function correctNBT() : void{ public function correctNBT() : void{
$tag = $this->getNamedTag(); $tag = $this->getNamedTag();
if(!$tag->hasTag(self::TAG_BASE, IntTag::class)){ if(!$tag->hasTag(self::TAG_BASE, IntTag::class)){
$tag->setInt(self::TAG_BASE, DyeColor::$BLACK->getInvertedMagicNumber()); $tag->setInt(self::TAG_BASE, DyeColor::BLACK()->getInvertedMagicNumber());
} }
if(!$tag->hasTag(self::TAG_PATTERNS, ListTag::class)){ if(!$tag->hasTag(self::TAG_PATTERNS, ListTag::class)){

View File

@ -99,7 +99,7 @@ abstract class Biome{
self::register(self::SMALL_MOUNTAINS, new SmallMountainsBiome()); self::register(self::SMALL_MOUNTAINS, new SmallMountainsBiome());
self::register(self::BIRCH_FOREST, new ForestBiome(TreeType::$BIRCH)); self::register(self::BIRCH_FOREST, new ForestBiome(TreeType::BIRCH()));
} }
/** /**

View File

@ -35,7 +35,7 @@ class ForestBiome extends GrassyBiome{
public function __construct(?TreeType $type = null){ public function __construct(?TreeType $type = null){
parent::__construct(); parent::__construct();
$this->type = $type ?? TreeType::$OAK; $this->type = $type ?? TreeType::OAK();
$trees = new Tree($type); $trees = new Tree($type);
$trees->setBaseAmount(5); $trees->setBaseAmount(5);
@ -48,7 +48,7 @@ class ForestBiome extends GrassyBiome{
$this->setElevation(63, 81); $this->setElevation(63, 81);
if($type === TreeType::$BIRCH){ if($type === TreeType::BIRCH()){
$this->temperature = 0.6; $this->temperature = 0.6;
$this->rainfall = 0.5; $this->rainfall = 0.5;
}else{ }else{

View File

@ -32,7 +32,7 @@ class TaigaBiome extends SnowyBiome{
public function __construct(){ public function __construct(){
parent::__construct(); parent::__construct();
$trees = new Tree(TreeType::$SPRUCE); $trees = new Tree(TreeType::SPRUCE());
$trees->setBaseAmount(10); $trees->setBaseAmount(10);
$this->addPopulator($trees); $this->addPopulator($trees);

View File

@ -34,7 +34,7 @@ class BirchTree extends Tree{
protected $superBirch = false; protected $superBirch = false;
public function __construct(bool $superBirch = false){ public function __construct(bool $superBirch = false){
parent::__construct(BlockFactory::get(Block::LOG, TreeType::$BIRCH->getMagicNumber()), BlockFactory::get(Block::LEAVES, TreeType::$BIRCH->getMagicNumber())); parent::__construct(BlockFactory::get(Block::LOG, TreeType::BIRCH()->getMagicNumber()), BlockFactory::get(Block::LEAVES, TreeType::BIRCH()->getMagicNumber()));
$this->superBirch = $superBirch; $this->superBirch = $superBirch;
} }

View File

@ -30,6 +30,6 @@ use pocketmine\block\utils\TreeType;
class JungleTree extends Tree{ class JungleTree extends Tree{
public function __construct(){ public function __construct(){
parent::__construct(BlockFactory::get(Block::LOG, TreeType::$JUNGLE->getMagicNumber()), BlockFactory::get(Block::LEAVES, TreeType::$JUNGLE->getMagicNumber()), 8); parent::__construct(BlockFactory::get(Block::LOG, TreeType::JUNGLE()->getMagicNumber()), BlockFactory::get(Block::LEAVES, TreeType::JUNGLE()->getMagicNumber()), 8);
} }
} }

View File

@ -32,7 +32,7 @@ use pocketmine\utils\Random;
class OakTree extends Tree{ class OakTree extends Tree{
public function __construct(){ public function __construct(){
parent::__construct(BlockFactory::get(Block::LOG, TreeType::$OAK->getMagicNumber()), BlockFactory::get(Block::LEAVES, TreeType::$OAK->getMagicNumber())); parent::__construct(BlockFactory::get(Block::LOG, TreeType::OAK()->getMagicNumber()), BlockFactory::get(Block::LEAVES, TreeType::OAK()->getMagicNumber()));
} }
public function placeObject(ChunkManager $level, int $x, int $y, int $z, Random $random) : void{ public function placeObject(ChunkManager $level, int $x, int $y, int $z, Random $random) : void{

View File

@ -34,7 +34,7 @@ use function abs;
class SpruceTree extends Tree{ class SpruceTree extends Tree{
public function __construct(){ public function __construct(){
parent::__construct(BlockFactory::get(Block::LOG, TreeType::$SPRUCE->getMagicNumber()), BlockFactory::get(Block::LEAVES, TreeType::$SPRUCE->getMagicNumber()), 10); parent::__construct(BlockFactory::get(Block::LOG, TreeType::SPRUCE()->getMagicNumber()), BlockFactory::get(Block::LEAVES, TreeType::SPRUCE()->getMagicNumber()), 10);
} }
protected function generateChunkHeight(Random $random) : int{ protected function generateChunkHeight(Random $random) : int{

View File

@ -62,18 +62,18 @@ abstract class Tree{
public static function growTree(ChunkManager $level, int $x, int $y, int $z, Random $random, ?TreeType $type = null) : void{ public static function growTree(ChunkManager $level, int $x, int $y, int $z, Random $random, ?TreeType $type = null) : void{
/** @var null|Tree $tree */ /** @var null|Tree $tree */
$tree = null; $tree = null;
$type = $type ?? TreeType::$OAK; $type = $type ?? TreeType::OAK();
if($type === TreeType::$SPRUCE){ if($type === TreeType::SPRUCE()){
$tree = new SpruceTree(); $tree = new SpruceTree();
}elseif($type === TreeType::$BIRCH){ }elseif($type === TreeType::BIRCH()){
if($random->nextBoundedInt(39) === 0){ if($random->nextBoundedInt(39) === 0){
$tree = new BirchTree(true); $tree = new BirchTree(true);
}else{ }else{
$tree = new BirchTree(); $tree = new BirchTree();
} }
}elseif($type === TreeType::$JUNGLE){ }elseif($type === TreeType::JUNGLE()){
$tree = new JungleTree(); $tree = new JungleTree();
}elseif($type === TreeType::$OAK){ //default }elseif($type === TreeType::OAK()){ //default
$tree = new OakTree(); $tree = new OakTree();
/*if($random->nextRange(0, 9) === 0){ /*if($random->nextRange(0, 9) === 0){
$tree = new BigTree(); $tree = new BigTree();

View File

@ -42,7 +42,7 @@ class Tree extends Populator{
* @param TreeType|null $type default oak * @param TreeType|null $type default oak
*/ */
public function __construct(?TreeType $type = null){ public function __construct(?TreeType $type = null){
$this->type = $type ?? TreeType::$OAK; $this->type = $type ?? TreeType::OAK();
} }
public function setRandomAmount(int $amount) : void{ public function setRandomAmount(int $amount) : void{

View File

@ -93,7 +93,7 @@ class Banner extends Spawnable implements Nameable{
private $patterns; private $patterns;
public function __construct(Level $level, Vector3 $pos){ public function __construct(Level $level, Vector3 $pos){
$this->baseColor = DyeColor::$BLACK; $this->baseColor = DyeColor::BLACK();
$this->patterns = new ListTag(self::TAG_PATTERNS); $this->patterns = new ListTag(self::TAG_PATTERNS);
parent::__construct($level, $pos); parent::__construct($level, $pos);
} }

View File

@ -35,7 +35,7 @@ class Bed extends Spawnable{
private $color; private $color;
public function __construct(Level $level, Vector3 $pos){ public function __construct(Level $level, Vector3 $pos){
$this->color = DyeColor::$RED; $this->color = DyeColor::RED();
parent::__construct($level, $pos); parent::__construct($level, $pos);
} }