62class Item implements \JsonSerializable{
65 public const TAG_ENCH =
"ench";
66 private const TAG_ENCH_ID =
"id";
67 private const TAG_ENCH_LVL =
"lvl";
69 public const TAG_DISPLAY =
"display";
70 public const TAG_BLOCK_ENTITY_TAG =
"BlockEntityTag";
72 public const TAG_DISPLAY_NAME =
"Name";
73 public const TAG_DISPLAY_LORE =
"Lore";
75 public const TAG_KEEP_ON_DEATH =
"minecraft:keep_on_death";
77 private const TAG_CAN_PLACE_ON =
"CanPlaceOn";
78 private const TAG_CAN_DESTROY =
"CanDestroy";
82 protected int $count = 1;
86 protected string $customName =
"";
88 protected array $lore = [];
96 protected array $canPlaceOn = [];
101 protected array $canDestroy = [];
103 protected bool $keepOnDeath =
false;
117 protected string $name =
"Unknown",
118 private array $enchantmentTags = []
123 public function hasCustomBlockData() : bool{
124 return $this->blockEntityTag !== null;
131 $this->blockEntityTag = null;
139 $this->blockEntityTag = clone $compound;
144 public function getCustomBlockData() : ?
CompoundTag{
145 return $this->blockEntityTag;
148 public function hasCustomName() : bool{
149 return $this->customName !==
"";
152 public function getCustomName() : string{
153 return $this->customName;
160 Utils::checkUTF8($name);
161 $this->customName = $name;
169 $this->setCustomName(
"");
186 foreach($lines as $line){
187 if(!is_string($line)){
188 throw new \TypeError(
"Expected string[], but found " . gettype($line) .
" in given array");
190 Utils::checkUTF8($line);
192 $this->lore = $lines;
201 return $this->canPlaceOn;
208 $this->canPlaceOn = [];
209 foreach($canPlaceOn as $value){
210 $this->canPlaceOn[$value] = $value;
220 return $this->canDestroy;
227 $this->canDestroy = [];
228 foreach($canDestroy as $value){
229 $this->canDestroy[$value] = $value;
238 return $this->keepOnDeath;
241 public function setKeepOnDeath(
bool $keepOnDeath) :
Item{
242 $this->keepOnDeath = $keepOnDeath;
250 return $this->getNamedTag()->count() > 0;
258 $this->serializeCompoundTag($this->nbt);
269 if($tag->getCount() === 0){
270 return $this->clearNamedTag();
273 $this->nbt = clone $tag;
274 $this->deserializeCompoundTag($this->nbt);
286 $this->deserializeCompoundTag($this->nbt);
294 $this->customName =
"";
298 if($display !==
null){
299 $this->customName = $display->getString(self::TAG_DISPLAY_NAME, $this->customName);
300 $lore = $display->getListTag(self::TAG_DISPLAY_LORE);
301 if($lore !==
null && $lore->getTagType() === NBT::TAG_String){
303 foreach($lore as $t){
304 $this->lore[] = $t->getValue();
310 $enchantments = $tag->
getListTag(self::TAG_ENCH);
311 if($enchantments !==
null && $enchantments->getTagType() === NBT::TAG_Compound){
313 foreach($enchantments as $enchantment){
314 $magicNumber = $enchantment->getShort(self::TAG_ENCH_ID, -1);
315 $level = $enchantment->getShort(self::TAG_ENCH_LVL, 0);
319 $type = EnchantmentIdMap::getInstance()->fromId($magicNumber);
321 $this->addEnchantment(
new EnchantmentInstance($type, $level));
326 $this->blockEntityTag = $tag->
getCompoundTag(self::TAG_BLOCK_ENTITY_TAG);
328 $this->canPlaceOn = [];
329 $canPlaceOn = $tag->
getListTag(self::TAG_CAN_PLACE_ON);
330 if($canPlaceOn !==
null && $canPlaceOn->getTagType() === NBT::TAG_String){
332 foreach($canPlaceOn as $entry){
333 $this->canPlaceOn[$entry->getValue()] = $entry->getValue();
336 $this->canDestroy = [];
337 $canDestroy = $tag->
getListTag(self::TAG_CAN_DESTROY);
338 if($canDestroy !==
null && $canDestroy->getTagType() === NBT::TAG_String){
340 foreach($canDestroy as $entry){
341 $this->canDestroy[$entry->getValue()] = $entry->getValue();
345 $this->keepOnDeath = $tag->getByte(self::TAG_KEEP_ON_DEATH, 0) !== 0;
348 protected function serializeCompoundTag(CompoundTag $tag) : void{
349 $display = $tag->getCompoundTag(self::TAG_DISPLAY);
351 if($this->customName !==
""){
352 $display ??=
new CompoundTag();
353 $display->setString(self::TAG_DISPLAY_NAME, $this->customName);
355 $display?->removeTag(self::TAG_DISPLAY_NAME);
358 if(count($this->lore) > 0){
359 $loreTag =
new ListTag();
360 foreach($this->lore as $line){
361 $loreTag->push(
new StringTag($line));
363 $display ??=
new CompoundTag();
364 $display->setTag(self::TAG_DISPLAY_LORE, $loreTag);
366 $display?->removeTag(self::TAG_DISPLAY_LORE);
368 $display !==
null && $display->count() > 0 ?
369 $tag->setTag(self::TAG_DISPLAY, $display) :
370 $tag->removeTag(self::TAG_DISPLAY);
372 if(count($this->enchantments) > 0){
373 $ench =
new ListTag();
374 $enchantmentIdMap = EnchantmentIdMap::getInstance();
375 foreach($this->enchantments as $enchantmentInstance){
376 $ench->push(CompoundTag::create()
377 ->setShort(self::TAG_ENCH_ID, $enchantmentIdMap->toId($enchantmentInstance->getType()))
378 ->setShort(self::TAG_ENCH_LVL, $enchantmentInstance->getLevel())
381 $tag->setTag(self::TAG_ENCH, $ench);
383 $tag->removeTag(self::TAG_ENCH);
386 $this->blockEntityTag !==
null ?
387 $tag->setTag(self::TAG_BLOCK_ENTITY_TAG, clone $this->blockEntityTag) :
388 $tag->removeTag(self::TAG_BLOCK_ENTITY_TAG);
390 if(count($this->canPlaceOn) > 0){
391 $canPlaceOn =
new ListTag();
392 foreach($this->canPlaceOn as $item){
393 $canPlaceOn->push(
new StringTag($item));
395 $tag->setTag(self::TAG_CAN_PLACE_ON, $canPlaceOn);
397 $tag->removeTag(self::TAG_CAN_PLACE_ON);
399 if(count($this->canDestroy) > 0){
400 $canDestroy =
new ListTag();
401 foreach($this->canDestroy as $item){
402 $canDestroy->push(
new StringTag($item));
404 $tag->setTag(self::TAG_CAN_DESTROY, $canDestroy);
406 $tag->removeTag(self::TAG_CAN_DESTROY);
409 if($this->keepOnDeath){
410 $tag->setByte(self::TAG_KEEP_ON_DEATH, 1);
412 $tag->removeTag(self::TAG_KEEP_ON_DEATH);
416 public function getCount() : int{
424 $this->count = $count;
436 if($count > $this->count){
437 throw new \InvalidArgumentException(
"Cannot pop $count items from a stack of $this->count");
441 $item->count = $count;
443 $this->count -= $count;
448 public function isNull() : bool{
449 return $this->count <= 0;
456 return $this->hasCustomName() ? $this->getCustomName() : $this->getVanillaName();
476 return $this->enchantmentTags;
489 final public function canBePlaced() : bool{
490 return $this->getBlock()->canBePlaced();
493 protected final function tryPlacementTransaction(Block $blockPlace, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?
Player $player) : ?BlockTransaction{
494 $position = $blockReplace->getPosition();
495 $blockPlace->position($position->getWorld(), $position->getFloorX(), $position->getFloorY(), $position->getFloorZ());
496 if(!$blockPlace->canBePlacedAt($blockReplace, $clickVector, $face, $blockReplace->getPosition()->equals($blockClicked->getPosition()))){
499 $transaction =
new BlockTransaction($position->getWorld());
500 return $blockPlace->place($transaction, $this, $blockReplace, $blockClicked, $face, $clickVector, $player) ? $transaction :
null;
503 public function getPlacementTransaction(Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player =
null) : ?BlockTransaction{
504 return $this->tryPlacementTransaction($this->getBlock($face), $blockReplace, $blockClicked, $face, $clickVector, $player);
514 final public function getTypeId() : int{
515 return $this->identifier->getTypeId();
518 final public function getStateId() : int{
519 return morton2d_encode($this->identifier->getTypeId(), $this->computeStateData());
522 private function computeStateData() : int{
523 $writer = new RuntimeDataWriter(16);
524 $this->describeState($writer);
525 return $writer->getValue();
539 public function getMaxStackSize() : int{
600 public function getMiningEfficiency(
bool $isCorrectTool) : float{
610 return ItemUseResult::NONE;
620 return ItemUseResult::NONE;
630 return ItemUseResult::NONE;
696 final public function equals(
Item $item,
bool $checkDamage =
true,
bool $checkCompound =
true) : bool{
697 return $this->getStateId() === $item->getStateId() &&
698 (!$checkCompound || $this->getNamedTag()->equals($item->getNamedTag()));
705 return $this->equals($other, true, true);
712 return $this->canStackWith($other) && $this->count === $other->count;
715 final public function __toString() : string{
716 return
"Item " . $this->name .
" (" . $this->getTypeId() .
":" . $this->computeStateData() .
")x" . $this->count . ($this->hasNamedTag() ?
" tags:0x" . base64_encode((new
LittleEndianNbtSerializer())->write(new
TreeRoot($this->getNamedTag()))) :
"");
723 throw new \LogicException(
"json_encode()ing Item instances is no longer supported. Make your own method to convert the item to an array or stdClass.");
738 if(isset($data[
"nbt"])){
740 }elseif(isset($data[
"nbt_hex"])){
741 $nbt = hex2bin($data[
"nbt_hex"]);
742 }elseif(isset($data[
"nbt_b64"])){
743 $nbt = base64_decode($data[
"nbt_b64"],
true);
746 $itemStackData = GlobalItemDataHandlers::getUpgrader()->upgradeItemTypeDataInt(
748 (
int) ($data[
"damage"] ?? 0),
749 (
int) ($data[
"count"] ?? 1),
750 $nbt !==
"" ? (
new LittleEndianNbtSerializer())->read($nbt)->mustGetCompoundTag() :
null
754 return GlobalItemDataHandlers::getDeserializer()->deserializeStack($itemStackData);
755 }
catch(ItemTypeDeserializeException $e){
756 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
766 return
GlobalItemDataHandlers::getSerializer()->serializeStack($this, $slot !== -1 ? $slot : null)->toNbt();
775 if($itemData ===
null){
776 return VanillaItems::AIR();
780 return GlobalItemDataHandlers::getDeserializer()->deserializeStack($itemData);
781 }
catch(ItemTypeDeserializeException $e){
782 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
786 public function __clone(){
787 $this->nbt = clone $this->nbt;
788 if($this->blockEntityTag !==
null){
789 $this->blockEntityTag = clone $this->blockEntityTag;