Mathematical Expressions

Complete guide to the mathematical expression evaluator in zAPI. Learn how to use mathematical operations, functions, and expressions with placeholders in your configurations.

zAPI includes a powerful mathematical expression evaluator that can parse and compute mathematical expressions with support for basic arithmetic, functions, and placeholder integration.

Table of Contents

Overview

The MathExpressionEvaluator class provides a static evaluate() method that can parse and compute mathematical expressions as strings. This is particularly useful for:

  • Dynamic calculations in configurations

  • Conditional logic in display conditions

  • Computing values based on placeholders

  • Creating flexible, data-driven systems

Basic Usage

Direct Evaluation

In Configuration Files

Mathematical expressions in configs are wrapped in {math: ...}:

The evaluator is automatically called when processing placeholders in ItemBuilder and InventoryBuilder.

Supported Operations

Basic Arithmetic

Operator
Description
Example
Result

+

Addition

10 + 5

15

-

Subtraction

20 - 8

12

*

Multiplication

6 * 7

42

/

Division

100 / 4

25

Operator Precedence

Operations follow standard mathematical precedence:

  1. Parentheses ()

  2. Multiplication and Division * /

  3. Addition and Subtraction + -

Negative Numbers

Negative numbers are fully supported:

Decimal Numbers

All calculations support decimal values:

Functions

The evaluator supports several mathematical functions:

sqrt() - Square Root

Calculates the square root of a number.

In config:

round() - Round to Nearest Integer

Rounds a number to the nearest integer using standard rounding rules (0.5 rounds up).

In config:

roundDown() - Floor Function

Always rounds down to the nearest integer.

In config:

Nested Functions

Functions can be nested within each other:

In config:

Using with Placeholders

Placeholder Replacement Order

When using math expressions with placeholders, processing happens in this order:

  1. Custom placeholders are replaced first

  2. Math expressions are evaluated

  3. PlaceholderAPI placeholders are processed last

This allows you to use PlaceholderAPI values in math:

In Item Configurations

In Display Conditions

Math expressions can be used in display conditions for dynamic logic:

In Slot Calculations

Integration with Items and Inventories

Automatic Processing in ItemBuilder

The ItemBuilder automatically processes math expressions in:

  • Item names

  • Lore lines

  • Enchantment levels

  • Custom commands

  • Display conditions

No manual evaluation needed:

Automatic Processing in InventoryBuilder

The InventoryBuilder automatically processes math expressions in:

  • Inventory titles

  • Slot positions

  • All item properties

  • Custom placeholders

Manual Evaluation

For custom use cases, you can manually evaluate expressions:

Advanced Examples

Example 1: Experience-Based Leveling

Example 2: Dynamic Pricing

Example 3: Grid Position Calculator

Example 4: Skill Point Requirements

Example 5: Compound Interest Calculator

Error Handling

Try-Catch Block

Common Errors

Division by Zero

Invalid Syntax

Invalid Function Names

Safe Evaluation

Result Formatting

Results are automatically formatted:

Whole Numbers

Decimals

This automatic formatting ensures clean display in item names and lore.

Performance Considerations

  1. Cache results when possible - if the expression doesn't change, evaluate once

  2. Avoid complex nested functions in loops

  3. Validate expressions before production use

  4. Pre-process static expressions at plugin startup

  5. Use simple arithmetic when possible instead of functions

Best Practices

  1. Always use {math: ...} wrapper in config files

  2. Validate expressions during testing

  3. Use parentheses to make order of operations explicit

  4. Handle errors gracefully with try-catch

  5. Test edge cases (division by zero, negative numbers, very large numbers)

  6. Document complex expressions with comments in configs

  7. Use meaningful placeholder names in math expressions

  8. Round appropriately when displaying to users

  9. Avoid extremely long expressions - split into multiple placeholders

  10. Test with actual placeholder values before deployment

Troubleshooting

Expression Not Evaluating

Problem: Math expression appears as literal text

Solution: Ensure you're using ItemBuilder or InventoryBuilder, which automatically process math expressions.

Wrong Result

Problem: Expression gives unexpected result

Solution: Use parentheses to control order: {math: (2+3)*4}

Placeholder Not Replaced

Problem: Placeholder in math not replaced

Solution: Ensure the placeholder exists and is defined before the math expression is evaluated.

Decimal Precision Issues

Problem: Unexpected decimal values

Solution: Formatting is automatic and limited to 2 decimals. Use round() or roundDown() for integer results.

See Also

Last updated

Was this helpful?