mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-04 00:55:14 +00:00
Added ContainerTrait, reduce copy-pasted code in Tile
This commit is contained in:
@ -36,7 +36,7 @@ use pocketmine\nbt\tag\ListTag;
|
|||||||
use pocketmine\Player;
|
use pocketmine\Player;
|
||||||
|
|
||||||
class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
|
class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
|
||||||
use NameableTrait;
|
use NameableTrait, ContainerTrait;
|
||||||
|
|
||||||
const TAG_PAIRX = "pairx";
|
const TAG_PAIRX = "pairx";
|
||||||
const TAG_PAIRZ = "pairz";
|
const TAG_PAIRZ = "pairz";
|
||||||
@ -50,14 +50,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
|
|||||||
public function __construct(Level $level, CompoundTag $nbt){
|
public function __construct(Level $level, CompoundTag $nbt){
|
||||||
parent::__construct($level, $nbt);
|
parent::__construct($level, $nbt);
|
||||||
$this->inventory = new ChestInventory($this);
|
$this->inventory = new ChestInventory($this);
|
||||||
|
$this->loadItems();
|
||||||
if(!$this->namedtag->hasTag("Items", ListTag::class)){
|
|
||||||
$this->namedtag->setTag(new ListTag("Items", [], NBT::TAG_Compound));
|
|
||||||
}
|
|
||||||
|
|
||||||
for($i = 0; $i < $this->getSize(); ++$i){
|
|
||||||
$this->inventory->setItem($i, $this->getItem($i), false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function close() : void{
|
public function close() : void{
|
||||||
@ -78,10 +71,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
|
|||||||
|
|
||||||
public function saveNBT() : void{
|
public function saveNBT() : void{
|
||||||
parent::saveNBT();
|
parent::saveNBT();
|
||||||
$this->namedtag->setTag(new ListTag("Items", [], NBT::TAG_Compound));
|
$this->saveItems();
|
||||||
for($index = 0; $index < $this->getSize(); ++$index){
|
|
||||||
$this->setItem($index, $this->inventory->getItem($index));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -91,72 +81,6 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
|
|||||||
return 27;
|
return 27;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $index
|
|
||||||
*
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
protected function getSlotIndex(int $index) : int{
|
|
||||||
$items = $this->namedtag->getListTag("Items");
|
|
||||||
assert($items instanceof ListTag);
|
|
||||||
foreach($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 ChestInventory|DoubleChestInventory
|
* @return ChestInventory|DoubleChestInventory
|
||||||
*/
|
*/
|
||||||
|
@ -27,6 +27,7 @@ use pocketmine\inventory\Inventory;
|
|||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
|
|
||||||
interface Container{
|
interface Container{
|
||||||
|
const TAG_ITEMS = "Items";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $index
|
* @param int $index
|
||||||
|
135
src/pocketmine/tile/ContainerTrait.php
Normal file
135
src/pocketmine/tile/ContainerTrait.php
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
<?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\tile;
|
||||||
|
|
||||||
|
use pocketmine\inventory\Inventory;
|
||||||
|
use pocketmine\item\Item;
|
||||||
|
use pocketmine\item\ItemFactory;
|
||||||
|
use pocketmine\nbt\NBT;
|
||||||
|
use pocketmine\nbt\tag\CompoundTag;
|
||||||
|
use pocketmine\nbt\tag\ListTag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This trait implements most methods in the {@link Container} interface. It should only be used by Tiles.
|
||||||
|
*/
|
||||||
|
trait ContainerTrait{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
abstract public function getSize() : int;
|
||||||
|
|
||||||
|
abstract public function getNBT() : CompoundTag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Inventory
|
||||||
|
*/
|
||||||
|
abstract public function getInventory();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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));
|
||||||
|
}
|
||||||
|
|
||||||
|
$inventory = $this->getInventory();
|
||||||
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
|
$inventory->setItem($i, $this->getItem($i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function saveItems() : void{
|
||||||
|
$this->getNBT()->setTag(new ListTag(Container::TAG_ITEMS, [], NBT::TAG_Compound));
|
||||||
|
|
||||||
|
$inventory = $this->getInventory();
|
||||||
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
|
$this->setItem($i, $inventory->getItem($i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -42,7 +42,7 @@ use pocketmine\network\mcpe\protocol\ContainerSetDataPacket;
|
|||||||
use pocketmine\Player;
|
use pocketmine\Player;
|
||||||
|
|
||||||
class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
||||||
use NameableTrait;
|
use NameableTrait, ContainerTrait;
|
||||||
|
|
||||||
const TAG_BURN_TIME = "BurnTime";
|
const TAG_BURN_TIME = "BurnTime";
|
||||||
const TAG_COOK_TIME = "CookTime";
|
const TAG_COOK_TIME = "CookTime";
|
||||||
@ -76,14 +76,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
|||||||
|
|
||||||
parent::__construct($level, $nbt);
|
parent::__construct($level, $nbt);
|
||||||
$this->inventory = new FurnaceInventory($this);
|
$this->inventory = new FurnaceInventory($this);
|
||||||
|
$this->loadItems();
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if($this->namedtag->getShort(self::TAG_BURN_TIME) > 0){
|
if($this->namedtag->getShort(self::TAG_BURN_TIME) > 0){
|
||||||
$this->scheduleUpdate();
|
$this->scheduleUpdate();
|
||||||
@ -108,10 +101,7 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
|||||||
|
|
||||||
public function saveNBT() : void{
|
public function saveNBT() : void{
|
||||||
parent::saveNBT();
|
parent::saveNBT();
|
||||||
$this->namedtag->setTag(new ListTag("Items", [], NBT::TAG_Compound));
|
$this->saveItems();
|
||||||
for($index = 0; $index < $this->getSize(); ++$index){
|
|
||||||
$this->setItem($index, $this->inventory->getItem($index));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -121,70 +111,6 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
|
|||||||
return 3;
|
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
|
* @return FurnaceInventory
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user