UNPKG

@throw-out-error/minecraft-datapack

Version:

A module for making minecraft datapacks with node to cut down on the repetition.

108 lines (107 loc) 4.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); } var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Datapack = void 0; const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const utility_1 = require("./utility"); const namespace_1 = require("./namespace"); // Exports __exportStar(require("@throw-out-error/minecraft-mcfunction"), exports); __exportStar(require("./loot"), exports); __exportStar(require("./namespace"), exports); __exportStar(require("./predicate"), exports); __exportStar(require("./recipes"), exports); __exportStar(require("./tag"), exports); class Datapack { /** * Creates a datapack * @param {string} name The name of the datapack * @param {string} path The root path of were the datapack will compile to eg. C:\Users\Ree will cause the datapack to compile to C:\Users\Ree\datapack_name * @param {object} options Additional information regarding variable names and the pack.mcmeta file * @param {number} [options.foramt=5] The datapack format version * @param {string} [options.description=name] The datapack's description */ constructor(name, path, options = {}) { /** @type {string} the name of the datapack */ this.name = name; /** @type {string} the root folder the datapack will compile to */ this.path = path; /** @type {number} the format version of the datapack */ this.format = options.format || 5; /** @type {string} the description of the datapack */ this.description = options.description || this.name; /** @type {Namespace} the datapacks minecraft folder */ this.minecraft = new (class Minecraft extends namespace_1.Namespace { constructor() { super("minecraft_namespace"); this.name = "minecraft"; } })(); /** @type {object} the namespaces the datapack will use */ this.namespaces = {}; } get mcmeta() { return { pack: { pack_format: this.format, description: this.description } }; } /** * Output the files of the datapack */ async compile(path) { const root = path_1.default.join(path, this.name); utility_1.mkdirIfNotExist(path_1.default.join(root, "data")); await fs_1.promises.writeFile(path_1.default.join(root, "pack.mcmeta"), JSON.stringify(this.mcmeta, null, 2)); const namespaces = [ this.minecraft, ...Object.values(this.namespaces) ]; await Promise.all(namespaces.map(ns => ns.compile(root))); } /** * Add a namespace to the datapack, minecraft is added by default this.minecraft * @param {Namespace} namespace The namespace to be added * @returns {Namespace} a reference to the added namespace */ addNamespace(namespace) { if (Object.prototype.hasOwnProperty.call(this.namespaces, namespace.name)) throw new Error(`The namespace ${namespace.name} has already been added to this datapack`); let copy = namespace_1.Namespace.copy(namespace); this.namespaces[namespace.name] = copy; return copy; } /** * Creates a namespace and appends it to the datapack * @param {string} name The name of the namespace * @returns {Namespace} a reference to the created namespace */ createNamespace(name) { let namespace = new namespace_1.Namespace(name); this.addNamespace(namespace); return namespace; } /** * Removes the namespace from the datapack * @param {string} name The name of the namespace */ deleteNamespace(name) { delete this.namespaces[name]; } } exports.Datapack = Datapack;