Skip to content

Commit

Permalink
It's expressions time.
Browse files Browse the repository at this point in the history
  • Loading branch information
melontini committed Apr 17, 2024
1 parent 875cb1f commit 949b0e9
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 70 deletions.
2 changes: 1 addition & 1 deletion docs/.vitepress/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const en = defineConfig({
{ text: 'Welcome!', link: '/' },
{ text: 'Events', link: '/Events' },
{ text: 'Commands', link: '/Commands' },
{ text: 'Arithmetica', link: '/Arithmetica' },
{ text: 'Expressions', link: '/Expressions' },
]
},
{
Expand Down
57 changes: 0 additions & 57 deletions docs/Arithmetica.md

This file was deleted.

17 changes: 9 additions & 8 deletions docs/Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,20 @@ This command type expects a selector and either a list of commands or a function

If you opt into specifying commands using the array, you are a given an option of using command macros.

A macro is a simple `${{}}` block, which allows you to dynamically insert info using extractions. There are two types of macros: string and arithmetic.

A string macro is self-explanatory, it will insert a string into the command, for example:
A macro is a simple `${{}}` block, which allows you to dynamically insert info using expressions. For example:
```
"/say hi, my name is ${{origin[world/key]}}!"
"/say hi, my name is ${{level.dimension.location}}!"
```
will say `hi, my name is minecraft:overworld` in the overworld.

An arithmetic macro is a lot more interesting. Arithmetic macros are supported by [Arithmetica](Arithmetica). You can learn more about mathematical expressions on that page.
You can learn more about expressions on this page: [Expressions](Expressions).

By default macros return a string, but you can cast (convert) a macro into some other types like so:

Arithmetic macros always return a floating point number (`1.7480`), but you can truncate the floating point by adding `(long)` to your expression like so:
```
$(long){{random(0, 34)}}
$(long){{random(0, 34)}} 34.52946 -> 34
$(double){{true}} true -> 1.0
$(bool){{0}} 0 -> false
```

### `commander:cancel`
Expand Down Expand Up @@ -116,7 +117,7 @@ Note: even if the condition is true early, the command will still execute all co
:::

### `commander:random`
This command type takes a weighted list of other commands and randomly executes some of them. By default, it rolls only once, but the number of rolls can be adjusted using the optional `rolls` field (Supports [Arithmetica](Arithmetica)).
This command type takes a weighted list of other commands and randomly executes some of them. By default, it rolls only once, but the number of rolls can be adjusted using the optional `rolls` field (Supports [Expressions](Expressions)).

::: details Example
```json
Expand Down
4 changes: 1 addition & 3 deletions docs/Events.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,4 @@ The subscription file can contain multiple subscriptions to different (or identi

Commander wraps most compatible fabric events under the `commander` namespace. "Return" here means the [cancel command](Commands#commandercancel)

Currently available events can be seen here: [EntityEvents](https://github.com/constellation-mc/commander/blob/main/src/main/java/me/melontini/commander/impl/builtin/events/EntityEvents.java), [PlayerEvents](https://github.com/constellation-mc/commander/blob/main/src/main/java/me/melontini/commander/impl/builtin/events/PlayerEvents.java), [ServerLifecycle](https://github.com/constellation-mc/commander/blob/main/src/main/java/me/melontini/commander/impl/builtin/events/ServerLifecycle.java), [ServerTick](https://github.com/constellation-mc/commander/blob/main/src/main/java/me/melontini/commander/impl/builtin/events/ServerTick.java)

TODO: Automatically generate event list.
Currently available events can be seen here: [EntityEvents](https://github.com/constellation-mc/commander/blob/main/src/main/java/me/melontini/commander/impl/builtin/events/EntityEvents.java), [PlayerEvents](https://github.com/constellation-mc/commander/blob/main/src/main/java/me/melontini/commander/impl/builtin/events/PlayerEvents.java), [ServerLifecycle](https://github.com/constellation-mc/commander/blob/main/src/main/java/me/melontini/commander/impl/builtin/events/ServerLifecycle.java), [ServerTick](https://github.com/constellation-mc/commander/blob/main/src/main/java/me/melontini/commander/impl/builtin/events/ServerTick.java)
67 changes: 67 additions & 0 deletions docs/Expressions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Expressions

Commander introduces support for evaluating math expressions with dynamic context data.

Expressions use [EvalEx](https://ezylang.github.io/EvalEx/) for evaluation, it may be useful to familiarize yourself with it.

## Additional functions

Commander adds some additional functions to EvalEx.

- `random` generates a random number in range. Accepts 2 arguments (min, max)
- `clamp` clamps the value between two other arguments. Accepts 3 arguments (value, min, max)
- `lerp` smoothly progresses the value to the target. `start + delta * (end - start)`. Accepts 3 arguments (delta, start, end)

## Context data access

Because Expressions require minecraft's loot context, you can read source data using a special syntax.
```
identifier.field
identifier.method
identifier.method.field.method
```
Keep in mind that requested fields and context **must** be present. Expression will fail without them.

Some examples:
```
minecraft:this_entity.getX
this_entity.getHealth
minecraft:this_entity.isInWaterOrRain
this_entity.blockPosition.getX
origin.x
origin.reverse.y
minecraft:level.getDayTime
```

Expressions use Mojang Mappings (downloaded on first load), this means that almost all public fields and getter methods are available in expressions. If Commander fails to setup mappings, you'll have to rely on platform names. (e.g. intermediary on Fabric)

## Using Expressions

The fastest way to get started is to use the `cmd:arithmetica` command. You should wrap your expression with `"` to satisfy Brigadier.

Other places are [command macros](Commands#command-macros), the `commander:arithmetica` number provider and the `commander:expression` predicate.

Using the predicate:

```json
{
"condition": "commander:expression",
"value": "level.isDay"
}
```

Using the provider in conditions:

```json
{
"condition": "minecraft:value_check",
"value": {
"type": "commander:arithmetica",
"value": "round(random(5, 15))"
},
"range": {
"min": 12.3,
"max": 32
}
}
```
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ comment: false

Commander is an extension of the vanilla data pack system.

It adds a new event system, flexible json commands, new `/` commands, support for advanced mathematical expressions and more!
It adds a new event system, flexible json commands, new `/` commands, support for advanced expressions with data access and more!

There are no client-side features available, everything is executed on the server.

Expand Down

0 comments on commit 949b0e9

Please sign in to comment.