61class Item implements \JsonSerializable{
64 public const TAG_ENCH =
"ench";
65 private const TAG_ENCH_ID =
"id";
66 private const TAG_ENCH_LVL =
"lvl";
68 public const TAG_DISPLAY =
"display";
69 public const TAG_BLOCK_ENTITY_TAG =
"BlockEntityTag";
71 public const TAG_DISPLAY_NAME =
"Name";
72 public const TAG_DISPLAY_LORE =
"Lore";
74 public const TAG_KEEP_ON_DEATH =
"minecraft:keep_on_death";
76 private const TAG_CAN_PLACE_ON =
"CanPlaceOn";
77 private const TAG_CAN_DESTROY =
"CanDestroy";
81 protected int $count = 1;
85 protected string $customName =
"";
87 protected array $lore = [];
95 protected array $canPlaceOn = [];
100 protected array $canDestroy = [];
102 protected bool $keepOnDeath =
false;
116 protected string $name =
"Unknown",
117 private array $enchantmentTags = []
122 public function hasCustomBlockData() : bool{
123 return $this->blockEntityTag !== null;
130 $this->blockEntityTag =
null;
138 $this->blockEntityTag = clone $compound;
143 public function getCustomBlockData() : ?
CompoundTag{
144 return $this->blockEntityTag;
147 public function hasCustomName() : bool{
148 return $this->customName !==
"";
151 public function getCustomName() : string{
152 return $this->customName;
159 Utils::checkUTF8($name);
160 $this->customName = $name;
168 $this->setCustomName(
"");
185 foreach($lines as $line){
186 if(!is_string($line)){
187 throw new \TypeError(
"Expected string[], but found " . gettype($line) .
" in given array");
189 Utils::checkUTF8($line);
191 $this->lore = $lines;
200 return $this->canPlaceOn;
207 $this->canPlaceOn = [];
208 foreach($canPlaceOn as $value){
209 $this->canPlaceOn[$value] = $value;
218 return $this->canDestroy;
225 $this->canDestroy = [];
226 foreach($canDestroy as $value){
227 $this->canDestroy[$value] = $value;
235 return $this->keepOnDeath;
238 public function setKeepOnDeath(
bool $keepOnDeath) : void{
239 $this->keepOnDeath = $keepOnDeath;
246 return $this->getNamedTag()->count() > 0;
254 $this->serializeCompoundTag($this->nbt);
265 if($tag->getCount() === 0){
266 return $this->clearNamedTag();
269 $this->nbt = clone $tag;
270 $this->deserializeCompoundTag($this->nbt);
282 $this->deserializeCompoundTag($this->nbt);
290 $this->customName =
"";
294 if($display !==
null){
295 $this->customName = $display->getString(self::TAG_DISPLAY_NAME, $this->customName);
296 $lore = $display->getListTag(self::TAG_DISPLAY_LORE);
297 if($lore !==
null && $lore->getTagType() === NBT::TAG_String){
299 foreach($lore as $t){
300 $this->lore[] = $t->getValue();
306 $enchantments = $tag->
getListTag(self::TAG_ENCH);
307 if($enchantments !==
null && $enchantments->getTagType() === NBT::TAG_Compound){
309 foreach($enchantments as $enchantment){
310 $magicNumber = $enchantment->getShort(self::TAG_ENCH_ID, -1);
311 $level = $enchantment->getShort(self::TAG_ENCH_LVL, 0);
315 $type = EnchantmentIdMap::getInstance()->fromId($magicNumber);
317 $this->addEnchantment(
new EnchantmentInstance($type, $level));
322 $this->blockEntityTag = $tag->
getCompoundTag(self::TAG_BLOCK_ENTITY_TAG);
324 $this->canPlaceOn = [];
325 $canPlaceOn = $tag->
getListTag(self::TAG_CAN_PLACE_ON);
326 if($canPlaceOn !==
null && $canPlaceOn->getTagType() === NBT::TAG_String){
328 foreach($canPlaceOn as $entry){
329 $this->canPlaceOn[$entry->getValue()] = $entry->getValue();
332 $this->canDestroy = [];
333 $canDestroy = $tag->
getListTag(self::TAG_CAN_DESTROY);
334 if($canDestroy !==
null && $canDestroy->getTagType() === NBT::TAG_String){
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,
int $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,
int $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{
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);
782 public function __clone(){
783 $this->nbt = clone $this->nbt;
784 if($this->blockEntityTag !==
null){
785 $this->blockEntityTag = clone $this->blockEntityTag;