@@ -1,6 +1,8 @@
|
||||
import { BuilderOptions } from "./BuilderOptions";
|
||||
import { world } from "@minecraft/server";
|
||||
|
||||
const DP_NAMESPACE = "builderOptions";
|
||||
|
||||
export class BuilderOption {
|
||||
identifier;
|
||||
displayName;
|
||||
@@ -8,7 +10,6 @@ export class BuilderOption {
|
||||
howToUse;
|
||||
#onEnable;
|
||||
#onDisable;
|
||||
#DP_NAMESPACE = "builderOptions";
|
||||
|
||||
constructor({ identifier, displayName, description, howToUse, onEnableCallback = () => {}, onDisableCallback = () => {} }) {
|
||||
this.identifier = identifier;
|
||||
@@ -20,10 +21,18 @@ export class BuilderOption {
|
||||
BuilderOptions.add(this);
|
||||
}
|
||||
|
||||
isEnabled(playerId) {
|
||||
return world.getDynamicProperty(`${this.#DP_NAMESPACE}:${playerId}:${this.identifier}`) === true;
|
||||
dynamicPropertyKey(playerId) {
|
||||
return `${DP_NAMESPACE}:${playerId}:${this.identifier}`;
|
||||
}
|
||||
|
||||
|
||||
isEnabled(playerId) {
|
||||
return world.getDynamicProperty(this.dynamicPropertyKey(playerId)) === true;
|
||||
}
|
||||
|
||||
getValue(playerId) {
|
||||
return this.isEnabled(playerId);
|
||||
}
|
||||
|
||||
setValue(playerId, value) {
|
||||
if (this.isEnabled(playerId) !== value) {
|
||||
this.save(playerId, value);
|
||||
@@ -38,6 +47,29 @@ export class BuilderOption {
|
||||
}
|
||||
|
||||
save(playerId, value) {
|
||||
world.setDynamicProperty(`${this.#DP_NAMESPACE}:${playerId}:${this.identifier}`, value);
|
||||
world.setDynamicProperty(this.dynamicPropertyKey(playerId), value);
|
||||
}
|
||||
}
|
||||
|
||||
addControlTo(form, playerId) {
|
||||
form.toggle(this.displayName, { defaultValue: this.isEnabled(playerId), tooltip: this.description });
|
||||
}
|
||||
|
||||
applyFormValue(playerId, formValue) {
|
||||
const changedToValue = this.setValue(playerId, formValue);
|
||||
if (changedToValue === void 0)
|
||||
return void 0;
|
||||
if (changedToValue)
|
||||
return { rawtext: [
|
||||
{ text: `§a` },
|
||||
this.displayName,
|
||||
{ translate: 'construct.option.enabled' },
|
||||
{ text: `§7 ` },
|
||||
this.howToUse
|
||||
]};
|
||||
return { rawtext: [
|
||||
{ text: `§c` },
|
||||
this.displayName,
|
||||
{ translate: 'construct.option.disabled' }
|
||||
]};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user