@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
204 lines (203 loc) • 7.47 kB
JavaScript
"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 });
const ste_events_1 = require("ste-events");
const StorageUtilities_1 = __importDefault(require("../storage/StorageUtilities"));
const Database_1 = __importDefault(require("./Database"));
const MinecraftUtilities_1 = __importDefault(require("./MinecraftUtilities"));
const Log_1 = __importDefault(require("../core/Log"));
const IProjectItemData_1 = require("../app/IProjectItemData");
const FeatureDefinition_1 = __importDefault(require("./FeatureDefinition"));
class FeatureRuleDefinition {
_file;
_id;
_isLoaded = false;
_loadedWithComments = false;
_data;
_onLoaded = new ste_events_1.EventDispatcher();
get data() {
return this._data;
}
get isLoaded() {
return this._isLoaded;
}
get file() {
return this._file;
}
get onLoaded() {
return this._onLoaded.asEvent();
}
set file(newFile) {
this._file = newFile;
}
get id() {
return this._id;
}
set id(newId) {
this._id = newId;
}
get shortId() {
if (this._id !== undefined) {
if (this._id.startsWith("minecraft:")) {
return this._id.substring(10, this._id.length);
}
return this._id;
}
return undefined;
}
async getFormatVersionIsCurrent() {
const fv = this.getFormatVersion();
if (fv === undefined || fv.length !== 3) {
return false;
}
return await Database_1.default.isRecentVersionFromVersionArray(fv);
}
getFormatVersion() {
if (!this._data || !this._data.format_version) {
return undefined;
}
return MinecraftUtilities_1.default.getVersionArrayFrom(this._data.format_version);
}
setResourcePackFormatVersion(versionStr) {
this._ensureDataInitialized();
if (this._data) {
this._data.format_version = versionStr;
}
}
_ensureDataInitialized() {
if (this._data === undefined) {
this._data = {
format_version: "1.21.0",
};
}
}
static async ensureOnFile(file, loadHandler) {
let fd;
if (file.manager === undefined) {
fd = new FeatureRuleDefinition();
fd.file = file;
file.manager = fd;
}
if (file.manager !== undefined && file.manager instanceof FeatureRuleDefinition) {
fd = file.manager;
if (!fd.isLoaded) {
if (loadHandler) {
fd.onLoaded.subscribe(loadHandler);
}
await fd.load();
}
}
return fd;
}
persist() {
if (this._file === undefined) {
return false;
}
Log_1.default.assert(this._data !== null, "ITDP");
if (!this._data) {
return false;
}
return this._file.setObjectContentIfSemanticallyDifferent(this._data);
}
/**
* Loads the definition from the file.
* @param preserveComments If true, uses comment-preserving JSON parsing for edit/save cycles.
* If false (default), uses efficient standard JSON parsing.
* Can be called again with true to "upgrade" a read-only load to read/write.
*/
async load(preserveComments = false) {
// If already loaded with comments, we have the "best" version - nothing more to do
if (this._isLoaded && this._loadedWithComments) {
return;
}
// If already loaded without comments and caller doesn't need comments, we're done
if (this._isLoaded && !preserveComments) {
return;
}
if (this._file === undefined) {
return;
}
if (!this._file.isContentLoaded) {
await this._file.loadContent();
}
if (this._file.content === null || this._file.content instanceof Uint8Array) {
this._isLoaded = true;
this._loadedWithComments = preserveComments;
this._onLoaded.dispatch(this, this);
return;
}
// Use comment-preserving parser only when needed for editing
this._data = preserveComments
? StorageUtilities_1.default.getJsonObjectWithComments(this._file)
: StorageUtilities_1.default.getJsonObject(this._file);
// Extract the identifier from the feature rule description
const featureRulesData = this._data?.["minecraft:feature_rules"];
if (featureRulesData?.description?.identifier) {
this._id = featureRulesData.description.identifier;
}
this._isLoaded = true;
this._loadedWithComments = preserveComments;
this._onLoaded.dispatch(this, this);
}
/**
* Gets the feature identifier that this feature rule places.
* Feature rules reference their target feature via description.places_feature
*/
getPlacesFeatureId() {
if (!this._data) {
return undefined;
}
const featureRulesData = this._data?.["minecraft:feature_rules"];
if (featureRulesData?.description?.places_feature) {
return featureRulesData.description.places_feature;
}
return undefined;
}
async addChildItems(project, item, index) {
const placesFeatureId = this.getPlacesFeatureId();
if (!placesFeatureId) {
return;
}
let foundMatch = false;
// Use index for O(1) lookup when available
if (index) {
const matchingItems = index.getItemsById(index.featureBehaviorsById, placesFeatureId);
if (matchingItems.length > 0) {
for (const matchItem of matchingItems) {
item.addChildItem(matchItem);
}
foundMatch = true;
}
}
else {
const featureItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.featureBehavior);
// Look for the matching feature item in the project
for (const candItem of featureItems) {
if (!candItem.isContentLoaded) {
await candItem.loadContent();
}
if (candItem.primaryFile) {
const featureDef = await FeatureDefinition_1.default.ensureOnFile(candItem.primaryFile);
if (featureDef) {
const candFeatureId = featureDef.id;
if (candFeatureId === placesFeatureId) {
item.addChildItem(candItem);
foundMatch = true;
break;
}
}
}
}
}
// If no matching feature was found, add as unfulfilled relationship
if (!foundMatch) {
const isVanilla = await Database_1.default.isVanillaToken(placesFeatureId);
item.addUnfulfilledRelationship(placesFeatureId, IProjectItemData_1.ProjectItemType.featureBehavior, isVanilla);
}
}
}
exports.default = FeatureRuleDefinition;