ItemTypeIds: added fromBlockTypeId() and toBlockTypeId()

this allows checking the type of a blockitem without being required to create a block to do it.
This commit is contained in:
Dylan K. Taylor 2023-03-02 15:28:50 +00:00
parent 77fe0a69ba
commit 33140482bb
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 21 additions and 10 deletions

View File

@ -31,7 +31,6 @@ use pocketmine\item\Armor;
use pocketmine\item\Banner;
use pocketmine\item\Dye;
use pocketmine\item\Item;
use pocketmine\item\ItemBlock;
use pocketmine\item\ItemTypeIds;
use pocketmine\item\Potion;
use pocketmine\item\PotionType;
@ -155,7 +154,7 @@ final class WaterCauldron extends FillableCauldron{
$this->position->getWorld()->setBlock($this->position, $this->withFillLevel($this->getFillLevel() - self::CLEAN_BANNER_USE_AMOUNT));
$this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new CauldronCleanItemSound());
}
}elseif($item instanceof ItemBlock && $item->getBlock()->getTypeId() === BlockTypeIds::DYED_SHULKER_BOX){
}elseif(ItemTypeIds::toBlockTypeId($item->getTypeId()) === BlockTypeIds::DYED_SHULKER_BOX){
if($this->customWaterColor === null){
$newItem = VanillaBlocks::SHULKER_BOX()->asItem();
$newItem->setNamedTag($item->getNamedTag());

View File

@ -26,7 +26,7 @@ namespace pocketmine\block\inventory;
use pocketmine\block\BlockTypeIds;
use pocketmine\inventory\SimpleInventory;
use pocketmine\item\Item;
use pocketmine\item\ItemBlock;
use pocketmine\item\ItemTypeIds;
use pocketmine\network\mcpe\protocol\BlockEventPacket;
use pocketmine\network\mcpe\protocol\types\BlockPosition;
use pocketmine\world\Position;
@ -51,11 +51,9 @@ class ShulkerBoxInventory extends SimpleInventory implements BlockInventory{
}
public function canAddItem(Item $item) : bool{
if($item instanceof ItemBlock){
$blockTypeId = $item->getBlock()->getTypeId();
if($blockTypeId === BlockTypeIds::SHULKER_BOX || $blockTypeId === BlockTypeIds::DYED_SHULKER_BOX){
return false;
}
$blockTypeId = ItemTypeIds::toBlockTypeId($item->getTypeId());
if($blockTypeId === BlockTypeIds::SHULKER_BOX || $blockTypeId === BlockTypeIds::DYED_SHULKER_BOX){
return false;
}
return parent::canAddItem($item);
}

View File

@ -31,9 +31,8 @@ class ItemIdentifier{
){}
public static function fromBlock(Block $block) : self{
//negative item type IDs are treated as block IDs
//TODO: maybe an ItemBlockIdentifier is in order?
return new self(-$block->getTypeId());
return new self(ItemTypeIds::fromBlockTypeId($block->getTypeId()));
}
public function getTypeId() : int{ return $this->typeId; }

View File

@ -311,4 +311,19 @@ final class ItemTypeIds{
public static function newId() : int{
return self::$nextDynamicId++;
}
public static function fromBlockTypeId(int $blockTypeId) : int{
if($blockTypeId < 0){
throw new \InvalidArgumentException("Block type IDs cannot be negative");
}
//negative item type IDs are treated as block IDs
return -$blockTypeId;
}
public static function toBlockTypeId(int $itemTypeId) : int{
if($itemTypeId > 0){
throw new \InvalidArgumentException("Item type ID $itemTypeId does not represent a block");
}
return -$itemTypeId;
}
}