Skip to content

Commit

Permalink
fix: Right click air is cancelled by default, check whether useItemIn…
Browse files Browse the repository at this point in the history
…Hand is set to allow instead
  • Loading branch information
0ffz committed Jul 30, 2024
1 parent ad94c89 commit 0df093a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.bukkit.Bukkit
import org.bukkit.event.Event
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.block.Action
import org.bukkit.event.player.PlayerInteractEntityEvent
import org.bukkit.event.player.PlayerInteractEvent

Expand All @@ -31,6 +33,11 @@ class OnItemLeftClickBlock
@SerialName("geary:item_right_click")
class OnItemRightClick


@Serializable
@SerialName("geary:item_right_click_block")
class OnItemRightClickBlock

@Serializable
@SerialName("geary:item_right_click_entity")
class OnItemRightClickEntity
Expand All @@ -51,25 +58,17 @@ class ItemInteractBridge : Listener {
heldItem.emit<OnItemLeftClick>()
}

@EventHandler(ignoreCancelled = true)
@EventHandler
fun PlayerInteractEvent.onClick() {
val gearyPlayer = player.toGearyOrNull() ?: return
if(useItemInHand() == Event.Result.DENY) return
val heldItem = player.inventory.toGeary()?.get(hand ?: return) ?: return

// Right click gets fired twice, so we manually prevent two right-clicks within several ticks of each other.
fun rightClicked(): Boolean {
val currTick = Bukkit.getServer().currentTick
val eId = player.entityId
val cooldownRightClicked = rightClicked && currTick - rightClickCooldowns[eId] > 3
if (cooldownRightClicked) {
rightClickCooldowns[eId] = currTick
}
return cooldownRightClicked
}

heldItem.emit<OnItemInteract>()

if (leftClicked) heldItem.emit<OnItemLeftClickBlock>()
if (rightClicked()) heldItem.emit<OnItemRightClick>()
if (rightClicked) heldItem.emit<OnItemRightClick>()
if (action == Action.RIGHT_CLICK_BLOCK) heldItem.emit<OnItemRightClickBlock>()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mineinabyss.geary.papermc.bridge.getters

import com.mineinabyss.geary.actions.Action
import com.mineinabyss.geary.actions.ActionGroupContext
import com.mineinabyss.idofront.typealiases.BukkitEntity
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.bukkit.Location
import org.bukkit.entity.LivingEntity


@Serializable
@SerialName("geary:get.location")
class GetLocation : Action {
override fun ActionGroupContext.execute(): Location? = entity.get<BukkitEntity>()?.location
}

0 comments on commit 0df093a

Please sign in to comment.