让按钮执行指令
这一页做一个有四个按钮的画面,每个按钮示范一种写法:以玩家身份执行、以后台身份执行、要权限才能点、要花费才能点。
还没写过画面的话,先做完 从零写第一个画面。
第 1 步:新建文件
在 plugins/CloudUI/screens/ 里新建 tut_commands.yml,先放背景、标题和关闭按钮:
portrait:
enabled: false
camera:
animation: false
elements:
backdrop:
type: image
image: main/backdrop.png
x: 0
y: 0
fit: cover
resolution: 0.25
title:
type: text
text: '按钮与指令'
font: title
size: 48
color: '#E6D3A1'
x: 960
y: 330
align: center
close:
type: button
image: main/close.png
hover: main/close_hover.png
x: 842
y: 620
click:
left: ['close']
close-label:
type: text
text: '关闭'
font: text
size: 20
color: '#E2EEF4'
x: 960
y: 637
align: center
下面每一步的按钮都接在 elements 下面。按钮用的底图 main/tile_s.png 宽 164、高 111,这里没写 hit,点击范围就是整张图。
第 2 步:以玩家身份执行
spawn:
type: button
image: main/tile_s.png
hover: main/tile_s_hover.png
x: 608
y: 440
click:
left: ['close', 'command: spawn']
spawn-label:
type: text
text: '回城'
font: text
size: 24
color: '#E2EEF4'
x: 690
y: 485
align: center
click.left 是一串动作,从左到右依次执行。
command: spawn:等于玩家自己在聊天栏输入/spawn,开头的斜线写不写都可以。玩家自己没有这条指令的权限,就执行不了。close写在前面:先关画面,画面关完才执行后面的指令。传送、打开别的插件的选单,都建议这样写。
/spawn 要你的服务器上有这条指令才有反应,没有的话换成任何一条玩家能用的指令。
第 3 步:以后台身份执行
gift:
type: button
image: main/tile_s.png
hover: main/tile_s_hover.png
x: 788
y: 440
click:
left:
- 'console: give {player} diamond 1'
- 'notice: <green>拿到 1 颗钻石'
gift-label:
type: text
text: '领钻石'
font: text
size: 24
color: '#E2EEF4'
x: 870
y: 485
align: center
console::由后台执行,不看玩家有没有权限。{player}会换成点按钮的玩家名字。notice::画面顶端滑出一条通知横幅,画面开着时用它提示最合适。- 这里没写
close,点完画面留着,可以连点。
动作列表可以像第 2 步那样写在一行的方括号里,也可以像这里一行一个,两种写法一样。
第 4 步:要权限才能点
要加条件时,left 下面不再直接写动作,改成几个键:
vip:
type: button
image: main/tile_s.png
hover: main/tile_s_hover.png
x: 968
y: 440
click:
left:
permission: tut.vip
actions:
- 'console: effect give {player} speed 60 1'
- 'notice: <green>已获得 60 秒加速'
deny:
- 'notice: <red>这个按钮只给 VIP 使用'
vip-label:
type: text
text: 'VIP 加速'
font: text
size: 24
color: '#E2EEF4'
x: 1050
y: 485
align: center
permission:玩家要有这个权限才执行。actions:条件都满足时执行的动作。deny:条件不满足时改做这些。不写deny的话,没有权限的玩家会听到拒绝音效,并看到一行「你没有权限」。
tut.vip 是随便取的权限名,用你的权限插件发给玩家就行。管理员默认什么权限都有,测试「没权限」的样子要用一个普通玩家的账号。
第 5 步:要花费才能点
buy:
type: button
image: main/tile_s.png
hover: main/tile_s_hover.png
x: 1148
y: 440
click:
left:
cost:
levels: 5
actions:
- 'console: give {player} golden_apple 1'
- 'notice: <green>花了 5 级经验,换到 1 个金苹果'
deny:
- 'notice: <red>需要 5 级经验'
buy-label:
type: text
text: '换金苹果'
font: text
size: 24
color: '#E2EEF4'
x: 1230
y: 485
align: center
cost 下面能写四种花费,可以同时写几种:
| 键 | 说明 |
|---|---|
money | 金钱,需要 Vault 和一个经济插件 |
points | 点券,需要 PlayerPoints |
levels | 经验等级 |
items | 物品,一项写成 物品 数量,数量不写就是 1,例如 ['DIAMOND 3'] |
例如同时要钱和物品:
cost:
money: 500
items: ['DIAMOND 3']
插件会先检查所有花费都够,才一次扣掉,然后执行 actions。有一样不够就什么都不扣,改执行 deny;没写 deny 时会提示缺了哪几样。
permission 和 cost 可以一起写,先看权限,再看花费。
第 6 步:重载,打开
存档后输入 /cui reload,再输入 /cui open tut_commands。
你应该看到标题下面一排四个按钮。依次点:
- 「回城」:画面关闭,执行
/spawn。 - 「领钻石」:背包多一颗钻石,画面顶端出现绿色通知,画面不关。
- 「VIP 加速」:有
tut.vip权限就获得加速,没有就看到红色通知。 - 「换金苹果」:等级够就扣 5 级、拿到金苹果,不够就看到红色通知。
完整文件
portrait:
enabled: false
camera:
animation: false
elements:
backdrop:
type: image
image: main/backdrop.png
x: 0
y: 0
fit: cover
resolution: 0.25
title:
type: text
text: '按钮与指令'
font: title
size: 48
color: '#E6D3A1'
x: 960
y: 330
align: center
spawn:
type: button
image: main/tile_s.png
hover: main/tile_s_hover.png
x: 608
y: 440
click:
left: ['close', 'command: spawn']
spawn-label:
type: text
text: '回城'
font: text
size: 24
color: '#E2EEF4'
x: 690
y: 485
align: center
gift:
type: button
image: main/tile_s.png
hover: main/tile_s_hover.png
x: 788
y: 440
click:
left:
- 'console: give {player} diamond 1'
- 'notice: <green>拿到 1 颗钻石'
gift-label:
type: text
text: '领钻石'
font: text
size: 24
color: '#E2EEF4'
x: 870
y: 485
align: center
vip:
type: button
image: main/tile_s.png
hover: main/tile_s_hover.png
x: 968
y: 440
click:
left:
permission: tut.vip
actions:
- 'console: effect give {player} speed 60 1'
- 'notice: <green>已获得 60 秒加速'
deny:
- 'notice: <red>这个按钮只给 VIP 使用'
vip-label:
type: text
text: 'VIP 加速'
font: text
size: 24
color: '#E2EEF4'
x: 1050
y: 485
align: center
buy:
type: button
image: main/tile_s.png
hover: main/tile_s_hover.png
x: 1148
y: 440
click:
left:
cost:
levels: 5
actions:
- 'console: give {player} golden_apple 1'
- 'notice: <green>花了 5 级经验,换到 1 个金苹果'
deny:
- 'notice: <red>需要 5 级经验'
buy-label:
type: text
text: '换金苹果'
font: text
size: 24
color: '#E2EEF4'
x: 1230
y: 485
align: center
close:
type: button
image: main/close.png
hover: main/close_hover.png
x: 842
y: 620
click:
left: ['close']
close-label:
type: text
text: '关闭'
font: text
size: 20
color: '#E2EEF4'
x: 960
y: 637
align: center
点完关不关画面
关不关全看动作里有没有 close,以及它写在哪里。
- 不写
close:点完画面留着。 close写在最前面:先关画面,后面的动作等画面关完才执行。- 想让玩家先看到提示再关:
['notice: <green>领取成功', 'delay: 30', 'close']。delay: 30是等 30 tick,也就是 1.5 秒。 - 想回到上一个画面而不是整个关掉:用
back。
其他几种点击
除了 left,还可以写 right(右键)、shift-left、shift-right,每一种都能有自己的动作、权限和花费:
click:
left: ['command: spawn']
right: ['message: <gray>左键回城']
整个按钮都要权限时,可以把 permission 直接写在按钮上(和 click 同一层),这个按钮的每一种点击都要这个权限。
常见问题
点了没反应,后台出现 Unknown action。 动作名写错了,后台那一行会列出所有能用的动作。另外动作名后面要有冒号,例如 command: spawn。
command: 的指令没执行。 command: 是以玩家身份执行,玩家自己没有权限的指令执行不了;竞技场、战斗标记这类会拦指令的插件也会把它拦下来。要绕过玩家权限就改用 console:,并把 {player} 写进指令里。
有花费的按钮一直提示花费设定有误。 cost 写错了,后台会有一行警告说明哪里错。cost 下面只能有 money、points、levels、items 四个键,points 和 levels 要是整数,items 每一项是 物品 数量。写错的按钮会一直拒绝,不会变成免费。服务器没装 Vault 经济或 PlayerPoints 时,money、points 一律当作不够。