Skip to content

annotations_us

Tki_sor edited this page Nov 12, 2026 · 1 revision

English · 中文

Annotations

NekoJS annotations go on Java classes exposed to scripts. They do two jobs:

  • Control which members scripts see: renaming and hiding. These take effect at runtime.
  • Add types and descriptions for the editor: these only affect the declarations generated by /nekojs probe, not whether scripts can call anything.

The annotations live in com.tkisor.nekojs.api.annotation; @PlatformAvailability is in com.tkisor.nekojs.api.spec. Write annotation text in English.

Example

import com.tkisor.nekojs.api.annotation.*;

@Doc("Helpers for my addon.")
public class MyHelperJS {
    @Remap("add")
    @Doc("Adds two integers.")
    @Param(name = "a", value = "first number")
    @Param(name = "b", value = "second number")
    @Return("the sum")
    public int addInternal(int a, int b) {
        return a + b;
    }

    @HideFromJS
    public void resetCache() { }
}

Scripts call MyHelper.add(1, 2) and cannot see addInternal or resetCache. After /nekojs probe, the editor shows the description and parameter docs for add.

Overview

Annotation Applies to Effect Where it applies
@Remap("name") Methods, fields Renames to the given script name Runtime and declarations
@RemapByPrefix({...}) Classes, methods, fields Strips a name prefix Runtime and declarations
@HideFromJS Classes, methods, fields Hides from scripts Runtime and declarations
@Doc("...") Classes, methods, constructors, fields Description text; repeatable Declarations
@Param(name, value) Methods, constructors Parameter description; repeatable Declarations
@Return("...") Methods Return value description Declarations
@Overload({...}) Methods, constructors Extra TypeScript signature; repeatable TypeScript declarations
@DeprecatedNekojs Classes, methods, constructors, fields Marks something scripts should stop using Declarations
@ScriptInput(Provider.class) Parameters Sets the input shape a parameter accepts TypeScript declarations
@ScriptOutput("...") Methods Sets the TypeScript return type TypeScript declarations
@RegisterNekoJSPlugin Classes Marks a plugin class Plugin discovery
@PlatformAvailability Interfaces, methods Declares which platforms must implement a Spec interface Compile-time check
@CalledByDynamicCode Classes, methods, constructors Marks code called by generated code Source only

A few less common declaration annotations are listed under "Other declaration annotations" below.

Renaming and hiding

These three annotations change the member names scripts see at runtime. Generated method declarations use the same names.

@Remap

Gives one method or field a specific script name:

public class NbtHelperJS {
    @Remap("byte")              // byte is a Java keyword, so the Java method needs another name
    public NbtValue byteValue(Number value) { ... }
}

Typical uses: the script name is a Java keyword (like byte in the built-in NBT API), or you want the internal Java name to differ from the script name.

@Remap can also be placed on a parameter, but it currently has no effect: declarations still take parameter names from Java reflection.

@RemapByPrefix

Strips a name prefix in bulk. On a class, it applies to every member that class declares:

@RemapByPrefix("neko$")
public interface ItemStackExtension extends ItemStackSpec {
    default String neko$getId() { ... }   // scripts see getId(), and the Bean property id
}
  • Only the first matching prefix is stripped, and the rest of the name must not be empty.
  • Case is kept: @RemapByPrefix("get") turns getName into Name, not name. For a lowercase name, use @Remap("name") on that member.
  • The main use is stripping the neko$ prefix (see Spec interfaces below). Stripping neko$ gives getId, so scripts can also read the Bean property id.

@HideFromJS

Hides a method or field from scripts:

public class TimerJS {
    @HideFromJS
    public void close() { ... }   // engine-internal; scripts cannot call it
}

On a class, it hides the methods and fields declared by that class. It does not hide inherited members, the class itself or its constructors.

Precedence

When a member has several of these annotations, its script name is decided in this order:

  1. @HideFromJS (on the member or its declaring class): hidden.
  2. @Remap on the member.
  3. @RemapByPrefix on the member.
  4. @RemapByPrefix on the declaring class or an interface it implements.
  5. Otherwise, the Java name.

Completion docs

These annotations only affect the declarations generated by /nekojs probe. They do not change whether or how scripts can call anything.

@Doc, @Param, @Return

@Doc("Creates an item stack from an id or item-like value.")
@Param(name = "id", value = "item id like 'minecraft:stone', '#tag', or an item-like object")
@Return("the resolved stack; never null")
public static ItemStack of(Object id) { ... }
  • @Doc can be repeated; each one is a paragraph.
  • Put @Param on the method or constructor, not on the parameter; name must match the parameter name.
  • The output is the JSDoc description plus @param and @returns lines.
  • To document a global binding, or for anything annotations cannot express, use the plugin hook registerTypeDocs; see Plugin development.
  • Edits made with ProbeEvents.modifyType in scripts take precedence over annotations.

@Overload

Declares extra TypeScript signatures for a method. Use it when reflection cannot work them out: different parameter counts, parameters declared as Object, or forms with different meanings.

@Doc("Creates a stack from an id.")
@Overload({"id: string"})
@Overload(value = {"item: $ItemStack", "count?: number"}, doc = "Copy with a count.")
public static ItemStack of(Object input, int count) { ... }
  • Each value entry is one parameter, written as name: Type or name?: Type and emitted as is.
  • An empty returns uses the reflected return type; doc describes this signature.
  • Type names must be visible in the generated module, usually in $Name form. NekoJS does not check them or add imports.
  • It is only a declaration: the Java implementation must actually support every call form.
  • If one parameter simply accepts several inputs (strings, objects and so on), a type adapter is enough and you do not need @Overload. See Type adapters.
  • Only TypeScript declarations use it; Python .pyi output does not.

