Skip to content

Commit

Permalink
更新至2.0.5, 添加阻止玩家点击实体/方块的配置项
Browse files Browse the repository at this point in the history
  • Loading branch information
4o4E committed Jul 5, 2022
1 parent 9a5e0a7 commit cc0bb1f
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 4 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "top.e404"
version = "2.0.4"
version = "2.0.5"

repositories {
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/")
Expand All @@ -20,7 +20,7 @@ dependencies {
// spigot
compileOnly("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT")
// eplugin
implementation("top.e404:eplugin:1.0.1")
implementation("top.e404:eplugin:1.0.2")
}

tasks {
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/Boom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Boom : EPlugin() {
ArmorStandListener.register()
StickListener.register()
DeathListener.register()
ClickListener.register()
Update.init()
for (line in logo) info(line)
info("&a加载完成, 作者404E, 感谢使用")
Expand Down
50 changes: 48 additions & 2 deletions src/main/kotlin/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ object Config : EConfig(
val preventUseCommand: PreventUseCommand?,
val transformUseCommand: TransformUseCommandConfig?,
val limitEntitySpawn: Map<String, Int>?,
val preventClickBlock: List<ClickBlockConfig>,
val preventClickEntity: List<ClickEntityConfig>,
)

fun ConfigurationSection.getWorldConfig(path: String) =
Expand Down Expand Up @@ -100,9 +102,14 @@ object Config : EConfig(
transformUseCommand = cfg.getTransformUseCommand("transform_use_command"),
limitEntitySpawn = cfg.getConfigurationSection("limit_entity_spawn")?.let { limit ->
limit.getKeys(false).associate { key -> key.formatAsConst() to (limit.getIntOrNull(key) ?: 100) }
}
},
preventClickBlock = cfg.getMapList("prevent_click_block").mapNotNull { map ->
map.asClickBlockConfig()
},
preventClickEntity = cfg.getMapList("prevent_click_entity").mapNotNull { map ->
map.asClickEntityConfig()
},
)

}

fun ConfigurationSection.getExplosionConfig(path: String) =
Expand Down Expand Up @@ -253,4 +260,43 @@ object Config : EConfig(
}
}
}


fun Map<*, *>.asClickBlockConfig(): ClickBlockConfig? {
val item = this["item"]?.toString()?.toRegex() ?: return null
val block = this["block"]?.toString()?.toRegex() ?: return null
val type = this["type"]?.toString()?.asClickType() ?: return null
return ClickBlockConfig(item, block, type)
}

data class ClickBlockConfig(
val item: Regex,
val block: Regex,
val type: ClickType
)

fun Map<*, *>.asClickEntityConfig(): ClickEntityConfig? {
val item = this["item"]?.toString()?.toRegex() ?: return null
val entity = this["entity"]?.toString()?.toRegex() ?: return null
val type = this["type"]?.toString()?.asClickType() ?: return null
return ClickEntityConfig(item, entity, type)
}

data class ClickEntityConfig(
val item: Regex,
val entity: Regex,
val type: ClickType
)

fun String.asClickType() = ClickType.values().firstOrNull { it.regex.matches(this) }

enum class ClickType(
regex: String
) {
LEFT("(?i)l|left"),
RIGHT("(?i)r|right"),
ALL("(?i)a|all");

val regex = Regex(regex)
}
}
145 changes: 145 additions & 0 deletions src/main/kotlin/listener/ClickListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package top.e404.boom.listener

import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.block.Action
import org.bukkit.event.entity.EntityDamageByEntityEvent
import org.bukkit.event.player.PlayerInteractEntityEvent
import org.bukkit.event.player.PlayerInteractEvent
import top.e404.boom.PL
import top.e404.boom.config.Config
import top.e404.boom.config.Lang
import top.e404.eplugin.listener.EListener

