Skip to content

Commit

Permalink
[6.2.0] 改进 PlayerWorldContactEvent,移除 beta 版本号
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Nov 26, 2024
1 parent d39091e commit 8acafa3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=taboolib
version=6.2.0-beta38
version=6.2.0
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.caching.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.util.Vector
import taboolib.common.Inject
import taboolib.common.LifeCycle
import taboolib.common.event.CancelableInternalEvent
import taboolib.common.event.InternalEventBus
import taboolib.common.platform.Awake
import taboolib.common.platform.Platform
import taboolib.common.platform.PlatformSide
import taboolib.common.platform.event.SubscribeEvent
import taboolib.common.platform.event.EventPriority
import taboolib.common.platform.function.registerBukkitListener
import taboolib.platform.util.*

/**
Expand Down Expand Up @@ -145,51 +148,50 @@ class PlayerWorldContactEvent(val player: Player, val action: Action) : Cancelab

@Inject
@PlatformSide(Platform.BUKKIT)
private companion object {

val isListened: Boolean
get() = InternalEventBus.isListening(PlayerWorldContactEvent::class.java)
companion object {

/**
* 左键交互实体(造成伤害)
* 使用优先级
*/
@SubscribeEvent
fun onDamage(e: EntityDamageByEntityEvent) {
val player = e.damager as? Player ?: return
// 仅限左键常规攻击
if (e.cause == EntityDamageEvent.DamageCause.ENTITY_ATTACK || e.cause == EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK) {
// 交互事件
if (isListened && !PlayerWorldContactEvent(player, Action.LeftClickEntity(EquipmentSlot.HAND, e.entity, e)).callIf()) {
e.isCancelled = true
}
}
}
var usePriority = EventPriority.NORMAL

/**
* 左键、右键、物理交互方块
* 本插件是否监听了 [PlayerWorldContactEvent] 事件
*/
@SubscribeEvent
fun onInteract(e: PlayerInteractEvent) {
val isListened: Boolean
get() = InternalEventBus.isListening(PlayerWorldContactEvent::class.java)

@Awake(LifeCycle.ENABLE)
private fun onEnable() {
if (!isListened) return
val action = when {
e.isRightClick() -> Action.RightClickBlock(e.hand ?: EquipmentSlot.HAND, e.clickedBlock, e.blockFace, e)
e.isLeftClick() -> Action.LeftClickBlock(e.hand ?: EquipmentSlot.HAND, e.clickedBlock, e.blockFace, e)
e.isPhysical() -> Action.Physical(e.clickedBlock, e.blockFace, e)
else -> return
// 左键交互实体(造成伤害)
registerBukkitListener(EntityDamageByEntityEvent::class.java, usePriority) { e ->
val player = e.damager as? Player ?: return@registerBukkitListener
// 仅限左键常规攻击
if (e.cause == EntityDamageEvent.DamageCause.ENTITY_ATTACK || e.cause == EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK) {
// 交互事件
if (!PlayerWorldContactEvent(player, Action.LeftClickEntity(EquipmentSlot.HAND, e.entity, e)).callIf()) {
e.isCancelled = true
}
}
}
if (!PlayerWorldContactEvent(e.player, action).callIf()) {
e.isCancelled = true
// 左键、右键、物理交互方块
registerBukkitListener(PlayerInteractEvent::class.java, usePriority) { e ->
val action = when {
e.isRightClick() -> Action.RightClickBlock(e.hand ?: EquipmentSlot.HAND, e.clickedBlock, e.blockFace, e)
e.isLeftClick() -> Action.LeftClickBlock(e.hand ?: EquipmentSlot.HAND, e.clickedBlock, e.blockFace, e)
e.isPhysical() -> Action.Physical(e.clickedBlock, e.blockFace, e)
else -> return@registerBukkitListener
}
if (!PlayerWorldContactEvent(e.player, action).callIf()) {
e.isCancelled = true
}
}
}

/**
* 右键交互实体
*/
@SubscribeEvent
fun onInteractEntity(e: PlayerInteractAtEntityEvent) {
if (!isListened) return
if (!PlayerWorldContactEvent(e.player, Action.RightClickEntity(e.hand, e.rightClicked, e.clickedPosition, e)).callIf()) {
e.isCancelled = true
// 右键交互实体
registerBukkitListener(PlayerInteractAtEntityEvent::class.java, usePriority) { e ->
if (!PlayerWorldContactEvent(e.player, Action.RightClickEntity(e.hand, e.rightClicked, e.clickedPosition, e)).callIf()) {
e.isCancelled = true
}
}
}
}
Expand Down

0 comments on commit 8acafa3

Please sign in to comment.