getId(); if(!$override and self::isRegistered($id)){ throw new \RuntimeException("Trying to overwrite an already registered item"); } self::$list[$id] = clone $item; } /** * Returns an instance of the Item with the specified id, meta, count and NBT. * * @param int $id * @param int $meta * @param int $count * @param CompoundTag|string $tags * * @return Item * @throws \TypeError */ public static function get(int $id, int $meta = 0, int $count = 1, $tags = "") : Item{ if(!is_string($tags) and !($tags instanceof CompoundTag)){ throw new \TypeError("`tags` argument must be a string or CompoundTag instance, " . (is_object($tags) ? "instance of " . get_class($tags) : gettype($tags)) . " given"); } $item = null; try{ if($id < 256){ /* Blocks must have a damage value 0-15, but items can have damage value -1 to indicate that they are * crafting ingredients with any-damage. */ $item = new ItemBlock(BlockFactory::get($id, $meta !== -1 ? $meta : 0), $meta); }else{ /** @var Item|null $listed */ $listed = self::$list[$id]; if($listed !== null){ $item = clone $listed; } } }catch(\RuntimeException $e){ throw new \InvalidArgumentException("Item ID $id is invalid or out of bounds"); } $item = ($item ?? new Item($id, $meta)); $item->setDamage($meta); $item->setCount($count); $item->setCompoundTag($tags); return $item; } /** * Tries to parse the specified string into Item ID/meta identifiers, and returns Item instances it created. * * Example accepted formats: * - `diamond_pickaxe:5` * - `minecraft:string` * - `351:4 (lapis lazuli ID:meta)` * * If multiple item instances are to be created, their identifiers must be comma-separated, for example: * `diamond_pickaxe,wooden_shovel:18,iron_ingot` * * @param string $str * @param bool $multiple * * @return Item[]|Item */ public static function fromString(string $str, bool $multiple = false){ if($multiple === true){ $blocks = []; foreach(explode(",", $str) as $b){ $blocks[] = self::fromString($b, false); } return $blocks; }else{ $b = explode(":", str_replace([" ", "minecraft:"], ["_", ""], trim($str))); if(!isset($b[1])){ $meta = 0; }else{ $meta = $b[1] & 0xFFFF; } if(defined(Item::class . "::" . strtoupper($b[0]))){ $item = self::get(constant(Item::class . "::" . strtoupper($b[0])), $meta); if($item->getId() === Item::AIR and strtoupper($b[0]) !== "AIR" and is_numeric($b[0])){ $item = self::get(((int) $b[0]) & 0xFFFF, $meta); } }elseif(is_numeric($b[0])){ $item = self::get(((int) $b[0]) & 0xFFFF, $meta); }else{ $item = self::get(Item::AIR, 0, 0); } return $item; } } /** * Returns whether the specified item ID is already registered in the item factory. * * @param int $id * @return bool */ public static function isRegistered(int $id) : bool{ if($id < 256){ return BlockFactory::isRegistered($id); } return self::$list[$id] !== null; } }