@@ -19,6 +19,7 @@ import org.bukkit.Bukkit
19
19
import org.bukkit.entity.HumanEntity
20
20
import org.bukkit.entity.Player
21
21
import org.bukkit.event.Cancellable
22
+ import org.bukkit.event.Event
22
23
import org.bukkit.event.EventHandler
23
24
import org.bukkit.event.EventPriority
24
25
import org.bukkit.event.Listener
@@ -149,7 +150,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
149
150
}
150
151
}
151
152
152
- @EventHandler
153
+ @EventHandler(priority = EventPriority . LOW , ignoreCancelled = true )
153
154
public fun onClick (event : InventoryClickEvent ) {
154
155
val holder = event.inventory.holder
155
156
val view = convertHolderToInterfaceView(holder) ? : return
@@ -163,10 +164,11 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
163
164
setOpenInterface(event.player.uniqueId, null )
164
165
}
165
166
166
- @EventHandler
167
+ @EventHandler(priority = EventPriority . LOW )
167
168
public fun onInteract (event : PlayerInteractEvent ) {
168
169
if (event.action !in VALID_INTERACT ) return
169
170
if (event.hand != EquipmentSlot .HAND ) return
171
+ if (event.useItemInHand() == Event .Result .DENY ) return
170
172
171
173
val player = event.player
172
174
val view = getOpenInterface(player.uniqueId) ? : return
@@ -202,22 +204,15 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
202
204
// place where it's become an issue.
203
205
if (event.inventory.holder is Player ) {
204
206
val index = event.slot
205
-
206
- if (index !in PLAYER_INVENTORY_RANGE ) {
207
- return null
208
- }
207
+ if (index !in PLAYER_INVENTORY_RANGE ) return null
209
208
210
209
val x = index / 9
211
210
val adjustedX = PlayerPane .PANE_ORDERING .indexOf(x)
212
211
return GridPoint (adjustedX, index % 9 )
213
212
}
214
213
215
214
val index = event.rawSlot
216
-
217
- if (index == OUTSIDE_CHEST_INDEX ) {
218
- return null
219
- }
220
-
215
+ if (index == OUTSIDE_CHEST_INDEX ) return null
221
216
return GridPoint .at(index / 9 , index % 9 )
222
217
}
223
218
@@ -226,19 +221,13 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
226
221
* their currently open player interface is returned.
227
222
*/
228
223
public fun convertHolderToInterfaceView (holder : InventoryHolder ? ): AbstractInterfaceView <* , * >? {
229
- if (holder == null ) {
230
- return null
231
- }
224
+ if (holder == null ) return null
232
225
233
226
// If it's an abstract view use that one
234
- if (holder is AbstractInterfaceView <* , * >) {
235
- return holder
236
- }
227
+ if (holder is AbstractInterfaceView <* , * >) return holder
237
228
238
229
// If it's the player's own inventory use the held one
239
- if (holder is HumanEntity ) {
240
- return getOpenInterface(holder.uniqueId)
241
- }
230
+ if (holder is HumanEntity ) return getOpenInterface(holder.uniqueId)
242
231
243
232
return null
244
233
}
@@ -251,21 +240,24 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
251
240
event : Cancellable ,
252
241
slot : Int
253
242
) {
243
+ // Determine the type of click, if nothing was clicked we allow it
244
+ val clickHandler = view.pane.getRaw(clickedPoint)?.clickHandler ? : return
245
+
246
+ // Automatically cancel if throttling or already processing
254
247
if (view.isProcessingClick || shouldThrottle(view.player)) {
255
248
event.isCancelled = true
256
249
return
257
250
}
258
251
252
+ // Only allow one click to be processed at the same time
259
253
view.isProcessingClick = true
260
254
255
+ // Forward this click to all pre-processors
261
256
val clickContext = ClickContext (view.player, view, click, slot)
262
-
263
257
view.backing.properties.clickPreprocessors
264
258
.forEach { handler -> ClickHandler .process(handler, clickContext) }
265
259
266
- val clickHandler = view.pane.getRaw(clickedPoint)
267
- ?.clickHandler ? : ClickHandler .ALLOW
268
-
260
+ // Run the click handler and deal with its result
269
261
val completedClickHandler = clickHandler
270
262
.run { CompletableClickHandler ().apply { handle(clickContext) } }
271
263
.onComplete { ex ->
@@ -286,27 +278,22 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
286
278
)
287
279
}
288
280
289
- event.isCancelled = completedClickHandler.cancelled
281
+ // Update the cancellation state of the event
282
+ if (completedClickHandler.cancelled) {
283
+ event.isCancelled = true
284
+ }
290
285
}
291
286
292
287
/* * Converts a bukkit [action] to a [ClickType]. */
293
288
private fun convertAction (action : Action , sneaking : Boolean ): ClickType {
294
289
if (action.isRightClick) {
295
- if (sneaking) {
296
- return ClickType .SHIFT_RIGHT
297
- }
298
-
290
+ if (sneaking) return ClickType .SHIFT_RIGHT
299
291
return ClickType .RIGHT
300
292
}
301
-
302
293
if (action.isLeftClick) {
303
- if (sneaking) {
304
- return ClickType .SHIFT_LEFT
305
- }
306
-
294
+ if (sneaking) return ClickType .SHIFT_LEFT
307
295
return ClickType .LEFT
308
296
}
309
-
310
297
return ClickType .UNKNOWN
311
298
}
312
299
0 commit comments