@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
263 lines (262 loc) • 8.84 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 Utilities_1 = __importDefault(require("../core/Utilities"));
const ste_events_1 = require("ste-events");
const Log_1 = __importDefault(require("../core/Log"));
const ManagedComponent_1 = require("./ManagedComponent");
const IProjectItemData_1 = require("../app/IProjectItemData");
const MinecraftDefinitions_1 = __importDefault(require("./MinecraftDefinitions"));
class BiomeBehaviorDefinition {
_typeId = "";
_file;
_id;
_isLoaded = false;
_data;
_managed = {};
_onLoaded = new ste_events_1.EventDispatcher();
_onComponentAdded = new ste_events_1.EventDispatcher();
_onComponentRemoved = new ste_events_1.EventDispatcher();
_onComponentChanged = 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();
}
get onComponentAdded() {
return this._onComponentAdded.asEvent();
}
get onComponentRemoved() {
return this._onComponentRemoved.asEvent();
}
get onComponentChanged() {
return this._onComponentChanged.asEvent();
}
constructor(typeId) {
if (typeId) {
this._typeId = typeId;
this._id = typeId;
}
}
get typeId() {
return this._typeId;
}
set typeId(newId) {
this._typeId = newId;
this._id = newId;
}
get id() {
return this._id || this._typeId || "";
}
set id(newId) {
this._id = newId;
this._typeId = newId;
}
_ensureBiomeDataInitialized() {
if (!this._data) {
this._data = {
format_version: "1.16.0",
"minecraft:biome": {
description: {
identifier: this._typeId,
},
components: {},
},
};
}
if (!this._data["minecraft:biome"]) {
this._data["minecraft:biome"] = {
description: {
identifier: this._typeId,
},
components: {},
};
}
if (!this._data["minecraft:biome"].description) {
this._data["minecraft:biome"].description = {
identifier: this._typeId,
};
}
if (!this._data["minecraft:biome"].components) {
this._data["minecraft:biome"].components = {};
}
}
ensureComponent(id, defaultData) {
const comp = this.getComponent(id);
if (comp) {
return comp;
}
return this.addComponent(id, defaultData);
}
getComponent(id) {
if (this._data === undefined) {
return undefined;
}
if (!this._managed[id]) {
if (this._data["minecraft:biome"]?.components) {
const comp = this._data["minecraft:biome"].components[id];
if (comp) {
this._managed[id] = new ManagedComponent_1.ManagedComponent(this._data["minecraft:biome"].components, id, comp);
}
}
}
return this._managed[id];
}
notifyComponentUpdated(id) {
const component = this.getComponent(id);
if (component === undefined) {
Log_1.default.unexpectedUndefined("BDNCU");
}
else {
this._onComponentChanged.dispatch(this, component);
}
}
getAllComponents() {
return this.getComponents();
}
getComponents() {
const componentSet = [];
if (this._data !== undefined && this._data["minecraft:biome"]?.components) {
for (const componentName in this._data["minecraft:biome"].components) {
const component = this.getComponent(componentName);
if (component !== undefined) {
componentSet.push(component);
}
}
}
return componentSet;
}
addComponent(id, componentOrData) {
this._ensureBiomeDataInitialized();
const biomeData = this._data;
const mc = componentOrData instanceof ManagedComponent_1.ManagedComponent
? componentOrData
: new ManagedComponent_1.ManagedComponent(biomeData["minecraft:biome"].components, id, componentOrData);
biomeData["minecraft:biome"].components[id] = mc.getData();
this._managed[id] = mc;
this._onComponentAdded.dispatch(this, mc);
return mc;
}
removeComponent(id) {
if (this._data === undefined || !this._data["minecraft:biome"]?.components) {
return;
}
const newComponents = {};
const newManagedComponents = {};
for (const name in this._data["minecraft:biome"].components) {
if (name !== id) {
if (Utilities_1.default.isUsableAsObjectKey(name)) {
const component = this._data["minecraft:biome"].components[name];
newComponents[name] = component;
}
}
}
for (const name in this._managed) {
if (name !== id) {
newManagedComponents[name] = this._managed[name];
}
}
this._data["minecraft:biome"].components = newComponents;
this._managed = newManagedComponents;
this._onComponentRemoved.dispatch(this, id);
}
static async ensureOnFile(file) {
if (file.manager === undefined) {
const bd = new BiomeBehaviorDefinition();
bd._file = file;
if (!bd.isLoaded) {
await bd.load();
}
file.manager = bd;
}
if (file.manager instanceof BiomeBehaviorDefinition) {
return file.manager;
}
return undefined;
}
async persist() {
if (this._file === undefined) {
return false;
}
Log_1.default.assert(this._data !== null, "BBDP");
if (!this._data) {
return false;
}
return this._file.setObjectContentIfSemanticallyDifferent(this._data);
}
async load() {
if (this._file === undefined) {
return;
}
if (this._isLoaded) {
return;
}
if (!this._file.isContentLoaded) {
await this._file.loadContent();
}
const fileContent = this._file.content;
if (fileContent === null || fileContent instanceof Uint8Array) {
this._isLoaded = true;
this._onLoaded.dispatch(this, this);
return;
}
try {
this._data = JSON.parse(fileContent);
if (this._data && this._data["minecraft:biome"]?.description?.identifier) {
this._typeId = this._data["minecraft:biome"].description.identifier;
this._id = this._typeId;
}
}
catch (e) {
Log_1.default.error("Could not load biome definition: " + e);
}
this._isLoaded = true;
this._onLoaded.dispatch(this, this);
}
async addChildItems(project, item) {
const biomeResourceItems = project.getItemsByType(IProjectItemData_1.ProjectItemType.biomeResource);
for (const candItem of biomeResourceItems) {
const biomeResourceDef = (await MinecraftDefinitions_1.default.get(candItem));
if (biomeResourceDef && biomeResourceDef.id === this.id) {
item.addChildItem(candItem);
}
}
}
static getComponentFromBaseFileName(name) {
let canonName = name;
if (canonName.startsWith("minecraft_")) {
canonName = canonName.substring(10);
}
return canonName;
}
get shortId() {
if (this._typeId && this._typeId.indexOf(":") >= 0) {
return this._typeId.substring(this._typeId.indexOf(":") + 1);
}
return this._typeId;
}
get namespace() {
if (this._typeId && this._typeId.indexOf(":") >= 0) {
return this._typeId.substring(0, this._typeId.indexOf(":"));
}
return undefined;
}
ensureDefinition() {
this._ensureBiomeDataInitialized();
if (this._data && this._data["minecraft:biome"]?.description) {
this._data["minecraft:biome"].description.identifier = this._typeId;
}
}
}
exports.default = BiomeBehaviorDefinition;