complete rename and init builder options
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
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));
|
||||
}
|
||||
|
||||
getOption(id) {
|
||||
return this.options.get(id);
|
||||
}
|
||||
|
||||
setOption(id, value) {
|
||||
return this.options.setValue(id, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { BuilderFormBuilder } from "./BuilderFormBuilder";
|
||||
import { Builders } from "./Builders";
|
||||
|
||||
export class BuilderForm {
|
||||
constructor(player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
show() {
|
||||
forceShow(this.player, BuilderFormBuilder.buildSettings(this.player)).then((response) => {
|
||||
if (response.canceled) return;
|
||||
this.applySettings(response.formValues);
|
||||
});
|
||||
}
|
||||
|
||||
applySettings(formValues) {
|
||||
const optionIds = Builders.get(this.player.id).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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ModalFormData } from "@minecraft/server-ui";
|
||||
import { MenuFormBuilder } from "../MenuFormBuilder";
|
||||
import { Builders } from "./Builders";
|
||||
|
||||
export class BuilderFormBuilder {
|
||||
static buildSettings(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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { BuilderOptions } from "./BuilderOptions";
|
||||
import { Option } from "../Option";
|
||||
|
||||
export class BuilderOption extends Option {
|
||||
playerId;
|
||||
identifier;
|
||||
displayName;
|
||||
description;
|
||||
value;
|
||||
#onEnable;
|
||||
#onDisable;
|
||||
#DP_NAMESPACE = "builderOptions";
|
||||
|
||||
constructor({ player, identifier, displayName, description, howToUse, onEnableCallback = () => {}, onDisableCallback = () => {} }) {
|
||||
this.playerId = player.id;
|
||||
this.identifier = identifier;
|
||||
this.displayName = displayName;
|
||||
this.description = description;
|
||||
this.howToUse = howToUse;
|
||||
this.#onEnable = onEnableCallback;
|
||||
this.#onDisable = onDisableCallback;
|
||||
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;
|
||||
}
|
||||
|
||||
setValue(value) {
|
||||
if (value)
|
||||
this.#onEnable();
|
||||
else
|
||||
this.#onDisable();
|
||||
if (this.value !== value) {
|
||||
this.value = value;
|
||||
return value;
|
||||
}
|
||||
return void 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { world } from "@minecraft/server";
|
||||
import { BuilderOption } from "./BuilderOption";
|
||||
|
||||
export class BuilderOptions {
|
||||
playerId = void 0;
|
||||
options = {};
|
||||
|
||||
constructor(playerId) {
|
||||
this.playerId = playerId;
|
||||
this.loadOptions();
|
||||
}
|
||||
|
||||
loadOptions() {
|
||||
const optionDPs = world.getDynamicPropertyIds().filter((id) => id.startsWith(`builderOptions:${this.playerId}:`));
|
||||
for (const optionDP of optionDPs)
|
||||
new BuilderOption(JSON.parse(world.getDynamicProperty(optionDP)));
|
||||
}
|
||||
|
||||
add(builderOption) {
|
||||
this.options[builderOption.identifer] = builderOption;
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.options[id];
|
||||
}
|
||||
|
||||
getValue(id) {
|
||||
return this.options[id].getValue();
|
||||
}
|
||||
|
||||
setValue(id, value) {
|
||||
return this.options[id].setValue(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { world } from "@minecraft/server";
|
||||
import { Builder } from "./Builder";
|
||||
|
||||
export class Builders {
|
||||
static builders = {};
|
||||
|
||||
add(playerId) {
|
||||
this.builders[playerId] = new Builder(playerId);
|
||||
}
|
||||
|
||||
remove(playerId) {
|
||||
delete this.builders[playerId];
|
||||
}
|
||||
|
||||
get(id) {
|
||||
return this.builders[id];
|
||||
}
|
||||
|
||||
onJoin(playerId) {
|
||||
this.add(playerId);
|
||||
}
|
||||
}
|
||||
|
||||
world.afterEvents.playerJoin.subscribe((event) => Builders.onJoin(event.playerId));
|
||||
world.beforeEvents.playerLeave.subscribe((event) => Builders.onLeave(event.player));
|
||||
Reference in New Issue
Block a user