Item factory refactor and added capability to register custom items

This commit is contained in:
Dylan K. Taylor
2017-08-20 19:11:21 +01:00
parent 604d11a8fd
commit 876659cc73
66 changed files with 491 additions and 347 deletions

View File

@ -27,6 +27,7 @@ use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityInventoryChangeEvent;
use pocketmine\event\inventory\InventoryOpenEvent;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\network\mcpe\protocol\ContainerSetContentPacket;
use pocketmine\network\mcpe\protocol\ContainerSetSlotPacket;
use pocketmine\Player;
@ -106,7 +107,7 @@ abstract class BaseInventory implements Inventory{
public function getItem(int $index) : Item{
assert($index >= 0, "Inventory slot should not be negative");
return isset($this->slots[$index]) ? clone $this->slots[$index] : Item::get(Item::AIR, 0, 0);
return isset($this->slots[$index]) ? clone $this->slots[$index] : ItemFactory::get(Item::AIR, 0, 0);
}
public function getContents() : array{
@ -340,7 +341,7 @@ abstract class BaseInventory implements Inventory{
public function clear(int $index) : bool{
if(isset($this->slots[$index])){
$item = Item::get(Item::AIR, 0, 0);
$item = ItemFactory::get(Item::AIR, 0, 0);
$old = $this->slots[$index];
$holder = $this->getHolder();
if($holder instanceof Entity){

View File

@ -25,6 +25,7 @@ namespace pocketmine\inventory;
use pocketmine\event\Timings;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\network\mcpe\protocol\CraftingDataPacket;
use pocketmine\Server;
use pocketmine\utils\Config;
@ -81,7 +82,7 @@ class CraftingManager{
case 3:
$result = $recipe["output"];
$resultItem = Item::jsonDeserialize($result);
$this->registerRecipe(new FurnaceRecipe($resultItem, Item::get($recipe["inputId"], $recipe["inputDamage"] ?? -1, 1)));
$this->registerRecipe(new FurnaceRecipe($resultItem, ItemFactory::get($recipe["inputId"], $recipe["inputDamage"] ?? -1, 1)));
break;
default:
break;

View File

@ -28,6 +28,7 @@ use pocketmine\event\entity\EntityArmorChangeEvent;
use pocketmine\event\entity\EntityInventoryChangeEvent;
use pocketmine\event\player\PlayerItemHeldEvent;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\network\mcpe\protocol\ContainerSetContentPacket;
use pocketmine\network\mcpe\protocol\ContainerSetSlotPacket;
use pocketmine\network\mcpe\protocol\MobArmorEquipmentPacket;
@ -76,7 +77,7 @@ class PlayerInventory extends BaseInventory{
}
if($inventorySlot === -1){
$item = Item::get(Item::AIR, 0, 0);
$item = ItemFactory::get(Item::AIR, 0, 0);
}else{
$item = $this->getItem($inventorySlot);
}
@ -143,7 +144,7 @@ class PlayerInventory extends BaseInventory{
if($inventorySlot !== -1){
return $this->getItem($inventorySlot);
}else{
return Item::get(Item::AIR, 0, 0);
return ItemFactory::get(Item::AIR, 0, 0);
}
}
@ -350,7 +351,7 @@ class PlayerInventory extends BaseInventory{
public function clear(int $index) : bool{
if(isset($this->slots[$index])){
$item = Item::get(Item::AIR, 0, 0);
$item = ItemFactory::get(Item::AIR, 0, 0);
$old = $this->slots[$index];
if($index >= $this->getSize() and $index < $this->size){ //Armor change
Server::getInstance()->getPluginManager()->callEvent($ev = new EntityArmorChangeEvent($this->getHolder(), $old, $item, $index));
@ -441,7 +442,7 @@ class PlayerInventory extends BaseInventory{
public function setArmorContents(array $items){
for($i = 0; $i < 4; ++$i){
if(!isset($items[$i]) or !($items[$i] instanceof Item)){
$items[$i] = Item::get(Item::AIR, 0, 0);
$items[$i] = ItemFactory::get(Item::AIR, 0, 0);
}
if($items[$i]->getId() === Item::AIR){
@ -500,7 +501,7 @@ class PlayerInventory extends BaseInventory{
//Because PE is stupid and shows 9 less slots than you send it, give it 9 dummy slots so it shows all the REAL slots.
for($i = $this->getSize(); $i < $this->getSize() + $this->getHotbarSize(); ++$i){
$pk->slots[$i] = Item::get(Item::AIR, 0, 0);
$pk->slots[$i] = ItemFactory::get(Item::AIR, 0, 0);
}
foreach($target as $player){

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\math\Vector2;
use pocketmine\Server;
use pocketmine\utils\UUID;
@ -141,7 +142,7 @@ class ShapedRecipe implements Recipe{
if($ingredient !== null){
$ingredients[$y][$x] = clone $ingredient;
}else{
$ingredients[$y][$x] = Item::get(Item::AIR);
$ingredients[$y][$x] = ItemFactory::get(Item::AIR);
}
}
}
@ -156,7 +157,7 @@ class ShapedRecipe implements Recipe{
* @return Item
*/
public function getIngredient(int $x, int $y) : Item{
return $this->ingredients[$y][$x] ?? Item::get(Item::AIR);
return $this->ingredients[$y][$x] ?? ItemFactory::get(Item::AIR);
}
/**