Text Placeholder API 表达式
为 Text Placeholder API 添加数学、条件判断和字符串操作表达式。
本项目使用 Keval 来解析数学表达式,如果没有它,开发难度将呈指数级增加。请给予作者支持并 Star 他的仓库!
在表达式中使用占位符
此扩展使用 ${category:placeholder} 语法来允许提供解析为数值的占位符作为组件。
例如;要在标签页列表等位置以百分比显示玩家的生命值,你可以使用以下代码:
%expr:math ${player:health} * 100 / ${player:max_health}%%
数学占位符
此扩展中大部分新增功能(没有双关语)都通过 expr:math 占位符访问。
你可以将这个占位符视为处理三种不同的“类型”:
- 组件,如数字、函数、常量和解析为数值的占位符。
- 二元运算符,如加法或减法,位于两个组件之间。
- 一元运算符,如取反和阶乘,应用于单个组件。
此扩展提供的组件和运算符
二元运算符应用于两个组件之间。
| 二元运算符 |
功能 |
示例 |
结果 |
| + |
加法 |
%expr:math 2 + 2% |
4 |
| - |
减法 |
%expr:math 4 - 1% |
3 |
| * |
乘法 |
%expr:math 3 * 3% |
9 |
| / |
除法 |
%expr:math 9 / 4% |
2.25 |
| ^ |
幂运算 |
%expr:math 3 ^ 3% |
27 |
一元运算符应用于单个组件。
| 一元运算符 |
功能 |
示例 |
结果 |
| - |
取反 |
%expr:math 1 + -1% |
0 |
| + |
恒等 |
%expr:math ??% |
这到底有什么用 |
| ! |
阶乘 |
%expr:math 5!% |
120 |
函数是组件,接受表达式作为参数。
| 函数 |
参数数 |
描述 |
示例 |
结果 |
| mod |
2 |
取模 / 余数 |
%expr:math mod(7, 2)% |
1 |
| neg |
1 |
取反 / 翻转 (类似一元 -) |
%expr:math neg(10)% |
-10 |
| abs |
1 |
绝对值 |
%expr:math abs(-10)% |
10 |
| sqrt |
1 |
平方根 |
%expr:math sqrt(34)% |
5.8309 |
| cbrt |
1 |
立方根 |
%expr:math cbrt(30)% |
3.1072 |
| exp |
1 |
指数 |
%expr:math exp(4)% |
54.5981 |
| ln |
1 |
自然对数 |
%expr:math ln(4)% |
1.3862 |
| log10 |
1 |
常用对数 (底数为 10) |
%expr:math log10(4)% |
0.6020 |
| log2 |
1 |
二进制对数 (底数为 2) |
%expr:math log2(4)% |
2 |
| rad |
1 |
角度转弧度 |
%expr:math rad(30)% |
0.5235 |
| sin |
1 |
正弦 |
%expr:math sin(rad(30))% |
0.4999 |
| cos |
1 |
余弦 |
%expr:math cos(rad(30))% |
0.8660 |
| tan |
1 |
正切 |
%expr:math tan(rad(30))% |
0.5773 |
| asin |
1 |
反正弦 |
%expr:math asin(rad(30))% |
0.5510 |
| acos |
1 |
反余弦 |
%expr:math acos(rad(30))% |
1.0197 |
| atan |
1 |
反正切 |
%expr:math atan(rad(30))% |
0.4823 |
| ceil |
1 |
向上取整 |
%expr:math ceil(0.4)% |
1 |
| floor |
1 |
向下取整 |
%expr:math floor(0.6)% |
0 |
| round |
1 |
四舍五入 |
%expr:math round(0.5)% |
1 |
| min |
2+ |
最小参数 |
%expr:math min(1, 0.3, 7)% |
0.3 |
| max |
2+ |
最大参数 |
%expr:math max(1, 0.3, 7)% |
7 |
| avg |
2+ |
参数平均值 |
%expr:math avg(1, 0.3, 7)% |
2.7666 |
常量是解析为固定值的组件。
条件占位符
这些占位符的参数必须用分号 (;) 分隔,而不是空格。
%expr:ifeq%
测试 a 和 b 是否相等,并根据结果返回 c 或 d。
%expr:ifeq aIsMath a bIsMath b cIsMath c dIsMath d%
| 参数 |
描述 |
| aIsMath |
是否将 a 解析为数学表达式。值为 true 或 false。 |
| a |
与 b 比较的数学表达式或文本。如果被视为文本,则去除字符串开头和结尾的空格。 |
| bIsMath |
是否将 b 解析为数学表达式。值为 true 或 false。 |
| b |
与 a 比较的数学表达式或文本。如果被视为文本,则去除字符串开头和结尾的空格。 |
| cIsMath |
是否将 c 解析为数学表达式。值为 true 或 false。 |
| c |
当 a 和 b 相等时使用的数学表达式或文本。 |
| dIsMath |
是否将 d 解析为数学表达式。值为 true 或 false。 |
| d |
当 a 和 b 不相等时使用的数学表达式或文本。 |
示例
玩家 %expr:ifeq false; ${player:equipment_slot mainhand}; false; 钻石剑; false; is; false; is not% 手持一把钻石剑。
| 条件 |
结果 |
%player:equipment_slot mainhand% == "Diamond Sword" |
玩家手持一把钻石剑。 |
%player:equipment_slot mainhand% != "Diamond Sword" |
玩家未手持钻石剑。 |
%expr:iflt%
测试 a 是否小于 b,并根据结果返回 c 或 d。
%expr:iflt a b cIsMath c dIsMath d%
| 参数 |
描述 |
| a |
与 b 进行比较的数学表达式。 |
| b |
与 a 进行比较的数学表达式。 |
| cIsMath |
是否将 c 解析为数学表达式。值为 true 或 false。 |
| c |
如果 a 小于 b,则使用的数学表达式或文本。 |
| dIsMath |
是否将 d 解析为数学表达式。值为 true 或 false。 |
| d |
如果 a 不小于 b,则使用的数学表达式或文本。 |
示例
玩家处于 %expr:iflt ${player:health}; 14; false; 较差; false; 良好% 的健康状态
| 条件 |
结果 |
%player:health% < 14 |
玩家健康状态较差 |
%player:health% >= 14 |
玩家健康状态良好 |
%expr:ifgt%
测试 a 是否大于 b,并根据结果返回 c 或 d。
%expr:ifgt a b cIsMath c dIsMath d%
| 参数 |
描述 |
| a |
与 b 进行比较的数学表达式。 |
| b |
与 a 进行比较的数学表达式。 |
| cIsMath |
是否将 c 解析为数学表达式。值为 true 或 false。 |
| c |
如果 a 大于 b,则使用的数学表达式或文本。 |
| dIsMath |
是否将 d 解析为数学表达式。值为 true 或 false。 |
| d |
如果 a 不大于 b,则使用的数学表达式或文本。 |
示例
生命值: %expr:ifgt ${player:health}; 4; true; ${player:health} * 100 / ${player:max_health}%%; false; 濒临死亡!%
| 条件 |
结果 |
%player:health% == 15 |
生命值:75 |
%player:health% == 14 |
生命值:70 |
%player:health% <= 4 |
生命值:濒临死亡! |
字符串占位符
这些占位符的参数必须使用分号(;)分隔,而不是空格。
%expr:pad%、%expr:padleft% 和 %expr:padright%
用字符 c 填充 str 的一侧或两侧,直到字符串长度达到 length。
%expr:pad length; str; c%
%expr:padleft length; str; c%
%expr:padright length; str; c%
| 参数 |
描述 |
| length |
数学表达式,填充后字符串的目标长度。 |
| str |
要填充的源字符串。字符串开头或结尾的空格会被移除。 |
| c |
用于填充的字符。 |
示例
%expr:padleft 2 ${server:online} 0% / %server:max_players%
| 条件 |
结果 |
%server:online% == 12 |
12 / 40 |
%server:online% == 4 |
04 / 40 |
%expr:padmatch%、%expr:padmatchleft% 和 %expr:padmatchright%
用字符 c 填充 str 的一侧或两侧,直到字符串长度与 match 的长度匹配。
%expr:padmatch match; str; c%
%expr:padmatchleft match; str; c%
%expr:padmatchright match; str; c%
| 参数 |
描述 |
| match |
其长度将被匹配的字符串。字符串开头或结尾的空格会被移除。 |
| str |
要填充的源字符串。字符串开头或结尾的空格会被移除。 |
| c |
用于填充的字符。 |
示例
%expr:padmatchleft ${server:max_players} ${server:online} 0% / %server:max_players%
| 条件 |
结果 |
%server:max_players% == 40, %server:online% == 4 |
04 / 40 |
%server:max_players% == 100, %server:online% == 4 |
004 / 100 |
Text Placeholder API Expressions
Adds math, conditional, and string manipulation expressions to Text Placeholder API.
This project uses Keval for parsing math expressions, and would have been exponentially more difficult without it. Show the author some love and star his repo!
Using Placeholders In Expressions
This extension uses the ${category:placeholder} syntax to allow supplying placeholders that resolve to numbers as components.
For example; to display a player's health as a percentage (such as within the tab list), you could use the following:
%expr:math ${player:health} * 100 / ${player:max_health}%%
Math Placeholder
The bulk of the additions (no pun intended) in this extension are accessed through the expr:math placeholder.
You can think of this placeholder as handling three distinct 'types':
- Components, such as numbers, functions, constants, and placeholders that resolve to numbers.
- Binary operators, such as addition or subtraction, which go between two components.
- Unary operators, such as negation and factorial, which apply to a single component.
Components and operators supplied by this extension
Binary operators are applied between two components.
| Binary Operator |
Function |
Example |
Result |
| + |
Add |
%expr:math 2 + 2% |
4 |
| - |
Subtract |
%expr:math 4 - 1% |
3 |
| * |
Multiply |
%expr:math 3 * 3% |
9 |
| / |
Divide |
%expr:math 9 / 4% |
2.25 |
| ^ |
Exponent |
%expr:math 3 ^ 3% |
27 |
Unary operators are applied to a single component.
| Unary Operator |
Function |
Example |
Result |
| - |
Negate |
%expr:math 1 + -1% |
0 |
| + |
Identity |
%expr:math ??% |
What does this even do |
| ! |
Factorial |
%expr:math 5!% |
120 |
Functions are components, taking expressions as arguments.
| Function |
Arguments |
Description |
Example |
Result |
| mod |
2 |
Modulo / remainder |
%expr:math mod(7, 2)% |
1 |
| neg |
1 |
Negate / invert (Like unary -) |
%expr:math neg(10)% |
-10 |
| abs |
1 |
Absolute value |
%expr:math abs(-10)% |
10 |
| sqrt |
1 |
Square root |
%expr:math sqrt(34)% |
5.8309 |
| cbrt |
1 |
Cube root |
%expr:math cbrt(30)% |
3.1072 |
| exp |
1 |
Exponential |
%expr:math exp(4)% |
54.5981 |
| ln |
1 |
Natural logarithm |
%expr:math ln(4)% |
1.3862 |
| log10 |
1 |
Base 10 logarithm |
%expr:math log10(4)% |
0.6020 |
| log2 |
1 |
Base 2 logarithm |
%expr:math log2(4)% |
2 |
| rad |
1 |
Degrees to radians |
%expr:math rad(30)% |
0.5235 |
| sin |
1 |
Sine |
%expr:math sin(rad(30))% |
0.4999 |
| cos |
1 |
Cosine |
%expr:math cos(rad(30))% |
0.8660 |
| tan |
1 |
Tangent |
%expr:math tan(rad(30))% |
0.5773 |
| asin |
1 |
Arcsine |
%expr:math asin(rad(30))% |
0.5510 |
| acos |
1 |
Arccosine |
%expr:math acos(rad(30))% |
1.0197 |
| atan |
1 |
Arctangent |
%expr:math atan(rad(30))% |
0.4823 |
| ceil |
1 |
Round up |
%expr:math ceil(0.4)% |
1 |
| floor |
1 |
Round down |
%expr:math floor(0.6)% |
0 |
| round |
1 |
Round |
%expr:math round(0.5)% |
1 |
| min |
2+ |
Smallest argument |
%expr:math min(1, 0.3, 7)% |
0.3 |
| max |
2+ |
Largest argument |
%expr:math max(1, 0.3, 7)% |
7 |
| avg |
2+ |
Average of arguments |
%expr:math avg(1, 0.3, 7)% |
2.7666 |
Constants are components which resolve to a constant value.
| Constant |
Description |
| PI |
π |
| e |
Euler's number |
Conditional Placeholders
Arguments to these placeholders must be separated by semicolons (;) instead of spaces.
%expr:ifeq%
Test whether a and b are equal, and return c or d depending on the result.
%expr:ifeq aIsMath a bIsMath b cIsMath c dIsMath d%
| Argument |
Description |
| aIsMath |
Whether to parse a as a math expression. Either true or false. |
| a |
Math expression or text to compare against b. If treated as text, and spaces at the start or end of the string are removed. |
| bIsMath |
Whether to parse b as a math expression. Either true or false. |
| b |
Math expression or text to compare against a. If treated as text, and spaces at the start or end of the string are removed. |
| cIsMath |
Whether to parse c as a math expression. Either true or false. |
| c |
Math expression or text to use if a and b are equal. |
| dIsMath |
Whether to parse d as a math expression. Either true or false. |
| d |
Math expression or text to use if a and b are not equal. |
Example
Player %expr:ifeq false; ${player:equipment_slot mainhand}; false; Diamond Sword; false; is; false; is not% holding a Diamond Sword.
| Condition |
Result |
%player:equipment_slot mainhand% == "Diamond Sword" |
Player is holding a Diamond Sword. |
%player:equipment_slot mainhand% != "Diamond Sword" |
Player is not holding a Diamond Sword. |
%expr:iflt%
Test whether a is less than b, and return c or d depending on the result.
%expr:iflt a b cIsMath c dIsMath d%
| Argument |
Description |
| a |
Math expression to compare against b. |
| b |
Math expression to compare against a. |
| cIsMath |
Whether to parse c as a math expression. Either true or false. |
| c |
Math expression or text to use if a is less than b. |
| dIsMath |
Whether to parse d as a math expression. Either true or false. |
| d |
Math expression or text to use if a is not less than b. |
Example
Player is in %expr:iflt ${player:health}; 14; false; poor; false; good% health
| Condition |
Result |
%player:health% < 14 |
Player is in poor health |
%player:health% >= 14 |
Player is in good health |
%expr:ifgt%
Test whether a is greater than b, and return c or d depending on the result.
%expr:ifgt a b cIsMath c dIsMath d%
| Argument |
Description |
| a |
Math expression to compare against b. |
| b |
Math expression to compare against a. |
| cIsMath |
Whether to parse c as a math expression. Either true or false. |
| c |
Math expression or text to use if a is greater than b. |
| dIsMath |
Whether to parse d as a math expression. Either true or false. |
| d |
Math expression or text to use if a is not greater than b. |
Example
Health: %expr:ifgt ${player:health}; 4; true; ${player:health} * 100 / ${player:max_health}%; false; Nearly dead!%
| Condition |
Result |
%player:health% == 15 |
Health: 75 |
%player:health% == 14 |
Health: 70 |
%player:health% <= 4 |
Health: Nearly dead! |
String Placeholders
Arguments to these placeholders must be separated by semicolons (;) instead of spaces.
%expr:pad%, %expr:padleft%, and %expr:padright%
Pad one or both sides of str with c until the string length reaches length.
%expr:pad length; str; c%
%expr:padleft length; str; c%
%expr:padright length; str; c%
| Argument |
Description |
| length |
Math expression, target length of the padded string. |
| str |
Source string to pad. Spaces at the start or end of the string are removed. |
| c |
Character to use for padding. |
Example
%expr:padleft 2 ${server:online} 0% / %server:max_players%
| Condition |
Result |
%server:online% == 12 |
12 / 40 |
%server:online% == 4 |
04 / 40 |
%expr:padmatch%, %expr:padmatchleft%, and %expr:padmatchright%
Pad one or both sides of str with c until the string length matches the length of match.
%expr:padmatch match; str; c%
%expr:padmatchleft match; str; c%
%expr:padmatchright match; str; c%
| Argument |
Description |
| match |
String to match the length of. Spaces at the start or end of the string are removed. |
| str |
Source string to pad. Spaces at the start or end of the string are removed. |
| c |
Character to use for padding. |
Example
%expr:padmatchleft ${server:max_players} ${server:online} 0% / %server:max_players%
| Condition |
Result |
%server:max_players% == 40, %server:online% == 4 |
04 / 40 |
%server:max_players% == 100, %server:online% == 4 |
004 / 100 |
评论 0
登录后参与评分
还没有评论,来抢沙发吧