configure for regolith
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import { BuilderOptions } from "./BuilderOptions";
|
||||
|
||||
export class Builder {
|
||||
playerId;
|
||||
materialInstanceName = void 0;
|
||||
|
||||
constructor(playerId) {
|
||||
this.playerId = playerId;
|
||||
}
|
||||
|
||||
isOptionEnabled(optionId) {
|
||||
return BuilderOptions.isEnabled(optionId, this.playerId);
|
||||
}
|
||||
|
||||
setOption(optionId, value) {
|
||||
return BuilderOptions.setValue(optionId, this.playerId, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { BuilderFormBuilder } from "./BuilderFormBuilder";
|
||||
import { BuilderOptions } from "./BuilderOptions";
|
||||
import { forceShow } from '../../utils';
|
||||
|
||||
export class BuilderForm {
|
||||
constructor(player) {
|
||||
this.player = player;
|
||||
this.show();
|
||||
}
|
||||
|
||||
show() {
|
||||
forceShow(this.player, BuilderFormBuilder.buildBuilderOptions(this.player)).then((response) => {
|
||||
if (response.canceled) return;
|
||||
this.applySettings(response.formValues);
|
||||
});
|
||||
}
|
||||
|
||||
applySettings(formValues) {
|
||||
const optionIds = BuilderOptions.getOptionIds();
|
||||
for (let i = 0; i < optionIds.length; i++) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { ModalFormData } from "@minecraft/server-ui";
|
||||
import { MenuFormBuilder } from "../MenuFormBuilder";
|
||||
import { BuilderOptions } from "./BuilderOptions";
|
||||
|
||||
export class BuilderFormBuilder {
|
||||
static buildBuilderOptions(player) {
|
||||
const form = new ModalFormData()
|
||||
.title(MenuFormBuilder.menuTitle);
|
||||
for (const optionId of BuilderOptions.getOptionIds()) {
|
||||
const option = BuilderOptions.get(optionId);
|
||||
form.toggle(`${option.displayName}`, { defaultValue: option.isEnabled(player.id), tooltip: option.description });
|
||||
}
|
||||
form.submitButton('§2Apply');
|
||||
return form;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { BuilderOptions } from "./BuilderOptions";
|
||||
import { world } from "@minecraft/server";
|
||||
|
||||
export class BuilderOption {
|
||||
identifier;
|
||||
displayName;
|
||||
description;
|
||||
howToUse;
|
||||
#onEnable;
|
||||
#onDisable;
|
||||
#DP_NAMESPACE = "builderOptions";
|
||||
|
||||
constructor({ identifier, displayName, description, howToUse, onEnableCallback = () => {}, onDisableCallback = () => {} }) {
|
||||
this.identifier = identifier;
|
||||
this.displayName = displayName;
|
||||
this.description = description;
|
||||
this.howToUse = howToUse;
|
||||
this.#onEnable = onEnableCallback;
|
||||
this.#onDisable = onDisableCallback;
|
||||
BuilderOptions.add(this);
|
||||
}
|
||||
|
||||
isEnabled(playerId) {
|
||||
return world.getDynamicProperty(`${this.#DP_NAMESPACE}:${playerId}:${this.identifier}`) === true;
|
||||
}
|
||||
|
||||
setValue(playerId, value) {
|
||||
if (this.isEnabled(playerId) !== value) {
|
||||
this.save(playerId, value);
|
||||
if (value)
|
||||
this.#onEnable(playerId);
|
||||
else
|
||||
this.#onDisable(playerId);
|
||||
return value;
|
||||
}
|
||||
this.save(playerId, value);
|
||||
return void 0;
|
||||
}
|
||||
|
||||
save(playerId, value) {
|
||||
world.setDynamicProperty(`${this.#DP_NAMESPACE}:${playerId}:${this.identifier}`, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
export class BuilderOptions {
|
||||
static options = {};
|
||||
|
||||
static add(builderOption) {
|
||||
this.options[builderOption.identifier] = builderOption;
|
||||
}
|
||||
|
||||
static get(optionId) {
|
||||
return this.options[optionId];
|
||||
}
|
||||
|
||||
static getOptionIds() {
|
||||
return Object.keys(this.options).sort((a, b) => a - b);
|
||||
}
|
||||
|
||||
static isEnabled(optionId, playerId) {
|
||||
return this.options[optionId].isEnabled(playerId);
|
||||
}
|
||||
|
||||
static setValue(optionId, playerId, value) {
|
||||
return this.options[optionId].setValue(playerId, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { world } from "@minecraft/server";
|
||||
import { Builder } from "./Builder";
|
||||
|
||||
export class Builders {
|
||||
static builders = {};
|
||||
|
||||
static add(playerId) {
|
||||
if (this.builders[playerId])
|
||||
return;
|
||||
this.builders[playerId] = new Builder(playerId);
|
||||
}
|
||||
|
||||
static remove(playerId) {
|
||||
delete this.builders[playerId];
|
||||
}
|
||||
|
||||
static get(id) {
|
||||
return this.builders[id];
|
||||
}
|
||||
|
||||
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) => {
|
||||
if (!event.player)
|
||||
return;
|
||||
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