Repository navigation
mixinjs_us
English · 中文
MixinJS lets a script run code at the start or the return of a Minecraft Java method. It can read the arguments, change the return value or cancel the call. It has two parts:
- An injection declaration that says which method to inject into. The loader applies it at game startup, so changing it needs a game restart.
- A callback in an ordinary script that decides what happens at the injection point. You can change callbacks at any time with
/nekojs reload.
This is an experimental feature for development environments only. In a normal game (production environment), NekoJS refuses to load injections. So far only simple cases have been verified, on a NeoForge 26.1.2 development dedicated server. Other versions, loaders and client setups have not been fully tested. Do not use it in a released modpack or on a real server.
MixinJS is off by default. Add this to nekojs/config/engine.toml:
[mixinjs]
enabled = trueThen fully restart the game. Every later change to this switch, including turning it off, needs a full restart. /nekojs reload and /nekojs probe do not reread it.
- If the config file is missing or malformed, MixinJS stays off.
- MixinJS does not need
allowAsmorallowReflection, and turning it on does not turn those on. - While it is off, code completion for declaration files and the offline tool still work. Injections just do not take effect.
For other settings, see Configuration.
| Part | Where it goes | How it takes effect |
|---|---|---|
| Injection declaration: target class, method, injection point, Hook ID |
*.mixin.js or *.mixin.ts under nekojs/mixins/
|
Generate the injection file, then fully restart the game |
| Callback: read arguments, change the return value |
nekojs/server_scripts/ or nekojs/client_scripts/
|
After the game loads the injection, load or reload it like any script |
The two parts are linked by a Hook ID such as demo:max_health. Declaration files use import { Mixin } from "nekojs/mixin". Ordinary scripts use the global MixinEvents.
This example sets every mob's maximum health to 40. Create nekojs/mixins/max-health.mixin.js:
import { Mixin } from "nekojs/mixin";
Mixin.target("net.minecraft.world.entity.LivingEntity", target => {
target.inject("getMaxHealth()F", "demo:max_health", build => {
build.at = "RETURN";
build.cancellable = true;
build.scriptType = "SERVER";
build.thread = "SERVER_MAIN";
});
});- The first argument is the method name plus its full JVM descriptor:
getMaxHealth()Fmeans no arguments, returns float;tick()Vmeans no arguments, returns void. - The second argument is the Hook ID, in the form
namespace:path. It may only contain lowercase letters, digits,_,.,-and/. It must be unique across all loaded declarations. - Class and method names must be the names the running game actually uses. They can differ between Minecraft versions and loaders. NekoJS does not translate mappings for you.
Fields you can set:
| Field | Values | Default |
|---|---|---|
at |
HEAD (start of the method), RETURN (when it returns) |
HEAD |
cancellable |
Must be true to cancel the call or change the return value |
false |
scriptType |
SERVER, CLIENT
|
SERVER |
thread |
Must match scriptType: SERVER_MAIN or CLIENT_MAIN
|
SERVER_MAIN |
require |
Only 1
|
1 |
staticMethod |
true when the target is a static method; must match the real method |
false |
Note that the injection only applies to the method in the class you name. If a subclass overrides the method and does not call the parent version, calls on that subclass skip your injection.
Declaration files run in a separate, restricted environment:
- No access to Java classes, files or the network.
- They can import only
nekojs/mixinand relative files with a.jsor.tsextension. Imported files cannot export anything. - No default imports, dynamic
import()or top-levelawait. -
build => { ... }must finish synchronously and return nothing. Setting an unknown field is an error. - One run may execute at most 100,000 statements.
NeoForge 26.1.2 and 26.2.0: generated for you. Put your declarations in nekojs/mixins/ and start the game. NekoJS generates mods/nekojs-mixins-auto-<hash>.jar, then stops this launch with NEKO-4090 and asks you to restart. After the next start, the injection is active.
- The jar does not appear in the mod list, and you do not install it yourself.
- Every time you change a declaration, it is regenerated and you need to restart once more.
- After you delete all declarations, the next launch no longer loads the injection. If the injection was loaded at that point, you need one more restart.
- Old jars are not deleted, but they are not loaded either. Delete them by hand while the game is closed.
- Do not also install an injection jar by hand; that causes an error.
Older NeoForge and Fabric: build it yourself. This needs this repository's development setup. You run it with Gradle from the repository root; it is not an in-game command. First create mixin-args.json (a separate argument file avoids PowerShell quoting problems):
[
"mixins", "validate",
"--source", "nekojs/mixins",
"--classes", "runtime-targets.jar",
"--minecraft", "26.1.2",
"--loader", "neoforge",
"--loader-version", "26.1.2.71",
"--side", "client"
]./gradlew.bat :common:packTool -PpackArgsFile=mixin-args.json- Replace
--classeswith a jar or folder that contains the target classes' bytecode. - Versions must match the running environment exactly. On a 26.2.0 environment, the Minecraft version is
26.2. For Fabric, use--loader fabricand set--loader-versionto the Fabric Loader version, not the Fabric API version. -
--side clientis for the client (including a single-player world's built-in server) and may contain both SERVER and CLIENT hooks.--side serveris for a dedicated server and cannot contain CLIENT hooks.
Once validation passes, change validate to build, add "--output", "build/demo-mixins.jar" and run the same command. An existing output file is never overwritten, so pick a new path.
Put the generated jar in the development instance's mods/, remove any older copy, and fully restart the game. The jar depends on NekoJS; the loader rejects it if NekoJS is missing.
Either way, after changing a declaration you must regenerate and fully restart. /nekojs reload cannot add, remove or change injections.
Once the injection is loaded, create nekojs/server_scripts/max-health.js:
MixinEvents.inject("demo:max_health", event => {
event.returnValue = 40;
});Run /nekojs reload. Every mob's maximum health is now 40.
The callback must not return a value. Do not write event => event.returnValue = 40: that returns 40, and the change for that call is thrown away. The callback cannot be an async function or return a Promise either.
- Put the callback in the folder that matches the declaration's
scriptType:SERVERinserver_scripts,CLIENTinclient_scripts. Startup and test scripts cannot use it. - Each Hook takes exactly one callback. An unknown Hook ID, the wrong script type or a second registration makes that load fail, and the game keeps the last scripts that loaded successfully.
- Callbacks must come from local script files. Script packs synced from a server cannot register callbacks.
- A successful reload replaces the old callback with the new one.
- Leaving the world clears callbacks. They are registered again the next time the scripts load.
| Member | Meaning |
|---|---|
self |
The object the method was called on; null for a static method |
args |
The method's arguments, read-only |
returnValue |
At RETURN, the original return value; at HEAD, nothing yet. You can only change it when cancellable = true and the method returns a value |
cancel() |
Cancels the call. Only for a method with cancellable = true and at = "HEAD" that returns void |
- If you leave
returnValuealone, the method returns its original result. - If the callback throws, writes a value that is not allowed, or returns a value, every change for that call is discarded and the method runs as normal. Anything else the callback already did (such as changing a block in the world) is not undone.
-
eventis only valid while the callback runs. Do not keep it for a timer or async code.
The callback is skipped, and the method runs as normal, when:
- scripts are in the middle of a reload;
- the method is called on the wrong thread (for example a SERVER hook off the server main thread);
- the callback triggers the same Hook again while it runs.
A wrong-thread call or a failed callback logs a NEKO-4092 warning once.
Arguments and return values can be: boolean, char (a single character), byte / short / int (range-checked), float / double (must be finite), and Java objects and arrays (the type must match; null is allowed). A method that returns String accepts a JavaScript string. A plain JavaScript object or array cannot stand in for a Java object.
Not supported yet:
- methods with
long(includinglong[]) in their arguments or return value; - constructors and static initializers;
- abstract, native, synthetic and bridge methods;
- injection points other than
HEADandRETURN; - adding or changing injections while the game is running.
Code completion: declaration files always get completion for nekojs/mixin. When MixinJS is on and the injection is loaded, running /nekojs probe makes MixinEvents.inject suggest the types of self, the arguments and the return value for each Hook ID. If there are no suggestions, first check that the switch is on, then check that the injection really loaded.
| Error code | Check first |
|---|---|
NEKO-4090 |
Whether you need to restart as prompted; whether [mixinjs] enabled is on; whether this is a development environment; whether the declaration's class name, descriptor and fields are right; duplicate Hook IDs; whether the build's version, loader and --side match the running game; whether you mixed a generated jar with a hand-installed one |
NEKO-4091 |
Whether the callback comes from a local script file rather than a synced script pack |
NEKO-4092 |
Whether the callback returns a value or is async; whether the return value has the right type and range; whether event is used after the callback ended; whether the Hook ran on the wrong thread |
For more error codes, see Error and log reference. Mixin errors reported by the loader itself do not carry these codes; check the game log.
Previous: Client rendering and post effects · Contents · Next: Node.js compatibility
- 首页 / Home · 中文 · EN
- 平台与兼容性 / Platforms and compatibility · 中文 · EN
- 快速开始 / Quick start · 中文 · EN
- 配置参考 / Configuration · 中文 · EN
- 常见问题 / FAQ · 中文 · EN
- 脚本基础 / Script basics · 中文 · EN
- 脚本 API 约定 / Script API conventions · 中文 · EN
- 脚本属性 / Script properties · 中文 · EN
- 全局绑定 / Global bindings · 中文 · EN
- 数据、状态与持久化 / Data, state and persistence · 中文 · EN
- Python 脚本 / Python scripts · 中文 · EN
- Python 特性清单 / Python feature support · 中文 · EN
- 事件参考 / Event reference · 中文 · EN
- AI 辅助脚本编写 / AI-assisted authoring · 中文 · EN
- 命令 / Commands · 中文 · EN
- 错误与日志参考 / Error and log reference · 中文 · EN
- 客户端绘制与后处理 / Client rendering and post effects · 中文 · EN
- 版本化脚本包 / Versioned script packs · 中文 · EN
- 配方系统 / Recipe system · 中文 · EN
- 注册新内容 / Registering new content · 中文 · EN
- 模块系统 / Module system · 中文 · EN
- TypeScript 与 JSX / TypeScript and JSX · 中文 · EN
- JSX 客户端 UI / JSX client UI · 中文 · EN
- MixinJS(实验性) / MixinJS (experimental) · 中文 · EN
- Node.js 兼容 / Node.js compatibility · 中文 · EN