mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-11 12:27:51 +00:00
Reduce chaos in inventory classes
This commit is contained in:
parent
2c29d272ad
commit
bf71eb448a
@ -59,6 +59,21 @@ abstract class BaseInventory implements Inventory{
|
|||||||
$this->maxStackSize = $size;
|
$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
|
* @param Item[] $items
|
||||||
*/
|
*/
|
||||||
@ -89,21 +104,6 @@ abstract class BaseInventory implements Inventory{
|
|||||||
$this->onContentChange($oldContents);
|
$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{
|
public function contains(Item $item) : bool{
|
||||||
$count = max(1, $item->getCount());
|
$count = max(1, $item->getCount());
|
||||||
$checkDamage = !$item->hasAnyDamageValue();
|
$checkDamage = !$item->hasAnyDamageValue();
|
||||||
@ -132,18 +132,6 @@ abstract class BaseInventory implements Inventory{
|
|||||||
|
|
||||||
return $slots;
|
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{
|
public function first(Item $item, bool $exact = false) : int{
|
||||||
$count = $exact ? $item->getCount() : max(1, $item->getCount());
|
$count = $exact ? $item->getCount() : max(1, $item->getCount());
|
||||||
$checkDamage = $exact || !$item->hasAnyDamageValue();
|
$checkDamage = $exact || !$item->hasAnyDamageValue();
|
||||||
@ -257,6 +245,17 @@ abstract class BaseInventory implements Inventory{
|
|||||||
return $slot;
|
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{
|
public function removeItem(Item ...$slots) : array{
|
||||||
/** @var Item[] $itemSlots */
|
/** @var Item[] $itemSlots */
|
||||||
/** @var Item[] $slots */
|
/** @var Item[] $slots */
|
||||||
|
@ -46,6 +46,16 @@ interface Inventory{
|
|||||||
*/
|
*/
|
||||||
public function setItem(int $index, Item $item) : void;
|
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
|
* Stores the given Items in the inventory. This will try to fill
|
||||||
* existing stacks and empty slots as well as it can.
|
* existing stacks and empty slots as well as it can.
|
||||||
@ -66,16 +76,6 @@ interface Inventory{
|
|||||||
*/
|
*/
|
||||||
public function getAddableItemQuantity(Item $item) : int;
|
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.
|
* Checks if the inventory contains any Item with the same material data.
|
||||||
* It will check id, amount, and metadata (if not null)
|
* It will check id, amount, and metadata (if not null)
|
||||||
|
@ -52,6 +52,10 @@ class SimpleInventory extends BaseInventory{
|
|||||||
return $this->slots[$index] !== null ? clone $this->slots[$index] : VanillaItems::AIR();
|
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[]
|
* @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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user