The District of Joban JCM:Scripting:Documentation:Resource Loading

JCM:Scripting:Documentation:Resource Loading

From The District of Joban

Resource Loading

NTE/JCM provides several methods for controlling the loading and retrieval of resources within resource packs in JavaScript scripts.

Code written in the top-level space outside of functions will be executed when a resource pack is loaded and can be used to initialize resources such as models, textures, etc. Resources that should not be different for each train (such as models and such) are recommended to be stored in global variables to avoid excessive memory usage when loading a copy of the same content for each train.

Identifier (ResourceLocation)

Minecraft uses a Identifier to identify files in a resource pack. Many but not all functions only accept Identifier paths rather than strings. To create a Identifier, you can use the following functions:

Functions Description
static Resources.id(idStr: string): Identifier Converts the string to the appropriate Identifier

e.g. Resources.id(“mtr:path/absolute.js”).

static Resources.idr(relPath: string): Identifier Converts the string to Identifier relative to the directory that the currently executed script are located in

e.g. Resources.idr(“relative.js”).

Model Loading

Model loading is not implemented in JCM for now.

Loading AWT Resources

These functions load the resources used to draw dynamic textures via the Java AWT.

Functions Description
static Resources.getSystemFont(name: string): Font Get a system or MTR built-in font.
Font Name Description
Noto Serif MTR's built-in serif font (similar to Song). Identical on all systems.
Noto Sans NTE Built-in sans serif font (similar to Helvetica). Identical on all systems. [Not available in JCM for now]
Serif A serif font selected by AWT that is installed on this computer. May be different on different devices.
SansSerif A sans serif font selected by AWT that is installed on this computer. May be different on different devices.
Monospaced A monospaced font selected by AWT that is installed on this computer. May be different on different devices.
static Resources.readFont(path: Identifier): Font
  • Load a custom TTF or OTF font file as an AWT Font.
  • Note: Due to the way Java and Minecraft Resource Pack works, new temporary font files are generated on your disk every time you call this function. If you find yourself running out of disk space during the development, you can free up disk space by deleting files starting with +~JF on your system temp directory, or restart your Minecraft/Computer.
static Resources.readBufferedImage(path: Identifier): BufferedImage Loads an image file as an AWT BufferedImage.
static Resources.getFontRenderContext(): FontRenderContext Get an AWT FontRenderContext.

Reading resource files

Functions Description
static Resources.readString(location: Identifier): string? Reads the contents of a resource file as a string. Returns null if reading fails.