mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-21 10:26:38 +00:00
Tile: Cleaned up utterly pointless overcomplicated code for inventories
This commit is contained in:
parent
8222b16d9a
commit
c4486d9ad7
@ -73,13 +73,6 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
|
||||
$this->saveItems();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSize() : int{
|
||||
return 27;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ChestInventory|DoubleChestInventory
|
||||
*/
|
||||
|
@ -29,24 +29,6 @@ use pocketmine\item\Item;
|
||||
interface Container{
|
||||
public const TAG_ITEMS = "Items";
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function getItem(int $index) : Item;
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
* @param Item $item
|
||||
*/
|
||||
public function setItem(int $index, Item $item);
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSize() : int;
|
||||
|
||||
/**
|
||||
* @return Inventory
|
||||
*/
|
||||
|
@ -35,11 +35,6 @@ use pocketmine\nbt\tag\ListTag;
|
||||
*/
|
||||
trait ContainerTrait{
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
abstract public function getSize() : int;
|
||||
|
||||
abstract public function getNBT() : CompoundTag;
|
||||
|
||||
/**
|
||||
@ -47,89 +42,23 @@ trait ContainerTrait{
|
||||
*/
|
||||
abstract public function getRealInventory();
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getSlotIndex(int $index) : int{
|
||||
foreach($this->getNBT()->getListTag(Container::TAG_ITEMS) as $i => $slot){
|
||||
/** @var CompoundTag $slot */
|
||||
if($slot->getByte("Slot") === $index){
|
||||
return (int) $i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should not be used by plugins, use the Inventory
|
||||
*
|
||||
* @param int $index
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function getItem(int $index) : Item{
|
||||
$i = $this->getSlotIndex($index);
|
||||
/** @var CompoundTag|null $itemTag */
|
||||
$itemTag = $this->getNBT()->getListTag(Container::TAG_ITEMS)[$i] ?? null;
|
||||
if($itemTag !== null){
|
||||
return Item::nbtDeserialize($itemTag);
|
||||
}
|
||||
|
||||
return ItemFactory::get(Item::AIR, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should not be used by plugins, use the Inventory
|
||||
*
|
||||
* @param int $index
|
||||
* @param Item $item
|
||||
*/
|
||||
public function setItem(int $index, Item $item) : void{
|
||||
$i = $this->getSlotIndex($index);
|
||||
|
||||
$d = $item->nbtSerialize($index);
|
||||
|
||||
$items = $this->getNBT()->getListTag(Container::TAG_ITEMS);
|
||||
assert($items instanceof ListTag);
|
||||
|
||||
if($item->isNull()){
|
||||
if($i >= 0){
|
||||
unset($items[$i]);
|
||||
}
|
||||
}elseif($i < 0){
|
||||
for($i = 0; $i <= $this->getSize(); ++$i){
|
||||
if(!isset($items[$i])){
|
||||
break;
|
||||
}
|
||||
}
|
||||
$items[$i] = $d;
|
||||
}else{
|
||||
$items[$i] = $d;
|
||||
}
|
||||
|
||||
$this->getNBT()->setTag($items);
|
||||
}
|
||||
|
||||
protected function loadItems() : void{
|
||||
if(!$this->getNBT()->hasTag(Container::TAG_ITEMS, ListTag::class)){
|
||||
$this->getNBT()->setTag(new ListTag(Container::TAG_ITEMS, [], NBT::TAG_Compound));
|
||||
}
|
||||
if($this->getNBT()->hasTag(Container::TAG_ITEMS, ListTag::class)){
|
||||
$inventoryTag = $this->getNBT()->getListTag(Container::TAG_ITEMS);
|
||||
|
||||
$inventory = $this->getRealInventory();
|
||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||
$inventory->setItem($i, $this->getItem($i));
|
||||
$inventory = $this->getRealInventory();
|
||||
foreach($inventoryTag as $itemNBT){
|
||||
$inventory->setItem($itemNBT->getByte("Slot"), Item::nbtDeserialize($itemNBT));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function saveItems() : void{
|
||||
$this->getNBT()->setTag(new ListTag(Container::TAG_ITEMS, [], NBT::TAG_Compound));
|
||||
|
||||
$inventory = $this->getRealInventory();
|
||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||
$this->setItem($i, $inventory->getItem($i));
|
||||
$items = [];
|
||||
foreach($this->getRealInventory()->getContents() as $slot => $item){
|
||||
$items[] = $item->nbtSerialize($slot);
|
||||
}
|
||||
|
||||
$this->getNBT()->setTag(new ListTag(Container::TAG_ITEMS, $items, NBT::TAG_Compound));
|
||||
}
|
||||
}
|
||||
|
@ -104,13 +104,6 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
||||
$this->saveItems();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSize() : int{
|
||||
return 3;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return FurnaceInventory
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user