Repository navigation
plugin development_us
English · 中文
A NekoJS plugin is an ordinary Minecraft mod that adds features to scripts from Java: new global bindings, event groups, type adapters, recipe namespaces, editor declarations and more.
NekoJS's own built-in features use the same plugin interface, so a plugin can do anything the built-ins do.
This plugin adds a global binding MyHelper to every script.
package com.example.myaddon;
import com.tkisor.nekojs.api.NekoJSPlugin;
import com.tkisor.nekojs.api.annotation.RegisterNekoJSPlugin;
import com.tkisor.nekojs.api.data.BindingRegistry;
@RegisterNekoJSPlugin
public class MyAddonPlugin implements NekoJSPlugin {
@Override
public void registerBinding(BindingRegistry registry) {
registry.register("MyHelper", new MyHelperJS());
}
}package com.example.myaddon;
import com.tkisor.nekojs.api.annotation.Doc;
public class MyHelperJS {
@Doc("Adds two integers.")
public int add(int a, int b) {
return a + b;
}
}Use it in a script:
// server_scripts/test.js
console.info(MyHelper.add(1, 2)) // 3To check it works:
- Put your plugin mod, NekoJS and Graal in
mods/and start the game. - Join a world and check that
logs/nekojs/server.logprints3. - Run
/nekojs probe. In VS Code, typingMyHelper.should showaddwith its description.
The plugin class must implement NekoJSPlugin, be a concrete class (not an interface or abstract class), and have a public no-argument constructor. Discovery differs by loader:
| Loader | How to register |
|---|---|
| NeoForge | Annotate the class with @RegisterNekoJSPlugin; NekoJS scans all mods for it |
| Fabric | Declare a nekojs entrypoint in fabric.mod.json. The annotation alone is not discovered |
The Fabric entrypoint snippet (merge it into your fabric.mod.json):
{
"entrypoints": {
"nekojs": ["com.example.myaddon.MyAddonPlugin"]
}
}To support both loaders, add both the annotation and the entrypoint. On Fabric, if the class also has the annotation, its priority, requiredMods and clientOnly are used; without it the priority is 1000.
There are no rules for package names, class names or how many plugin classes one mod has.
@RegisterNekoJSPlugin(
priority = 1000, // higher runs first; default 1000
clientOnly = false, // true: not loaded on a dedicated server
requiredMods = { "create" } // loaded only when all listed mods are present
)| Parameter | Default | Effect |
|---|---|---|
priority |
1000 |
Plugin order; higher runs first. The built-in core plugin uses NekoJSPlugin.CORE_PRIORITY (Integer.MAX_VALUE) and always runs first |
clientOnly |
false |
Load only on the client; skipped on a dedicated server |
requiredMods |
{} |
Load only when every listed mod is present; useful for integration plugins |
Plugins with the same priority are ordered by fully qualified class name. The order is stable, but renaming the class changes it, so do not rely on it.
A skipped plugin (missing required mod or clientOnly) only logs at debug level. A plugin class that cannot be instantiated logs the error Failed to instantiate plugin ....
Every NekoJSPlugin method has an empty default, so override only what you need. There are two kinds of hooks:
-
Registration hooks (
registerXxx) are called once each while NekoJS initializes plugins at game startup, to record your contributions. Write to the registry you are given only while the hook runs. Do not keep the registry for later: later writes have no effect, and some registries throwIllegalStateException. -
Callback hooks (
init,afterScriptsLoadedand so on) are called at the matching moment. Overriding them is enough.
A normal /nekojs reload does not call registration hooks again. Restart the game after changing a plugin.
| Hook | Purpose |
|---|---|
registerBinding(BindingRegistry) |
Register global bindings; see below |
registerAdapters(JSTypeAdapterRegistry) |
Register type adapters so Java parameters accept strings, objects and other JS inputs. See Type adapters |
registerTypeDocs(TypeDocsRegister) |
Add or replace editor declarations; see below |
registerNodeModules(NodeModuleRegister) |
Register JS modules loadable with require('id')
|
registerNodeTypeDocs(TypeDocsRegister) |
Provide declare module declarations for those modules |
| Hook | Purpose |
|---|---|
registerEvents(EventGroupRegistry) |
Register event groups (server and common) |
registerClientEvents(EventGroupRegistry) |
Register event groups that exist only on the client |
See Event extensions for how to write them.
| Hook | Purpose |
|---|---|
registerRecipeNamespaces(RecipeNamespaceRegister) |
Register the Java implementation behind event.recipes.<namespace>.<method>(...)
|
registerRecipeSchemas(RecipeSchemaRegister) |
Register or correct the field definitions of a recipe type |
registerRecipeLifecycleHooks(RecipeLifecycleRegister) |
Register callbacks around recipe loading; the default wires up beforeRecipeLoading and afterRecipes
|
Recipe schema priority, highest first: event.registerSchema in scripts > data-driven > plugins > automatic scanning.
| Hook | Purpose |
|---|---|
registerScriptCompilers(ScriptCompilerRegistry) |
Register a new script language or file extension |
registerScriptProperty(ScriptPropertyRegistry) |
Register a new script property (// key: value); see Script properties
|
registerProbeBackends(ProbeBackendRegistry) |
Register a new declaration generator; see Probe type generation |
modifyWorkspaceConfig(JSConfigModel, String) |
Edit each script folder's jsconfig.json before it is written |
registerApiSurface(ApiContributionRegistry) |
Provide API surface metadata |
| Method | When it is called |
|---|---|
init() |
After plugins are initialized, before startup scripts load |
initStartup() |
After startup scripts load |
afterInit() |
After all mods finish initializing (FMLLoadCompleteEvent on NeoForge) |
beforeScriptsLoaded(ScriptType) |
Before a full load or full reload of one script type; single-file reloads do not trigger it |
afterScriptsLoaded(ScriptType) |
After a full load or full reload of one script type; single-file reloads do not trigger it |
beforeRecipeLoading(RecipeLifecycleContext) |
Before recipes load; you can edit the raw recipe JSON |
afterRecipes(RecipeLifecycleContext) |
After all recipes have loaded |
If you override registerLifecycleHooks or registerRecipeLifecycleHooks without calling the default implementation, the matching convenience callbacks are no longer called.
| Method | Purpose |
|---|---|
attachServerData / attachLevelData / attachPlayerData
|
Attach in-memory custom data to the server, a level or a player; it is not saved |
generateData(DataGeneratorJS) |
Generate data pack content. Runs before the script ServerEvents.generateData and shares its generator |
generateAssets(DataGeneratorJS) |
Generate resource pack content. Runs before the script ClientEvents.generateAssets
|
generateLang(LangGeneratorJS) |
Generate language file entries |
generatedLangs() |
Declare which languages generateLang is called for; default en_us
|
generateLang is called once for every language in the current round, including languages requested by other plugins and scripts. If you only generate one language, check generator.getLang() first:
@Override
public void generateLang(LangGeneratorJS generator) {
if (!"zh_cn".equals(generator.getLang())) return;
// add zh_cn entries here
}registerBinding is called once per script type, and each call gets the registry for that type.
@Override
public void registerBinding(BindingRegistry registry) {
// available to every script type
registry.register("MyHelper", new MyHelperJS());
// only available to client scripts
registry.register(ScriptType.CLIENT, "MyClientHelper", new MyClientHelperJS());
// bind a Java class: scripts can use its static members and call new
registry.register("MyMath", MyMath.class);
}- The value can be an instance (scripts call its public methods) or a
Class(scripts use its static members). -
The first binding with a name wins. Later ones are ignored,
registerreturnsfalse, and the log shows aNEKO-4001warning. Built-in plugins have the highest priority, so you cannot replace built-in bindings such asItemorIngredientby reusing the name. - Names have no required format, but they are global variables in scripts. Include something specific to your mod to avoid clashes.
-
CLIENTbindings are not collected on a dedicated server.
Instead of a global variable, a binding can be exported only as a module that scripts import:
registry.register(Binding.of("MyHelper", new MyHelperJS()), exposure -> exposure.versioned());import { MyHelper } from 'nekojs/api/version/myaddon'-
myaddonin the module path is the mod ID that owns the plugin, as reported by the loader. -
versioned()exports tonekojs/api/version/<modid>;stable()exports tonekojs/api/stable/<modid>. Choose exactly one. -
stable()rejects public types that expose Minecraft or loader classes. A class that must expose native types needs@ScriptNativeBridgeto pass. - This form does not also create a global variable.
- A missing mod owner, an invalid export name or a duplicate export fails with
NEKO-4057.
For how scripts use modules, see Module system.
Binding.of(name, value) infers the type from the value. When the value is a proxy (such as ProxyObject), Java reflection cannot see its members, so the pre-load member check and declaration generation get it wrong. Use the three-argument form to name the class that really has those members:
registry.register(Binding.of("MyBlock", new DelegatingBinding(
new MyBlockHelperJS(), // provides of / empty
net.minecraft.world.level.block.Block.class, // other static members go here
Set.of("of", "empty")
), MyBlockHelperJS.class));DelegatingBinding looks up the listed members on the helper first and sends everything else to the target class's static members. The built-in Item binding works this way. It is only a proxy: the native class from Java.type(...) does not have these extra methods.
Binding.close(ScriptType) is called when that script type's runtime is reset or shut down, not on every reload.
There are two ways to give your bindings hints and descriptions in the editor:
-
Annotations: put
@Doc,@Param,@Returnand others on classes and methods. See Annotations. -
registerTypeDocs: register them in code, for cases annotations cannot express.
Describe a global binding, or set the type it has in declarations:
@Override
public void registerTypeDocs(TypeDocsRegister docs) {
docs.register(TypeDocCatalogEntry.binding(
"MyHelper", // binding name
null, // type override; null uses the reflected type
"Math helpers for my addon.",
List.of("MyHelper.add(1, 2)")));
}TypeDocsRegister has some more advanced methods:
| Method | Use |
|---|---|
register(TypeDocCatalogEntry) |
Description, examples and type override of a global binding |
registerManualDeclaration(ManualDeclarationCatalogEntry) |
A hand-written global TypeScript declaration |
registerClassDeclaration(ClassDeclarationCatalogEntry) |
Replace the TypeScript declaration generated for a Java class |
registerBeanAliasExclusion(...) / registerBeanMethodProjection(...)
|
Adjust how Bean properties appear in declarations |
When a hand-written declaration uses Java types, name them with withReferences:
docs.registerManualDeclaration(ManualDeclarationCatalogEntry.of(
"myaddon:stack", "type MyStack = StackValue;", "A script-visible item stack.", List.of())
.withReferences(Map.of("StackValue",
ManualDeclarationCatalogEntry.TypeReference.host(ItemStack.class))));TypeReference.host(...) refers to the Java type itself; TypeReference.input(...) refers to its input alias (every input form its adapters accept).
To replace a class declaration, you can write generic relationships that Java reflection cannot express:
docs.registerClassDeclaration(ClassDeclarationCatalogEntry.of(
RpcBuilder.class,
"""
export class $RpcBuilder<T extends Record<string, 'string' | 'int'> = {}> {
schema<const S extends Record<string, 'string' | 'int'>>(value: S): $RpcBuilder<S>;
fn(callback: (args: { [K in keyof T]: T[K] extends 'string' ? string : number }) => void): void;
}
"""));When writing a class declaration:
- Write only the class body, without
declare module. Keep the generator's class name format$JavaName(nested classes are$Outer$Inner). - Do not redefine input aliases such as
$JavaName_; the generator provides them. - If the declaration uses other Java types, list them with the
ofoverload that takes aSet<Class<?>>. A hidden or ungeneratable dependency fails withNEKO-4033. - When one class has several replacements, the highest priority wins. Different definitions at the same priority fail with
NEKO-4032. - Replacements only change TypeScript declarations. The declaration must match the real Java behavior; NekoJS does not check this.
The built-in hand-written declarations live in NekoCommonManualDeclarations and make good references.
If your plugin should let other plugins register things with it (for example, you provide a machine framework and other mods register machines), define your own extension point. It uses the same mechanism as NekoJS's built-in hooks.
public final class MachinesPoint {
public static final String ID = "myaddon:machines";
// other plugins implement this interface to contribute
public interface Contributor extends NekoJSPlugin {
default void registerMachines(MachineCollector collector) {}
}
public static final NekoPluginExtensionPoint<Contributor, MachineCollector, List<Machine>> POINT =
NekoPluginExtensionPoint.<Contributor, MachineCollector, List<Machine>>builder(ID, Contributor.class)
.merge(MergePolicy.append())
.initializer(ctx -> new MachineCollector())
.collector(Contributor::registerMachines)
.finish(MachineCollector::snapshot)
.build();
}The plugin that defines the point also implements NekoPluginExtensionProvider and registers it in registerPluginExtensionPoints:
@RegisterNekoJSPlugin
public class MyAddonPlugin implements NekoPluginExtensionProvider {
private static NekoPluginExtensionHandle<List<Machine>> machines;
@Override
public void registerPluginExtensionPoints(NekoPluginExtensionRegistry registry) {
machines = registry.register(MachinesPoint.POINT);
}
public static List<Machine> machines() {
return machines.result(); // available after plugin bootstrap
}
}Rules:
-
initializer,collector,mergeandfinishare all required; if one is missing,build()throwsIllegalStateException. -
mergehas no default. Choose a conflict policy:append(),firstWin(),overrideWarn()orfailFast(). - The ID must be globally unique, including against the built-in
nekojs:*IDs; a duplicate throwsIllegalArgumentException. Use<your modid>:<name>. - Extension points can only be registered inside
registerPluginExtensionPoints; registering later throwsIllegalStateException. - If you need another point's result, declare it with
dependsOn(...). -
MachineCollectorandMachineare your own types, not NekoJS API.
Each built-in hook corresponds to an XxxPoint in the com.tkisor.nekojs.core.plugin package. Overriding the NekoJSPlugin hook and implementing the matching XxxPoint.Contributor interface have exactly the same effect.
- Prefer NekoJS's platform-neutral types, for example
NekoIdinstead of Minecraft'sIdentifier/ResourceLocation. - Check runtime feature differences with
Platform.capabilities()instead of hard-coding loader names. - Use
requiredModsorclientOnlyto control loading of plugins that only apply to some platforms. - To implement the same set of methods in several platforms' Extension interfaces, you can use the Spec interface pattern; see Annotations.
NekoJS does not publish Maven artifacts yet, so plugins depend on the NekoJS release jar directly. Graal comes from CurseMaven:
repositories {
maven { url = "https://cursemaven.com" }
}
dependencies {
// Graal 25.1.3.7. File 8762962 is the NeoForge build; use 8762963 for Fabric.
compileOnly "curse.maven:graal-1504336:8762962"
// The NekoJS release jar for your Minecraft version and loader.
compileOnly files("libs/nekojs-<version>.jar")
}NekoJS's public API has no semantic versioning guarantee yet. Recompile and test your plugin after upgrading NekoJS.
The built-in features are spread over several plugins that make good references:
-
common/.../core/plugin/NekoCommonBuiltinPlugin.java: platform-neutral bindings, compilers, script properties and type docs. -
src/main/java/.../core/NekoJSCorePlugin.java: Minecraft-facing bindings, adapters and events. -
NekoBuiltinPointsPlugin: registers all built-in extension points. - Fabric also has
FabricCorePlugin.
Next steps:
- Type adapters: let Java methods accept several JS input forms.
- Event extensions: register custom event groups.
- Annotations: control member names, visibility and editor descriptions.
-
Probe type generation: how bindings end up in
.d.ts.
Previous: Node.js compatibility · Contents · Next: Type adapters
- 首页 / 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