mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-12 14:35:35 +00:00
Extract inventory helper methods from BaseInventory to a trait
this allows easier alternate implementations of Inventory that don't extend BaseInventory (e.g. double chest, or delegate inventory).
This commit is contained in:
parent
f538440bce
commit
9b8f32c584
@ -30,11 +30,10 @@ use pocketmine\utils\ObjectSet;
|
|||||||
use function array_map;
|
use function array_map;
|
||||||
use function array_slice;
|
use function array_slice;
|
||||||
use function count;
|
use function count;
|
||||||
use function max;
|
|
||||||
use function min;
|
|
||||||
use function spl_object_id;
|
use function spl_object_id;
|
||||||
|
|
||||||
abstract class BaseInventory implements Inventory{
|
abstract class BaseInventory implements Inventory{
|
||||||
|
use InventoryHelpersTrait;
|
||||||
|
|
||||||
/** @var int */
|
/** @var int */
|
||||||
protected $maxStackSize = Inventory::MAX_STACK;
|
protected $maxStackSize = Inventory::MAX_STACK;
|
||||||
@ -140,202 +139,6 @@ abstract class BaseInventory implements Inventory{
|
|||||||
$this->onSlotChange($index, $oldItem);
|
$this->onSlotChange($index, $oldItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function contains(Item $item) : bool{
|
|
||||||
$count = max(1, $item->getCount());
|
|
||||||
$checkDamage = !$item->hasAnyDamageValue();
|
|
||||||
$checkTags = $item->hasNamedTag();
|
|
||||||
foreach($this->getContents() as $i){
|
|
||||||
if($item->equals($i, $checkDamage, $checkTags)){
|
|
||||||
$count -= $i->getCount();
|
|
||||||
if($count <= 0){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function all(Item $item) : array{
|
|
||||||
$slots = [];
|
|
||||||
$checkDamage = !$item->hasAnyDamageValue();
|
|
||||||
$checkTags = $item->hasNamedTag();
|
|
||||||
foreach($this->getContents() as $index => $i){
|
|
||||||
if($item->equals($i, $checkDamage, $checkTags)){
|
|
||||||
$slots[$index] = $i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
$checkTags = $exact || $item->hasNamedTag();
|
|
||||||
|
|
||||||
foreach($this->getContents() as $index => $i){
|
|
||||||
if($item->equals($i, $checkDamage, $checkTags) and ($i->getCount() === $count or (!$exact and $i->getCount() > $count))){
|
|
||||||
return $index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function firstEmpty() : int{
|
|
||||||
foreach($this->slots as $i => $slot){
|
|
||||||
if($slot === null or $slot->isNull()){
|
|
||||||
return $i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isSlotEmpty(int $index) : bool{
|
|
||||||
return $this->slots[$index] === null or $this->slots[$index]->isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function canAddItem(Item $item) : bool{
|
|
||||||
$count = $item->getCount();
|
|
||||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
|
||||||
$slot = $this->getItem($i);
|
|
||||||
if($item->equals($slot)){
|
|
||||||
if(($diff = min($slot->getMaxStackSize(), $item->getMaxStackSize()) - $slot->getCount()) > 0){
|
|
||||||
$count -= $diff;
|
|
||||||
}
|
|
||||||
}elseif($slot->isNull()){
|
|
||||||
$count -= min($this->getMaxStackSize(), $item->getMaxStackSize());
|
|
||||||
}
|
|
||||||
|
|
||||||
if($count <= 0){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addItem(Item ...$slots) : array{
|
|
||||||
/** @var Item[] $itemSlots */
|
|
||||||
/** @var Item[] $slots */
|
|
||||||
$itemSlots = [];
|
|
||||||
foreach($slots as $slot){
|
|
||||||
if(!$slot->isNull()){
|
|
||||||
$itemSlots[] = clone $slot;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$emptySlots = [];
|
|
||||||
|
|
||||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
|
||||||
$item = $this->getItem($i);
|
|
||||||
if($item->isNull()){
|
|
||||||
$emptySlots[] = $i;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach($itemSlots as $index => $slot){
|
|
||||||
if($slot->equals($item) and $item->getCount() < $item->getMaxStackSize()){
|
|
||||||
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
|
|
||||||
if($amount > 0){
|
|
||||||
$slot->setCount($slot->getCount() - $amount);
|
|
||||||
$item->setCount($item->getCount() + $amount);
|
|
||||||
$this->setItem($i, $item);
|
|
||||||
if($slot->getCount() <= 0){
|
|
||||||
unset($itemSlots[$index]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(count($itemSlots) === 0){
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(count($itemSlots) > 0 and count($emptySlots) > 0){
|
|
||||||
foreach($emptySlots as $slotIndex){
|
|
||||||
//This loop only gets the first item, then goes to the next empty slot
|
|
||||||
foreach($itemSlots as $index => $slot){
|
|
||||||
$amount = min($slot->getMaxStackSize(), $slot->getCount(), $this->getMaxStackSize());
|
|
||||||
$slot->setCount($slot->getCount() - $amount);
|
|
||||||
$item = clone $slot;
|
|
||||||
$item->setCount($amount);
|
|
||||||
$this->setItem($slotIndex, $item);
|
|
||||||
if($slot->getCount() <= 0){
|
|
||||||
unset($itemSlots[$index]);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $itemSlots;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function removeItem(Item ...$slots) : array{
|
|
||||||
/** @var Item[] $itemSlots */
|
|
||||||
/** @var Item[] $slots */
|
|
||||||
$itemSlots = [];
|
|
||||||
foreach($slots as $slot){
|
|
||||||
if(!$slot->isNull()){
|
|
||||||
$itemSlots[] = clone $slot;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
|
||||||
$item = $this->getItem($i);
|
|
||||||
if($item->isNull()){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach($itemSlots as $index => $slot){
|
|
||||||
if($slot->equals($item, !$slot->hasAnyDamageValue(), $slot->hasNamedTag())){
|
|
||||||
$amount = min($item->getCount(), $slot->getCount());
|
|
||||||
$slot->setCount($slot->getCount() - $amount);
|
|
||||||
$item->setCount($item->getCount() - $amount);
|
|
||||||
$this->setItem($i, $item);
|
|
||||||
if($slot->getCount() <= 0){
|
|
||||||
unset($itemSlots[$index]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(count($itemSlots) === 0){
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $itemSlots;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function clear(int $index) : void{
|
|
||||||
$this->setItem($index, ItemFactory::air());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function clearAll() : void{
|
|
||||||
$this->setContents([]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function swap(int $slot1, int $slot2) : void{
|
|
||||||
$i1 = $this->getItem($slot1);
|
|
||||||
$i2 = $this->getItem($slot2);
|
|
||||||
$this->setItem($slot1, $i2);
|
|
||||||
$this->setItem($slot2, $i1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Player[]
|
* @return Player[]
|
||||||
*/
|
*/
|
||||||
|
244
src/inventory/InventoryHelpersTrait.php
Normal file
244
src/inventory/InventoryHelpersTrait.php
Normal file
@ -0,0 +1,244 @@
|
|||||||
|
<?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\inventory;
|
||||||
|
|
||||||
|
use pocketmine\item\Item;
|
||||||
|
use pocketmine\item\ItemFactory;
|
||||||
|
use function count;
|
||||||
|
use function max;
|
||||||
|
use function min;
|
||||||
|
|
||||||
|
trait InventoryHelpersTrait{
|
||||||
|
|
||||||
|
abstract public function getMaxStackSize() : int;
|
||||||
|
abstract public function getSize() : int;
|
||||||
|
abstract public function getItem(int $index) : Item;
|
||||||
|
abstract public function setItem(int $index, Item $item) : void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Item[]
|
||||||
|
*/
|
||||||
|
abstract public function getContents(bool $includeEmpty = false) : array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Item[] $items
|
||||||
|
*/
|
||||||
|
abstract public function setContents(array $items) : void;
|
||||||
|
|
||||||
|
public function contains(Item $item) : bool{
|
||||||
|
$count = max(1, $item->getCount());
|
||||||
|
$checkDamage = !$item->hasAnyDamageValue();
|
||||||
|
$checkTags = $item->hasNamedTag();
|
||||||
|
foreach($this->getContents() as $i){
|
||||||
|
if($item->equals($i, $checkDamage, $checkTags)){
|
||||||
|
$count -= $i->getCount();
|
||||||
|
if($count <= 0){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function all(Item $item) : array{
|
||||||
|
$slots = [];
|
||||||
|
$checkDamage = !$item->hasAnyDamageValue();
|
||||||
|
$checkTags = $item->hasNamedTag();
|
||||||
|
foreach($this->getContents() as $index => $i){
|
||||||
|
if($item->equals($i, $checkDamage, $checkTags)){
|
||||||
|
$slots[$index] = $i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
$checkTags = $exact || $item->hasNamedTag();
|
||||||
|
|
||||||
|
foreach($this->getContents() as $index => $i){
|
||||||
|
if($item->equals($i, $checkDamage, $checkTags) and ($i->getCount() === $count or (!$exact and $i->getCount() > $count))){
|
||||||
|
return $index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function firstEmpty() : int{
|
||||||
|
foreach($this->getContents(true) as $i => $slot){
|
||||||
|
if($slot === null or $slot->isNull()){
|
||||||
|
return $i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isSlotEmpty(int $index) : bool{
|
||||||
|
return $this->getItem($index)->isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canAddItem(Item $item) : bool{
|
||||||
|
$count = $item->getCount();
|
||||||
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
|
$slot = $this->getItem($i);
|
||||||
|
if($item->equals($slot)){
|
||||||
|
if(($diff = min($slot->getMaxStackSize(), $item->getMaxStackSize()) - $slot->getCount()) > 0){
|
||||||
|
$count -= $diff;
|
||||||
|
}
|
||||||
|
}elseif($slot->isNull()){
|
||||||
|
$count -= min($this->getMaxStackSize(), $item->getMaxStackSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
if($count <= 0){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addItem(Item ...$slots) : array{
|
||||||
|
/** @var Item[] $itemSlots */
|
||||||
|
/** @var Item[] $slots */
|
||||||
|
$itemSlots = [];
|
||||||
|
foreach($slots as $slot){
|
||||||
|
if(!$slot->isNull()){
|
||||||
|
$itemSlots[] = clone $slot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$emptySlots = [];
|
||||||
|
|
||||||
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
|
$item = $this->getItem($i);
|
||||||
|
if($item->isNull()){
|
||||||
|
$emptySlots[] = $i;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($itemSlots as $index => $slot){
|
||||||
|
if($slot->equals($item) and $item->getCount() < $item->getMaxStackSize()){
|
||||||
|
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
|
||||||
|
if($amount > 0){
|
||||||
|
$slot->setCount($slot->getCount() - $amount);
|
||||||
|
$item->setCount($item->getCount() + $amount);
|
||||||
|
$this->setItem($i, $item);
|
||||||
|
if($slot->getCount() <= 0){
|
||||||
|
unset($itemSlots[$index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($itemSlots) === 0){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($itemSlots) > 0 and count($emptySlots) > 0){
|
||||||
|
foreach($emptySlots as $slotIndex){
|
||||||
|
//This loop only gets the first item, then goes to the next empty slot
|
||||||
|
foreach($itemSlots as $index => $slot){
|
||||||
|
$amount = min($slot->getMaxStackSize(), $slot->getCount(), $this->getMaxStackSize());
|
||||||
|
$slot->setCount($slot->getCount() - $amount);
|
||||||
|
$item = clone $slot;
|
||||||
|
$item->setCount($amount);
|
||||||
|
$this->setItem($slotIndex, $item);
|
||||||
|
if($slot->getCount() <= 0){
|
||||||
|
unset($itemSlots[$index]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $itemSlots;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeItem(Item ...$slots) : array{
|
||||||
|
/** @var Item[] $itemSlots */
|
||||||
|
/** @var Item[] $slots */
|
||||||
|
$itemSlots = [];
|
||||||
|
foreach($slots as $slot){
|
||||||
|
if(!$slot->isNull()){
|
||||||
|
$itemSlots[] = clone $slot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
|
$item = $this->getItem($i);
|
||||||
|
if($item->isNull()){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($itemSlots as $index => $slot){
|
||||||
|
if($slot->equals($item, !$slot->hasAnyDamageValue(), $slot->hasNamedTag())){
|
||||||
|
$amount = min($item->getCount(), $slot->getCount());
|
||||||
|
$slot->setCount($slot->getCount() - $amount);
|
||||||
|
$item->setCount($item->getCount() - $amount);
|
||||||
|
$this->setItem($i, $item);
|
||||||
|
if($slot->getCount() <= 0){
|
||||||
|
unset($itemSlots[$index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($itemSlots) === 0){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $itemSlots;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clear(int $index) : void{
|
||||||
|
$this->setItem($index, ItemFactory::air());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clearAll() : void{
|
||||||
|
$this->setContents([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function swap(int $slot1, int $slot2) : void{
|
||||||
|
$i1 = $this->getItem($slot1);
|
||||||
|
$i2 = $this->getItem($slot2);
|
||||||
|
$this->setItem($slot1, $i2);
|
||||||
|
$this->setItem($slot2, $i1);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user