Skip to content

Commit d251b85

Browse files
committed
Implement missing last interacted slot property in chiseled bookshelf (pmmp#6440)
1 parent 955b874 commit d251b85

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/block/ChiseledBookshelf.php

+40
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,32 @@ class ChiseledBookshelf extends Opaque{
4848
*/
4949
private array $slots = [];
5050

51+
private ?ChiseledBookshelfSlot $lastInteractedSlot = null;
52+
5153
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
5254
$w->horizontalFacing($this->facing);
5355
$w->enumSet($this->slots, ChiseledBookshelfSlot::cases());
5456
}
5557

58+
public function readStateFromWorld() : Block{
59+
$tile = $this->position->getWorld()->getTile($this->position);
60+
if($tile instanceof TileChiseledBookshelf){
61+
$this->lastInteractedSlot = $tile->getLastInteractedSlot();
62+
}else{
63+
$this->lastInteractedSlot = null;
64+
}
65+
return $this;
66+
}
67+
68+
public function writeStateToWorld() : void{
69+
parent::writeStateToWorld();
70+
71+
$tile = $this->position->getWorld()->getTile($this->position);
72+
if($tile instanceof TileChiseledBookshelf){
73+
$tile->setLastInteractedSlot($this->lastInteractedSlot);
74+
}
75+
}
76+
5677
/**
5778
* Returns whether the given slot is displayed as occupied.
5879
* This doesn't guarantee that there is or isn't a book in the bookshelf's inventory.
@@ -92,6 +113,23 @@ public function getSlots() : array{
92113
return $this->slots;
93114
}
94115

116+
/**
117+
* Returns the last slot interacted by a player or null if no slot has been interacted with yet.
118+
*/
119+
public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{
120+
return $this->lastInteractedSlot;
121+
}
122+
123+
/**
124+
* Sets the last slot interacted by a player.
125+
*
126+
* @return $this
127+
*/
128+
public function setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot) : self{
129+
$this->lastInteractedSlot = $lastInteractedSlot;
130+
return $this;
131+
}
132+
95133
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
96134
if($face !== $this->facing){
97135
return false;
@@ -112,10 +150,12 @@ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player
112150
$returnedItems[] = $inventory->getItem($slot->value);
113151
$inventory->clear($slot->value);
114152
$this->setSlot($slot, false);
153+
$this->lastInteractedSlot = $slot;
115154
}elseif($item instanceof WritableBookBase || $item instanceof Book || $item instanceof EnchantedBook){
116155
//TODO: type tags like blocks would be better for this
117156
$inventory->setItem($slot->value, $item->pop());
118157
$this->setSlot($slot, true);
158+
$this->lastInteractedSlot = $slot;
119159
}else{
120160
return true;
121161
}

src/block/tile/ChiseledBookshelf.php

+22
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@
4040
class ChiseledBookshelf extends Tile implements Container{
4141
use ContainerTrait;
4242

43+
private const TAG_LAST_INTERACTED_SLOT = "LastInteractedSlot"; //TAG_Int
44+
4345
private SimpleInventory $inventory;
4446

47+
private ?ChiseledBookshelfSlot $lastInteractedSlot = null;
48+
4549
public function __construct(World $world, Vector3 $pos){
4650
parent::__construct($world, $pos);
4751
$this->inventory = new SimpleInventory(count(ChiseledBookshelfSlot::cases()));
@@ -55,12 +59,30 @@ public function getRealInventory() : SimpleInventory{
5559
return $this->inventory;
5660
}
5761

62+
public function getLastInteractedSlot() : ?ChiseledBookshelfSlot{
63+
return $this->lastInteractedSlot;
64+
}
65+
66+
public function setLastInteractedSlot(?ChiseledBookshelfSlot $lastInteractedSlot) : void{
67+
$this->lastInteractedSlot = $lastInteractedSlot;
68+
}
69+
5870
public function readSaveData(CompoundTag $nbt) : void{
5971
$this->loadItems($nbt);
72+
73+
$lastInteractedSlot = $nbt->getInt(self::TAG_LAST_INTERACTED_SLOT, 0);
74+
if($lastInteractedSlot !== 0){
75+
$this->lastInteractedSlot = ChiseledBookshelfSlot::tryFrom($lastInteractedSlot - 1);
76+
}
6077
}
6178

6279
protected function writeSaveData(CompoundTag $nbt) : void{
6380
$this->saveItems($nbt);
81+
82+
$nbt->setInt(self::TAG_LAST_INTERACTED_SLOT, $this->lastInteractedSlot !== null ?
83+
$this->lastInteractedSlot->value + 1 :
84+
0
85+
);
6486
}
6587

6688
protected function loadItems(CompoundTag $tag) : void{

0 commit comments

Comments
 (0)