/** * @param Item $item * @param int $slot * @return CompoundTag */ public static function putItemHelper(Item $item, $slot = null) { $tag = new CompoundTag(null, ["id" => new ShortTag("id", $item->getId()), "Count" => new ByteTag("Count", $item->getCount()), "Damage" => new ShortTag("Damage", $item->getDamage())]); if ($slot !== null) { $tag->Slot = new ByteTag("Slot", (int) $slot); } if ($item->hasCompoundTag()) { $tag->tag = clone $item->getNamedTag(); $tag->tag->setName("tag"); } return $tag; }
public function onActivate(Item $item, Player $player = null) { $tile = $this->getLevel()->getTile($this); if (!$tile instanceof ItemFrameTile) { $nbt = new CompoundTag("", [new StringTag("id", Tile::ITEM_FRAME), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z), new ByteTag("ItemRotation", 0), new FloatTag("ItemDropChance", 1.0)]); Tile::createTile(Tile::ITEM_FRAME, $this->getLevel()->getChunk($this->x >> 4, $this->z >> 4), $nbt); } $tile = $this->getLevel()->getTile($this); if (!$tile instanceof ItemFrameTile) { return false; } if ($tile->getItem()->getId() === 0) { $tile->setItem(Item::get($item->getId(), $item->getDamage(), 1)); if ($player instanceof Player) { if ($player->isSurvival()) { $count = $item->getCount(); if (--$count <= 0) { $player->getInventory()->setItemInHand(Item::get(Item::AIR)); return true; } $item->setCount($count); $player->getInventory()->setItemInHand($item); } } } else { $itemRot = $tile->getItemRotation(); if ($itemRot === 7) { $itemRot = 0; } else { $itemRot++; } $tile->setItemRotation($itemRot); } return true; }
public function onActivate(Item $item, Player $player = null) { $tile = $this->getLevel()->getTile($this); if ($tile instanceof FlowerPotTile) { if ($tile->getFlowerPotItem() === Item::AIR) { switch ($item->getId()) { case Item::TALL_GRASS: if ($item->getDamage() === 1) { break; } case Item::SAPLING: case Item::DEAD_BUSH: case Item::DANDELION: case Item::RED_FLOWER: case Item::BROWN_MUSHROOM: case Item::RED_MUSHROOM: case Item::CACTUS: $tile->setFlowerPotData($item->getId(), $item->getDamage()); $this->setDamage($item->getId()); $this->getLevel()->setBlock($this, $this, true, false); if ($player->isSurvival()) { $item->setCount($item->getCount() - 1); $player->getInventory()->setItemInHand($item->getCount() > 0 ? $item : Item::get(Item::AIR)); } return true; break; } } } return false; }
/** * Returns the change in inventory resulting from this transaction * @return Item[ * "in" => items added to the inventory * "out" => items removed from the inventory * ] */ public function getChange() { $sourceItem = $this->getInventory()->getItem($this->slot); if ($sourceItem->deepEquals($this->targetItem, true, true, true)) { //This should never happen, somehow a change happened where nothing changed return null; } elseif ($sourceItem->deepEquals($this->targetItem)) { //Same item, change of count $item = clone $sourceItem; $countDiff = $this->targetItem->getCount() - $sourceItem->getCount(); $item->setCount(abs($countDiff)); if ($countDiff < 0) { //Count decreased return ["in" => null, "out" => $item]; } elseif ($countDiff > 0) { //Count increased return ["in" => $item, "out" => null]; } else { //Should be impossible (identical items and no count change) //This should be caught by the first condition even if it was possible return null; } } elseif ($sourceItem->getId() !== Item::AIR and $this->targetItem->getId() === Item::AIR) { //Slot emptied (item removed) return ["in" => null, "out" => clone $sourceItem]; } elseif ($sourceItem->getId() === Item::AIR and $this->targetItem->getId() !== Item::AIR) { //Slot filled (item added) return ["in" => $this->getTargetItem(), "out" => null]; } else { //Some other slot change - an item swap (tool damage changes will be ignored as they are processed server-side before any change is sent by the client return ["in" => $this->getTargetItem(), "out" => clone $sourceItem]; } }
/** * @param Item $item * * @return $this */ public function removeIngredient(Item $item) { foreach ($this->ingredients as $index => $ingredient) { if ($item->getCount() <= 0) { break; } if ($ingredient->equals($item, $item->getDamage() === null ? false : true, $item->getCompoundTag() === null ? false : true)) { unset($this->ingredients[$index]); $item->setCount($item->getCount() - 1); } } return $this; }
public function saveNBT() { $this->namedtag->Item = new Compound("Item", ["id" => new Short("id", $this->item->getID()), "Damage" => new Short("Damage", $this->item->getDamage()), "Count" => new Byte("Count", $this->item->getCount())]); $this->namedtag->Health = new Short("Health", $this->getHealth()); $this->namedtag->Age = new Short("Age", $this->age); $this->namedtag->PickupDelay = new Short("PickupDelay", $this->pickupDelay); if ($this->owner !== null) { $this->namedtag->Owner = new String("Owner", $this->owner); } if ($this->thrower !== null) { $this->namedtag->Thrower = new String("Thrower", $this->thrower); } }
public final function equals(Item $item, bool $checkDamage = true, bool $checkCompound = true, bool $checkCount = false) : bool { return $this->id === $item->getId() and ($checkCount === false or $this->getCount() === $item->getCount()) and ($checkDamage === false or $this->getDamage() === $item->getDamage()) and ($checkCompound === false or $this->getCompoundTag() === $item->getCompoundTag()); }
/** * This method should not be used by plugins, use the Inventory * * @param int $index * @param Item $item * * @return bool */ public function setItem($index, Item $item) { $i = $this->getSlotIndex($index); $d = $item->nbtSerialize($index); if ($item->getId() === Item::AIR or $item->getCount() <= 0) { if ($i >= 0) { unset($this->namedtag->Items[$i]); } } elseif ($i < 0) { for ($i = 0; $i <= $this->getSize(); ++$i) { if (!isset($this->namedtag->Items[$i])) { break; } } $this->namedtag->Items[$i] = $d; } else { $this->namedtag->Items[$i] = $d; } return true; }
public function putSlot(Item $item) { if ($item->getId() === 0) { $this->putShort(0); return; } $this->putShort($item->getId()); $this->putByte($item->getCount()); $this->putShort($item->getDamage() === null ? -1 : $item->getDamage()); $nbt = $item->getCompoundTag(); $this->putLShort(strlen($nbt)); $this->put($nbt); }
protected function checkFuel(Item $fuel) { $this->server->getPluginManager()->callEvent($ev = new FurnaceBurnEvent($this, $fuel, $fuel->getFuelTime())); if ($ev->isCancelled()) { return; } $this->namedtag->MaxTime = new Short("MaxTime", $ev->getBurnTime()); $this->namedtag->BurnTime = new Short("BurnTime", $ev->getBurnTime()); $this->namedtag->BurnTicks = new Short("BurnTicks", 0); if ($this->getBlock()->getId() === Item::FURNACE) { $this->getLevel()->setBlock($this, Block::get(Item::BURNING_FURNACE, $this->getBlock()->getDamage()), true); } if ($this->namedtag["BurnTime"] > 0 and $ev->isBurning()) { $fuel->setCount($fuel->getCount() - 1); if ($fuel->getCount() === 0) { $fuel = Item::get(Item::AIR, 0, 0); } $this->inventory->setFuel($fuel); } }
/** * This method should not be used by plugins, use the Inventory * * @param int $index * @param Item $item * * @return bool */ public function setItem($index, Item $item) { $i = $this->getSlotIndex($index); $d = new Compound(false, [new Byte("Count", $item->getCount()), new Byte("Slot", $index), new Short("id", $item->getID()), new Short("Damage", $item->getDamage())]); if ($item->getID() === Item::AIR or $item->getCount() <= 0) { if ($i >= 0) { unset($this->namedtag->Items[$i]); } } elseif ($i < 0) { for ($i = 0; $i <= $this->getSize(); ++$i) { if (!isset($this->namedtag->Items[$i])) { break; } } $this->namedtag->Items[$i] = $d; } else { $this->namedtag->Items[$i] = $d; } return true; }
protected function putSlot(Item $item) { if ($item->getID() === 0) { $this->putShort(-1); } else { $this->putShort($item->getID()); $this->putByte($item->getCount()); $this->putShort($item->getDamage()); $nbt = $item->getCompoundTag(); $this->putByte(strlen($nbt)); $this->put($nbt); } }
/** * Returns an array of Item objects to be dropped * * @param Item $item * * @return array */ public function getDrops(Item $item) { if (!isset(self::$list[$this->getId()])) { //Unknown blocks return []; } else { return [[$this->getId(), $this->getDamage(), 1], [$item->getId(), $item->getDamage(), $item->getCount()]]; } }
/** * @param Player $player * @param Item $item * @return boolean */ private function hasItemPlayer(Player $player, Item $item) { if ($this->SignShop->getProvider()->getPlayer(strtolower($player->getName()))["authorized"] == "root") { return true; } $ris = 0; if ($player->getGamemode() != 1) { for ($i = 0; $i <= $player->getInventory()->getSize(); ++$i) { $inv = $player->getInventory()->getItem($i); if ($inv->getID() == $item->getID() && $inv->getDamage() == $item->getDamage()) { $ris = $ris + $inv->getCount(); } } } if ($ris >= $item->getCount()) { return true; } return false; }
protected function putSlot(Item $item) { if ($item->getID() === 0) { $this->putShort(-1); } else { $this->putShort($item->getID()); $this->putByte($item->getCount()); $this->putShort($item->getDamage()); $this->putShort(-1); } }
protected function putSlot(Item $item) { $this->buffer .= \pack("n", $item->getId()); $this->buffer .= \chr($item->getCount()); $this->buffer .= \pack("n", $item->getDamage()); }
/** * @param Item $item * @return Item|null */ private function condenseRecipes(Item $item) { if (isset($this->condenseShapes[0][$item->getId()])) { // 2x2 Shapes $shape = 4; } elseif (isset($this->condenseShapes[1][$item->getId()])) { // 3x3 Shapes $shape = 9; } else { return null; } $index = (int) sqrt($shape) - 2; $newId = $this->condenseShapes[$index][$item->getId()]; $damage = 0; if (is_array($newId)) { if (!isset($newId[1][$item->getDamage()])) { return null; } $damage = $newId[1][$item->getDamage()]; $newId = $newId[0]; } $count = floor($item->getCount() / $shape); if ($count < 1) { return null; } $condensed = new Item($newId, $damage, $count); if ($condensed->getId() === Item::AIR) { return null; } $item->setCount($item->getCount() - $count * $shape); return $condensed; }
public function onActivate(Item $item, Player $player = null) { $tile = $this->level->getTile($this); if ($tile instanceof ItemFrame) { if ($tile->getItem()->getId() === Item::AIR) { $tile->setItem(Item::get($item->getId(), $item->getDamage(), 1)); $item->setCount($item->getCount() - 1); } else { $rot = $tile->getItemRotation() + 1; $tile->setItemRotation($rot > 8 ? 0 : $rot); } return true; } return false; }
public function onActivate(Item $item, Player $player = null) { $tile = $this->getLevel()->getTile($this); if (!$tile instanceof Cauldron) { return false; } switch ($item->getId()) { case Item::BUCKET: if ($item->getDamage() === 0) { //empty bucket if (!$this->isFull() or $tile->isCustomColor() or $tile->hasPotion()) { break; } $bucket = clone $item; $bucket->setDamage(8); //water bucket Server::getInstance()->getPluginManager()->callEvent($ev = new PlayerBucketFillEvent($player, $this, 0, $item, $bucket)); if (!$ev->isCancelled()) { if ($player->isSurvival()) { $player->getInventory()->setItemInHand($ev->getItem(), $player); } $this->meta = 0; //empty $this->getLevel()->setBlock($this, $this, true); $tile->clearCustomColor(); $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5))); } } elseif ($item->getDamage() === 8) { //water bucket if ($this->isFull() and !$tile->isCustomColor() and !$tile->hasPotion()) { break; } $bucket = clone $item; $bucket->setDamage(0); //empty bucket Server::getInstance()->getPluginManager()->callEvent($ev = new PlayerBucketEmptyEvent($player, $this, 0, $item, $bucket)); if (!$ev->isCancelled()) { if ($player->isSurvival()) { $player->getInventory()->setItemInHand($ev->getItem(), $player); } if ($tile->hasPotion()) { //if has potion $this->meta = 0; //empty $this->getLevel()->setBlock($this, $this, true); $tile->setPotionId(0xffff); //reset potion $tile->clearCustomColor(); $this->getLevel()->addSound(new ExplodeSound($this->add(0.5, 0, 0.5))); } else { $this->meta = 6; //fill $this->getLevel()->setBlock($this, $this, true); $tile->clearCustomColor(); $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5))); } $this->update(); } } break; case Item::DYE: if ($tile->hasPotion()) { break; } $color = Color::getDyeColor($item->getDamage()); if ($tile->isCustomColor()) { $color = Color::averageColor($color, $tile->getCustomColor()); } if ($player->isSurvival()) { $item->setCount($item->getCount() - 1); /*if($item->getCount() <= 0){ $player->getInventory()->setItemInHand(Item::get(Item::AIR)); }*/ } $tile->setCustomColor($color); $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5))); $this->update(); break; case Item::LEATHER_CAP: case Item::LEATHER_TUNIC: case Item::LEATHER_PANTS: case Item::LEATHER_BOOTS: if ($this->isEmpty()) { break; } if ($tile->isCustomColor()) { --$this->meta; $this->getLevel()->setBlock($this, $this, true); $newItem = clone $item; Utils::setCustomColorToArmor($newItem, $tile->getCustomColor()); $player->getInventory()->setItemInHand($newItem); $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5))); if ($this->isEmpty()) { $tile->clearCustomColor(); } } else { --$this->meta; $this->getLevel()->setBlock($this, $this, true); $newItem = clone $item; Utils::clearCustomColorToArmor($newItem); $player->getInventory()->setItemInHand($newItem); $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5))); } break; case Item::POTION: case Item::SPLASH_POTION: if (!$this->isEmpty() and ($tile->getPotionId() !== $item->getDamage() and $item->getDamage() !== Potion::WATER_BOTTLE or $item->getId() === Item::POTION and $tile->getSplashPotion() or $item->getId() === Item::SPLASH_POTION and !$tile->getSplashPotion() or $item->getDamage() === Potion::WATER_BOTTLE and $tile->hasPotion())) { //long... $this->meta = 0x0; $this->getLevel()->setBlock($this, $this, true); $tile->setPotionId(0xffff); //reset $tile->setSplashPotion(false); if ($player->isSurvival()) { $player->getInventory()->setItemInHand(Item::get(Item::GLASS_BOTTLE)); } $this->getLevel()->addSound(new ExplodeSound($this->add(0.5, 0, 0.5))); } elseif ($item->getDamage() === Potion::WATER_BOTTLE) { //water bottle $this->meta += 2; if ($this->meta > 0x6) { $this->meta = 0x6; } $this->getLevel()->setBlock($this, $this, true); if ($player->isSurvival()) { $player->getInventory()->setItemInHand(Item::get(Item::GLASS_BOTTLE)); } $tile->setPotionId(0xffff); $tile->setSplashPotion(false); $tile->clearCustomColor(); $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5))); } elseif (!$this->isFull()) { $this->meta += 2; if ($this->meta > 0x6) { $this->meta = 0x6; } $this->getLevel()->setBlock($this, $this, true); $tile->setPotionId($item->getDamage()); $tile->setSplashPotion($item->getId() === Item::SPLASH_POTION); $tile->clearCustomColor(); if ($player->isSurvival()) { $player->getInventory()->setItemInHand(Item::get(Item::GLASS_BOTTLE)); } $color = Potion::getColor($item->getDamage()); $this->getLevel()->addSound(new SpellSound($this->add(0.5, 1, 0.5), $color[0], $color[1], $color[2])); } break; case Item::GLASS_BOTTLE: if ($this->meta < 2) { break; } if ($tile->hasPotion()) { $this->meta -= 2; if ($this->meta < 0x0) { $this->meta = 0x0; } $this->getLevel()->setBlock($this, $this, true); if ($player->isSurvival()) { $result = Item::get(Item::POTION, $tile->getPotionId()); if ($item->getCount() === 1) { $player->getInventory()->setItemInHand($result); } else { $player->getInventory()->addItem($result); } } if ($this->isEmpty()) { $tile->setPotionId(0xffff); //reset $tile->setSplashPotion(false); } $color = Potion::getColor($result->getDamage()); $this->getLevel()->addSound(new SpellSound($this->add(0.5, 1, 0.5), $color[0], $color[1], $color[2])); } else { $this->meta -= 2; if ($this->meta < 0x0) { $this->meta = 0x0; } $this->getLevel()->setBlock($this, $this, true); if ($player->isSurvival()) { $result = Item::get(Item::POTION, Potion::WATER_BOTTLE); if ($item->getCount() > 1 and $player->getInventory()->canAddItem($result)) { $player->getInventory()->addItem($result); } else { $player->getInventory()->setItemInHand($result); } } $this->getLevel()->addSound(new GraySplashSound($this->add(0.5, 1, 0.5))); } break; } return true; }
public function addItem(Item $item, Player $player, Item $result) { if ($item->getCount() <= 1) { $player->getInventory()->setItemInHand($result); } else { $item->setCount($item->getCount() - 1); if ($player->getInventory()->canAddItem($result) === true) { $player->getInventory()->addItem($result); } else { $motion = $player->getDirectionVector()->multiply(0.4); $position = clone $player->getPosition(); $player->getLevel()->dropItem($position->add(0, 0.5, 0), $result, $motion, 40); } } }
public function putSlot(Item $item) { if ($item->getId() === 0) { $this->buffer .= \pack("n", 0); return; } $this->buffer .= \pack("n", $item->getId()); $this->buffer .= \chr($item->getCount()); $this->buffer .= \pack("n", $item->getDamage() === \null ? -1 : $item->getDamage()); $nbt = $item->getCompoundTag(); $this->buffer .= \pack("n", \strlen($nbt)); $this->buffer .= $nbt; }
/** * @param Item $item * * Drops the specified item in front of the player. */ public function dropItem(Item $item) { if ($this->spawned === false or $this->blocked === true or !$this->isAlive()) { return; } if ($this->isCreative() and $this->server->limitedCreative or $this->isSpectator()) { //Ignore for limited creative return; } if ($item->getId() === Item::AIR or $item->getCount() < 1) { //Ignore dropping air or items with bad counts return; } $ev = new PlayerDropItemEvent($this, $item); $this->server->getPluginManager()->callEvent($ev); if ($ev->isCancelled()) { $this->getFloatingInventory()->removeItem($item); $this->getInventory()->addItem($item); return; } $motion = $this->getDirectionVector()->multiply(0.4); $this->level->dropItem($this->add(0, 1.3, 0), $item, $motion, 40); $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false); }
public function first(Item $item) { $count = \max(1, $item->getCount()); $checkDamage = $item->getDamage() === \null ? \false : \true; $checkTags = $item->getCompoundTag() === \null ? \false : \true; foreach ($this->getContents() as $index => $i) { if ($item->equals($i, $checkDamage, $checkTags) and $i->getCount() >= $count) { return $index; } } return -1; }
protected function putSlot(Item $item) { $this->putShort($item->getId()); $this->putByte($item->getCount()); $this->putShort($item->getDamage()); }
public function sort(Item $i1, Item $i2) { if ($i1->getId() > $i2->getId()) { return 1; } elseif ($i1->getId() < $i2->getId()) { return -1; } elseif ($i1->getDamage() > $i2->getDamage()) { return 1; } elseif ($i1->getDamage() < $i2->getDamage()) { return -1; } elseif ($i1->getCount() > $i2->getCount()) { return 1; } elseif ($i1->getCount() < $i2->getCount()) { return -1; } else { return 0; } }
public function putSlot(Item $item) { if ($item->getId() === 0) { $this->putVarInt(0); return; } $this->putVarInt($item->getId()); $auxValue = ($item->getDamage() ?? -1) << 8 | $item->getCount(); $this->putVarInt($auxValue); $nbt = $item->getCompoundTag(); $this->putLShort(strlen($nbt)); $this->put($nbt); }
public function setItem($index, Item $item) { if ($index < 0 or $index >= $this->size) { return false; } elseif ($item->getId() === 0 or $item->getCount() <= 0) { return $this->clear($index); } if ($index >= $this->getSize()) { //Armor change Server::getInstance()->getPluginManager()->callEvent($ev = new EntityArmorChangeEvent($this->getHolder(), $this->getItem($index), $item, $index)); if ($ev->isCancelled() and $this->getHolder() instanceof Human) { $this->sendArmorSlot($index, $this->getViewers()); return false; } $item = $ev->getNewItem(); } else { Server::getInstance()->getPluginManager()->callEvent($ev = new EntityInventoryChangeEvent($this->getHolder(), $this->getItem($index), $item, $index)); if ($ev->isCancelled()) { $this->sendSlot($index, $this->getViewers()); return false; } $item = $ev->getNewItem(); } $old = $this->getItem($index); $this->slots[$index] = clone $item; $this->onSlotChange($index, $old); return true; }
public function first(Item $item) { $count = max(1, $item->getCount()); $checkDamage = $item->getDamage() === null ? false : true; foreach ($this->getContents() as $index => $i) { if ($item->equals($i, $checkDamage) and $i->getCount() >= $count) { return $index; } } return -1; }
/** * @param Vector3 $source * @param Item $item * @param Vector3 $motion * @param int $delay */ public function dropItem(Vector3 $source, Item $item, Vector3 $motion = null, int $delay = 10) { $motion = $motion === null ? new Vector3(lcg_value() * 0.2 - 0.1, 0.2, lcg_value() * 0.2 - 0.1) : $motion; $itemTag = $item->nbtSerialize(); $itemTag->setName("Item"); if ($item->getId() > 0 and $item->getCount() > 0) { $itemEntity = Entity::createEntity("Item", $this->getChunk($source->getX() >> 4, $source->getZ() >> 4, true), new CompoundTag("", ["Pos" => new ListTag("Pos", [new DoubleTag("", $source->getX()), new DoubleTag("", $source->getY()), new DoubleTag("", $source->getZ())]), "Motion" => new ListTag("Motion", [new DoubleTag("", $motion->x), new DoubleTag("", $motion->y), new DoubleTag("", $motion->z)]), "Rotation" => new ListTag("Rotation", [new FloatTag("", lcg_value() * 360), new FloatTag("", 0)]), "Health" => new ShortTag("Health", 5), "Item" => $itemTag, "PickupDelay" => new ShortTag("PickupDelay", $delay)])); $itemEntity->spawnToAll(); } }
/** * @param Vector3 $source * @param Item $item * @param Vector3 $motion * @param int $delay */ public function dropItem(Vector3 $source, Item $item, Vector3 $motion = null, $delay = 10) { $motion = $motion === null ? new Vector3(lcg_value() * 0.2 - 0.1, 0.2, lcg_value() * 0.2 - 0.1) : $motion; if ($item->getId() > 0 and $item->getCount() > 0) { $itemEntity = Entity::createEntity("Item", $this->getChunk($source->getX() >> 4, $source->getZ() >> 4), new Compound("", ["Pos" => new Enum("Pos", [new Double("", $source->getX()), new Double("", $source->getY()), new Double("", $source->getZ())]), "Motion" => new Enum("Motion", [new Double("", $motion->x), new Double("", $motion->y), new Double("", $motion->z)]), "Rotation" => new Enum("Rotation", [new Float("", lcg_value() * 360), new Float("", 0)]), "Health" => new Short("Health", 5), "Item" => new Compound("Item", ["id" => new Short("id", $item->getId()), "Damage" => new Short("Damage", $item->getDamage()), "Count" => new Byte("Count", $item->getCount())]), "PickupDelay" => new Short("PickupDelay", $delay)])); $itemEntity->spawnToAll(); } }