Skip to main content

Introduction

mochafloats is a Java library that reads and runs MoLang, the small expression language Minecraft Bedrock uses for data-driven values. It contains a lexer, a parser, an interpreter and a compiler that turns expressions into JVM bytecode.

It started as a fork of Mocha by Unnamed Team and is maintained by the PlayerAnimationLibrary team. PlayerAnimationLibrary and Emotecraft run every MoLang expression in their animations through it.

MolangInterpreter<?> molang = MolangInterpreter.standard();

float result = molang.eval("math.sqrt(3 * 3 + 4 * 4)"); // 5.0
float swing = molang.eval("v.t = 0.25; return -math.sin(v.t * 360) * 30;"); // -30.0

What MoLang looks like​

Everything in MoLang evaluates to a number: true is 1, false is 0, and a whole script is basically one formula. It has variables (variable.x, or v.x for short), host-provided values (query.anim_time), a math library, conditionals and loops:

v.speed = q.ground_speed * 4;
return q.is_on_ground ? math.sin(q.anim_time * 360 * v.speed) * 20 : 0;

See MoLang support for everything mochafloats understands.

What is different from Mocha​

mochafloats computes with 32-bit float values instead of double, splits the library into modules, drops Javassist for the JDK's own ClassFile API and adds a binary format for sending parsed expressions over the network. It also fixes bugs that are still present in Mocha 3.0.1, such as -math.sin(90) * 30 evaluating to 0, and its results were checked against Bedrock itself.

Why mochafloats? lists every difference, with the results of running the same expressions on both libraries.

Modules​

ModuleContainsDepends on
lexerThe tokenizer.—
parserThe parser, the syntax tree and the network format for it.lexer, Netty ByteBuf
runtimeMolangInterpreter, scopes, values, bindings and the standard math library.parser
runtime-compilerMolangCompiler, which compiles expressions to bytecode, and MochaEngine.runtime

Each module brings the ones it depends on, so you only ever declare one of them. Most projects only need runtime.

Where to start​

  1. Add the library to your project
  2. Evaluate your first expressions
  3. Expose your own values and functions to MoLang
  4. Compile hot expressions to bytecode if interpreting them isn't fast enough

Coming from Mocha or an older mochafloats? Read Migrating to mochafloats 6.

Credits and license​

mochafloats is a fork of Mocha © Unnamed Team, and like Mocha it is distributed under the MIT license. Parts of these pages are based on Mocha's documentation.

The source code lives at PlayerAnimationLibrary/mochafloats.