UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

125 lines (124 loc) 3.9 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getModelTemplateAsync = getModelTemplateAsync; exports.getModelTemplate = getModelTemplate; exports.getAvailableTemplateTypes = getAvailableTemplateTypes; exports.getAvailableTemplateTypesAsync = getAvailableTemplateTypesAsync; exports.isValidTemplateType = isValidTemplateType; exports.preloadAllTemplates = preloadAllTemplates; const Database_1 = __importDefault(require("./Database")); /** * List of all available template types for iteration and validation. */ const ALL_TEMPLATE_TYPES = [ "humanoid", "small_animal", "large_animal", "vehicle", "block", "item", "bird", "insect", "flying", "fish", "slime", "wizard", "golem", "fox", "crystal", "enchanted_sword", "tropical_fish", "ghost", "robot", "mushroom_creature", "treasure_chest", "stone_brick", "wooden_crate", "glowing_ore", "mossy_stone", "crystal_block", "tech_block", "potion_bottle", "magic_wand", "ornate_key", "gemstone", "apple", "pickaxe", ]; /** * Runtime cache for templates loaded from JSON files. * Populated lazily as templates are requested via getModelTemplateAsync(). * @internal */ const templateCache = {}; /** * Get a model template by type (async, loads from JSON file) * This is the preferred method for loading templates. * Templates are cached after first load. * * @param templateType The type of template to retrieve * @returns Promise resolving to the template design, or undefined if not found */ async function getModelTemplateAsync(templateType) { // Check cache first if (templateCache[templateType]) { return templateCache[templateType]; } // Load from JSON file const loaded = await Database_1.default.ensureModelTemplateLoaded(templateType); if (loaded) { // Cache for future access templateCache[templateType] = loaded; return loaded; } return undefined; } /** * Get a model template by type (synchronous, uses cached templates only) * NOTE: This only returns templates that have already been loaded via getModelTemplateAsync(). * For reliable access, use getModelTemplateAsync() instead. * * @deprecated Prefer getModelTemplateAsync() for reliable template access * @param templateType The type of template to retrieve * @returns The template design if cached, or undefined if not loaded yet */ function getModelTemplate(templateType) { return templateCache[templateType]; } /** * Get all available template types (synchronous) * @returns Array of available template type names */ function getAvailableTemplateTypes() { return [...ALL_TEMPLATE_TYPES]; } /** * Get all available template types from JSON files (async) * This queries the actual JSON files available in the data folder. * @returns Promise resolving to array of available template type names */ async function getAvailableTemplateTypesAsync() { return await Database_1.default.getModelTemplateNames(); } /** * Check if a template type is valid * @param templateType The template type to check * @returns True if the template type is valid */ function isValidTemplateType(templateType) { return ALL_TEMPLATE_TYPES.includes(templateType); } /** * Preload all templates into cache (async) * Call this once at startup if you need synchronous access to all templates later. * @returns Promise resolving when all templates are loaded */ async function preloadAllTemplates() { const loadPromises = ALL_TEMPLATE_TYPES.map((type) => getModelTemplateAsync(type)); await Promise.all(loadPromises); }