Skip to content

Commit 5b4f0ec

Browse files
committed
feat(menu): 添加物品不可破坏与数据值配置支持
- 在 Meta 类中新增 unbreakable 和 data 字段及其动态解析逻辑 - 支持通过配置文件设置物品的 unbreakable 属性 - 支持通过配置文件设置物品的数据值(damage) - 更新 MenuSerializer 以支持新属性的继承与解析 - 修复 Item 构建时名称可能为空的问题 - 为 EvalResult 添加 asInt 方法以支持整数类型转换
1 parent 87b414a commit 5b4f0ec

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

common/src/main/kotlin/trplugins/menu/util/EvalResult.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package trplugins.menu.util
22

3+
import taboolib.common5.cint
4+
35
/**
46
* @author Arasple
57
* @date 2021/1/31 11:53
@@ -19,6 +21,10 @@ value class EvalResult(val any: Any? = null) {
1921
return any.toString()
2022
}
2123

24+
fun asInt(def: Int = 0): Int {
25+
return any?.cint ?: def
26+
}
27+
2228
companion object {
2329

2430
val TRUE = EvalResult(true)

common/src/main/kotlin/trplugins/menu/util/conf/Property.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ enum class Property(val default: String, val regex: Regex) {
208208
*/
209209
ICON_DISPLAY_LORE("lore", "(display)?-?lores?"),
210210

211+
/**
212+
* 菜单图标显示 - 物品损伤值
213+
*/
214+
ICON_DISPLAY_DATA("data", "(display)?-?data"),
215+
211216
/**
212217
* 菜单图标显示 - 物品数量
213218
*/
@@ -243,6 +248,11 @@ enum class Property(val default: String, val regex: Regex) {
243248
*/
244249
ICON_DISPLAY_HIDE_TOOLTIP("hide_tooltip", "hide_?tool(tip)?"),
245250

251+
/**
252+
* 菜单图标 - Unbreakable
253+
*/
254+
ICON_DISPLAY_UNBREAKABLE("unbreakable", "unbreak(able)?"),
255+
246256
/**
247257
* 菜单图标 - 子图标
248258
*/

plugin/src/main/kotlin/trplugins/menu/module/conf/MenuSerializer.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ object MenuSerializer : ISerializer {
360360
val hideTooltip = if (inherit.contains(Property.ICON_DISPLAY_HIDE_TOOLTIP)) {
361361
def!!.display.meta.hideTooltip
362362
} else Property.ICON_DISPLAY_HIDE_TOOLTIP.ofString(display, "false")
363+
val unbreakable = if (inherit.contains(Property.ICON_DISPLAY_UNBREAKABLE)) {
364+
def!!.display.meta.unbreakable
365+
} else Property.ICON_DISPLAY_UNBREAKABLE.ofString(display, "false")
366+
val data = if (inherit.contains(Property.ICON_DISPLAY_DATA)) {
367+
def!!.display.meta.data
368+
} else Property.ICON_DISPLAY_DATA.ofString(display, "")
363369

364370
// only for the subIcon
365371
val priority = Property.PRIORITY.ofInt(it, order)
@@ -396,7 +402,7 @@ object MenuSerializer : ISerializer {
396402
if (def != null && inherit.contains(Property.ICON_DISPLAY_LORE) && lore.isEmpty()) def.display.lore
397403
else CycleList(lore.map { Lore(line(it)) }),
398404
// 图标附加属性
399-
Meta(amount, shiny, flags, nbt, tooltipStyle, itemModel, hideTooltip)
405+
Meta(amount, shiny, flags, nbt, tooltipStyle, itemModel, hideTooltip, unbreakable, data)
400406
)
401407

402408
// i18n

plugin/src/main/kotlin/trplugins/menu/module/display/item/Item.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ open class Item(
9090
meta.tooltipStyle(session, this)
9191
meta.itemModel(session, this)
9292
meta.hideTooltip(session, this)
93+
meta.unbreakable(session, this)
94+
meta.data(session, this)
9395

9496
if (meta.hasAmount()) this.amount = meta.amount(session)
9597
}
@@ -132,7 +134,7 @@ open class Item(
132134
else {
133135
val current = cache[session.id]
134136
try {
135-
val new = buildItem(current!!) { name = parsedName(session) }
137+
val new = buildItem(current!!) { parsedName(session)?.let { name = it } }
136138
cache[session.id] = new
137139
} catch (t: Throwable) {
138140
t.stackTrace

plugin/src/main/kotlin/trplugins/menu/module/display/item/Meta.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ class Meta(
2424
val tooltip: String?,
2525
val itemModel: String?,
2626
val hideTooltip: String,
27+
val unbreakable: String,
28+
val data: String,
2729
) {
2830

2931
private val isAmountDynamic = amount.toIntOrNull() == null
3032
private val isShinyDynamic = !shiny.matches(Regexs.BOOLEAN)
3133
private val isHideTooltipDynamic = !hideTooltip.matches(Regexs.BOOLEAN)
3234
private val isNBTDynamic = nbt != null && Regexs.containsPlaceholder(nbt.toJsonSimplified())
35+
private val isUnbreakableDynamic = !unbreakable.matches(Regexs.BOOLEAN)
36+
private val isDataDynamic = data.toIntOrNull() == null
3337
val isDynamic = isAmountDynamic || isNBTDynamic || isShinyDynamic || isHideTooltipDynamic
3438

3539
fun amount(session: MenuSession): Int {
@@ -82,9 +86,23 @@ class Meta(
8286
}
8387

8488
fun hideTooltip(session: MenuSession, builder: ItemBuilder) {
85-
if ((hideTooltip.toBoolean()) || (isHideTooltipDynamic && session.placeholderPlayer.evalScript(hideTooltip).asBoolean())) {
89+
if (hideTooltip.toBoolean() || (isHideTooltipDynamic && session.placeholderPlayer.evalScript(hideTooltip).asBoolean())) {
8690
builder.isHideTooltip = true
8791
}
8892
}
8993

94+
fun unbreakable(session: MenuSession, builder: ItemBuilder) {
95+
if (unbreakable.toBoolean() || (isUnbreakableDynamic && session.placeholderPlayer.evalScript(unbreakable).asBoolean())) {
96+
builder.isUnbreakable = true
97+
}
98+
}
99+
100+
fun data(session: MenuSession, builder: ItemBuilder) {
101+
if (data.isEmpty()) {
102+
return
103+
}
104+
val evalData = session.parse(amount).toIntOrNull() ?: session.placeholderPlayer.evalScript(data).asInt(0)
105+
builder.damage = evalData
106+
}
107+
90108
}

0 commit comments

Comments
 (0)