@@ -54,11 +54,10 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
54
54
require(! ::INSTANCE .isInitialized) { " Already installed!" }
55
55
INSTANCE = InterfacesListeners (plugin)
56
56
Bukkit .getPluginManager().registerEvents(INSTANCE , plugin)
57
- println (" Installed interfaces listeners" )
58
57
}
59
58
60
- /* * All valid closing reasons that should re-open the opened player inventory. */
61
- private val VALID_REASON = EnumSet .of(
59
+ /* * All valid closing reasons that should re-open the previously opened player inventory. */
60
+ private val REOPEN_REASONS = EnumSet .of(
62
61
Reason .PLAYER ,
63
62
Reason .UNKNOWN ,
64
63
Reason .PLUGIN
@@ -126,32 +125,27 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
126
125
@EventHandler
127
126
public fun onOpen (event : InventoryOpenEvent ) {
128
127
val holder = event.inventory.holder
128
+ val view = convertHolderToInterfaceView(holder) ? : return
129
129
130
- if (holder !is InterfaceView ) {
131
- return
132
- }
133
-
130
+ // Abort any previous query the player had
134
131
abortQuery(event.player.uniqueId, null )
135
- holder .onOpen()
132
+ view .onOpen()
136
133
}
137
134
138
135
@EventHandler
139
136
public fun onClose (event : InventoryCloseEvent ) {
140
137
val holder = event.inventory.holder
138
+ val view = convertHolderToInterfaceView(holder) ? : return
141
139
val reason = event.reason
142
140
143
- if (holder !is InterfaceView ) {
144
- return
145
- }
146
-
147
141
SCOPE .launch {
148
- val view = convertHolderToInterfaceView(holder)
149
- if (view != null ) {
150
- view.backing.closeHandlers[reason]?.invoke(reason, view)
151
- }
142
+ // Mark the current view as closed properly
143
+ view.markClosed(reason)
152
144
153
- if (reason !in VALID_REASON ) return @launch
154
- getOpenInterface(event.player.uniqueId)?.open()
145
+ // Try to open back up a previous interface
146
+ if (reason in REOPEN_REASONS ) {
147
+ getOpenInterface(event.player.uniqueId)?.open()
148
+ }
155
149
}
156
150
}
157
151
@@ -171,19 +165,13 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
171
165
172
166
@EventHandler
173
167
public fun onInteract (event : PlayerInteractEvent ) {
174
- if (event.action !in VALID_INTERACT ) {
175
- return
176
- }
177
- if (event.hand != EquipmentSlot .HAND ) {
178
- return
179
- }
168
+ if (event.action !in VALID_INTERACT ) return
169
+ if (event.hand != EquipmentSlot .HAND ) return
180
170
181
171
val player = event.player
182
- val view = getOpenInterface(player.uniqueId) as ? AbstractInterfaceView <* , * > ? : return
183
-
172
+ val view = getOpenInterface(player.uniqueId) ? : return
184
173
val slot = player.inventory.heldItemSlot
185
174
val clickedPoint = GridPoint .at(3 , slot)
186
-
187
175
val click = convertAction(event.action, player.isSneaking)
188
176
189
177
handleClick(view, clickedPoint, click, event, - 1 )
@@ -272,7 +260,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
272
260
273
261
val clickContext = ClickContext (view.player, view, click, slot)
274
262
275
- view.backing.clickPreprocessors
263
+ view.backing.properties. clickPreprocessors
276
264
.forEach { handler -> ClickHandler .process(handler, clickContext) }
277
265
278
266
val clickHandler = view.pane.getRaw(clickedPoint)
@@ -391,12 +379,13 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
391
379
// Run the cancellation handler
392
380
query.onCancel()
393
381
394
- // Try to run the close handler on the view as it got closed now
395
- val reason = Reason .PLAYER
396
- (query.view as AbstractInterfaceView <* , * >).backing.closeHandlers[reason]?.also { handler ->
397
- SCOPE .launch {
398
- handler.invoke(reason, query.view)
399
- }
382
+ // If a view is given we are already in a markClosed call
383
+ // and we can leave it here!
384
+ if (view != null ) return
385
+
386
+ // Mark the view as properly closed
387
+ SCOPE .launch {
388
+ (query.view as AbstractInterfaceView <* , * >).markClosed(Reason .PLAYER )
400
389
}
401
390
}
402
391
}
0 commit comments