Skip to content

Commit a8a5830

Browse files
committed
Directly ignore slots without click handlers
1 parent 760b680 commit a8a5830

File tree

2 files changed

+23
-36
lines changed

2 files changed

+23
-36
lines changed

interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesListeners.kt

+22-35
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.bukkit.Bukkit
1919
import org.bukkit.entity.HumanEntity
2020
import org.bukkit.entity.Player
2121
import org.bukkit.event.Cancellable
22+
import org.bukkit.event.Event
2223
import org.bukkit.event.EventHandler
2324
import org.bukkit.event.EventPriority
2425
import org.bukkit.event.Listener
@@ -149,7 +150,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
149150
}
150151
}
151152

152-
@EventHandler
153+
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
153154
public fun onClick(event: InventoryClickEvent) {
154155
val holder = event.inventory.holder
155156
val view = convertHolderToInterfaceView(holder) ?: return
@@ -163,10 +164,11 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
163164
setOpenInterface(event.player.uniqueId, null)
164165
}
165166

166-
@EventHandler
167+
@EventHandler(priority = EventPriority.LOW)
167168
public fun onInteract(event: PlayerInteractEvent) {
168169
if (event.action !in VALID_INTERACT) return
169170
if (event.hand != EquipmentSlot.HAND) return
171+
if (event.useItemInHand() == Event.Result.DENY) return
170172

171173
val player = event.player
172174
val view = getOpenInterface(player.uniqueId) ?: return
@@ -202,22 +204,15 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
202204
// place where it's become an issue.
203205
if (event.inventory.holder is Player) {
204206
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
209208

210209
val x = index / 9
211210
val adjustedX = PlayerPane.PANE_ORDERING.indexOf(x)
212211
return GridPoint(adjustedX, index % 9)
213212
}
214213

215214
val index = event.rawSlot
216-
217-
if (index == OUTSIDE_CHEST_INDEX) {
218-
return null
219-
}
220-
215+
if (index == OUTSIDE_CHEST_INDEX) return null
221216
return GridPoint.at(index / 9, index % 9)
222217
}
223218

@@ -226,19 +221,13 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
226221
* their currently open player interface is returned.
227222
*/
228223
public fun convertHolderToInterfaceView(holder: InventoryHolder?): AbstractInterfaceView<*, *>? {
229-
if (holder == null) {
230-
return null
231-
}
224+
if (holder == null) return null
232225

233226
// 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
237228

238229
// 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)
242231

243232
return null
244233
}
@@ -251,21 +240,24 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
251240
event: Cancellable,
252241
slot: Int
253242
) {
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
254247
if (view.isProcessingClick || shouldThrottle(view.player)) {
255248
event.isCancelled = true
256249
return
257250
}
258251

252+
// Only allow one click to be processed at the same time
259253
view.isProcessingClick = true
260254

255+
// Forward this click to all pre-processors
261256
val clickContext = ClickContext(view.player, view, click, slot)
262-
263257
view.backing.properties.clickPreprocessors
264258
.forEach { handler -> ClickHandler.process(handler, clickContext) }
265259

266-
val clickHandler = view.pane.getRaw(clickedPoint)
267-
?.clickHandler ?: ClickHandler.ALLOW
268-
260+
// Run the click handler and deal with its result
269261
val completedClickHandler = clickHandler
270262
.run { CompletableClickHandler().apply { handle(clickContext) } }
271263
.onComplete { ex ->
@@ -286,27 +278,22 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
286278
)
287279
}
288280

289-
event.isCancelled = completedClickHandler.cancelled
281+
// Update the cancellation state of the event
282+
if (completedClickHandler.cancelled) {
283+
event.isCancelled = true
284+
}
290285
}
291286

292287
/** Converts a bukkit [action] to a [ClickType]. */
293288
private fun convertAction(action: Action, sneaking: Boolean): ClickType {
294289
if (action.isRightClick) {
295-
if (sneaking) {
296-
return ClickType.SHIFT_RIGHT
297-
}
298-
290+
if (sneaking) return ClickType.SHIFT_RIGHT
299291
return ClickType.RIGHT
300292
}
301-
302293
if (action.isLeftClick) {
303-
if (sneaking) {
304-
return ClickType.SHIFT_LEFT
305-
}
306-
294+
if (sneaking) return ClickType.SHIFT_LEFT
307295
return ClickType.LEFT
308296
}
309-
310297
return ClickType.UNKNOWN
311298
}
312299

interfaces/src/main/kotlin/com/noxcrew/interfaces/interfaces/InterfaceProperties.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ public data class InterfaceProperties<P : Pane>(
1515
/** A collection of click handlers that will be run before each click without blocking. */
1616
public val clickPreprocessors: Collection<ClickHandler> = emptySet(),
1717
/** A post-processor applied to all items placed in the inventory. */
18-
public val itemPostProcessor: ((ItemStack) -> Unit)? = {}
18+
public val itemPostProcessor: ((ItemStack) -> Unit)? = {},
1919
)

0 commit comments

Comments
 (0)