AdventureKt

插件

· 作者 DeeChael

Adventure 库的 Kotlin DSL 支持。

0 本站下载 0 关注 更新于 1 年前

最新 2.1.1 · 1.21.6 · Velocity

一键开服(送7天)

该项目仍处于开发中。

部分接口可能无法使用/将在未来发生变化。

请自行承担使用风险。

⛏️ Kyori Adventure 的(可能)完整 Kotlin 支持。

❗ 注意

对于 Paper 平台,你有两个插件可供下载:bundle 和 spacefree。
bundle 版本会将 kotlin stdlib 阴影打包(shade)进插件,而 spacefree 版本将通过 Paper 提供的库加载器下载 kotlin stdlib。

对于 Velocity 平台,仅提供 bundle 版本,因为 Velocity 不提供从 Maven 仓库下载依赖的方式。

? 目标

由于 Kotlin 为我们带来了创建扩展的能力,我们可以创建 DSL。

本项目旨在为 adventure 的构建器模式 API 创建许多 DSL 实用程序,从而使你的生活更轻松。

? 制品

仓库

repositories {
    maven(uri("https://maven.nostal.ink/repository/maven-public/"))
}

依赖

dependencies {
    // 使用 shadowJar 将制品阴影打包到你的 jar 中
    // 或使用 Modrinth 上提供的独立 Paper 插件(你当前就在这里!)
    api("plutoproject.adventurekt:core:2.1.1")
}

// 如果你想将此项目阴影打包,我们建议将包重定位到你的命名空间下。
tasks.shadowJar {
    relocate("plutoproject.adventurekt", "com.example.libs.adventurekt")
}

☕ 用法

创建组件

你可以简单地使用 component 函数来创建组件。

val component: Component = component {
    text("This is a text component, nothing special.") // 简单创建一个组件
    text { "Text component here" } // 带返回值的 lambda
    translatable { "gui.ok" } // 使用与区域设置相关的字符串
    keybind { "key.inventory" } // 使用与按键绑定相关的字符串
    raw { Component.text("text") } // 直接使用 adventure 组件
    mini("<red>mini message!") // 使用 mini message
    newline() // 换行
    space() // 仅仅是一个 " "
    empty() // 无内容
}

如果你希望使用 adventure-kt 为 Minecraft 物品构建描述(lore),可以使用 componentList 函数。

val components: List<Component> = componentList {
    text { "first line" }
    text { "second line" }
}
ItemLore.lore(components)

应用样式

你可以使用 with 关键字添加样式,使用 without 关键字移除样式。
在 with 关键字之后通过 and 连接的所有样式都将被视为 with 样式。
否则,在 without 关键字之后通过 and 连接的所有样式都将被视为 without 样式。

此外,你还可以像 v1 那样使用旧式样式,即直接仅用 with 或 without 关键字连接所有样式。

component {
    // 由于阴影颜色是可自定义的,现在颜色分为文本类型和阴影类型
    // 只需在颜色前加上 text 和 shadow 即可在 advkt v2 中使用颜色类型
    text("text") with textRed and shadowBlack without italic and color // 结果没有任何变化,因为“without color”是最后一步
    text("text") with underlined with textAqua with runCommand("/say hello") // 带有“下划线”装饰、“ aqua ”文本颜色和“运行命令”点击事件
    
    // 2.1.0 之后,adventure kt 添加了更简单的颜色用法
    text("text") with red.text
}

如果你担心产生歧义,可以使用样式名称来添加样式。

component {
    text("text") color red.text deco underlind undeco italic // 带有红色文本颜色、下划线装饰且无斜体装饰
    text("text") color null shadow blue.shadow // 使用 null 移除颜色,使用 shadow 应用阴影颜色
}

如果你使用过 ArkUI 或 SwiftUI,可以在组件后使用函数来添加样式。

component {
    text("text")
        .color(red.text)
        .decoration(italic, false)
}

2.1.0 之后,你可以使用 style 函数来创建可复用的样式,或以更明显的方式应用样式。

