Skip to content

Writing scripts

The-Shortman edited this page Sep 27, 2025 · 1 revision

This page explains how to write scripts in a way that matches the rest of the codebase.


  • Wrap every script inside an Immediately Invoked Function Expression (IIFE). This ensures no variables leak into global scope unless you really intend to by assigning to the global object:
// Example of an IIFE
(function () {
  const private = "I'm not global and thus accessible only in this script!";
  global.globalString = "I'm global and thus accessible in every script!";
})();
// IIFEs can be named
(function myScript() {
  // ...
})();
  • For long lists of recipes, use an array of objects. Do not use arrays of arrays (tuples), as they are harder to document with JSDoc.
// Do this
const hauntingRecipes = [
  {
    input: "naturalist:snail_shell",
    output: "minecraft:nautilus_shell",
  },
  // ...
];

hauntingRecipes.forEach((recipe) => {
  // ...
});
// Don't do this
const hauntingRecipes = [
  ["naturalist:snail_shell", "minecraft:nautilus_shell"],
  // ...
];

hauntingRecipes.forEach((recipe) => {
  // ...
});
  • For stacks of multiple items, prefer using Item.of(item, count) instead of ${count}x ${item}, especially when you are dynamically creating the stacks. This makes it easier to type-check the source code against ProbeJS types.
// Encouraged
{
  input: Item.of("techreborn:coal_dust", 2),
  output: "techreborn:coal_dust",
},
// Discouraged
{
  input: "2x minecraft:charcoal",
  output: "minecraft:coal",
},
  • Use template literals instead of string concatenation whenever possible. This allows for type-checking all possible variants of a template literal against ProbeJS types.
// TypeScript is able to type-check JavaScript code with JSDoc annotations.
// This one tells TypeScript that the array won't ever change, so each result can be type-checked.
const gems = /** @type {const} */ (["peridot", "red_garnet", "ruby", "sapphire", "yellow_garnet"]);
// Do this
gems.forEach((gem) => {
  HIDDEN_ITEMS.push(`techreborn:${gem}_storage_block`);
});

// Don't this
gems.forEach((gem) => {
  HIDDEN_ITEMS.push("techreborn:" + gem + "_storage_block");
});
  • This repository defines constants and common code in the global object.
    • global.server stores common constants and functions for server code.
    • global.fluids stores constants related to fluid amounts, like BUCKET or mB.
    • global.startup stores constants and functions for startup code.
  • To access the constants, destructure the object inside the IIFE:
(function myScript() {
  const { createSequencedAssembly } = global.server
  const { BUCKET, mB } = global.fluids

  // ...
})();

Clone this wiki locally