Item: Removed protected block field, items should now override getBlock()

This commit is contained in:
Dylan K. Taylor 2018-02-15 18:44:25 +00:00
parent 2cabdca3f7
commit 88a05845c2
13 changed files with 50 additions and 37 deletions

View File

@ -38,10 +38,13 @@ class Banner extends Item{
public const TAG_PATTERN_NAME = TileBanner::TAG_PATTERN_NAME; public const TAG_PATTERN_NAME = TileBanner::TAG_PATTERN_NAME;
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::STANDING_BANNER);
parent::__construct(self::BANNER, $meta, "Banner"); parent::__construct(self::BANNER, $meta, "Banner");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::STANDING_BANNER);
}
public function getMaxStackSize() : int{ public function getMaxStackSize() : int{
return 16; return 16;
} }

View File

@ -28,10 +28,13 @@ use pocketmine\block\BlockFactory;
class Bed extends Item{ class Bed extends Item{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::BED_BLOCK);
parent::__construct(self::BED, $meta, "Bed"); parent::__construct(self::BED, $meta, "Bed");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::BED_BLOCK);
}
public function getMaxStackSize() : int{ public function getMaxStackSize() : int{
return 1; return 1;
} }

View File

@ -28,7 +28,10 @@ use pocketmine\block\BlockFactory;
class BeetrootSeeds extends Item{ class BeetrootSeeds extends Item{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::BEETROOT_BLOCK);
parent::__construct(self::BEETROOT_SEEDS, $meta, "Beetroot Seeds"); parent::__construct(self::BEETROOT_SEEDS, $meta, "Beetroot Seeds");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::BEETROOT_BLOCK);
}
} }

View File

@ -28,10 +28,13 @@ use pocketmine\block\BlockFactory;
class Carrot extends Food{ class Carrot extends Food{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::CARROT_BLOCK);
parent::__construct(self::CARROT, $meta, "Carrot"); parent::__construct(self::CARROT, $meta, "Carrot");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::CARROT_BLOCK);
}
public function getFoodRestore() : int{ public function getFoodRestore() : int{
return 3; return 3;
} }

View File

@ -42,7 +42,6 @@ use pocketmine\nbt\tag\NamedTag;
use pocketmine\nbt\tag\ShortTag; use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag; use pocketmine\nbt\tag\StringTag;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\Server;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
use pocketmine\utils\Config; use pocketmine\utils\Config;
@ -176,8 +175,6 @@ class Item implements ItemIds, \JsonSerializable{
return -1; return -1;
} }
/** @var Block|null */
protected $block;
/** @var int */ /** @var int */
protected $id; protected $id;
/** @var int */ /** @var int */
@ -206,10 +203,6 @@ class Item implements ItemIds, \JsonSerializable{
$this->id = $id & 0xffff; $this->id = $id & 0xffff;
$this->setDamage($meta); $this->setDamage($meta);
$this->name = $name; $this->name = $name;
if(!isset($this->block) and $this->id <= 0xff){
$this->block = BlockFactory::get($this->id, $this->meta);
$this->name = $this->block->getName();
}
} }
/** /**
@ -648,7 +641,7 @@ class Item implements ItemIds, \JsonSerializable{
* @return bool * @return bool
*/ */
final public function canBePlaced() : bool{ final public function canBePlaced() : bool{
return $this->block !== null and $this->block->canBePlaced(); return $this->getBlock()->canBePlaced();
} }
/** /**
@ -656,12 +649,8 @@ class Item implements ItemIds, \JsonSerializable{
* @return Block * @return Block
*/ */
public function getBlock() : Block{ public function getBlock() : Block{
if($this->block instanceof Block){
return clone $this->block;
}else{
return BlockFactory::get(self::AIR); return BlockFactory::get(self::AIR);
} }
}
/** /**
* @return int * @return int
@ -997,10 +986,6 @@ class Item implements ItemIds, \JsonSerializable{
} }
public function __clone(){ public function __clone(){
if($this->block !== null){
$this->block = clone $this->block;
}
$this->cachedNBT = null; $this->cachedNBT = null;
} }

View File

@ -30,6 +30,8 @@ use pocketmine\block\BlockFactory;
* Class used for Items that can be Blocks * Class used for Items that can be Blocks
*/ */
class ItemBlock extends Item{ class ItemBlock extends Item{
/** @var int */
protected $blockId;
/** /**
* @param int $blockId * @param int $blockId
@ -37,21 +39,16 @@ class ItemBlock extends Item{
* @param int|null $itemId * @param int|null $itemId
*/ */
public function __construct(int $blockId, int $meta = 0, int $itemId = null){ public function __construct(int $blockId, int $meta = 0, int $itemId = null){
$this->block = BlockFactory::get($blockId, $meta & 0xf); $this->blockId = $blockId;
parent::__construct($itemId ?? $this->block->getId(), $meta, $this->block->getName()); parent::__construct($itemId ?? $blockId, $meta, $this->getBlock()->getName());
}
public function setDamage(int $meta) : Item{
$this->block->setDamage($meta !== -1 ? $meta & 0xf : 0);
return parent::setDamage($meta);
} }
public function getBlock() : Block{ public function getBlock() : Block{
return clone $this->block; return BlockFactory::get($this->blockId, $this->meta === -1 ? 0 : $this->meta & 0xf);
} }
public function getFuelTime() : int{ public function getFuelTime() : int{
return $this->block->getFuelTime(); return $this->getBlock()->getFuelTime();
} }
} }