val generalStyle = style {
    textColor("#66ccff")
    shadowColor(green)
    decoration(underlined)
    undecoration(italic)
}

component {
    text("text") provide generalStyle
    
    // 或者就这样
    text("text") provide style {
        textColor("#66ccff")
        shadowColor(green)
        decoration(underlined)
        undecoration(italic)
    }
}

点击与悬停事件

就像样式一样,你可以使用 with 和 without 关键字应用点击事件和悬停事件。

component {
    text("text") with /* 点击事件 */ runCommand() with /* 悬停事件 */ showText {
        text("hover text!")
    }

修改 MiniMessage

当添加 MiniMessage 字符串时,你可能需要更改 MiniMessage 对象,你可以修改组件的 mini message 对象。

component {
    miniMessage {
        strict()
        tags {
            tag("aa", Tag.inserting(Component.text("aa")))
        }
        
        // 你也可以将一个 MiniMessage 对象传递给构建器
        provide(MiniMessage.miniMessage())
    }
}

当添加 MiniMessage 文本时,你可以在添加文本时进行修改。

component {
    mini("<red> Welcome, <name>, <events>") {
        target(audience)

unparsedPlaceholder("name", "DeeChael")
        componentPlaceholder("events") {
            text("No events here!")
        }
    }
}

覆盖操作

你可以覆盖某些操作以获得更好的使用体验。

component {
    actions {
        newlineAction {
            // 执行一些操作
            it.container // ComponentKt 对象,即 adventure-kt 的组件构建器
            it.value // Component? 对象,将其设置为 null,构建器将不会追加此组件;或设置为其他组件以添加你想要的组件
        }
        spaceAction {
            // 执行一些操作
        }
        emptyAction {
            // 执行一些操作
        }
    }
}

例如,为了利用 newline 组件来创建组件列表而不是使用换行符:

fun lore(content: ComponentKt.() -> Unit): ItemLore {
    val components = mutableListOf<Component>()
    val last = component {
        actions {
            newlineAction {
                components.add(it.cleanBuild())
            }
        }
        content()
    }
    components.add(last)
    return ItemLore.lore(components)
}

在 2.1.0 版本之后,上述示例可以被 componentList 函数替代。

fun lore(content: ComponentKt.() -> Unit): ItemLore {
    return ItemLore.lore(componentList(content))
}

创建标题(v2 版本的 WIP)

title {
    mainTitle {
        text("This is a main title.")
    }
    subTitle {
        text("This is a sub title.")
    }
    // 支持 Kotlin duration 和 Java duration
    times {
        fadeIn(1.seconds)
        stay(1.seconds)
        fadeOut(1.seconds)
    }
}

完整用法示例

截图

result in game

代码

component {
        miniMessage {
            strict()
            tags {
                tag("aa", Tag.inserting(Component.text("aa")))
            }
            provide(MiniMessage.miniMessage()) // 最高优先级
        }

        actions {

            newlineAction {
                it.value = null // 清除原始组件
            }

        }

        replacements {
            override()
            replacement {
                once()
                matchLiteral("fuck")
                replace {
                    space()
                    text("****") with textRed and underlined
                    space()
                }
            }
        }

        text { "supplier style" }
        text { true }
        text("aaa") with "#66ccff" and bold and strikethrough without italic and strikethrough
        text("color showcase") with textRed with shadowDarkBlue
        text("bedrock colors") with textMaterialGold

        text("this is a super looooooooooooooooooooooong text to test gradient") with textGradient(red, green, yellow, darkPurple)

        // 使用组件默认的 mini message
        mini("<blue><bold>no extra data mini message")

        // 使用带有自定义占位符和其他内容的默认 mini message
        mini("<red> example, <name>, <events>") {
            unparsedPlaceholder("name", "DeeChael")
            componentPlaceholder("events") {
                text("No events here!")
            }
        }
    }

你是这个资源的作者?登录后可认领此资源。

评论 0

0.0
暂无评分
5 星
0
4 星
0
3 星
0
2 星
0
1 星
0
我的评分

登录后参与评分

还没有评论,来抢沙发吧

评论配图

相似资源