Now is standalone from Canopy
This commit is contained in:
@@ -1,22 +1,15 @@
|
||||
import { BuilderOptions } from "./BuilderOptions";
|
||||
import { Builders } from "./Builders";
|
||||
|
||||
export class Builder {
|
||||
constructor(playerId) {
|
||||
this.playerId = playerId;
|
||||
this.options = new BuilderOptions(playerId);
|
||||
Builders.add(this);
|
||||
}
|
||||
|
||||
getOptionIds() {
|
||||
return Object.keys(this.options.options).sort((a, b) => a.localeCompare(b));
|
||||
isOptionEnabled(optionId) {
|
||||
return BuilderOptions.isEnabled(optionId, this.playerId);
|
||||
}
|
||||
|
||||
getOption(id) {
|
||||
return this.options.get(id);
|
||||
}
|
||||
|
||||
setOption(id, value) {
|
||||
return this.options.setValue(id, value);
|
||||
setOption(optionId, value) {
|
||||
return BuilderOptions.setValue(optionId, this.playerId, value);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,37 @@
|
||||
import { BuilderFormBuilder } from "./BuilderFormBuilder";
|
||||
import { Builders } from "./Builders";
|
||||
import { BuilderOptions } from "./BuilderOptions";
|
||||
import { forceShow } from '../../utils';
|
||||
|
||||
export class BuilderForm {
|
||||
constructor(player) {
|
||||
this.player = player;
|
||||
this.show();
|
||||
}
|
||||
|
||||
show() {
|
||||
forceShow(this.player, BuilderFormBuilder.buildSettings(this.player)).then((response) => {
|
||||
forceShow(this.player, BuilderFormBuilder.buildBuilderOptions(this.player)).then((response) => {
|
||||
if (response.canceled) return;
|
||||
this.applySettings(response.formValues);
|
||||
});
|
||||
}
|
||||
|
||||
applySettings(formValues) {
|
||||
const optionIds = Builders.get(this.player.id).getOptionIds();
|
||||
const optionIds = BuilderOptions.getOptionIds();
|
||||
for (let i = 0; i < optionIds.length; i++) {
|
||||
const option = optionIds[i];
|
||||
if (option.setValue(formValues[i]) && formValues[i] === true)
|
||||
this.player.sendMessage(`§a${option.displayName} is enabled!§7 ${option.howToUse}`);
|
||||
const option = BuilderOptions.get(optionIds[i]);
|
||||
const changedToValue = option.setValue(this.player.id, formValues[i]);
|
||||
if (changedToValue === true)
|
||||
this.player.sendMessage(`§a${option.displayName} is now enabled!§7 ${option.howToUse}`);
|
||||
else if (changedToValue === false)
|
||||
this.player.sendMessage(`§c${option.displayName} is now disabled.`)
|
||||
}
|
||||
}
|
||||
|
||||
valueChangedToEnabled(hasChanged, newValue) {
|
||||
return hasChanged && newValue === true;
|
||||
}
|
||||
|
||||
valueChangedToDisabled(hasChanged, newValue) {
|
||||
return hasChanged && newValue === false;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,16 @@
|
||||
import { ModalFormData } from "@minecraft/server-ui";
|
||||
import { MenuFormBuilder } from "../MenuFormBuilder";
|
||||
import { Builders } from "./Builders";
|
||||
import { BuilderOptions } from "./BuilderOptions";
|
||||
|
||||
export class BuilderFormBuilder {
|
||||
static buildSettings(player) {
|
||||
static buildBuilderOptions(player) {
|
||||
const form = new ModalFormData()
|
||||
.title(MenuFormBuilder.menuTitle);
|
||||
const builder = Builders.get(player.id);
|
||||
for (const optionId of builder.getOptionIds()) {
|
||||
const option = builder.getOption(optionId);
|
||||
form.toggle(`${option.displayName} - ${option.description}`, option.getValue());
|
||||
for (const optionId of BuilderOptions.getOptionIds()) {
|
||||
const option = BuilderOptions.get(optionId);
|
||||
form.toggle(`${option.displayName} - ${option.description}`, option.isEnabled(player.id));
|
||||
}
|
||||
form.submitButton('§2Apply');
|
||||
return form;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,16 @@
|
||||
import { BuilderOptions } from "./BuilderOptions";
|
||||
import { Option } from "../Option";
|
||||
import { world } from "@minecraft/server";
|
||||
|
||||
export class BuilderOption extends Option {
|
||||
playerId;
|
||||
export class BuilderOption {
|
||||
identifier;
|
||||
displayName;
|
||||
description;
|
||||
value;
|
||||
howToUse;
|
||||
#onEnable;
|
||||
#onDisable;
|
||||
#DP_NAMESPACE = "builderOptions";
|
||||
|
||||
constructor({ player, identifier, displayName, description, howToUse, onEnableCallback = () => {}, onDisableCallback = () => {} }) {
|
||||
this.playerId = player.id;
|
||||
constructor({ identifier, displayName, description, howToUse, onEnableCallback = () => {}, onDisableCallback = () => {} }) {
|
||||
this.identifier = identifier;
|
||||
this.displayName = displayName;
|
||||
this.description = description;
|
||||
@@ -22,32 +20,24 @@ export class BuilderOption extends Option {
|
||||
BuilderOptions.add(this);
|
||||
}
|
||||
|
||||
save() {
|
||||
this.saveToDP(this.#DP_NAMESPACE, `${this.playerId}:${this.identifier}`, this);
|
||||
}
|
||||
|
||||
load() {
|
||||
this.loadFromDP(this.#DP_NAMESPACE, `${this.playerId}:${this.identifier}`);
|
||||
this.value = this.value ?? false;
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.clearDP(this.#DP_NAMESPACE, `${this.playerId}:${this.identifier}`);
|
||||
}
|
||||
|
||||
getValue() {
|
||||
return this.value;
|
||||
isEnabled(playerId) {
|
||||
return world.getDynamicProperty(`${this.#DP_NAMESPACE}:${playerId}:${this.identifier}`) === true;
|
||||
}
|
||||
|
||||
setValue(value) {
|
||||
setValue(playerId, value) {
|
||||
if (value)
|
||||
this.#onEnable();
|
||||
this.#onEnable(playerId);
|
||||
else
|
||||
this.#onDisable();
|
||||
if (this.value !== value) {
|
||||
this.value = value;
|
||||
this.#onDisable(playerId);
|
||||
if (this.isEnabled(playerId) !== value) {
|
||||
this.save(playerId, value);
|
||||
return value;
|
||||
}
|
||||
this.save(playerId, value);
|
||||
return void 0;
|
||||
}
|
||||
|
||||
save(playerId, value) {
|
||||
world.setDynamicProperty(`${this.#DP_NAMESPACE}:${playerId}:${this.identifier}`, value);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,23 @@
|
||||
import { world } from "@minecraft/server";
|
||||
import { BuilderOption } from "./BuilderOption";
|
||||
|
||||
export class BuilderOptions {
|
||||
playerId = void 0;
|
||||
options = {};
|
||||
static options = {};
|
||||
|
||||
constructor(playerId) {
|
||||
this.playerId = playerId;
|
||||
this.loadOptions();
|
||||
static add(builderOption) {
|
||||
this.options[builderOption.identifier] = builderOption;
|
||||
}
|
||||
|
||||
loadOptions() {
|
||||
const optionDPs = world.getDynamicPropertyIds().filter((id) => id.startsWith(`builderOptions:${this.playerId}:`));
|
||||
for (const optionDP of optionDPs)
|
||||
new BuilderOption(JSON.parse(world.getDynamicProperty(optionDP)));
|
||||
static get(optionId) {
|
||||
return this.options[optionId];
|
||||
}
|
||||
|
||||
add(builderOption) {
|
||||
this.options[builderOption.identifer] = builderOption;
|
||||
static getOptionIds() {
|
||||
return Object.keys(this.options).sort((a, b) => a - b);
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.options[id];
|
||||
static isEnabled(optionId, playerId) {
|
||||
return this.options[optionId].isEnabled(playerId);
|
||||
}
|
||||
|
||||
getValue(id) {
|
||||
return this.options[id].getValue();
|
||||
}
|
||||
|
||||
setValue(id, value) {
|
||||
return this.options[id].setValue(value);
|
||||
static setValue(optionId, playerId, value) {
|
||||
return this.options[optionId].setValue(playerId, value);
|
||||
}
|
||||
}
|
||||
@@ -4,22 +4,33 @@ import { Builder } from "./Builder";
|
||||
export class Builders {
|
||||
static builders = {};
|
||||
|
||||
add(playerId) {
|
||||
static add(playerId) {
|
||||
if (this.builders[playerId])
|
||||
return;
|
||||
this.builders[playerId] = new Builder(playerId);
|
||||
}
|
||||
|
||||
remove(playerId) {
|
||||
static remove(playerId) {
|
||||
delete this.builders[playerId];
|
||||
}
|
||||
|
||||
get(id) {
|
||||
static get(id) {
|
||||
return this.builders[id];
|
||||
}
|
||||
|
||||
onJoin(playerId) {
|
||||
static onJoin(playerId) {
|
||||
this.add(playerId);
|
||||
}
|
||||
|
||||
static onLeave(playerId) {
|
||||
this.remove(playerId);
|
||||
}
|
||||
}
|
||||
|
||||
world.afterEvents.playerJoin.subscribe((event) => Builders.onJoin(event.playerId));
|
||||
world.beforeEvents.playerLeave.subscribe((event) => Builders.onLeave(event.player));
|
||||
world.beforeEvents.playerLeave.subscribe((event) => Builders.onLeave(event.player.id));
|
||||
world.afterEvents.worldLoad.subscribe((event) => {
|
||||
for (const player of world.getAllPlayers()) {
|
||||
Builders.onJoin(player.id);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user