Skip to content

[EN] Dev Cheat Sheet

Redeix edited this page Jul 6, 2025 · 21 revisions

Cheat Sheet

The purpose of this page is for developers to document helpful functions for future use. If you have any JavaScript or java code that would be nice to share please put it here.


JavaScript:

Tag to array

Here is an example of turning an item tag into an array, you can omit any part as necessary.

	const tag_array = Ingredient.of('#forge:tag').itemIds.toArray().map(String);
  • .itemIds This accesses the list of item ids from that ingredient tag. The result is a Java Set.
  • .toArray() This converts the list (which is a Java Set) into a JavaScript array.
  • .map(String) This converts each item ID in the array to a string. Required for most js functions, but not all.

Tag Subtraction

Here is an example of using an array in a recipe to produce the same array, except the original item. Useful for stonecutters, dyeing, etc.

	const tag_array = Ingredient.of('#forge:tag').itemIds.toArray().map(String);

	tag_array.forEach(item => {
		event.stonecutting(item, 
			Ingredient.of('#forge:tag').subtract(item)
		).id(`tfg:stonecutter/${item.replace(/:/g, "/")}`)
	})

JavaScript to JSON

The example below is used in the armor trim builder.

materials.forEach(material => {
    const trimfilepaths = [
      `kubejs/data/minecraft/trim_material/${material.materialName}.json`,
      `kubejs/data/tfc/trim_material/${material.materialName}.json`
    ];
  
    const newtrimdata = {
      asset_name: material.materialName,
      description: {
        color: material.nameColor,
        translate: `trim_material.tfc.${material.materialName}`
      },
      ingredient: material.itemName,
      item_model_index: material.indexNumber
    };
  
    trimfilepaths.forEach(trimfilepaths => {
      const existingData = JsonIO.read(trimfilepaths);
  
      // Only write if the file is missing or contents are different
      if (JSON.stringify(existingData) !== JSON.stringify(newtrimdata)) {
        JsonIO.write(trimfilepaths, newtrimdata);
      }
    });
  });
  1. Declare the file paths. (example above: trimfilepaths)
  2. Create a const version of the json information. (example above: newtrimdata)
  3. For each file path, read its contents, and write new data if missing. (example above: existingData)

Using Fluid Tags

If you want to use a fluid tag; use the example below. Instead of Fluid.of()

    JsonIO.of({ amount: int, value: { tag: "forge:fluid_tag" }})

Java:


JSON:


Clone this wiki locally