Merge changes from master

This commit is contained in:
Dylan K. Taylor
2017-08-22 14:13:31 +01:00
131 changed files with 690 additions and 505 deletions

View File

@ -42,6 +42,6 @@ class BeetrootSoup extends Food{
}
public function getResidue(){
return Item::get(Item::BOWL);
return ItemFactory::get(Item::BOWL);
}
}

View File

@ -25,7 +25,6 @@ namespace pocketmine\item;
use pocketmine\block\Block;
use pocketmine\block\BlockFactory;
use pocketmine\block\Fire;
use pocketmine\block\Solid;
use pocketmine\level\Level;
use pocketmine\math\Vector3;

View File

@ -40,7 +40,7 @@ abstract class Food extends Item implements FoodSource{
public function getResidue(){
if($this->getCount() === 1){
return Item::get(0);
return ItemFactory::get(0);
}else{
$new = clone $this;
$new->count--;

View File

@ -76,185 +76,41 @@ 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;
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();
/**
* Returns a new Item instance with the specified ID, damage, count and NBT.
*
* This function redirects to {@link ItemFactory#get}.
*
* @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{
return ItemFactory::get($id, $meta, $count, $tags);
}
/**
* Tries to parse the specified string into Item ID/meta identifiers, and returns Item instances it created.
*
* This function redirects to {@link ItemFactory#fromString}.
*
* @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 +172,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
@ -729,6 +538,7 @@ class Item implements ItemIds, \JsonSerializable{
$tag = $this->getNamedTagEntry("display");
if($tag instanceof CompoundTag and isset($tag->Lore) and $tag->Lore instanceof ListTag){
$lines = [];
/** @var StringTag $line */
foreach($tag->Lore->getValue() as $line){
$lines[] = $line->getValue();
}
@ -1064,7 +874,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"],
@ -1108,16 +918,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{
@ -1125,6 +935,7 @@ class Item implements ItemIds, \JsonSerializable{
}
if(isset($tag->tag) and $tag->tag instanceof CompoundTag){
/** @var CompoundTag $t */
$t = clone $tag->tag;
$t->setName("");
$item->setNamedTag($t);

View File

@ -0,0 +1,295 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\block\BlockFactory;
use pocketmine\nbt\tag\CompoundTag;
/**
* Manages Item instance creation and registration
*/
class ItemFactory{
/** @var \SplFixedArray */
public static $list = null;
public static function init(){
if(self::$list === null){
self::$list = new \SplFixedArray(65536);
self::registerItem(new IronShovel());
self::registerItem(new IronPickaxe());
self::registerItem(new IronAxe());
self::registerItem(new FlintSteel());
self::registerItem(new Apple());
self::registerItem(new Bow());
self::registerItem(new Arrow());
self::registerItem(new Coal());
self::registerItem(new Diamond());
self::registerItem(new IronIngot());
self::registerItem(new GoldIngot());
self::registerItem(new IronSword());
self::registerItem(new WoodenSword());
self::registerItem(new WoodenShovel());
self::registerItem(new WoodenPickaxe());
self::registerItem(new WoodenAxe());
self::registerItem(new StoneSword());
self::registerItem(new StoneShovel());
self::registerItem(new StonePickaxe());
self::registerItem(new StoneAxe());
self::registerItem(new DiamondSword());
self::registerItem(new DiamondShovel());
self::registerItem(new DiamondPickaxe());
self::registerItem(new DiamondAxe());
self::registerItem(new Stick());
self::registerItem(new Bowl());
self::registerItem(new MushroomStew());
self::registerItem(new GoldSword());
self::registerItem(new GoldShovel());
self::registerItem(new GoldPickaxe());
self::registerItem(new GoldAxe());
self::registerItem(new StringItem());
self::registerItem(new Feather());
self::registerItem(new Gunpowder());
self::registerItem(new WoodenHoe());
self::registerItem(new StoneHoe());
self::registerItem(new IronHoe());
self::registerItem(new DiamondHoe());
self::registerItem(new GoldHoe());
self::registerItem(new WheatSeeds());
self::registerItem(new Wheat());
self::registerItem(new Bread());
self::registerItem(new LeatherCap());
self::registerItem(new LeatherTunic());
self::registerItem(new LeatherPants());
self::registerItem(new LeatherBoots());
self::registerItem(new ChainHelmet());
self::registerItem(new ChainChestplate());
self::registerItem(new ChainLeggings());
self::registerItem(new ChainBoots());
self::registerItem(new IronHelmet());
self::registerItem(new IronChestplate());
self::registerItem(new IronLeggings());
self::registerItem(new IronBoots());
self::registerItem(new DiamondHelmet());
self::registerItem(new DiamondChestplate());
self::registerItem(new DiamondLeggings());
self::registerItem(new DiamondBoots());
self::registerItem(new GoldHelmet());
self::registerItem(new GoldChestplate());
self::registerItem(new GoldLeggings());
self::registerItem(new GoldBoots());
self::registerItem(new Flint());
self::registerItem(new RawPorkchop());
self::registerItem(new CookedPorkchop());
self::registerItem(new Painting());
self::registerItem(new GoldenApple());
self::registerItem(new Sign());
self::registerItem(new WoodenDoor());
self::registerItem(new Bucket());
self::registerItem(new Minecart());
self::registerItem(new IronDoor());
self::registerItem(new Redstone());
self::registerItem(new Snowball());
self::registerItem(new Boat());
self::registerItem(new Leather());
self::registerItem(new Brick());
self::registerItem(new Clay());
self::registerItem(new Sugarcane());
self::registerItem(new Paper());
self::registerItem(new Book());
self::registerItem(new Slimeball());
self::registerItem(new Egg());
self::registerItem(new Compass());
self::registerItem(new FishingRod());
self::registerItem(new Clock());
self::registerItem(new GlowstoneDust());
self::registerItem(new Fish());
self::registerItem(new CookedFish());
self::registerItem(new Dye());
self::registerItem(new Bone());
self::registerItem(new Sugar());
self::registerItem(new Cake());
self::registerItem(new Bed());
self::registerItem(new Cookie());
self::registerItem(new Shears());
self::registerItem(new Melon());
self::registerItem(new PumpkinSeeds());
self::registerItem(new MelonSeeds());
self::registerItem(new RawBeef());
self::registerItem(new Steak());
self::registerItem(new RawChicken());
self::registerItem(new CookedChicken());
self::registerItem(new GoldNugget());
self::registerItem(new NetherWart());
self::registerItem(new Potion());
self::registerItem(new GlassBottle());
self::registerItem(new SpiderEye());
self::registerItem(new FermentedSpiderEye());
self::registerItem(new BlazePowder());
self::registerItem(new MagmaCream());
self::registerItem(new BrewingStand());
self::registerItem(new GlisteringMelon());
self::registerItem(new SpawnEgg());
self::registerItem(new Emerald());
self::registerItem(new ItemFrame());
self::registerItem(new FlowerPot());
self::registerItem(new Carrot());
self::registerItem(new Potato());
self::registerItem(new BakedPotato());
self::registerItem(new GoldenCarrot());
self::registerItem(new Skull());
self::registerItem(new NetherStar());
self::registerItem(new PumpkinPie());
self::registerItem(new NetherBrick());
self::registerItem(new NetherQuartz());
self::registerItem(new PrismarineShard());
self::registerItem(new CookedRabbit());
self::registerItem(new PrismarineCrystals());
self::registerItem(new Beetroot());
self::registerItem(new BeetrootSeeds());
self::registerItem(new BeetrootSoup());
self::registerItem(new GoldenAppleEnchanted());
}
Item::initCreativeItems();
}
/**
* Registers an item type into the index. Plugins may use this method to register new item types or override existing
* ones.
*
* NOTE: If you are registering a new item type, you will need to add it to the creative inventory yourself - it
* will not automatically appear there.
*
* @param Item $item
* @param bool $override
*
* @throws \RuntimeException if something attempted to override an already-registered item without specifying the
* $override parameter.
*/
public static function registerItem(Item $item, bool $override = false){
$id = $item->getId();
if(!$override and self::$list[$id] !== null){
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
*/
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{
/** @var Item|null $item */
$item = self::$list[$id];
if($item === null){
return (new Item($id, $meta, $count))->setCompoundTag($tags);
}else{
$item = clone $item;
$item->setDamage($meta);
$item->setCount($count);
$item->setCompoundTag($tags);
return $item;
}
}
}catch(\RuntimeException $e){
return (new Item($id, $meta, $count))->setCompoundTag($tags);
}
}
/**
* 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"){
$item = self::get($b[0] & 0xFFFF, $meta);
}
}else{
$item = self::get($b[0] & 0xFFFF, $meta);
}
return $item;
}
}
}

View File

@ -41,6 +41,6 @@ class MushroomStew extends Food{
}
public function getResidue(){
return Item::get(Item::BOWL);
return ItemFactory::get(Item::BOWL);
}
}