View File

@ -28,7 +28,10 @@ use pocketmine\block\BlockFactory;
class MelonSeeds extends Item{ class MelonSeeds extends Item{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::MELON_STEM);
parent::__construct(self::MELON_SEEDS, $meta, "Melon Seeds"); parent::__construct(self::MELON_SEEDS, $meta, "Melon Seeds");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::MELON_STEM);
}
} }

View File

@ -28,10 +28,13 @@ use pocketmine\block\BlockFactory;
class Potato extends Food{ class Potato extends Food{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::POTATO_BLOCK);
parent::__construct(self::POTATO, $meta, "Potato"); parent::__construct(self::POTATO, $meta, "Potato");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::POTATO_BLOCK);
}
public function getFoodRestore() : int{ public function getFoodRestore() : int{
return 1; return 1;
} }

View File

@ -28,7 +28,10 @@ use pocketmine\block\BlockFactory;
class PumpkinSeeds extends Item{ class PumpkinSeeds extends Item{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::PUMPKIN_STEM);
parent::__construct(self::PUMPKIN_SEEDS, $meta, "Pumpkin Seeds"); parent::__construct(self::PUMPKIN_SEEDS, $meta, "Pumpkin Seeds");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::PUMPKIN_STEM);
}
} }

View File

@ -28,9 +28,11 @@ use pocketmine\block\BlockFactory;
class Redstone extends Item{ class Redstone extends Item{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::REDSTONE_WIRE);
parent::__construct(self::REDSTONE, $meta, "Redstone"); parent::__construct(self::REDSTONE, $meta, "Redstone");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::REDSTONE_WIRE);
}
} }

View File

@ -28,10 +28,13 @@ use pocketmine\block\BlockFactory;
class Sign extends Item{ class Sign extends Item{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::SIGN_POST);
parent::__construct(self::SIGN, $meta, "Sign"); parent::__construct(self::SIGN, $meta, "Sign");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::SIGN_POST);
}
public function getMaxStackSize() : int{ public function getMaxStackSize() : int{
return 16; return 16;
} }

View File

@ -28,9 +28,11 @@ use pocketmine\block\BlockFactory;
class StringItem extends Item{ class StringItem extends Item{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::TRIPWIRE);
parent::__construct(self::STRING, $meta, "String"); parent::__construct(self::STRING, $meta, "String");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::TRIPWIRE);
}
} }

View File

@ -28,7 +28,10 @@ use pocketmine\block\BlockFactory;
class WheatSeeds extends Item{ class WheatSeeds extends Item{
public function __construct(int $meta = 0){ public function __construct(int $meta = 0){
$this->block = BlockFactory::get(Block::WHEAT_BLOCK);
parent::__construct(self::WHEAT_SEEDS, $meta, "Wheat Seeds"); parent::__construct(self::WHEAT_SEEDS, $meta, "Wheat Seeds");
} }
public function getBlock() : Block{
return BlockFactory::get(Block::WHEAT_BLOCK);
}
} }