在画面上显示玩家资料
这一页做一张玩家资料卡:头像、名字、等级、血量条、经验条,再加一行来自 PlaceholderAPI 的金币数。每个玩家打开看到的都是自己的资料。
还没写过画面的话,先做完 从零写第一个画面。
第 1 步:新建文件
在 plugins/CloudUI/screens/ 里新建 tut_player.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: 320
align: center
panel:
type: image
image: typewriter/panel.png
x: 222
y: 400
close:
type: button
image: main/close.png
hover: main/close_hover.png
x: 842
y: 680
click:
left: ['close']
close-label:
type: text
text: '关闭'
font: text
size: 20
color: '#E2EEF4'
x: 960
y: 697
align: center
面板 typewriter/panel.png 宽 1476、高 240,放在 x: 222 刚好左右居中。后面的资料都摆在这块面板上。写在后面的元素会画在前面的上面,所以面板要写在资料之前。
第 2 步:内建变量
文字里写 {变量名},打开画面时会换成这位玩家的资料。插件自带这几个:
| 变量 | 内容 |
|---|---|
{player} | 玩家名称 |
{level} | 经验等级 |
{exp} | 目前等级的经验进度,0 到 1 的小数 |
{exp_percent} | 经验进度百分比,例如 42% |
{health} | 目前血量(小数进位成整数) |
{max_health} | 最大血量 |
{world} | 所在世界的名称 |
{online} | 在线人数 |
在 elements 下面加名字和一行资料:
name:
type: text
text: '{player}'
font: text
size: 40
color: '#E6D3A1'
x: 450
y: 440
info:
type: text
text: '等级 {level} · 所在世界 {world} · 在线 {online} 人'
font: text
size: 20
color: '#C8D4DE'
x: 450
y: 500
变量可以和普通文字混着写,一行里写几个都可以。
再加一个头像。物品元素写 item: '{head}' 就是玩家自己的头:
head:
type: item
item: '{head}'
x: 280
y: 456
size: 128
第 3 步:进度条跟着变量走
进度条是 type: bar,value 决定填多满。value 认三种写法:
- 小数:
0.75 - 百分比:
75% - 分数:
30/40
所以血量条直接把两个变量用斜线连起来,经验条用现成的百分比:
hp-label:
type: text
text: '生命 {health} / {max_health}'
font: text
size: 18
color: '#E2EEF4'
x: 450
y: 544
hp:
type: bar
value: '{health}/{max_health}'
x: 680
y: 550
width: 400
height: 10
color: '#E06C6C'
back: '#2A3440'
xp-label:
type: text
text: '经验 {exp_percent}'
font: text
size: 18
color: '#E2EEF4'
x: 450
y: 584
xp:
type: bar
value: '{exp_percent}'
x: 680
y: 590
width: 400
height: 10
color: '#8FD8EA'
back: '#2A3440'
width、height:进度条的大小。color:填满部分的颜色;back:底色。value超过 1 当成满,小于 0 当成空,认不出来的值当成空。
别的插件的数值也一样接,例如魔力条写 value: '%mmocore_mana%/%mmocore_stat_max_mana%'。
第 4 步:PlaceholderAPI 变量
服务器装了 PlaceholderAPI 时,文字里的 %变量% 也会换掉。下面这行显示 Vault 的余额(要装 Vault、经济插件,以及 PlaceholderAPI 的 Vault 扩展):
money:
type: text
text: '金币 {num:%vault_eco_balance%}'
visible: '%vault_eco_balance%'
font: text
size: 22
color: '#E6D3A1'
x: 1650
y: 448
align: right
这里有两个新东西。
{num:…} 是数字整理。很多变量给的数字是 1286.00、24.50% 这种样子,用 {num: 和 } 包起来会变成 1,286、24.5%:加千位逗号,小数最多留两位,多余的 0 去掉。内建变量也能包,例如 {num:{exp}}。
visible 是显示条件。这里写同一个变量,意思是:这个变量没被换掉(没装 PlaceholderAPI,或没装对应的扩展)时,整行字不显示,不会把 %vault_eco_balance% 原样露给玩家看。值是 hidden 或 false 时也不显示。
第 5 步:重载,打开
存档后输入 /cui reload,再输入 /cui open tut_player。
你应该看到:一块横向的面板,左边是你的头像,旁边是你的名字、等级、所在世界和在线人数,下面一红一蓝两条进度条。装了 PlaceholderAPI 和 Vault 的话,右上角有一行金币数;没装就没有这一行。
去挨几下打或捡点经验,再打开一次,两条进度条的长度会跟着变。
完整文件
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: 320
align: center
panel:
type: image
image: typewriter/panel.png
x: 222
y: 400
head:
type: item
item: '{head}'
x: 280
y: 456
size: 128
name:
type: text
text: '{player}'
font: text
size: 40
color: '#E6D3A1'
x: 450
y: 440
info:
type: text
text: '等级 {level} · 所在世界 {world} · 在线 {online} 人'
font: text
size: 20
color: '#C8D4DE'
x: 450
y: 500
hp-label:
type: text
text: '生命 {health} / {max_health}'
font: text
size: 18
color: '#E2EEF4'
x: 450
y: 544
hp:
type: bar
value: '{health}/{max_health}'
x: 680
y: 550
width: 400
height: 10
color: '#E06C6C'
back: '#2A3440'
xp-label:
type: text
text: '经验 {exp_percent}'
font: text
size: 18
color: '#E2EEF4'
x: 450
y: 584
xp:
type: bar
value: '{exp_percent}'
x: 680
y: 590
width: 400
height: 10
color: '#8FD8EA'
back: '#2A3440'
money:
type: text
text: '金币 {num:%vault_eco_balance%}'
visible: '%vault_eco_balance%'
font: text
size: 22
color: '#E6D3A1'
x: 1650
y: 448
align: right
close:
type: button
image: main/close.png
hover: main/close_hover.png
x: 842
y: 680
click:
left: ['close']
close-label:
type: text
text: '关闭'
font: text
size: 20
color: '#E2EEF4'
x: 960
y: 697
align: center
变量还能用在哪
除了文字和进度条的 value,这些地方也能写变量:
- 按钮的动作,例如
console: give {player} diamond 1。 - 图片和按钮的
state,依变量换图,见 状态切换。 - 文字的
color、进度条的color、图片的tint,写成'{变量}',由变量给颜色。
想在打开画面时自己带值进去(例如 NPC 的名字),用 values 和 /cui open 画面 玩家 键=值,见 画面的默认值。
常见问题
画面上原样显示 {xxx}。 变量名打错了,或者这个变量根本不存在。没人认得的 {xxx} 会原样留着,方便你发现。内建的只有上面表里那几个,注意是 {max_health} 不是 {maxhealth}。
画面上原样显示 %xxx%,或者数字的位置是一条横线。 PlaceholderAPI 没装,或者那个变量的扩展没装。先在游戏里用 /papi parse me %变量% 确认变量本身有值。{num:…} 里的变量没被换掉时会显示成「—」。不想让玩家看到,就像第 4 步那样加 visible。
玩家名字或变量里的中文变成方块。 画面文件里写死的字会自动做进资源包,但变量的内容是执行时才知道的,要靠字体的常用字库。text 字体默认带常用简繁体字,title 字体默认只有英文和数字,所以内容可能有中文的变量要用 font: text。