mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
Item factory refactor and added capability to register custom items
This commit is contained in:
@ -76,185 +76,35 @@ class Item implements ItemIds, \JsonSerializable{
|
||||
return self::$cachedParser->write();
|
||||
}
|
||||
|
||||
/** @var \SplFixedArray */
|
||||
public static $list = null;
|
||||
/** @var Block|null */
|
||||
protected $block;
|
||||
/** @var int */
|
||||
protected $id;
|
||||
/** @var int */
|
||||
protected $meta;
|
||||
/** @var string */
|
||||
private $tags = "";
|
||||
/** @var CompoundTag|null */
|
||||
private $cachedNBT = null;
|
||||
/** @var int */
|
||||
public $count;
|
||||
/** @var string */
|
||||
protected $name;
|
||||
/**
|
||||
* @param int $id
|
||||
* @param int $meta
|
||||
* @param int $count
|
||||
* @param CompoundTag|string $tags
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
|
||||
public static function init(){
|
||||
if(self::$list === null){
|
||||
self::$list = new \SplFixedArray(65536);
|
||||
|
||||
self::$list[self::IRON_SHOVEL] = IronShovel::class;
|
||||
self::$list[self::IRON_PICKAXE] = IronPickaxe::class;
|
||||
self::$list[self::IRON_AXE] = IronAxe::class;
|
||||
self::$list[self::FLINT_STEEL] = FlintSteel::class;
|
||||
self::$list[self::APPLE] = Apple::class;
|
||||
self::$list[self::BOW] = Bow::class;
|
||||
self::$list[self::ARROW] = Arrow::class;
|
||||
self::$list[self::COAL] = Coal::class;
|
||||
self::$list[self::DIAMOND] = Diamond::class;
|
||||
self::$list[self::IRON_INGOT] = IronIngot::class;
|
||||
self::$list[self::GOLD_INGOT] = GoldIngot::class;
|
||||
self::$list[self::IRON_SWORD] = IronSword::class;
|
||||
self::$list[self::WOODEN_SWORD] = WoodenSword::class;
|
||||
self::$list[self::WOODEN_SHOVEL] = WoodenShovel::class;
|
||||
self::$list[self::WOODEN_PICKAXE] = WoodenPickaxe::class;
|
||||
self::$list[self::WOODEN_AXE] = WoodenAxe::class;
|
||||
self::$list[self::STONE_SWORD] = StoneSword::class;
|
||||
self::$list[self::STONE_SHOVEL] = StoneShovel::class;
|
||||
self::$list[self::STONE_PICKAXE] = StonePickaxe::class;
|
||||
self::$list[self::STONE_AXE] = StoneAxe::class;
|
||||
self::$list[self::DIAMOND_SWORD] = DiamondSword::class;
|
||||
self::$list[self::DIAMOND_SHOVEL] = DiamondShovel::class;
|
||||
self::$list[self::DIAMOND_PICKAXE] = DiamondPickaxe::class;
|
||||
self::$list[self::DIAMOND_AXE] = DiamondAxe::class;
|
||||
self::$list[self::STICK] = Stick::class;
|
||||
self::$list[self::BOWL] = Bowl::class;
|
||||
self::$list[self::MUSHROOM_STEW] = MushroomStew::class;
|
||||
self::$list[self::GOLDEN_SWORD] = GoldSword::class;
|
||||
self::$list[self::GOLDEN_SHOVEL] = GoldShovel::class;
|
||||
self::$list[self::GOLDEN_PICKAXE] = GoldPickaxe::class;
|
||||
self::$list[self::GOLDEN_AXE] = GoldAxe::class;
|
||||
self::$list[self::STRING] = StringItem::class;
|
||||
self::$list[self::FEATHER] = Feather::class;
|
||||
self::$list[self::GUNPOWDER] = Gunpowder::class;
|
||||
self::$list[self::WOODEN_HOE] = WoodenHoe::class;
|
||||
self::$list[self::STONE_HOE] = StoneHoe::class;
|
||||
self::$list[self::IRON_HOE] = IronHoe::class;
|
||||
self::$list[self::DIAMOND_HOE] = DiamondHoe::class;
|
||||
self::$list[self::GOLDEN_HOE] = GoldHoe::class;
|
||||
self::$list[self::WHEAT_SEEDS] = WheatSeeds::class;
|
||||
self::$list[self::WHEAT] = Wheat::class;
|
||||
self::$list[self::BREAD] = Bread::class;
|
||||
self::$list[self::LEATHER_CAP] = LeatherCap::class;
|
||||
self::$list[self::LEATHER_TUNIC] = LeatherTunic::class;
|
||||
self::$list[self::LEATHER_PANTS] = LeatherPants::class;
|
||||
self::$list[self::LEATHER_BOOTS] = LeatherBoots::class;
|
||||
self::$list[self::CHAINMAIL_HELMET] = ChainHelmet::class;
|
||||
self::$list[self::CHAINMAIL_CHESTPLATE] = ChainChestplate::class;
|
||||
self::$list[self::CHAINMAIL_LEGGINGS] = ChainLeggings::class;
|
||||
self::$list[self::CHAINMAIL_BOOTS] = ChainBoots::class;
|
||||
self::$list[self::IRON_HELMET] = IronHelmet::class;
|
||||
self::$list[self::IRON_CHESTPLATE] = IronChestplate::class;
|
||||
self::$list[self::IRON_LEGGINGS] = IronLeggings::class;
|
||||
self::$list[self::IRON_BOOTS] = IronBoots::class;
|
||||
self::$list[self::DIAMOND_HELMET] = DiamondHelmet::class;
|
||||
self::$list[self::DIAMOND_CHESTPLATE] = DiamondChestplate::class;
|
||||
self::$list[self::DIAMOND_LEGGINGS] = DiamondLeggings::class;
|
||||
self::$list[self::DIAMOND_BOOTS] = DiamondBoots::class;
|
||||
self::$list[self::GOLDEN_HELMET] = GoldHelmet::class;
|
||||
self::$list[self::GOLDEN_CHESTPLATE] = GoldChestplate::class;
|
||||
self::$list[self::GOLDEN_LEGGINGS] = GoldLeggings::class;
|
||||
self::$list[self::GOLDEN_BOOTS] = GoldBoots::class;
|
||||
self::$list[self::FLINT] = Flint::class;
|
||||
self::$list[self::RAW_PORKCHOP] = RawPorkchop::class;
|
||||
self::$list[self::COOKED_PORKCHOP] = CookedPorkchop::class;
|
||||
self::$list[self::PAINTING] = Painting::class;
|
||||
self::$list[self::GOLDEN_APPLE] = GoldenApple::class;
|
||||
self::$list[self::SIGN] = Sign::class;
|
||||
self::$list[self::WOODEN_DOOR] = WoodenDoor::class;
|
||||
self::$list[self::BUCKET] = Bucket::class;
|
||||
|
||||
self::$list[self::MINECART] = Minecart::class;
|
||||
|
||||
self::$list[self::IRON_DOOR] = IronDoor::class;
|
||||
self::$list[self::REDSTONE] = Redstone::class;
|
||||
self::$list[self::SNOWBALL] = Snowball::class;
|
||||
self::$list[self::BOAT] = Boat::class;
|
||||
self::$list[self::LEATHER] = Leather::class;
|
||||
|
||||
self::$list[self::BRICK] = Brick::class;
|
||||
self::$list[self::CLAY] = Clay::class;
|
||||
self::$list[self::SUGARCANE] = Sugarcane::class;
|
||||
self::$list[self::PAPER] = Paper::class;
|
||||
self::$list[self::BOOK] = Book::class;
|
||||
self::$list[self::SLIMEBALL] = Slimeball::class;
|
||||
|
||||
self::$list[self::EGG] = Egg::class;
|
||||
self::$list[self::COMPASS] = Compass::class;
|
||||
self::$list[self::FISHING_ROD] = FishingRod::class;
|
||||
self::$list[self::CLOCK] = Clock::class;
|
||||
self::$list[self::GLOWSTONE_DUST] = GlowstoneDust::class;
|
||||
self::$list[self::RAW_FISH] = Fish::class;
|
||||
self::$list[self::COOKED_FISH] = CookedFish::class;
|
||||
self::$list[self::DYE] = Dye::class;
|
||||
self::$list[self::BONE] = Bone::class;
|
||||
self::$list[self::SUGAR] = Sugar::class;
|
||||
self::$list[self::CAKE] = Cake::class;
|
||||
self::$list[self::BED] = Bed::class;
|
||||
|
||||
self::$list[self::COOKIE] = Cookie::class;
|
||||
|
||||
self::$list[self::SHEARS] = Shears::class;
|
||||
self::$list[self::MELON] = Melon::class;
|
||||
self::$list[self::PUMPKIN_SEEDS] = PumpkinSeeds::class;
|
||||
self::$list[self::MELON_SEEDS] = MelonSeeds::class;
|
||||
self::$list[self::RAW_BEEF] = RawBeef::class;
|
||||
self::$list[self::STEAK] = Steak::class;
|
||||
self::$list[self::RAW_CHICKEN] = RawChicken::class;
|
||||
self::$list[self::COOKED_CHICKEN] = CookedChicken::class;
|
||||
|
||||
self::$list[self::GOLD_NUGGET] = GoldNugget::class;
|
||||
self::$list[self::NETHER_WART] = NetherWart::class;
|
||||
self::$list[self::POTION] = Potion::class;
|
||||
self::$list[self::GLASS_BOTTLE] = GlassBottle::class;
|
||||
self::$list[self::SPIDER_EYE] = SpiderEye::class;
|
||||
self::$list[self::FERMENTED_SPIDER_EYE] = FermentedSpiderEye::class;
|
||||
self::$list[self::BLAZE_POWDER] = BlazePowder::class;
|
||||
self::$list[self::MAGMA_CREAM] = MagmaCream::class;
|
||||
self::$list[self::BREWING_STAND] = BrewingStand::class;
|
||||
|
||||
self::$list[self::GLISTERING_MELON] = GlisteringMelon::class;
|
||||
self::$list[self::SPAWN_EGG] = SpawnEgg::class;
|
||||
|
||||
self::$list[self::EMERALD] = Emerald::class;
|
||||
self::$list[self::ITEM_FRAME] = ItemFrame::class;
|
||||
self::$list[self::FLOWER_POT] = FlowerPot::class;
|
||||
self::$list[self::CARROT] = Carrot::class;
|
||||
self::$list[self::POTATO] = Potato::class;
|
||||
self::$list[self::BAKED_POTATO] = BakedPotato::class;
|
||||
|
||||
self::$list[self::GOLDEN_CARROT] = GoldenCarrot::class;
|
||||
self::$list[self::SKULL] = Skull::class;
|
||||
|
||||
self::$list[self::NETHER_STAR] = NetherStar::class;
|
||||
self::$list[self::PUMPKIN_PIE] = PumpkinPie::class;
|
||||
|
||||
self::$list[self::NETHER_BRICK] = NetherBrick::class;
|
||||
self::$list[self::NETHER_QUARTZ] = NetherQuartz::class;
|
||||
|
||||
self::$list[self::PRISMARINE_SHARD] = PrismarineShard::class;
|
||||
|
||||
self::$list[self::COOKED_RABBIT] = CookedRabbit::class;
|
||||
|
||||
self::$list[self::PRISMARINE_CRYSTALS] = PrismarineCrystals::class;
|
||||
|
||||
self::$list[self::BEETROOT] = Beetroot::class;
|
||||
self::$list[self::BEETROOT_SEEDS] = BeetrootSeeds::class;
|
||||
self::$list[self::BEETROOT_SOUP] = BeetrootSoup::class;
|
||||
|
||||
self::$list[self::ENCHANTED_GOLDEN_APPLE] = GoldenAppleEnchanted::class;
|
||||
}
|
||||
|
||||
self::initCreativeItems();
|
||||
public static function get(int $id, int $meta = 0, int $count = 1, $tags = "") : Item{
|
||||
return ItemFactory::get($id, $meta, $count, $tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $str
|
||||
* @param bool $multiple
|
||||
*
|
||||
* @return Item[]|Item
|
||||
*/
|
||||
|
||||
public static function fromString(string $str, bool $multiple = false){
|
||||
return ItemFactory::fromString($str, $multiple);
|
||||
}
|
||||
|
||||
|
||||
/** @var Item[] */
|
||||
private static $creative = [];
|
||||
|
||||
private static function initCreativeItems(){
|
||||
public static function initCreativeItems(){
|
||||
self::clearCreativeItems();
|
||||
|
||||
$creativeItems = new Config(Server::getInstance()->getFilePath() . "src/pocketmine/resources/creativeitems.json", Config::JSON, []);
|
||||
@ -316,67 +166,20 @@ class Item implements ItemIds, \JsonSerializable{
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public static function get(int $id, int $meta = 0, int $count = 1, $tags = "") : Item{
|
||||
try{
|
||||
if($id < 256){
|
||||
return (new ItemBlock(BlockFactory::get($id, $meta), $meta, $count))->setCompoundTag($tags);
|
||||
}else{
|
||||
$class = self::$list[$id];
|
||||
if($class === null){
|
||||
return (new Item($id, $meta, $count))->setCompoundTag($tags);
|
||||
}else{
|
||||
return (new $class($meta, $count))->setCompoundTag($tags);
|
||||
}
|
||||
}
|
||||
}catch(\RuntimeException $e){
|
||||
return (new Item($id, $meta, $count))->setCompoundTag($tags);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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() === self::AIR and strtoupper($b[0]) !== "AIR"){
|
||||
$item = self::get($b[0] & 0xFFFF, $meta);
|
||||
}
|
||||
}else{
|
||||
$item = self::get($b[0] & 0xFFFF, $meta);
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
/** @var Block|null */
|
||||
protected $block;
|
||||
/** @var int */
|
||||
protected $id;
|
||||
/** @var int */
|
||||
protected $meta;
|
||||
/** @var string */
|
||||
private $tags = "";
|
||||
/** @var CompoundTag|null */
|
||||
private $cachedNBT = null;
|
||||
/** @var int */
|
||||
public $count;
|
||||
/** @var string */
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
@ -1061,7 +864,7 @@ class Item implements ItemIds, \JsonSerializable{
|
||||
* @return Item
|
||||
*/
|
||||
final public static function jsonDeserialize(array $data) : Item{
|
||||
return Item::get(
|
||||
return ItemFactory::get(
|
||||
(int) $data["id"],
|
||||
(int) $data["damage"],
|
||||
(int) $data["count"],
|
||||
@ -1105,16 +908,16 @@ class Item implements ItemIds, \JsonSerializable{
|
||||
*/
|
||||
public static function nbtDeserialize(CompoundTag $tag) : Item{
|
||||
if(!isset($tag->id) or !isset($tag->Count)){
|
||||
return Item::get(0);
|
||||
return ItemFactory::get(0);
|
||||
}
|
||||
|
||||
$count = Binary::unsignByte($tag->Count->getValue());
|
||||
$meta = isset($tag->Damage) ? $tag->Damage->getValue() : 0;
|
||||
|
||||
if($tag->id instanceof ShortTag){
|
||||
$item = Item::get($tag->id->getValue(), $meta, $count);
|
||||
$item = ItemFactory::get($tag->id->getValue(), $meta, $count);
|
||||
}elseif($tag->id instanceof StringTag){ //PC item save format
|
||||
$item = Item::fromString($tag->id->getValue());
|
||||
$item = ItemFactory::fromString($tag->id->getValue());
|
||||
$item->setDamage($meta);
|
||||
$item->setCount($count);
|
||||
}else{
|
||||
|
Reference in New Issue
Block a user