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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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;
}

View File

@ -40,8 +40,12 @@ use function count;
class ChiseledBookshelf extends Tile implements Container{
use ContainerTrait;
private const TAG_LAST_INTERACTED_SLOT = "LastInteractedSlot"; //TAG_Int
private SimpleInventory $inventory;
private ?ChiseledBookshelfSlot $lastInteractedSlot = null;
public function __construct(World $world, Vector3 $pos){
parent::__construct($world, $pos);
$this->inventory = new SimpleInventory(count(ChiseledBookshelfSlot::cases()));
@ -55,12 +59,30 @@ class ChiseledBookshelf extends Tile implements Container{
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{
$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{
$this->saveItems($nbt);
$nbt->setInt(self::TAG_LAST_INTERACTED_SLOT, $this->lastInteractedSlot !== null ?
$this->lastInteractedSlot->value + 1 :
0
);
}
protected function loadItems(CompoundTag $tag) : void{