@DeprecatedNekojs

Tells script authors to stop using a member. Editors strike it through.

@DeprecatedNekojs(value = "Tag filters moved to Ingredient.matchTag.", replacedBy = "matchTag")
public static IngredientJS anyTag(String tag) { ... }

The output is @deprecated Tag filters moved to Ingredient.matchTag. Use matchTag instead.

It is separate from Java's @Deprecated: @Deprecated alone does not mark anything deprecated in script declarations.

@ScriptInput and @ScriptOutput

When a parameter has a broad type such as Map<String, Object>, or the real shape of a return value cannot be expressed with Java types, use these to write the exact TypeScript type.

public boolean register(String id, @ScriptInput(PostEffectOptions.class) Map<String, Object> options) { ... }

@ScriptOutput("import('java:java/util').$Map<string, unknown> & { readonly width: number; readonly height: number }")
public Map<String, Object> measure() { ... }
  • The @ScriptInput class must implement ScriptInputShapeProvider and have a public no-argument constructor. Its inputShape() describes the input with AdapterInputShape.
  • @ScriptOutput takes a TypeScript type expression and also applies to Bean getters and inherited methods. Refer to external types with inline import('module').Type. If the returned value still has callable Java methods, intersect with the Java type as in the example.
  • Both only affect TypeScript declarations; they do not convert or check runtime values. The declaration must match the real behavior.
  • An empty @ScriptOutput expression fails generation with NEKO-4064.

Other declaration annotations

Annotation Applies to Effect
@ScriptNullable Methods, parameters A registry builder getter or setter parameter may be null; declarations add | null
@ScriptInternalConstructor Constructors Leaves the constructor out of declarations, for objects only the engine creates. Does not change whether it can be called at runtime
@ScriptTypeView(Interface.class) Classes Return values and event objects appear as the given public interface in declarations instead of the implementation class
@ScriptNativeBridge Classes Lets a stable() API module expose Minecraft or loader types
@ContractReceiver Classes NekoJS internal; marks data types that can receive extension methods

@RegisterNekoJSPlugin

Marks a plugin class on NeoForge. Fabric also needs a nekojs entrypoint. Parameters: priority (default 1000, higher runs first), clientOnly (default false) and requiredMods (default {}, all must be present). See Plugin development.

Spec interfaces and @PlatformAvailability

NekoJS uses Spec interfaces to describe "a set of script methods every platform must implement", and each platform's Extension interface implements them. Method names carry a neko$ prefix to avoid clashing with Minecraft's own methods, and @RemapByPrefix("neko$") strips it for scripts.

// Spec: the cross-platform script surface
@RemapByPrefix("neko$")
@PlatformAvailability(PlatformAvailability.Scope.ALL)
public interface EntitySpec {
    String neko$getId();
    void neko$kill();
}

// Platform Extension: implements the spec with Minecraft types
@RemapByPrefix("neko$")
public interface EntityExtension extends EntitySpec {
    @Override
    default String neko$getId() { ... }

    @Override
    default void neko$kill() { ... }
}

Scope values:

Value Meaning
ALL Every platform must implement it
NF_ONLY NeoForge only (1.21.1 and 26.x)
NF26_ONLY NeoForge 26.x only
CR_ONLY Old Cleanroom marker; there is currently no supported build for it

Check Fabric support separately; @PlatformAvailability itself does not implement anything.

Compile-time checks: NekoJS's own NeoForge build runs the common-api-processor annotation processor, which checks that:

  • An Extension interface that directly extends a Spec implements every neko$ method of that Spec itself; a missing one is a compile error.
  • When compiled with -Anekojs.platform=nf26 or nf121 (cr is also accepted), every Spec the current platform requires has an Extension. The old value nf is an error.

NekoJS's Fabric build does not run this processor yet, and third-party plugins usually do not either. Without it, Spec interfaces are just a convention: the neko$ prefix and renaming still help, but nothing is checked at compile time.

@CalledByDynamicCode

Marks classes, methods and constructors called by generated JS code or reflection, so maintainers do not delete them as unused. It is kept in source only and does nothing at runtime. Whether your IDE stops reporting them as unused depends on the tool.

Notes

  • Public field declarations ignore renaming and hiding. At runtime, @Remap, @RemapByPrefix and @HideFromJS all apply to fields, but generated declarations still show fields under their Java names, even with @HideFromJS. Adjust declarations with ProbeEvents.modifyType in scripts if needed.
  • Declaration files are not a security boundary. Hiding or renaming something in declarations does not restrict runtime access; use @HideFromJS to really hide it.

Next steps:


Previous: Event extensions · Contents · Next: Project architecture

NekoJS Wiki

中文 · English

先看这里 / Start here

  • 首页 / Home · 中文 · EN
  • 平台与兼容性 / Platforms and compatibility · 中文 · EN
  • 快速开始 / Quick start · 中文 · EN
  • 配置参考 / Configuration · 中文 · EN
  • 常见问题 / FAQ · 中文 · EN

脚本作者 / Script authors

  • 脚本基础 / 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

整合包作者 / Modpack authors

  • 版本化脚本包 / 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

插件开发者 / Plugin developers

  • 插件开发 / Plugin development · 中文 · EN
  • 类型适配器 / Type adapters · 中文 · EN
  • 事件扩展 / Event extensions · 中文 · EN
  • 注解体系 / Annotations · 中文 · EN

项目贡献者 / Project contributors

  • 项目架构 / Project architecture · 中文 · EN
  • Probe 类型生成 / Probe type generation · 中文 · EN
  • 构建系统 / Build system · 中文 · EN

Clone this wiki locally