@throw-out-error/minecraft-datapack
Version:
A module for making minecraft datapacks with node to cut down on the repetition.
142 lines (141 loc) • 6.51 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShapedCraftingRecipe = exports.ShapelessCraftingRecipe = exports.StonecutterRecipe = exports.SmeltingRecipe = exports.Recipe = void 0;
const utility_1 = require("./utility");
const fs_1 = __importDefault(require("fs"));
const path_1 = require("path");
class Recipe {
/**
* Creates a Recipe
* @param {string} path The path of the recipe file relative to namespace/recipes (excluding the file extension)
* @param {('smelting'|'stonecutting'|'shapless'|'shaped')} type The type of recipe
*/
constructor(path, type) {
/** @type {string} The type of recipe */
this.type = type;
if (utility_1.hasIllegalCharsSlash(path))
throw new Error("The names of recipes can only contain the following characters 0-9, a-z, _, -, ., /");
/** @type {string} The path of the recipe file relative to namespace/recipes (excluding the file extension) */
this.path = path;
/** @type {object} The content of the file when it is compiled */
this.file_contents = {};
}
/**
* Outputs the recipe json file
* @param {string} path The path of the namespace the recipe will compile to
*/
compile(path) {
let recipePath = `${path}/${this.path}.json`;
utility_1.mkdirIfNotExist(path_1.dirname(recipePath));
fs_1.default.writeFileSync(recipePath, utility_1.jsonBeautify(this.file_contents));
}
/**
* Creates a copy of the recipe
* @param {Recipe} recipe
*/
static copy(recipe) {
let copy = new Recipe("_", "shapeless");
for (let key in { ...recipe })
copy[key] = recipe[key];
return copy;
}
}
exports.Recipe = Recipe;
class SmeltingRecipe extends Recipe {
/**
* Creates a SmeltingRecipe
* @param {string} path The path of the recipe file relative to namespace/recipes (excluding the file extension)
* @param {object} options The configuration of the recipe
* @param {string} options.ingredient The ingredient that needs to be smelted for the recipe, can also be a tag
* @param {string} options.result The resulting item from the recipe
* @param {number} options.experience The amount of experience gained from smelting
* @param {('minecraft:smelting'|'minecraft:blasting'|'minecraft:smoking'|'minecraft:campfire_cooking'|'smelting')} [options.type='smelting'] The type of smelting recipe
* @param {number} [options.cookingtime=200] The amount of time the item has to smelt
*/
constructor(path, options) {
super(path, "smelting");
/** @type {string} The contents of the outputted file */
this.file_contents = {
type: options.type || "minecraft:smelting",
ingredient: utility_1.itemArrayFromString(options.ingredient.split("||").map(utility_1.assumeMinecraft).join("||")),
result: options.result,
experience: options.experience,
cookingtime: options.cookingtime || 200
};
}
}
exports.SmeltingRecipe = SmeltingRecipe;
class StonecutterRecipe extends Recipe {
/**
* Creates a StonecuttingRecipe
* @param {string} path The path of the recipe file relative to namespace/recipes (excluding the file extension)
* @param {object} options The configuration of the recipe
* @param {string} options.ingredient The stone block needed for the stonecutting
* @param {string} options.result The resulting item from the stonecutting
* @param {number} [options.count=1] The amount of the resulting item
*/
constructor(path, options) {
super(path, "stonecutting");
/** @type The contents of the outputted file */
this.file_contents = {
type: "minecraft:stonecutting",
ingredient: utility_1.itemArrayFromString(options.ingredient.split("||").map(utility_1.assumeMinecraft).join("||")),
result: options.result,
count: options.count || 1
};
}
}
exports.StonecutterRecipe = StonecutterRecipe;
class ShapelessCraftingRecipe extends Recipe {
/**
* Creates a ShaplessCraftingRecipe
* @param {string} path The path of the recipe file relative to namespace/recipes (excluding the file extension)
* @param {object} options The configuration of the recipe
* @param {string[]} options.ingredients The ingredients of the recipe, can be tags
* @param {string} options.result The result of the crafting recipe
* @param {number} [options.count=1] The number of resulting items
*/
constructor(path, options) {
super(path, "shapeless");
/** @type {string} The contents of the outputted file */
this.file_contents = {
type: "minecraft:crafting_shapeless",
ingredients: options.ingredients.map(ingredient => utility_1.itemArrayFromString(ingredient.split("||").map(utility_1.assumeMinecraft).join("||"))),
result: {
item: options.result,
count: options.count || 1
}
};
}
}
exports.ShapelessCraftingRecipe = ShapelessCraftingRecipe;
class ShapedCraftingRecipe extends Recipe {
/**
* Creates a ShapedCraftingRecipe
* @param {string} path The path of the recipe file relative to namespace/recipes (excluding the file extension)
* @param {object} options The configuration of the recipe
* @param {string[]} options.pattern The 2d grid of items used in the recipe, use strings with a space ' ' to signify an empty slot
* @param {object} options.key What the characters in the pattern will be replaced with in the crafting grid
* @param {string} options.result The result of the crafting recipe
* @param {number} [options.count=1] The amount of the resulting item
*/
constructor(path, options) {
super(path, "shaped");
let key;
for (let k in options.key)
key[k] = utility_1.assumeMinecraft(options.key[k]);
this.file_contents = {
type: "minecraft:crafting_shaped",
pattern: options.pattern,
key,
result: {
item: options.result,
count: options.count || 1
}
};
}
}
exports.ShapedCraftingRecipe = ShapedCraftingRecipe;