mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-22 19:06:35 +00:00
Implement missing last interacted slot property in chiseled bookshelf (#6440)
This commit is contained in:
parent
93a270d251
commit
8cb2e577a1
@ -48,11 +48,32 @@ class ChiseledBookshelf extends Opaque{
|
|||||||
*/
|
*/
|
||||||
private array $slots = [];
|
private array $slots = [];
|
||||||
|
|
||||||
|
private ?ChiseledBookshelfSlot $lastInteractedSlot = null;
|
||||||
|
|
||||||
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
|
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
|
||||||
$w->horizontalFacing($this->facing);
|
$w->horizontalFacing($this->facing);
|
||||||
$w->enumSet($this->slots, ChiseledBookshelfSlot::cases());
|
$w->enumSet($this->slots, ChiseledBookshelfSlot::cases());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function readStateFromWorld() : Block{
|
||||||
|
$tile = $this->position->getWorld()->getTile($this->position);
|
||||||
|
if($tile instanceof TileChiseledBookshelf){
|
||||||
|
$this->lastInteractedSlot = $tile->getLastInteractedSlot();
|
||||||
|
}else{
|
||||||
|
$this->lastInteractedSlot = null;
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function writeStateToWorld() : void{
|
||||||
|
parent::writeStateToWorld();
|
||||||
|
|
||||||
|
$tile = $this->position->getWorld()->getTile($this->position);
|
||||||
|
if($tile instanceof TileChiseledBookshelf){
|
||||||
|
$tile->setLastInteractedSlot($this->lastInteractedSlot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether the given slot is displayed as occupied.
|
* Returns whether the given slot is displayed as occupied.
|
||||||
* This doesn't guarantee that there is or isn't a book in the bookshelf's inventory.
|
* This doesn't guarantee that there is or isn't a book in the bookshelf's inventory.
|
||||||
@ -92,6 +113,23 @@ class ChiseledBookshelf extends Opaque{
|
|||||||
return $this->slots;
|
return $this->slots;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the last slot interacted by a player or null if no slot has been interacted with yet.
|
||||||
|
*/
|
||||||
|
public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{
|
||||||
|
return $this->lastInteractedSlot;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the last slot interacted by a player.
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot) : self{
|
||||||
|
$this->lastInteractedSlot = $lastInteractedSlot;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
|
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
|
||||||
if($face !== $this->facing){
|
if($face !== $this->facing){
|
||||||
return false;
|
return false;
|
||||||
@ -112,10 +150,12 @@ class ChiseledBookshelf extends Opaque{
|
|||||||
$returnedItems[] = $inventory->getItem($slot->value);
|
$returnedItems[] = $inventory->getItem($slot->value);
|
||||||
$inventory->clear($slot->value);
|
$inventory->clear($slot->value);
|
||||||
$this->setSlot($slot, false);
|
$this->setSlot($slot, false);
|
||||||
|
$this->lastInteractedSlot = $slot;
|
||||||
}elseif($item instanceof WritableBookBase || $item instanceof Book || $item instanceof EnchantedBook){
|
}elseif($item instanceof WritableBookBase || $item instanceof Book || $item instanceof EnchantedBook){
|
||||||
//TODO: type tags like blocks would be better for this
|
//TODO: type tags like blocks would be better for this
|
||||||
$inventory->setItem($slot->value, $item->pop());
|
$inventory->setItem($slot->value, $item->pop());
|
||||||
$this->setSlot($slot, true);
|
$this->setSlot($slot, true);
|
||||||
|
$this->lastInteractedSlot = $slot;
|
||||||
}else{
|
}else{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -40,8 +40,12 @@ use function count;
|
|||||||
class ChiseledBookshelf extends Tile implements Container{
|
class ChiseledBookshelf extends Tile implements Container{
|
||||||
use ContainerTrait;
|
use ContainerTrait;
|
||||||
|
|
||||||
|
private const TAG_LAST_INTERACTED_SLOT = "LastInteractedSlot"; //TAG_Int
|
||||||
|
|
||||||
private SimpleInventory $inventory;
|
private SimpleInventory $inventory;
|
||||||
|
|
||||||
|
private ?ChiseledBookshelfSlot $lastInteractedSlot = null;
|
||||||
|
|
||||||
public function __construct(World $world, Vector3 $pos){
|
public function __construct(World $world, Vector3 $pos){
|
||||||
parent::__construct($world, $pos);
|
parent::__construct($world, $pos);
|
||||||
$this->inventory = new SimpleInventory(count(ChiseledBookshelfSlot::cases()));
|
$this->inventory = new SimpleInventory(count(ChiseledBookshelfSlot::cases()));
|
||||||
@ -55,12 +59,30 @@ class ChiseledBookshelf extends Tile implements Container{
|
|||||||
return $this->inventory;
|
return $this->inventory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{
|
||||||
|
return $this->lastInteractedSlot;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot) : void{
|
||||||
|
$this->lastInteractedSlot = $lastInteractedSlot;
|
||||||
|
}
|
||||||
|
|
||||||
public function readSaveData(CompoundTag $nbt) : void{
|
public function readSaveData(CompoundTag $nbt) : void{
|
||||||
$this->loadItems($nbt);
|
$this->loadItems($nbt);
|
||||||
|
|
||||||
|
$lastInteractedSlot = $nbt->getInt(self::TAG_LAST_INTERACTED_SLOT, 0);
|
||||||
|
if($lastInteractedSlot !== 0){
|
||||||
|
$this->lastInteractedSlot = ChiseledBookshelfSlot::tryFrom($lastInteractedSlot - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function writeSaveData(CompoundTag $nbt) : void{
|
protected function writeSaveData(CompoundTag $nbt) : void{
|
||||||
$this->saveItems($nbt);
|
$this->saveItems($nbt);
|
||||||
|
|
||||||
|
$nbt->setInt(self::TAG_LAST_INTERACTED_SLOT, $this->lastInteractedSlot !== null ?
|
||||||
|
$this->lastInteractedSlot->value + 1 :
|
||||||
|
0
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function loadItems(CompoundTag $tag) : void{
|
protected function loadItems(CompoundTag $tag) : void{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user