UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

274 lines (273 loc) 9.57 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); exports.projectItemNameProvider = exports.projectItemProvider = exports.projectTemplateProvider = exports.allTraitsProvider = exports.contentTypeProvider = exports.PROJECT_TEMPLATES = exports.CONTENT_TYPE_TO_GALLERY = exports.CONTENT_TYPES = exports.ALL_TRAITS = exports.ITEM_TRAITS = exports.BLOCK_TRAITS = exports.ENTITY_TRAITS = void 0; exports.createTraitProvider = createTraitProvider; exports.createGalleryItemProvider = createGalleryItemProvider; const IGalleryItem_1 = require("../IGalleryItem"); // ============================================================================ // TRAIT DEFINITIONS (from ContentWizard) // ============================================================================ // MAINTENANCE: These trait lists are manually maintained and must be updated // when new traits are added to the ContentWizard / ContentIndexManager system. // Cross-reference with ContentWizard trait definitions to keep in sync. // ============================================================================ /** * Entity traits available in the content wizard. */ exports.ENTITY_TRAITS = [ "humanoid", "quadruped", "flying", "aquatic", "hostile", "passive", "neutral", "melee_attacker", "ranged_attacker", "exploder", "tameable", "rideable", "breedable", "undead", "wanders", "teleporter", ]; /** * Block traits available in the content wizard. */ exports.BLOCK_TRAITS = [ "solid", "transparent", "slab", "stairs", "fence", "door", "container", "light_source", "gravity", "redstone_signal", ]; /** * Item traits available in the content wizard. */ exports.ITEM_TRAITS = [ "sword", "pickaxe", "axe", "shovel", "food", "armor_helmet", "armor_chestplate", "armor_leggings", "armor_boots", "throwable", ]; /** * All traits combined for general trait completion. */ exports.ALL_TRAITS = [...exports.ENTITY_TRAITS, ...exports.BLOCK_TRAITS, ...exports.ITEM_TRAITS]; // ============================================================================ // CONTENT TYPE NAMES // ============================================================================ // MAINTENANCE: This list must be updated when new content types are added. // Cross-reference with GalleryItemType enum and CONTENT_TYPE_TO_GALLERY mapping. // ============================================================================ /** * Content types that can be added via the /add command. */ exports.CONTENT_TYPES = [ "entity", "block", "item", "script", "function", "spawn_rule", "loot_table", "trade_table", "recipe", "biome", "feature", "feature_rule", "structure", "animation", "animation_controller", "render_controller", "model", "texture", "particle", "fog", "sound", ]; /** * Map content type names to GalleryItemType for lookup. */ exports.CONTENT_TYPE_TO_GALLERY = { entity: IGalleryItem_1.GalleryItemType.entityType, block: IGalleryItem_1.GalleryItemType.blockType, item: IGalleryItem_1.GalleryItemType.itemType, script: IGalleryItem_1.GalleryItemType.codeSample, spawn_rule: IGalleryItem_1.GalleryItemType.spawnLootRecipes, loot_table: IGalleryItem_1.GalleryItemType.spawnLootRecipes, trade_table: IGalleryItem_1.GalleryItemType.spawnLootRecipes, recipe: IGalleryItem_1.GalleryItemType.spawnLootRecipes, biome: IGalleryItem_1.GalleryItemType.worldGen, feature: IGalleryItem_1.GalleryItemType.worldGen, feature_rule: IGalleryItem_1.GalleryItemType.worldGen, structure: IGalleryItem_1.GalleryItemType.chunk, animation: IGalleryItem_1.GalleryItemType.visuals, animation_controller: IGalleryItem_1.GalleryItemType.visuals, render_controller: IGalleryItem_1.GalleryItemType.visuals, model: IGalleryItem_1.GalleryItemType.visuals, texture: IGalleryItem_1.GalleryItemType.visuals, particle: IGalleryItem_1.GalleryItemType.visuals, fog: IGalleryItem_1.GalleryItemType.visuals, sound: IGalleryItem_1.GalleryItemType.visuals, }; // ============================================================================ // PROJECT TEMPLATE NAMES // ============================================================================ /** * Common project template identifiers. */ exports.PROJECT_TEMPLATES = [ "addonstarter", "tsstarter", "addonfull", "scriptbox", "dlstarter", "editorscriptbox", "editorbasics", ]; // ============================================================================ // AUTOCOMPLETE PROVIDER IMPLEMENTATIONS // ============================================================================ /** * Provides autocomplete for content type names (entity, block, item, etc.) */ const contentTypeProvider = (partial, _context) => { const lower = partial.toLowerCase(); return exports.CONTENT_TYPES.filter((t) => t.startsWith(lower)); }; exports.contentTypeProvider = contentTypeProvider; /** * Provides autocomplete for traits based on the content type being created. * @param contentType The type of content (entity, block, item) */ function createTraitProvider(contentType) { return (partial, _context) => { const lower = partial.toLowerCase(); let traits; switch (contentType?.toLowerCase()) { case "entity": traits = exports.ENTITY_TRAITS; break; case "block": traits = exports.BLOCK_TRAITS; break; case "item": traits = exports.ITEM_TRAITS; break; default: traits = exports.ALL_TRAITS; } return traits.filter((t) => t.startsWith(lower)); }; } /** * Provides autocomplete for all traits (entity, block, and item). */ const allTraitsProvider = (partial, _context) => { const lower = partial.toLowerCase(); return exports.ALL_TRAITS.filter((t) => t.startsWith(lower)); }; exports.allTraitsProvider = allTraitsProvider; /** * Provides autocomplete for project templates. */ const projectTemplateProvider = async (partial, context) => { const lower = partial.toLowerCase(); if (!context.creatorTools) { return exports.PROJECT_TEMPLATES.filter((t) => t.startsWith(lower)); } // Try to get templates from gallery await context.creatorTools.loadGallery(); const projects = context.creatorTools.getGalleryProjectByType(IGalleryItem_1.GalleryItemType.project) || []; const editorProjects = context.creatorTools.getGalleryProjectByType(IGalleryItem_1.GalleryItemType.editorProject) || []; const allProjects = [...projects, ...editorProjects]; if (allProjects.length > 0) { return allProjects.map((p) => p.id).filter((id) => id.toLowerCase().startsWith(lower)); } // Fall back to static list return exports.PROJECT_TEMPLATES.filter((t) => t.startsWith(lower)); }; exports.projectTemplateProvider = projectTemplateProvider; /** * Provides autocomplete for gallery items of a specific type. * @param itemType The gallery item type to filter by */ function createGalleryItemProvider(itemType) { return async (partial, context) => { const lower = partial.toLowerCase(); if (!context.creatorTools) { return []; } await context.creatorTools.loadGallery(); let items; if (itemType !== undefined) { items = context.creatorTools.getGalleryProjectByType(itemType) || []; } else { // Return all gallery items const gallery = context.creatorTools.gallery; items = gallery?.items || []; } return items.map((item) => item.id).filter((id) => id.toLowerCase().startsWith(lower)); }; } /** * Provides autocomplete for project items (files in the current project). */ /** * Paths excluded from remove autocomplete to prevent accidental deletion of * config files, dotfiles, and other non-content project infrastructure. */ const REMOVE_EXCLUDED_PATTERNS = [ /(?:^|\/)\./, // dotfiles and dot-directories (e.g., .vscode/, .env) /(?:^|\/)package\.json$/i, /(?:^|\/)package-lock\.json$/i, /(?:^|\/)tsconfig\.json$/i, /\.config\./i, // e.g., vite.config.ts, jest.config.js /(?:^|\/)\.env/i, // .env, .env.local, etc. ]; function isExcludedFromRemove(path) { return REMOVE_EXCLUDED_PATTERNS.some((pattern) => pattern.test(path)); } const projectItemProvider = (partial, context) => { if (!context.project) { return []; } const lower = partial.toLowerCase(); const items = context.project.items || []; return items .filter((item) => item.projectPath) .map((item) => item.projectPath) .filter((path) => path.toLowerCase().includes(lower) && !isExcludedFromRemove(path)); }; exports.projectItemProvider = projectItemProvider; /** * Provides autocomplete for project item names (display names). */ const projectItemNameProvider = (partial, context) => { if (!context.project) { return []; } const lower = partial.toLowerCase(); const items = context.project.items || []; return items .filter((item) => item.name) .map((item) => item.name) .filter((name) => name.toLowerCase().includes(lower)); }; exports.projectItemNameProvider = projectItemNameProvider;