diff --git a/src/inventory/BaseInventory.php b/src/inventory/BaseInventory.php index a19b2b22c..e593893ce 100644 --- a/src/inventory/BaseInventory.php +++ b/src/inventory/BaseInventory.php @@ -59,6 +59,21 @@ abstract class BaseInventory implements Inventory{ $this->maxStackSize = $size; } + abstract protected function internalSetItem(int $index, Item $item) : void; + + public function setItem(int $index, Item $item) : void{ + if($item->isNull()){ + $item = VanillaItems::AIR(); + }else{ + $item = clone $item; + } + + $oldItem = $this->getItem($index); + + $this->internalSetItem($index, $item); + $this->onSlotChange($index, $oldItem); + } + /** * @param Item[] $items */ @@ -89,21 +104,6 @@ abstract class BaseInventory implements Inventory{ $this->onContentChange($oldContents); } - abstract protected function internalSetItem(int $index, Item $item) : void; - - public function setItem(int $index, Item $item) : void{ - if($item->isNull()){ - $item = VanillaItems::AIR(); - }else{ - $item = clone $item; - } - - $oldItem = $this->getItem($index); - - $this->internalSetItem($index, $item); - $this->onSlotChange($index, $oldItem); - } - public function contains(Item $item) : bool{ $count = max(1, $item->getCount()); $checkDamage = !$item->hasAnyDamageValue(); @@ -132,18 +132,6 @@ abstract class BaseInventory implements Inventory{ return $slots; } - - public function remove(Item $item) : void{ - $checkDamage = !$item->hasAnyDamageValue(); - $checkTags = $item->hasNamedTag(); - - foreach($this->getContents() as $index => $i){ - if($item->equals($i, $checkDamage, $checkTags)){ - $this->clear($index); - } - } - } - public function first(Item $item, bool $exact = false) : int{ $count = $exact ? $item->getCount() : max(1, $item->getCount()); $checkDamage = $exact || !$item->hasAnyDamageValue(); @@ -257,6 +245,17 @@ abstract class BaseInventory implements Inventory{ return $slot; } + public function remove(Item $item) : void{ + $checkDamage = !$item->hasAnyDamageValue(); + $checkTags = $item->hasNamedTag(); + + foreach($this->getContents() as $index => $i){ + if($item->equals($i, $checkDamage, $checkTags)){ + $this->clear($index); + } + } + } + public function removeItem(Item ...$slots) : array{ /** @var Item[] $itemSlots */ /** @var Item[] $slots */ diff --git a/src/inventory/Inventory.php b/src/inventory/Inventory.php index 8f2dab7ee..cbb09ed24 100644 --- a/src/inventory/Inventory.php +++ b/src/inventory/Inventory.php @@ -46,6 +46,16 @@ interface Inventory{ */ public function setItem(int $index, Item $item) : void; + /** + * @return Item[] + */ + public function getContents(bool $includeEmpty = false) : array; + + /** + * @param Item[] $items + */ + public function setContents(array $items) : void; + /** * Stores the given Items in the inventory. This will try to fill * existing stacks and empty slots as well as it can. @@ -66,16 +76,6 @@ interface Inventory{ */ public function getAddableItemQuantity(Item $item) : int; - /** - * @return Item[] - */ - public function getContents(bool $includeEmpty = false) : array; - - /** - * @param Item[] $items - */ - public function setContents(array $items) : void; - /** * Checks if the inventory contains any Item with the same material data. * It will check id, amount, and metadata (if not null) diff --git a/src/inventory/SimpleInventory.php b/src/inventory/SimpleInventory.php index 28b7d8b47..3f44a3994 100644 --- a/src/inventory/SimpleInventory.php +++ b/src/inventory/SimpleInventory.php @@ -52,6 +52,10 @@ class SimpleInventory extends BaseInventory{ return $this->slots[$index] !== null ? clone $this->slots[$index] : VanillaItems::AIR(); } + protected function internalSetItem(int $index, Item $item) : void{ + $this->slots[$index] = $item->isNull() ? null : $item; + } + /** * @return Item[] */ @@ -78,8 +82,4 @@ class SimpleInventory extends BaseInventory{ } } } - - protected function internalSetItem(int $index, Item $item) : void{ - $this->slots[$index] = $item->isNull() ? null : $item; - } }