object ClickListener : EListener(PL) {
@EventHandler
fun PlayerInteractEvent.onEvent() {
val block = clickedBlock ?: return
val hand = hand ?: return
val cfg = Config.getEachOrGlobal(player.world.name) { preventClickBlock } ?: return
val item = player.inventory.getItem(hand).type.name
for ((itemRegex, blockRegex, type) in cfg) {
// 检测破坏 && 动作不是破坏
if (type == Config.ClickType.LEFT && action != Action.LEFT_CLICK_BLOCK) continue
// 检测点击 && 动作不是点击
if (type == Config.ClickType.RIGHT && action != Action.RIGHT_CLICK_BLOCK) continue
// 物品或方块不匹配
if (!blockRegex.matches(block.type.name)
|| !itemRegex.matches(item)
) continue
isCancelled = true
plugin.debug {
Lang[
"click.prevent",
"player" to player.name,
"item" to item,
"target" to block.type.name,
"world" to player.world.name,
"x" to block.x,
"y" to block.y,
"z" to block.z,
]
}
return
}
plugin.debug {
Lang[
"click.pass",
"player" to player.name,
"item" to item,
"target" to block.type.name,
"world" to player.world.name,
"x" to block.x,
"y" to block.y,
"z" to block.z,
]
}
}

@EventHandler
fun PlayerInteractEntityEvent.onEvent() {
val cfg = Config.getEachOrGlobal(player.world.name) { preventClickEntity } ?: return
val item = player.inventory.getItem(hand).type.name
val entity = rightClicked.type.name
for ((itemRegex, entityRegex, type) in cfg) {
// 忽略left, 检测right/all
if (type == Config.ClickType.LEFT) continue
// 物品或方块不匹配
if (!entityRegex.matches(entity)
|| !itemRegex.matches(item)
) continue
isCancelled = true
plugin.debug {
val l = rightClicked.location
Lang[
"click.prevent",
"player" to player.name,
"item" to item,
"target" to entity,
"world" to player.world.name,
"x" to l.blockX,
"y" to l.blockY,
"z" to l.blockZ,
]
}
return
}
plugin.debug {
val l = rightClicked.location
Lang[
"click.pass",
"player" to player.name,
"item" to item,
"target" to entity,
"world" to player.world.name,
"x" to l.blockX,
"y" to l.blockY,
"z" to l.blockZ,
]
}
}

@EventHandler
fun EntityDamageByEntityEvent.onEvent() {
val damager = damager
if (damager !is Player) return
val cfg = Config.getEachOrGlobal(damager.world.name) { preventClickEntity } ?: return
val item = damager.inventory.itemInMainHand.type.name
for ((itemRegex, entityRegex, type) in cfg) {
// 忽略right, 检测left/all
if (type == Config.ClickType.RIGHT) continue
// 物品或方块不匹配
if (!entityRegex.matches(entityType.name)
|| !itemRegex.matches(item)
) continue
isCancelled = true
plugin.debug {
val l = entity.location
Lang[
"click.prevent",
"player" to damager.name,
"item" to item,
"target" to entityType.name,
"world" to damager.world.name,
"x" to l.blockX,
"y" to l.blockY,
"z" to l.blockZ,
]
}
return
}
plugin.debug {
val l = entity.location
Lang[
"click.pass",
"player" to damager.name,
"item" to item,
"target" to entityType.name,
"world" to damager.world.name,
"x" to l.blockX,
"y" to l.blockY,
"z" to l.blockZ,
]
}
}
}
24 changes: 24 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,30 @@ global:
# bat: 0 # 意为阻止所有的蝙蝠生成(几率0%)
# zombified_piglin: 50 # 意为僵尸猪灵只有50%的几率生成(其他的生成会被取消)

# 阻止玩家使用物品点击方块
# 空手时item为AIR
prevent_click_block:
# 以下这条配置的效果是阻止玩家使用刷怪蛋点击刷怪笼
# - # 玩家点击方块时的物品类型匹配正则
# item: "(?i)[a-z]+_spawn_egg"
# # 玩家点击的方块类型匹配正则
# block: "(?i)spawner"
# # 点击的类型 (left/right/all)
# # left对应挖掘, right对应点击
# type: right

# 阻止玩家使用物品点击实体
# 空手时item为AIR
prevent_click_entity:
# 以下这条配置的效果是阻止玩家使用胡萝卜点击猪
# - # 玩家点击方块时的物品类型匹配正则
# item: "(?i)carrot"
# # 玩家点击的方块类型匹配正则
# entity: "(?i)pig"
# # 点击的类型 (left/right/all)
# # left对应伤害, right对应点击
# type: right

# 单独世界设置
# 可以自行扩展, 配置格式与global的一致
# 缺失的内容将使用global的
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ weather:
sun: ""
thunder: "雷暴"

click:
prevent: "阻止玩家{player}手持{item}点击{target}(world: {world}, x: {x}, y: {y}, z: {z})"
pass: "未处理玩家{player}手持{item}点击{target}(world: {world}, x: {x}, y: {y}, z: {z})"

plugin_command:
reload_done: "&a重载完成"
stick_give: "&a已给予"
Expand Down

0 comments on commit cc0bb1f

Please sign in to comment.