Welcome to the Code Editor! This guide will help you get started writing and customizing crafting recipes using JavaScript.
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.
// 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"};
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);
}
}
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});
name: The name of the resource.imgUrl?: Optional image URL.sourceUrl?: Optional source or reference link.isBase: True if the item exists naturally.isDisabled: True if the item is blocked from use.durability: Tool durability, if applicable (default -1).value: General worth (default 1).tags: Custom categories or filters.baseQuantity: Quantity produced in basic form (default 1).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 });
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
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)],
);
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
};
You can modify this code to:
All changes are applied dynamically through the data object passed in.
data.data itself.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")])
);