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, StringTag::class);
302 foreach($lore as $t){
303 $this->lore[] = $t->getValue();
309 $enchantments = $tag->
getListTag(self::TAG_ENCH, CompoundTag::class);
310 if($enchantments !==
null){
311 foreach($enchantments as $enchantment){
312 $magicNumber = $enchantment->getShort(self::TAG_ENCH_ID, -1);
313 $level = $enchantment->getShort(self::TAG_ENCH_LVL, 0);
317 $type = EnchantmentIdMap::getInstance()->fromId($magicNumber);
319 $this->addEnchantment(
new EnchantmentInstance($type, $level));
324 $this->blockEntityTag = $tag->
getCompoundTag(self::TAG_BLOCK_ENTITY_TAG);
326 $this->canPlaceOn = [];
327 $canPlaceOn = $tag->
getListTag(self::TAG_CAN_PLACE_ON, StringTag::class);
328 if($canPlaceOn !==
null){
329 foreach($canPlaceOn as $entry){
330 $this->canPlaceOn[$entry->getValue()] = $entry->getValue();
333 $this->canDestroy = [];
334 $canDestroy = $tag->
getListTag(self::TAG_CAN_DESTROY, StringTag::class);
335 if($canDestroy !==
null){
336 foreach($canDestroy as $entry){
337 $this->canDestroy[$entry->getValue()] = $entry->getValue();
341 $this->keepOnDeath = $tag->getByte(self::TAG_KEEP_ON_DEATH, 0) !== 0;
344 protected function serializeCompoundTag(CompoundTag $tag) : void{
345 $display = $tag->getCompoundTag(self::TAG_DISPLAY);
347 if($this->customName !==
""){
348 $display ??=
new CompoundTag();
349 $display->setString(self::TAG_DISPLAY_NAME, $this->customName);
351 $display?->removeTag(self::TAG_DISPLAY_NAME);
354 if(count($this->lore) > 0){
355 $loreTag =
new ListTag();
356 foreach($this->lore as $line){
357 $loreTag->push(
new StringTag($line));
359 $display ??=
new CompoundTag();
360 $display->setTag(self::TAG_DISPLAY_LORE, $loreTag);
362 $display?->removeTag(self::TAG_DISPLAY_LORE);
364 $display !==
null && $display->count() > 0 ?
365 $tag->setTag(self::TAG_DISPLAY, $display) :
366 $tag->removeTag(self::TAG_DISPLAY);
368 if(count($this->enchantments) > 0){
369 $ench =
new ListTag();
370 $enchantmentIdMap = EnchantmentIdMap::getInstance();
371 foreach($this->enchantments as $enchantmentInstance){
372 $ench->push(CompoundTag::create()
373 ->setShort(self::TAG_ENCH_ID, $enchantmentIdMap->toId($enchantmentInstance->getType()))
374 ->setShort(self::TAG_ENCH_LVL, $enchantmentInstance->getLevel())
377 $tag->setTag(self::TAG_ENCH, $ench);
379 $tag->removeTag(self::TAG_ENCH);
382 $this->blockEntityTag !==
null ?
383 $tag->setTag(self::TAG_BLOCK_ENTITY_TAG, clone $this->blockEntityTag) :
384 $tag->removeTag(self::TAG_BLOCK_ENTITY_TAG);
386 if(count($this->canPlaceOn) > 0){
387 $canPlaceOn =
new ListTag();
388 foreach($this->canPlaceOn as $item){
389 $canPlaceOn->push(
new StringTag($item));
391 $tag->setTag(self::TAG_CAN_PLACE_ON, $canPlaceOn);
393 $tag->removeTag(self::TAG_CAN_PLACE_ON);
395 if(count($this->canDestroy) > 0){
396 $canDestroy =
new ListTag();
397 foreach($this->canDestroy as $item){
398 $canDestroy->push(
new StringTag($item));
400 $tag->setTag(self::TAG_CAN_DESTROY, $canDestroy);
402 $tag->removeTag(self::TAG_CAN_DESTROY);
405 if($this->keepOnDeath){
406 $tag->setByte(self::TAG_KEEP_ON_DEATH, 1);
408 $tag->removeTag(self::TAG_KEEP_ON_DEATH);
412 public function getCount() : int{
420 $this->count = $count;
432 if($count > $this->count){
433 throw new \InvalidArgumentException(
"Cannot pop $count items from a stack of $this->count");
437 $item->count = $count;
439 $this->count -= $count;
444 public function isNull() : bool{
445 return $this->count <= 0;
452 return $this->hasCustomName() ? $this->getCustomName() : $this->getVanillaName();
472 return $this->enchantmentTags;
485 final public function canBePlaced() : bool{
486 return $this->getBlock()->canBePlaced();
489 protected final function tryPlacementTransaction(Block $blockPlace, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?
Player $player) : ?BlockTransaction{
490 $position = $blockReplace->getPosition();
491 $blockPlace->position($position->getWorld(), $position->getFloorX(), $position->getFloorY(), $position->getFloorZ());
492 if(!$blockPlace->canBePlacedAt($blockReplace, $clickVector, $face, $blockReplace->getPosition()->equals($blockClicked->getPosition()))){
495 $transaction =
new BlockTransaction($position->getWorld());
496 return $blockPlace->place($transaction, $this, $blockReplace, $blockClicked, $face, $clickVector, $player) ? $transaction :
null;
499 public function getPlacementTransaction(Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player =
null) : ?BlockTransaction{
500 return $this->tryPlacementTransaction($this->getBlock($face), $blockReplace, $blockClicked, $face, $clickVector, $player);
510 final public function getTypeId() : int{
511 return $this->identifier->getTypeId();
514 final public function getStateId() : int{
515 return morton2d_encode($this->identifier->getTypeId(), $this->computeStateData());
518 private function computeStateData() : int{
519 $writer = new RuntimeDataWriter(16);
520 $this->describeState($writer);
521 return $writer->getValue();
535 public function getMaxStackSize() : int{
596 public function getMiningEfficiency(
bool $isCorrectTool) : float{
606 return ItemUseResult::NONE;
616 return ItemUseResult::NONE;
626 return ItemUseResult::NONE;
692 final public function equals(
Item $item,
bool $checkDamage =
true,
bool $checkCompound =
true) : bool{
693 return $this->getStateId() === $item->getStateId() &&
694 (!$checkCompound || $this->getNamedTag()->equals($item->getNamedTag()));
701 return $this->equals($other, true, true);
708 return $this->canStackWith($other) && $this->count === $other->count;
711 final public function __toString() : string{
712 return
"Item " . $this->name .
" (" . $this->getTypeId() .
":" . $this->computeStateData() .
")x" . $this->count . ($this->hasNamedTag() ?
" tags:0x" . base64_encode((new
LittleEndianNbtSerializer())->write(new
TreeRoot($this->getNamedTag()))) :
"");
719 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.");
734 if(isset($data[
"nbt"])){
736 }elseif(isset($data[
"nbt_hex"])){
737 $nbt = hex2bin($data[
"nbt_hex"]);
738 }elseif(isset($data[
"nbt_b64"])){
739 $nbt = base64_decode($data[
"nbt_b64"],
true);
742 $itemStackData = GlobalItemDataHandlers::getUpgrader()->upgradeItemTypeDataInt(
744 (
int) ($data[
"damage"] ?? 0),
745 (
int) ($data[
"count"] ?? 1),
746 $nbt !==
"" ? (
new LittleEndianNbtSerializer())->read($nbt)->mustGetCompoundTag() :
null
750 return GlobalItemDataHandlers::getDeserializer()->deserializeStack($itemStackData);
751 }
catch(ItemTypeDeserializeException $e){
752 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
762 return
GlobalItemDataHandlers::getSerializer()->serializeStack($this, $slot !== -1 ? $slot : null)->toNbt();
771 if($itemData ===
null){
772 return VanillaItems::AIR();
776 return GlobalItemDataHandlers::getDeserializer()->deserializeStack($itemData);
777 }
catch(ItemTypeDeserializeException $e){
778 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
790 return self::nbtDeserialize($tag);
793 $logger ??= \GlobalLogger::get();
794 $logger->error(
"$errorLogContext: Error deserializing item (item will be replaced by AIR): " . $e->getMessage());
796 return VanillaItems::AIR();
800 public function __clone(){
801 $this->nbt = clone $this->nbt;
802 if($this->blockEntityTag !==
null){
803 $this->blockEntityTag = clone $this->blockEntityTag;