SkDialogs - Skript Dialogs

插件

· 作者 ch99q

使用纯 Skript 创建对话框、按钮菜单和确认弹窗,并读取玩家提交的所有输入。

0 本站下载 0 关注 更新于 2 个月前

需要前置:Skript

最新 1.2.0 · 26.2 · Paper / Purpur

一键开服(送7天)

SkDialogs

build paper-api skript-api

一个用于 Paper 的对话 API 的 Skript 插件,支持在 Minecraft 1.21.6 中新增的模态窗口。它提供了通过脚本构建和显示对话框的语法:包含文本字段、开关、滑块和下拉菜单的表单,可执行命令或打开其他对话框的按钮,以及收集玩家所有输入内容的点击事件。

例如,每位玩家在游玩前必须接受的服务器规则:

on join:
    if {rules accepted::%player's uuid%} is true:
        stop
    create dialog "rules":
        set title to "&6Server rules"
        set close on escape to false
        add body "&71. Be kind."
        add body "&72. No griefing."
        add button "accept" labeled "&2I accept the rules"
        add button "decline" labeled "&cDecline"
    show dialog "rules" to player

on dialog click "rules":
    if the clicked button is "accept":
        set {rules accepted::%event-player's uuid%} to true
        send "&aThanks for accepting. Have fun!" to event-player
    else:
        kick event-player due to "&cYou must accept the rules to play."

安装

  1. 在 Paper 1.21.8 或更高版本上安装 Skript 2.15 或更新版本。
  2. 将 最新发行版 中的 skdialogs-<version>.jar 放入 plugins/ 目录。
  3. 重启服务器。

本页面的每个示例都是完整的。将其粘贴到 plugins/Skript/scripts/ 中的一个文件中,运行 /sk reload all,然后在游戏中尝试使用。examples/ 目录包含了所有这些示例文件,以及一些更复杂的示例。

工作原理

每个对话框都遵循相同的三个步骤。

  1. create dialog "id": 定义一个对话框并将其注册到一个 ID 下。该部分内的行设置其标题、内容、输入项和按钮。
  2. show dialog "id" to player 显示该对话框。同一个对话框可以多次显示给任何玩家。
  3. on dialog click "id": 当按下按钮时触发。在此事件中,the clicked button 是按钮的 ID,而 dialog input "key" 是玩家输入的内容。

按钮还可以携带操作(执行命令、打开链接),从而完全跳过第 3 步。所有字符串都会经过 Skript 的聊天格式处理,与 send 相同:包括 & 颜色代码、十六进制颜色以及像 <translate:item.minecraft.filled_map> 这样的标签,每个客户端会以自己的语言进行渲染。

请求输入

对话框可以包含四种类型的输入:文本字段、开关、滑块和下拉菜单。每个输入都有一个键,点击处理器通过该键读取每个值。

command /character:
    trigger:
        create dialog "character":
            set title to "Create your character"
            add body "This cannot be changed later."
            add text input "name" labeled "Name" with default "%player%" with max length 16
            add toggle input "hardcore" labeled "Hardcore mode?" with default false
            add slider input "level" labeled "Starting level" from 1 to 10 with step 1 with default 1
            add dropdown input "class" labeled "Class" with options "knight", "ranger" and "mage" with default "knight"
            add button "confirm" labeled "&2Confirm"
        show dialog "character" to player

on dialog click "character":
    set {_name} to dialog input "name"
    set {_class} to dialog input "class"
    set {_level} to dialog input "level"
    send "&aWelcome, %{_name}% the %{_class}%, starting at level %{_level}%." to event-player

下拉菜单也可以列出其选项作为子部分,这样每个选项显示的标签可以与读取回的 ID 不同:

            add dropdown input "class" labeled "Class":
                option "knight" labeled "&fThe Knight" as default
                option "ranger" labeled "&aThe Ranger"
                option "mage" labeled "&bThe Mage"

自带操作的按钮

带有操作子句的按钮不需要点击处理器。它可以执行命令、打开链接、复制文本、预填充聊天框或打开另一个对话框。

command /menu:
    trigger:
        create dialog "menu":
            set title to "&6Server menu"
            add button labeled "&aSpawn" that runs "/spawn"
            add button labeled "&bWiki" that opens "https://docs.papermc.io"
            add button labeled "Copy server IP" that copies "play.example.com"
            add button labeled "Message an admin" that suggests "/msg admin "
            add exit button "close" labeled "Close"
        show dialog "menu" to player

that runs 可以直接获取来自对话框的输入。$(key) 占位符会在点击时填充该输入的值,从而使对话框变成向命令提交数据的表单:

command /rename:
    trigger:
        create dialog "rename":
            set title to "Rename yourself"
            add text input "name" labeled "New name" with max length 16
            add button labeled "&2Apply" that runs "/nick $(name)"
            add exit button "cancel" labeled "Cancel"
        show dialog "rename" to player

注意这里有两个替换过程。$(name) 由对话框在点击时填充,而 Skript 自己的 %player% 则在创建对话框时填充。

对话框菜单

add dialog "id" to the list 将一个对话框转换为菜单,其条目可以打开其他对话框。带有 that shows 的按钮可以链接回来,从而实现菜单循环。

command /hub:
    trigger:
        create dialog "hub-rules":
            set title to "&e规则"
            add body "1. 友善待人。2. 禁止破坏。3. 玩得开心。"
            add button "back" labeled "← 返回" that shows "hub"
        create dialog "hub-kit":
            set title to "&a新手套装"
            add button "warrior" labeled "战士"
            add button "mage" labeled "法师"
            add button "back" labeled "← 返回" that shows "hub"
        create dialog "hub":
            set title to "&6服务器大厅"
            add dialog "hub-rules" to the list
            add dialog "hub-kit" to the list
            add exit button "close" labeled "关闭"
        show dialog "hub" to player

on dialog click "hub-kit":
    if the clicked button is "warrior" or "mage":
        send "&a你选择了 %the clicked button% 套装。" to event-player

保持对话框打开

默认情况下,按下按钮后对话框会关闭。使用 set after click to keep open 可使其保留在屏幕上,这样玩家可以多次点击按钮,每次点击都会重新读取当前的输入值。

command /volume:
    trigger:
        create dialog "volume":
            set title to "音乐音量"
            set after click to keep open
            add slider input "level" labeled "音量" from 0 to 100 with step 5 with default 50
            add button "apply" labeled "应用"
            add exit button "done" labeled "完成"
        show dialog "volume" to player

on dialog click "volume":
    if the clicked button is "apply":
        set {volume::%event-player's uuid%} to dialog input "level"
        send "&7音量已设置为 &f%{volume::%event-player's uuid%}%&7。" to event-player

语法参考

Skript 完整模式集(使用 Skript 自身表示法):[方括号] 表示可选内容,(a|b) 表示选择项,%string% 表示该类型的任意表达式。

创建对话框

(create|make) dialog [(named|with id)] %string%:

打开一个定义对话框的部分,并按给定的 ID 注册它。该部分主体仅运行一次,立即执行;其中仅允许使用以下效果。如果创建的对话框 ID 已存在,则会替换旧的对话框。

ID 是你在其他所有地方引用该对话框的方式:show dialog、on dialog click、that shows 以及 add dialog ... to the list。

属性

set [the] title [of [the] dialog] to %string%
set [the] external title [of [the] dialog] to %string%
set [the] columns [of [the] dialog] to %number%
set (closing|close) on escape [of [the] dialog] to %boolean%
set [the] after[ ]click [behaviour|behavior] [of [the] dialog] to (close|keep open|wait [for response])
属性 默认值 含义
title 对话框的 ID 显示在窗口顶部的文本。
external title title 客户端在对话框外部列出该对话框时显示的标签。
columns 2 多操作或列表对话框布局中按钮的列数。
close on escape true 是否按 Escape 键关闭对话框。
after click close 按下按钮后对话框的行为。

after click 的选项包括 close(对话框关闭)、keep open(保留在屏幕上,以便玩家可以多次点击按钮并每次重新读取输入)和 wait for response(变灰直到服务器回复)。

原版暂停菜单和快捷操作按钮来自加载时解析的数据包标签,因此运行时创建的对话框无法出现在其中。

主体内容

add [a] [plain] body %string% [with width %number%]
add [an] item body %itemstack% [with description %string%] [without tooltip] [without decorations] [sized %number% by %number%]

添加一行内容:一条消息或渲染的物品。物品的描述会显示在其旁边,without tooltip 隐藏其悬停提示框,without decorations 隐藏其数量和耐久度栏,sized 设置其渲染所在的框(宽 x 高,默认为 16x16,除非另行设置)。

输入控件

每个输入控件都有一个键(其在点击事件中的名称)和一个可选的 labeled 标签,默认为键名。

add text input %string% [labeled %string%] [with default %string%] [with max[imum] length %number%] [with width %number%] [multiline [up to %number% lines]]
add (boolean|toggle) input %string% [labeled %string%] [with default %boolean%] [with values %string% and %string%]
add (slider|number) input %string% [labeled %string%] (from|between) %number% (to|and) %number% [(by|with step) %number%] [with default %number%] [with format %string%] [with width %number%]
add (dropdown|choice) input %string% [labeled %string%] with options %strings% [with default %string%] [with width %number%]
输入类型 玩家看到的内容 dialog input "key" 返回值
text 文本字段,配合 multiline 支持多行 输入的文本(max length 默认为 32)
toggle 开关 true 或 false
slider 给定范围内的滑块 数字
dropdown 选项列表 所选选项的 ID

没有默认值的滑块从范围的低端开始。with format 用于格式化滑块的标签,其中 {label} 和 {value} 标记替换位置,例如 with format "{value} blocks"。
toggle 的 with values 设置 $(key) 命令占位符在不同状态下的值,例如使用 "64" 和 "1" 代替 true 和 false。

下拉列表还可以将其选项列为一个部分,使每个选项显示与回读 ID 不同的标签:

add (dropdown|choice) input %string% [labeled %string%]:
    option %string% [labeled %string%] [[as] [the] default]

按钮

add [a] button [%string%] [labeled %string%] [with tooltip %string%] [with width %number%] [that (runs|opens|copies|suggests|shows) %string%]
add [an] exit button [%string%] [labeled %string%] [with tooltip %string%] [with width %number%]

标签默认使用 id。按钮的功能取决于其动作子句:

子句 点击时
none 触发 on dialog click;此处需要 id,以便你在其中进行匹配。
that runs %string% 以玩家身份运行命令。在点击时,$(key) 占位符会被填入该输入的值。
that opens %string% 请求打开 URL。
that copies %string% 将文本复制到剪贴板。
that suggests %string% 在聊天框中预填文本。
that shows %string% 打开具有该 id 的对话框。

退出按钮是多操作或列表对话框在其他按钮下方显示的单个“返回/取消”按钮;一个对话框最多只能有一个。

that runs 以玩家身份并以玩家的权限运行命令,且客户端会先要求玩家确认一条普通命令。需要比玩家更高权限的工作应放在 on dialog click 中处理。

对话框的布局由其按钮决定。没有按钮则是纯提示;一个按钮是带有该按钮的提示;一个按钮加一个退出按钮是“是/否”确认;更多按钮则是按配置列数显示的多操作对话框。设置 columns 会将确认对话框变回多操作对话框。

对话框列表

add dialog %string% to [the] list
add [the] server links to [the] list

通过 id 将另一个对话框作为条目添加到当前对话框的菜单中。包含任何列表条目的对话框会以列表形式显示:每个条目都是一个打开对应对话框的按钮。server links 则展示服务器广告链接列表。

服务器链接

add [a] server link labeled %string% to [url] %string%
add [a|the] (bug report|community guidelines|support|status|feedback|community|website|forums|news|announcements) server link to [url] %string%
(clear|delete) [all] [the] server links

管理服务器的链接列表,这些链接会在暂停菜单和 server links 对话框中的“Server Links”部分下显示。链接是全局的,因此 on load 是设置它们的自然位置。输入的链接由每个客户端以其自己的语言进行标记;带标签的链接显示你的文本,而 <translate:...> 标签也使其支持多语言。先清除再添加可以让重新加载的脚本重建列表,避免重复堆叠。

on load:
    clear the server links
    add server link labeled "<translate:item.minecraft.filled_map>" to "https://map.example.com"
    add website server link to "https://example.com"

显示与关闭

(show|open) dialog %string% (to|for) %players%
close [the] [current] dialog (for|of|to) %players%

show dialog 显示已创建的对话框;向 all players 显示也可以。close dialog 关闭每个玩家当前打开的任何对话框(来自任何插件)。

点击事件

on dialog (click|submit|button click):
on dialog (click|submit) [(of|for)] %string%:

当玩家按下本插件创建的对话框上的事件按钮(即没有动作子句的按钮)时触发。可选的 id 可将事件限制为特定对话框。

在事件内部:

event-player                                # 提交对话框的玩家
[the] clicked [dialog] button               # 被按下按钮的 id
[the] [clicked] dialog id                   # 对话框的 id
[the] [dialog] input [value] [(of|for)] %string%    # 某个输入的值,通过 key 获取
[the] [dialog] value of [input] %string%            # 同上,表述不同

输入的值可以是文本、布尔值或数字,与上述输入表一致。读取对话框不存在的 key 将返回空值。

构建

需要 JDK 25,这是 Paper 26.2 所需的版本(包装器会自动获取 Gradle 本身)。

./gradlew build

生成的 jar 包位于 build/libs/ 中。该插件针对 gradle.properties 中固定的确切 Paper 构建版本和 Skript 发布版本进行编译;若要跟随更新版本,请提升相应属性并修复不再编译的代码。

CI 会对每次推送和拉取请求进行构建,每周还有两个任务分别针对各依赖项的最新版本进行编译:paper-api 用于 Paper(其对话框 API 处于实验阶段),以及 skript-api 用于 Skript(后者正在弃用插件所基于的注册 API)。任一破坏性变更都会表现为失败的运行记录,而不是在服务器上突然出现意外。推送 vX.Y.Z 标签会将 jar 包附加到 GitHub Release 并发布至 Modrinth。

贡献

欢迎提交错误报告、功能请求和拉取请求。有关如何设置开发环境以及什么样的拉取请求是合格的,请参阅 CONTRIBUTING.md。

许可证

MIT

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

评论 0

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

登录后参与评分

还没有评论,来抢沙发吧

评论配图

相似资源