mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 09:56:06 +00:00
Added ContainerTrait, reduce copy-pasted code in Tile
This commit is contained in:
@ -42,7 +42,7 @@ use pocketmine\network\mcpe\protocol\ContainerSetDataPacket;
|
||||
use pocketmine\Player;
|
||||
|
||||
class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
||||
use NameableTrait;
|
||||
use NameableTrait, ContainerTrait;
|
||||
|
||||
const TAG_BURN_TIME = "BurnTime";
|
||||
const TAG_COOK_TIME = "CookTime";
|
||||
@ -76,14 +76,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
||||
|
||||
parent::__construct($level, $nbt);
|
||||
$this->inventory = new FurnaceInventory($this);
|
||||
|
||||
if(!($this->namedtag->getTag("Items") instanceof ListTag)){
|
||||
$this->namedtag->setTag(new ListTag("Items", [], NBT::TAG_Compound));
|
||||
}
|
||||
|
||||
for($i = 0; $i < $this->getSize(); ++$i){
|
||||
$this->inventory->setItem($i, $this->getItem($i), false);
|
||||
}
|
||||
$this->loadItems();
|
||||
|
||||
if($this->namedtag->getShort(self::TAG_BURN_TIME) > 0){
|
||||
$this->scheduleUpdate();
|
||||
@ -108,10 +101,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
||||
|
||||
public function saveNBT() : void{
|
||||
parent::saveNBT();
|
||||
$this->namedtag->setTag(new ListTag("Items", [], NBT::TAG_Compound));
|
||||
for($index = 0; $index < $this->getSize(); ++$index){
|
||||
$this->setItem($index, $this->inventory->getItem($index));
|
||||
}
|
||||
$this->saveItems();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -121,70 +111,6 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
||||
return 3;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $index
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getSlotIndex(int $index) : int{
|
||||
foreach($this->namedtag->getListTag("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);
|
||||
if($i < 0){
|
||||
return ItemFactory::get(Item::AIR, 0, 0);
|
||||
}else{
|
||||
return Item::nbtDeserialize($this->namedtag->getListTag("Items")[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should not be used by plugins, use the Inventory
|
||||
*
|
||||
* @param int $index
|
||||
* @param Item $item
|
||||
*/
|
||||
public function setItem(int $index, Item $item){
|
||||
$i = $this->getSlotIndex($index);
|
||||
|
||||
$d = $item->nbtSerialize($index);
|
||||
|
||||
$items = $this->namedtag->getListTag("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->namedtag->setTag($items);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return FurnaceInventory
|
||||
*/
|
||||
|
Reference in New Issue
Block a user