@@ -13,8 +13,12 @@ import org.bukkit.entity.Player
1313import org.bukkit.event.EventHandler
1414import org.bukkit.event.EventPriority
1515import org.bukkit.event.Listener
16+ import org.bukkit.event.block.Action
17+ import org.bukkit.event.player.PlayerInteractEvent
1618import org.bukkit.event.player.PlayerSwapHandItemsEvent
1719import org.bukkit.event.player.PlayerToggleSneakEvent
20+ import org.bukkit.inventory.EquipmentSlot
21+
1822
1923private val confirmationKeyString by config(
2024 " confirmationKey" , ConfirmationKey .JUMP .name, comment = """
@@ -37,13 +41,17 @@ enum class ConfirmationKey(val keybind: String) {
3741 JUMP (" <key:key.jump>" ),
3842 SWAP_HANDS (" <key:key.swapOffhand>" ),
3943 SNEAK (" <key:key.sneak>" ),
44+ LEFT_CLICK (" <key:key.attack>" ),
45+ RIGHT_CLICK (" <key:key.use>" ),
4046 ;
4147
4248 fun handler (player : Player , block : () -> Unit ): ConfirmationKeyHandler {
4349 return when (this ) {
4450 SWAP_HANDS -> SwapHandsHandler (player, block)
4551 JUMP -> JumpHandler (player, block)
4652 SNEAK -> SneakHandler (player, block)
53+ LEFT_CLICK -> ClickHandler (player, block, Action .LEFT_CLICK_AIR , Action .LEFT_CLICK_BLOCK )
54+ RIGHT_CLICK -> ClickHandler (player, block, Action .RIGHT_CLICK_AIR , Action .RIGHT_CLICK_BLOCK )
4755 }.apply { initialize() }
4856 }
4957
@@ -107,4 +115,16 @@ class SneakHandler(override val player: Player, override val block: () -> Unit)
107115 event.isCancelled = true
108116 block()
109117 }
110- }
118+ }
119+
120+ class ClickHandler (override val player : Player , override val block : () -> Unit , vararg val actions : Action ) : ConfirmationKeyHandler {
121+ @EventHandler(priority = EventPriority .HIGHEST , ignoreCancelled = true )
122+ fun onInteract (event : PlayerInteractEvent ) {
123+ if (event.player.uniqueId != player.uniqueId) return
124+ if (event.hand != EquipmentSlot .HAND ) return
125+ if (event.action !in actions) return
126+ event.isCancelled = true
127+ block()
128+ }
129+ }
130+
0 commit comments