Implement missing last interacted slot property in chiseled bookshelf (#6440)

This commit is contained in:
ipad54
2024-09-03 02:02:06 +03:00
committed by GitHub
parent 93a270d251
commit 8cb2e577a1
2 changed files with 62 additions and 0 deletions

View File

@ -48,11 +48,32 @@ class ChiseledBookshelf extends Opaque{
*/
private array $slots = [];
private ?ChiseledBookshelfSlot $lastInteractedSlot = null;
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
$w->horizontalFacing($this->facing);
$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.
* 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;
}
/**
* 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{
if($face !== $this->facing){
return false;
@ -112,10 +150,12 @@ class ChiseledBookshelf extends Opaque{
$returnedItems[] = $inventory->getItem($slot->value);
$inventory->clear($slot->value);
$this->setSlot($slot, false);
$this->lastInteractedSlot = $slot;
}elseif($item instanceof WritableBookBase || $item instanceof Book || $item instanceof EnchantedBook){
//TODO: type tags like blocks would be better for this
$inventory->setItem($slot->value, $item->pop());
$this->setSlot($slot, true);
$this->lastInteractedSlot = $slot;
}else{
return true;
}