Skip to content

Commit 66ab912

Browse files
committed
Document math expression syntax
1 parent b91d89b commit 66ab912

1 file changed

Lines changed: 227 additions & 1 deletion

File tree

README.md

Lines changed: 227 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,232 @@ etc.
130130

131131
---
132132

133+
## Math Expressions
134+
135+
UltimateShop can calculate math expressions in product amounts, price amounts,
136+
limits, placeholders, and other numeric options. Math is enabled by default in
137+
`config.yml`:
138+
139+
```yaml
140+
math:
141+
enabled: true
142+
scale: 2
143+
static-scale: false
144+
```
145+
146+
When `math.enabled` is `false`, numeric options are read as plain numbers. When
147+
it is `true`, expressions are evaluated by EvalEx and then rounded with
148+
`math.scale` where the option requires a fixed scale.
149+
150+
### Common Syntax
151+
152+
```text
153+
1 + 1
154+
(1 + 2) * 3
155+
10 / 3
156+
10 % 3
157+
2 ^ 10
158+
-5 + 8
159+
2(3 + 4)
160+
```
161+
162+
- `+`, `-`, `*`, `/`, `%`, and `^` are supported.
163+
- Parentheses can be used to control priority.
164+
- Unary `+` and `-` are supported.
165+
- Implicit multiplication is supported, so `2(3 + 4)` means `2 * (3 + 4)`.
166+
- Scientific notation may appear in output, for example `1E+2` means `100`.
167+
168+
### Constants
169+
170+
```text
171+
PI
172+
E
173+
TRUE
174+
FALSE
175+
NULL
176+
```
177+
178+
Use `E ^ x` for exponential formulas. `EXP(x)` is not an EvalEx built-in in the
179+
current dependency version.
180+
181+
```text
182+
E ^ 1
183+
E ^ (-0.1 * 10)
184+
1 - E ^ (-0.1 * 5)
185+
```
186+
187+
### Comparisons, Booleans, and Conditions
188+
189+
```text
190+
1 > 0
191+
1 >= 1
192+
1 < 2
193+
1 <= 2
194+
1 == 1
195+
1 != 2
196+
1 < 2 && 2 < 3
197+
1 > 2 || 2 < 3
198+
NOT(1 > 2)
199+
IF(1 < 2, 100, 0)
200+
IF(1 < 2 && 2 < 3, 400, 0)
201+
```
202+
203+
Use `&&` and `||` for boolean AND/OR in formulas. Do not write `AND` or `OR` as
204+
bare infix operators.
205+
206+
### Number Functions
207+
208+
```text
209+
ABS(-5)
210+
SQRT(16)
211+
ROUND(3.14159, 2)
212+
FLOOR(3.9)
213+
CEILING(3.1)
214+
LOG(E)
215+
LOG10(100)
216+
FACT(5)
217+
MIN(3, 1, 4, 2)
218+
MAX(3, 1, 4, 2)
219+
SUM(1, 2, 3)
220+
AVERAGE(1, 2, 3)
221+
RANDOM()
222+
COALESCE(NULL, 5)
223+
SWITCH(2, 1, 100, 2, 200, 0)
224+
```
225+
226+
`COALESCE` returns the first non-null value. `SWITCH(value, case1, result1,
227+
case2, result2, default)` returns the matching case result or the default.
228+
229+
### Trigonometric Functions
230+
231+
The normal trigonometric functions use degrees:
232+
233+
```text
234+
SIN(90)
235+
COS(180)
236+
TAN(45)
237+
COT(45)
238+
SEC(60)
239+
CSC(30)
240+
ASIN(1)
241+
ACOS(-1)
242+
ATAN(1)
243+
ACOT(1)
244+
ATAN2(1, 1)
245+
```
246+
247+
Use the `R` suffix for radians:
248+
249+
```text
250+
SINR(1.5707963267948966)
251+
COSR(3.141592653589793)
252+
TANR(0.7853981633974483)
253+
COTR(0.7853981633974483)
254+
SECR(1.0471975511965976)
255+
CSCR(0.5235987755982988)
256+
ASINR(1)
257+
ACOSR(-1)
258+
ATANR(1)
259+
ACOTR(1)
260+
ATAN2R(1, 1)
261+
RAD(180)
262+
DEG(3.141592653589793)
263+
```
264+
265+
Hyperbolic functions are also available:
266+
267+
```text
268+
SINH(1)
269+
COSH(1)
270+
TANH(1)
271+
ASINH(1)
272+
ACOSH(2)
273+
ATANH(0.5)
274+
ACOTH(2)
275+
COTH(1)
276+
SECH(1)
277+
CSCH(1)
278+
```
279+
280+
### Placeholder Values in Formulas
281+
282+
PlaceholderAPI values are replaced before the expression is calculated, so a
283+
price can depend on player data:
284+
285+
```yaml
286+
amount: "%vault_eco_balance% * 0.05"
287+
```
288+
289+
UltimateShop dynamic price/product expressions can also use built-in usage
290+
placeholders before calculation:
291+
292+
```yaml
293+
amount: "100 + {buy-times-player} * 5"
294+
amount: "100 - {sell-times-server} * 2"
295+
amount: "MAX(10, 100 + {buy-total-server} - {sell-total-server})"
296+
```
297+
298+
Common usage placeholders include:
299+
300+
```text
301+
{buy-times-player}
302+
{sell-times-player}
303+
{buy-times-server}
304+
{sell-times-server}
305+
{buy-total-player}
306+
{sell-total-player}
307+
{buy-total-server}
308+
{sell-total-server}
309+
```
310+
311+
You can also calculate inside display text with the math placeholder:
312+
313+
```text
314+
{math_1+1}
315+
{math_ROUND(10/3, 2)}
316+
{math_SIGMA(1, 10, "i")}
317+
```
318+
319+
### Custom Function: SIGMA
320+
321+
UltimateShop adds one custom math function:
322+
323+
```text
324+
SIGMA(start, end, "body")
325+
```
326+
327+
`SIGMA` loops from `start` to `end` inclusively. The current loop value is
328+
available as `i` inside the quoted `body` expression.
329+
330+
```text
331+
SIGMA(1, 10, "i")
332+
SIGMA(1, 10, "i * i")
333+
SIGMA(1, 5, "i ^ 3")
334+
SIGMA(1, 4, "FACT(i)")
335+
SIGMA(1, 10, "IF(i % 2 == 0, i, 0)")
336+
SIGMA(1, 5, "E ^ (-0.1 * i)")
337+
```
338+
339+
Examples:
340+
341+
```text
342+
SIGMA(1, 10, "i") = 55
343+
SIGMA(1, 10, "i * i") = 385
344+
SIGMA(1, 5, "i ^ 3") = 225
345+
SIGMA(5, 1, "i") = 0
346+
```
347+
348+
For YAML, wrap the whole expression in single quotes when the `SIGMA` body uses
349+
double quotes:
350+
351+
```yaml
352+
amount: 'SIGMA(1, 10, "i * i")'
353+
```
354+
355+
`SIGMA` is limited to 100000 iterations per call to protect server performance.
356+
357+
---
358+
133359
## ⚙️ Actions and Conditions
134360

135361
UltimateShop allows actions and conditions to be triggered by:
@@ -171,4 +397,4 @@ UltimateShop allows actions and conditions to be triggered by:
171397

172398
Consider respect my work and buy the plugin here, you can get free support, subbmit suggestion service. [Click to buy](https://www.spigotmc.org/resources/ultimateshop-premium-menu-dynamic-price-limits-apply-settings-sell-all-and-more-1-17-1-20.113069/)
173399

174-
You can also get free version here. [Click to download](https://www.spigotmc.org/resources/ultimateshop-menus-limits-apply-settings-10-directly-hook-and-more-1-17-1-20.110601/)
400+
You can also get free version here. [Click to download](https://www.spigotmc.org/resources/ultimateshop-menus-limits-apply-settings-10-directly-hook-and-more-1-17-1-20.110601/)

0 commit comments

Comments
 (0)