๐Ÿ› ๏ธ How to Use the Code Editor

Welcome to the Code Editor! This guide will help you get started writing and customizing crafting recipes using JavaScript.

๐Ÿ“„ Overview

This code editor allows you to define resources, processes, and recipes in a crafting system using JavaScript. Your script is executed in a sandboxed environment (your browser) using eval(), with an input object (arguments[0]) that serves as the connection to the rest of the application.

๐Ÿงฉ Default/Example Template

// This is written in javascript and passed to an eval function.

let data = arguments[0];  // Connection to the outside

let resources = {
    "Stick": new Resource("Stick"),
    "Iron Ingot": new Resource("Iron Ingot"),
    ...
}

resources["Oak Log"].isBase = true;
...

let processes = {
    "Crafting Table": new Process("Crafting Table"),
    ...
}

let recipes = [
    new Recipe("Furnace", [new Stack("Iron Ore")], [new Stack("Iron Ingot")]),
    ...
]

data.resources = resources;
data.processes = processes;
data.recipes = recipes;
data.meta = {dataVersion: 1, name: "Pickaxe"};

๐Ÿงฆ Key Concepts

The exact details which are guarenteeded to be up to date with the latest version of the site can be found in the units.ts file in the CraftingCalc repository.

BaseThing is the foundation for all resources and processes, providing common properties found in anything that extends BaseThing. It is not meant to be used directly. As of writing, imgUrl, sourceUrl, and tags are not used in the calculator.

class BaseThing {
  name: string;
  imgUrl?: string;
  sourceUrl?: string;
  isBase: boolean = false;
  isDisabled: boolean = false;
  durability: number = -1;
  value: number = 1;
  tags: Array = [];
    
    constructor(name: string, config = {}) {
      this.name = name;
      if (config) Object.assign(this, config);
    }
  }
  

๐Ÿ”น Resources

This class is a resource or item in the game. This can include items such as "Iron Ingots" or more ethereal things such as "Energy".

class Resource extends BaseThing {
  baseQuantity: number = 1;
  constructor(name: string, config = {}) {
    super(name, config);
  }
}

// Example usages:
// Building by hand
let ironIngot = {
  name: "Iron Ingot",
  isBase: false,
  durability: -1,
  value: 1,
  tags: [],
};

// Shortend versions
let ironIngot = new Resource("Iron Ingot");
let oakLogResource = new Resource("Oak Log", { isBase: true, value: 2});

๐Ÿ”ธ Processes

Processes represent crafting stations or other methods to transform resources. Under the hood, it works the same as BaseThing, with no added fields.

class Process extends BaseThing {
  constructor(name: string, config = {}) {
    super(name, config);
  }
}

// Example usages following the same pattern as Resource:
let craftingTable = new Process("Crafting Table");
let furnace = new Process("Furnace", { imgUrl: "furnace.png" });
let solarPanel = new Process("Solar Panel", { isBase: true, isDisabled: true });

๐Ÿ“ฆ Stacks

Defines quantities of a specific resource. There is no restriction on how large amount can be. (for modded reasons)

class Stack {
  constructor(
    public resourceName: string,
    public amount: number = 1
  ) {}
}

// Example usage:
let singlePickaxe = new Stack("Wooden Pickaxe");
let ingots = new Stack("Iron Ingot", 128); // 128 Iron Ingots

๐Ÿ“œ Recipes

Defines how resources can be transformed. Bonus outputs are not implemented yet.

class Recipe {
  constructor(
    public processUsed: string,
    public inputResources: Stack[],
    public outputResources: Stack[],
    public outputBonusChances: [string, ProbabilityStyle][] = [],
    public timeSpent: number = 0,
    public id?: number, // This should not be set by the user, it is set by the system.
    public isDisabled: boolean = false
  ) {}
}

// Example usages:
// Simple recipe constructor is recomended
let simpleRecipe = new Recipe(
  "Crafting Table",
  [new Stack("Wooden Plank", 4)], // Stacks are used because order does not matter, net input -> output does.
  [new Stack("Crafting Table")]
);

let solarPanelEnergyRecipe = new Recipe(
  "Solar Panel",
  [],
  [new Stack("Energy", 1000)],
);

Meta Object

The meta object contains metadata about the preset, such as its version and name. It is not used in the crafting calculator but can be useful for organization. It exists as a bit of a sanity check for the calculator.

data.meta = {
  dataVersion: 1, // Version of the data format
  name: "Pickaxe" // Name of the preset
};

๐Ÿงช Live Editing

You can modify this code to:

  • Add new materials
  • Create new recipes
  • Disable or enable resources
  • Add new processes (e.g., "Smelter", "Grinder")

All changes are applied dynamically through the data object passed in.

โš ๏ธ Notes

  • JavaScript errors will prevent your code from running.
  • Ensure all variables are properly declared and added to data.
  • Do not redeclare data itself.

๐Ÿงฑ Example: Add a New Recipe

Add a recipe to turn Plank Dust into Plank using a Dryer:

processes["Dryer"] = new Process("Dryer");
recipes.push(
new Recipe("Dryer", [new Stack("Plank Dust")], [new Stack("Plank")])
);

๐Ÿ“š See Also