Skip to content

Commit

Permalink
Tile: Cleaned up utterly pointless overcomplicated code for inventories
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Feb 6, 2018
1 parent 8222b16 commit c4486d9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 114 deletions.
7 changes: 0 additions & 7 deletions src/pocketmine/tile/Chest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,6 @@ public function saveNBT() : void{
$this->saveItems();
}

/**
* @return int
*/
public function getSize() : int{
return 27;
}

/**
* @return ChestInventory|DoubleChestInventory
*/
Expand Down
18 changes: 0 additions & 18 deletions src/pocketmine/tile/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,6 @@
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
*/
Expand Down
93 changes: 11 additions & 82 deletions src/pocketmine/tile/ContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,101 +35,30 @@
*/
trait ContainerTrait{

/**
* @return int
*/
abstract public function getSize() : int;

abstract public function getNBT() : CompoundTag;

/**
* @return Inventory
*/
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));
}
}
7 changes: 0 additions & 7 deletions src/pocketmine/tile/Furnace.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ public function saveNBT() : void{
$this->saveItems();
}

/**
* @return int
*/
public function getSize() : int{
return 3;
}

/**
* @return FurnaceInventory
*/
Expand Down

0 comments on commit c4486d9

Please sign in to comment.