@@ -75,7 +75,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
75
75
Reason .PLUGIN ,
76
76
Reason .TELEPORT ,
77
77
Reason .CANT_USE ,
78
- Reason .UNLOADED
78
+ Reason .UNLOADED ,
79
79
)
80
80
81
81
/* * An incomplete set of blocks that have some interaction when clicked on. */
@@ -85,7 +85,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
85
85
MaterialTags .WOODEN_DOORS ,
86
86
MaterialTags .WOODEN_TRAPDOORS ,
87
87
MaterialTags .FENCE_GATES ,
88
- MaterialSetTag .BUTTONS
88
+ MaterialSetTag .BUTTONS ,
89
89
)
90
90
// Add blocks with inventories
91
91
.add(
@@ -100,7 +100,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
100
100
Material .LOOM ,
101
101
Material .CARTOGRAPHY_TABLE ,
102
102
Material .ENCHANTING_TABLE ,
103
- Material .SMITHING_TABLE
103
+ Material .SMITHING_TABLE ,
104
104
)
105
105
.add(Material .LEVER )
106
106
.add(Material .CAKE )
@@ -109,25 +109,25 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
109
109
Material .COPPER_DOOR ,
110
110
Material .EXPOSED_COPPER_DOOR ,
111
111
Material .WEATHERED_COPPER_DOOR ,
112
- Material .OXIDIZED_COPPER_DOOR
112
+ Material .OXIDIZED_COPPER_DOOR ,
113
113
)
114
114
.add(
115
115
Material .WAXED_COPPER_DOOR ,
116
116
Material .WAXED_EXPOSED_COPPER_DOOR ,
117
117
Material .WAXED_WEATHERED_COPPER_DOOR ,
118
- Material .WAXED_OXIDIZED_COPPER_DOOR
118
+ Material .WAXED_OXIDIZED_COPPER_DOOR ,
119
119
)
120
120
.add(
121
121
Material .COPPER_TRAPDOOR ,
122
122
Material .EXPOSED_COPPER_TRAPDOOR ,
123
123
Material .WEATHERED_COPPER_TRAPDOOR ,
124
- Material .OXIDIZED_COPPER_TRAPDOOR
124
+ Material .OXIDIZED_COPPER_TRAPDOOR ,
125
125
)
126
126
.add(
127
127
Material .WAXED_COPPER_TRAPDOOR ,
128
128
Material .WAXED_EXPOSED_COPPER_TRAPDOOR ,
129
129
Material .WAXED_WEATHERED_COPPER_TRAPDOOR ,
130
- Material .WAXED_OXIDIZED_COPPER_TRAPDOOR
130
+ Material .WAXED_OXIDIZED_COPPER_TRAPDOOR ,
131
131
)
132
132
// You can click signs to edit them
133
133
.add(MaterialTags .SIGNS )
@@ -138,7 +138,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
138
138
val view : InterfaceView ,
139
139
val onCancel : suspend () -> Unit ,
140
140
val onComplete : suspend (Component ) -> Boolean ,
141
- val id : UUID
141
+ val id : UUID ,
142
142
)
143
143
144
144
/* * The view currently being opened. */
@@ -150,19 +150,14 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
150
150
.expireAfterWrite(200 .toLong(), TimeUnit .MILLISECONDS )
151
151
.build()
152
152
153
- /* * A cache of all ongoing chat queries. */
154
- private val queries: Cache <UUID , ChatQuery > = Caffeine .newBuilder()
155
- .build()
153
+ /* * A map of all ongoing chat queries. */
154
+ private val queries = mutableMapOf<UUID , ChatQuery >()
156
155
157
- /* * A cache of player interfaces that should be opened again in the future. */
158
- private val backgroundPlayerInterfaceViews: Cache <UUID , PlayerInterfaceView > = Caffeine .newBuilder()
159
- .weakValues()
160
- .build()
156
+ /* * A map of player interfaces that should be opened again in the future. */
157
+ private val backgroundPlayerInterfaceViews = mutableMapOf<UUID , PlayerInterfaceView >()
161
158
162
- /* * A cache of actively open player interfaces. */
163
- private val openPlayerInterfaceViews: Cache <UUID , PlayerInterfaceView > = Caffeine .newBuilder()
164
- .weakValues()
165
- .build()
159
+ /* * A map of actively open player interfaces. */
160
+ private val openPlayerInterfaceViews = mutableMapOf<UUID , PlayerInterfaceView >()
166
161
167
162
/* * Re-opens the current background interface of [player]. */
168
163
public fun reopenInventory (player : Player ) {
@@ -182,50 +177,46 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
182
177
*/
183
178
public fun getBackgroundPlayerInterface (playerId : UUID ): PlayerInterfaceView ? {
184
179
// Check if the menu is definitely still meant to be open
185
- val result = backgroundPlayerInterfaceViews.getIfPresent( playerId) ? : return null
180
+ val result = backgroundPlayerInterfaceViews[ playerId] ? : return null
186
181
if (result.shouldStillBeOpened) return result
187
- backgroundPlayerInterfaceViews.invalidate( playerId)
182
+ backgroundPlayerInterfaceViews - = playerId
188
183
return null
189
184
}
190
185
191
186
/* *
192
187
* Returns the currently open player interface for [playerId].
193
188
*/
194
189
public fun getOpenPlayerInterface (playerId : UUID ): PlayerInterfaceView ? {
195
- val result = openPlayerInterfaceViews.getIfPresent( playerId) ? : return null
190
+ val result = openPlayerInterfaceViews[ playerId] ? : return null
196
191
if (result.shouldStillBeOpened) return result
197
- openPlayerInterfaceViews.invalidate( playerId)
192
+ openPlayerInterfaceViews - = playerId
198
193
return null
199
194
}
200
195
201
196
/* * Marks the given [view] as the opened player interface. */
202
197
public fun openPlayerInterface (playerId : UUID , view : PlayerInterfaceView ) {
203
- backgroundPlayerInterfaceViews.invalidate( playerId)
204
- openPlayerInterfaceViews.put( playerId, view)
198
+ backgroundPlayerInterfaceViews - = playerId
199
+ openPlayerInterfaceViews[ playerId] = view
205
200
}
206
201
207
202
/* * Sets the background view for [playerId] to [view]. */
208
203
public fun setBackgroundView (playerId : UUID , view : PlayerInterfaceView ? ) {
209
204
if (view == null ) {
210
- backgroundPlayerInterfaceViews.invalidate( playerId)
205
+ backgroundPlayerInterfaceViews - = playerId
211
206
} else {
212
- backgroundPlayerInterfaceViews.put( playerId, view)
207
+ backgroundPlayerInterfaceViews[ playerId] = view
213
208
}
214
209
}
215
210
216
211
/* * Closes the given [view] of a player interface. */
217
212
public fun closePlayerInterface (playerId : UUID , view : PlayerInterfaceView ? ) {
218
213
abortQuery(playerId, view)
219
214
if (view == null ) {
220
- backgroundPlayerInterfaceViews.invalidate( playerId)
221
- openPlayerInterfaceViews.invalidate( playerId)
215
+ backgroundPlayerInterfaceViews - = playerId
216
+ openPlayerInterfaceViews - = playerId
222
217
} else {
223
- if (backgroundPlayerInterfaceViews.getIfPresent(playerId) == = view) {
224
- backgroundPlayerInterfaceViews.invalidate(playerId)
225
- }
226
- if (openPlayerInterfaceViews.getIfPresent(playerId) == = view) {
227
- openPlayerInterfaceViews.invalidate(playerId)
228
- }
218
+ backgroundPlayerInterfaceViews.remove(playerId, view)
219
+ openPlayerInterfaceViews.remove(playerId, view)
229
220
}
230
221
}
231
222
@@ -253,9 +244,8 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
253
244
254
245
// Move the current open inventory to the background to indicate
255
246
// it is no longer the actually opened inventory!
256
- openPlayerInterfaceViews.getIfPresent (event.player.uniqueId)?.also {
247
+ openPlayerInterfaceViews.remove (event.player.uniqueId)?.also {
257
248
setBackgroundView(event.player.uniqueId, it)
258
- openPlayerInterfaceViews.invalidate(event.player.uniqueId)
259
249
}
260
250
261
251
// Abort any previous query the player had
@@ -314,7 +304,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
314
304
! canFreelyMove(
315
305
view,
316
306
view.backing.relativizePlayerInventorySlot(GridPoint .at(3 , event.hotbarButton)),
317
- true
307
+ true ,
318
308
)
319
309
) {
320
310
event.isCancelled = true
@@ -326,7 +316,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
326
316
! canFreelyMove(
327
317
view,
328
318
view.backing.relativizePlayerInventorySlot(GridPoint .at(4 , 4 )),
329
- true
319
+ true ,
330
320
)
331
321
) {
332
322
event.isCancelled = true
@@ -352,7 +342,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
352
342
! canFreelyMove(
353
343
view,
354
344
requireNotNull(GridPoint .fromBukkitChestSlot(index)),
355
- false
345
+ false ,
356
346
)
357
347
}
358
348
) ||
@@ -363,9 +353,9 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
363
353
! canFreelyMove(
364
354
view,
365
355
view.backing.relativizePlayerInventorySlot(
366
- requireNotNull(GridPoint .fromBukkitPlayerSlot(index))
356
+ requireNotNull(GridPoint .fromBukkitPlayerSlot(index)),
367
357
),
368
- true
358
+ true ,
369
359
)
370
360
}
371
361
) {
@@ -398,7 +388,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
398
388
} else {
399
389
targetSlot
400
390
},
401
- isMovingIntoPlayerInventory
391
+ isMovingIntoPlayerInventory,
402
392
)
403
393
) {
404
394
event.isCancelled = true
@@ -428,8 +418,6 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
428
418
429
419
@EventHandler
430
420
public fun onPlayerQuit (event : PlayerQuitEvent ) {
431
- // Save the contents of their currently shown inventory
432
- abortQuery(event.player.uniqueId, null )
433
421
closePlayerInterface(event.player.uniqueId, null )
434
422
}
435
423
@@ -456,7 +444,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
456
444
GridPoint .at(3 , player.inventory.heldItemSlot)
457
445
} else {
458
446
PlayerPane .OFF_HAND_SLOT
459
- }
447
+ },
460
448
)
461
449
val click = convertAction(event.action, player.isSneaking)
462
450
@@ -548,8 +536,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
548
536
val player = event.player
549
537
550
538
// Determine if they have a pending query and end it
551
- val query = queries.getIfPresent(player.uniqueId) ? : return
552
- queries.invalidate(player.uniqueId)
539
+ val query = queries.remove(player.uniqueId) ? : return
553
540
554
541
// Complete the query and re-open the view
555
542
SCOPE .launch(InterfacesCoroutineDetails (event.player.uniqueId, " completing chat query" )) {
@@ -609,7 +596,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
609
596
click : ClickType ,
610
597
slot : Int ,
611
598
isPlayerInventory : Boolean ,
612
- interact : Boolean
599
+ interact : Boolean ,
613
600
): Boolean {
614
601
// Determine the type of click, if nothing was clicked we allow it
615
602
val raw = view.completedPane?.getRaw(clickedPoint)
@@ -654,7 +641,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
654
641
Bukkit .getScheduler().runTaskLaterAsynchronously(
655
642
plugin,
656
643
Runnable { completedClickHandler.cancel() },
657
- 120
644
+ 120 ,
658
645
)
659
646
}
660
647
@@ -691,15 +678,15 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
691
678
view : InterfaceView ,
692
679
timeout : Duration ,
693
680
onCancel : suspend () -> Unit ,
694
- onComplete : suspend (Component ) -> Boolean
681
+ onComplete : suspend (Component ) -> Boolean ,
695
682
) {
696
683
// Determine if the player has this inventory open
697
684
if (! view.isOpen()) return
698
685
699
686
// Store the current open inventory and remove it from the cache so it does
700
687
// not interfere and we can have the player be itemless
701
688
val playerId = view.player.uniqueId
702
- openPlayerInterfaceViews.invalidate( playerId)
689
+ openPlayerInterfaceViews - = playerId
703
690
704
691
runSync {
705
692
// Close the current inventory to open another to avoid close reasons
@@ -721,36 +708,34 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
721
708
view,
722
709
onCancel,
723
710
onComplete,
724
- id
725
- )
711
+ id,
712
+ ),
726
713
)
727
714
728
715
// Set a timer for to automatically cancel this query to prevent players
729
716
// from being stuck in a mode they don't understand for too long
730
717
Bukkit .getScheduler().runTaskLater(
731
718
plugin,
732
719
Runnable {
733
- val queryThen = queries.getIfPresent( playerId) ? : return @Runnable
720
+ val queryThen = queries[ playerId] ? : return @Runnable
734
721
if (queryThen.id != id) return @Runnable
735
722
736
723
// Remove the query, run the cancel handler, and re-open the view
737
- queries.invalidate( playerId)
724
+ queries - = playerId
738
725
SCOPE .launch(InterfacesCoroutineDetails (playerId, " cancelling chat query due to timeout" )) {
739
726
onCancel()
740
727
view.reopen()
741
728
}
742
729
},
743
- timeout.inWholeMilliseconds / 50
730
+ timeout.inWholeMilliseconds / 50 ,
744
731
)
745
732
}
746
733
747
734
/* * Aborts an ongoing query for [playerId], without re-opening the original view. */
748
735
public fun abortQuery (playerId : UUID , view : InterfaceView ? ) {
749
- val query = queries.getIfPresent(playerId) ? : return
750
-
751
736
// Only end the specific view on request
752
- if (view != null && query .view != view) return
753
- queries.invalidate (playerId)
737
+ if (view != null && queries[playerId]? .view != view) return
738
+ val query = queries.remove (playerId) ? : return
754
739
755
740
SCOPE .launch(InterfacesCoroutineDetails (playerId, " aborting chat query" )) {
756
741
// Run the cancellation handler
0 commit comments