@@ -48,11 +48,32 @@ class ChiseledBookshelf extends Opaque{
48
48
*/
49
49
private array $ slots = [];
50
50
51
+ private ?ChiseledBookshelfSlot $ lastInteractedSlot = null ;
52
+
51
53
protected function describeBlockOnlyState (RuntimeDataDescriber $ w ) : void {
52
54
$ w ->horizontalFacing ($ this ->facing );
53
55
$ w ->enumSet ($ this ->slots , ChiseledBookshelfSlot::cases ());
54
56
}
55
57
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
+
56
77
/**
57
78
* Returns whether the given slot is displayed as occupied.
58
79
* 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{
92
113
return $ this ->slots ;
93
114
}
94
115
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
+
95
133
public function onInteract (Item $ item , int $ face , Vector3 $ clickVector , ?Player $ player = null , array &$ returnedItems = []) : bool {
96
134
if ($ face !== $ this ->facing ){
97
135
return false ;
@@ -112,10 +150,12 @@ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player
112
150
$ returnedItems [] = $ inventory ->getItem ($ slot ->value );
113
151
$ inventory ->clear ($ slot ->value );
114
152
$ this ->setSlot ($ slot , false );
153
+ $ this ->lastInteractedSlot = $ slot ;
115
154
}elseif ($ item instanceof WritableBookBase || $ item instanceof Book || $ item instanceof EnchantedBook){
116
155
//TODO: type tags like blocks would be better for this
117
156
$ inventory ->setItem ($ slot ->value , $ item ->pop ());
118
157
$ this ->setSlot ($ slot , true );
158
+ $ this ->lastInteractedSlot = $ slot ;
119
159
}else {
120
160
return true ;
121
161
}
0 commit comments