Expression - coolsquid/HungerTweaker GitHub Wiki
Expression
An expression is a string that can be interpreted as a mathematical expression. Expressions accept an unknown variable, 'x', which is passed by HungerTweaker whenever the expression is executed.
Available Operators
- +, -, *, /, ^
sqrt(value)
,sin(value)
,cos(value)
,tan(value)
,ceil(value)
,round(value)
,nextUp(value)
,nextDown(value)
random(bounds)
- Generates a random integer between 0 (inclusive) and
bounds
(exclusive).
- Generates a random integer between 0 (inclusive) and
random()
- Generates a random number between 0 (inclusive) and 1 (exclusive).
max(a, b)
- Returns the larger of the two numbers.
min(a, b)
- Returns the smaller of the two numbers.
clamp(value, min, max)
- If value is smaller than min, the function returns min. If value is larger than max, the function returns max. Otherwise, the function returns value.
Example
mods.hungertweaker.Hunger.setMaxHunger("x*2");
Whenever AppleCore fires an event to retrieve the maximum hunger, HungerTweaker returns "x*2", where 'x' is the previous maximum hunger. In an otherwise Vanilla game instance, "x*2" would be 40, as the maximum hunger in Vanilla is 20.
CraftTweaker expressions vs HungerTweaker expressions
If an expression is not wrapped in double-quotes, CraftTweaker will interpret it, evaluate it, and pass the resulting value to HungerTweaker when the script is loaded. If an expression is wrapped in double-quotes, CraftTweaker will interpret it as a string and pass it directly to HungerTweaker. HungerTweaker will interpret the string immediately, and evaluate the parsed expression every time a value is checked. Thus HungerTweaker expressions can use an unknown variable ('x'), which changes throughout a game session, and return different values based on it.
If you are accessing 'x', you should use a HungerTweaker expression. Otherwise, you should use a CraftTweaker expression, as evaluating the expression once is quicker than evaluating it every time the value is retrieved.
Good:
mods.hungertweaker.Hunger.setMaxHunger("x*2");
mods.hungertweaker.Hunger.setMaxHunger(2*2);
Bad:
mods.hungertweaker.Hunger.setMaxHunger(x*2); // This will break things
mods.hungertweaker.Hunger.setMaxHunger("2*2"); // This will not break things, but it is somewhat slower