60class Item implements \JsonSerializable{
63 public const TAG_ENCH =
"ench";
64 private const TAG_ENCH_ID =
"id";
65 private const TAG_ENCH_LVL =
"lvl";
67 public const TAG_DISPLAY =
"display";
68 public const TAG_BLOCK_ENTITY_TAG =
"BlockEntityTag";
70 public const TAG_DISPLAY_NAME =
"Name";
71 public const TAG_DISPLAY_LORE =
"Lore";
73 public const TAG_KEEP_ON_DEATH =
"minecraft:keep_on_death";
75 private const TAG_CAN_PLACE_ON =
"CanPlaceOn";
76 private const TAG_CAN_DESTROY =
"CanDestroy";
80 protected int $count = 1;
84 protected string $customName =
"";
86 protected array $lore = [];
94 protected array $canPlaceOn = [];
99 protected array $canDestroy = [];
101 protected bool $keepOnDeath =
false;
115 protected string $name =
"Unknown",
116 private array $enchantmentTags = []
121 public function hasCustomBlockData() : bool{
122 return $this->blockEntityTag !== null;
129 $this->blockEntityTag = null;
137 $this->blockEntityTag = clone $compound;
142 public function getCustomBlockData() : ?
CompoundTag{
143 return $this->blockEntityTag;
146 public function hasCustomName() : bool{
147 return $this->customName !==
"";
150 public function getCustomName() : string{
151 return $this->customName;
158 Utils::checkUTF8($name);
159 $this->customName = $name;
167 $this->setCustomName(
"");
184 foreach($lines as $line){
185 if(!is_string($line)){
186 throw new \TypeError(
"Expected string[], but found " . gettype($line) .
" in given array");
188 Utils::checkUTF8($line);
190 $this->lore = $lines;
199 return $this->canPlaceOn;
206 $this->canPlaceOn = [];
207 foreach($canPlaceOn as $value){
208 $this->canPlaceOn[$value] = $value;
218 return $this->canDestroy;
225 $this->canDestroy = [];
226 foreach($canDestroy as $value){
227 $this->canDestroy[$value] = $value;
236 return $this->keepOnDeath;
239 public function setKeepOnDeath(
bool $keepOnDeath) :
Item{
240 $this->keepOnDeath = $keepOnDeath;
248 return $this->getNamedTag()->count() > 0;
256 $this->serializeCompoundTag($this->nbt);
267 if($tag->getCount() === 0){
268 return $this->clearNamedTag();
271 $this->nbt = clone $tag;
272 $this->deserializeCompoundTag($this->nbt);
284 $this->deserializeCompoundTag($this->nbt);
292 $this->customName =
"";
296 if($display !==
null){
297 $this->customName = $display->getString(self::TAG_DISPLAY_NAME, $this->customName);
298 $lore = $display->getListTag(self::TAG_DISPLAY_LORE);
299 if($lore !==
null && $lore->getTagType() === NBT::TAG_String){
301 foreach($lore as $t){
302 $this->lore[] = $t->getValue();
308 $enchantments = $tag->
getListTag(self::TAG_ENCH);
309 if($enchantments !==
null && $enchantments->getTagType() === NBT::TAG_Compound){
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);
328 if($canPlaceOn !==
null && $canPlaceOn->getTagType() === NBT::TAG_String){
330 foreach($canPlaceOn as $entry){
331 $this->canPlaceOn[$entry->getValue()] = $entry->getValue();
334 $this->canDestroy = [];
335 $canDestroy = $tag->
getListTag(self::TAG_CAN_DESTROY);
336 if($canDestroy !==
null && $canDestroy->getTagType() === NBT::TAG_String){
338 foreach($canDestroy as $entry){
339 $this->canDestroy[$entry->getValue()] = $entry->getValue();
343 $this->keepOnDeath = $tag->getByte(self::TAG_KEEP_ON_DEATH, 0) !== 0;
346 protected function serializeCompoundTag(CompoundTag $tag) : void{
347 $display = $tag->getCompoundTag(self::TAG_DISPLAY);
349 if($this->customName !==
""){
350 $display ??=
new CompoundTag();
351 $display->setString(self::TAG_DISPLAY_NAME, $this->customName);
353 $display?->removeTag(self::TAG_DISPLAY_NAME);
356 if(count($this->lore) > 0){
357 $loreTag =
new ListTag();
358 foreach($this->lore as $line){
359 $loreTag->push(
new StringTag($line));
361 $display ??=
new CompoundTag();
362 $display->setTag(self::TAG_DISPLAY_LORE, $loreTag);
364 $display?->removeTag(self::TAG_DISPLAY_LORE);
366 $display !==
null && $display->count() > 0 ?
367 $tag->setTag(self::TAG_DISPLAY, $display) :
368 $tag->removeTag(self::TAG_DISPLAY);
370 if(count($this->enchantments) > 0){
371 $ench =
new ListTag();
372 $enchantmentIdMap = EnchantmentIdMap::getInstance();
373 foreach($this->enchantments as $enchantmentInstance){
374 $ench->push(CompoundTag::create()
375 ->setShort(self::TAG_ENCH_ID, $enchantmentIdMap->toId($enchantmentInstance->getType()))
376 ->setShort(self::TAG_ENCH_LVL, $enchantmentInstance->getLevel())
379 $tag->setTag(self::TAG_ENCH, $ench);
381 $tag->removeTag(self::TAG_ENCH);
384 $this->blockEntityTag !==
null ?
385 $tag->setTag(self::TAG_BLOCK_ENTITY_TAG, clone $this->blockEntityTag) :
386 $tag->removeTag(self::TAG_BLOCK_ENTITY_TAG);
388 if(count($this->canPlaceOn) > 0){
389 $canPlaceOn =
new ListTag();
390 foreach($this->canPlaceOn as $item){
391 $canPlaceOn->push(
new StringTag($item));
393 $tag->setTag(self::TAG_CAN_PLACE_ON, $canPlaceOn);
395 $tag->removeTag(self::TAG_CAN_PLACE_ON);
397 if(count($this->canDestroy) > 0){
398 $canDestroy =
new ListTag();
399 foreach($this->canDestroy as $item){
400 $canDestroy->push(
new StringTag($item));
402 $tag->setTag(self::TAG_CAN_DESTROY, $canDestroy);
404 $tag->removeTag(self::TAG_CAN_DESTROY);
407 if($this->keepOnDeath){
408 $tag->setByte(self::TAG_KEEP_ON_DEATH, 1);
410 $tag->removeTag(self::TAG_KEEP_ON_DEATH);
414 public function getCount() : int{
422 $this->count = $count;
434 if($count > $this->count){
435 throw new \InvalidArgumentException(
"Cannot pop $count items from a stack of $this->count");
439 $item->count = $count;
441 $this->count -= $count;
446 public function isNull() : bool{
447 return $this->count <= 0;
454 return $this->hasCustomName() ? $this->getCustomName() : $this->getVanillaName();
474 return $this->enchantmentTags;
487 final public function canBePlaced() : bool{
488 return $this->getBlock()->canBePlaced();
498 final public function getTypeId() : int{
499 return $this->identifier->getTypeId();
502 final public function getStateId() : int{
503 return morton2d_encode($this->identifier->getTypeId(), $this->computeStateData());
506 private function computeStateData() : int{
507 $writer = new RuntimeDataWriter(16);
508 $this->describeState($writer);
509 return $writer->getValue();
523 public function getMaxStackSize() : int{
584 public function getMiningEfficiency(
bool $isCorrectTool) : float{
594 return ItemUseResult::NONE;
604 return ItemUseResult::NONE;
614 return ItemUseResult::NONE;
680 final public function equals(
Item $item,
bool $checkDamage =
true,
bool $checkCompound =
true) : bool{
681 return $this->getStateId() === $item->getStateId() &&
682 (!$checkCompound || $this->getNamedTag()->equals($item->getNamedTag()));
689 return $this->equals($other, true, true);
696 return $this->canStackWith($other) && $this->count === $other->count;
699 final public function __toString() : string{
700 return
"Item " . $this->name .
" (" . $this->getTypeId() .
":" . $this->computeStateData() .
")x" . $this->count . ($this->hasNamedTag() ?
" tags:0x" . base64_encode((new
LittleEndianNbtSerializer())->write(new
TreeRoot($this->getNamedTag()))) :
"");
707 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.");
722 if(isset($data[
"nbt"])){
724 }elseif(isset($data[
"nbt_hex"])){
725 $nbt = hex2bin($data[
"nbt_hex"]);
726 }elseif(isset($data[
"nbt_b64"])){
727 $nbt = base64_decode($data[
"nbt_b64"],
true);
730 $itemStackData = GlobalItemDataHandlers::getUpgrader()->upgradeItemTypeDataInt(
732 (
int) ($data[
"damage"] ?? 0),
733 (
int) ($data[
"count"] ?? 1),
734 $nbt !==
"" ? (
new LittleEndianNbtSerializer())->read($nbt)->mustGetCompoundTag() :
null
738 return GlobalItemDataHandlers::getDeserializer()->deserializeStack($itemStackData);
739 }
catch(ItemTypeDeserializeException $e){
740 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
750 return
GlobalItemDataHandlers::getSerializer()->serializeStack($this, $slot !== -1 ? $slot : null)->toNbt();
759 if($itemData ===
null){
760 return VanillaItems::AIR();
764 return GlobalItemDataHandlers::getDeserializer()->deserializeStack($itemData);
765 }
catch(ItemTypeDeserializeException $e){
766 throw new SavedDataLoadingException($e->getMessage(), 0, $e);
770 public function __clone(){
771 $this->nbt = clone $this->nbt;
772 if($this->blockEntityTag !==
null){
773 $this->blockEntityTag = clone $this->blockEntityTag;