@block_factory/lib 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/_module/BlockFactory.ts +18 -0
  2. package/_module/sys/Threads.ts +43 -0
  3. package/_module/util/Forms/Form.ts +31 -0
  4. package/{util → _module/util/Forms}/FormAction.ts +15 -26
  5. package/_module/util/Forms/FormMessage.ts +87 -0
  6. package/_module/util/Forms/FormModal.ts +183 -0
  7. package/_module/util/Forms/FormRegistry.ts +43 -0
  8. package/_module/util/RawText.ts +75 -0
  9. package/_module/util/Signal.ts +31 -0
  10. package/_module/util/System.ts +21 -0
  11. package/_module/util/Wrapper/Container.ts +34 -0
  12. package/_module/util/Wrapper/IEntity.ts +34 -0
  13. package/_module/util/Wrapper/IPlayer.ts +34 -0
  14. package/_types/_module/BlockFactory.d.ts +16 -0
  15. package/_types/_module/BlockFactory.d.ts.map +1 -0
  16. package/_types/_module/sys/Threads.d.ts +16 -0
  17. package/_types/_module/sys/Threads.d.ts.map +1 -0
  18. package/_types/_module/util/Command.d.ts +92 -0
  19. package/_types/_module/util/Command.d.ts.map +1 -0
  20. package/_types/_module/util/Forms/Form.d.ts +12 -0
  21. package/_types/_module/util/Forms/Form.d.ts.map +1 -0
  22. package/_types/_module/util/Forms/FormAction.d.ts +73 -0
  23. package/_types/_module/util/Forms/FormAction.d.ts.map +1 -0
  24. package/_types/_module/util/Forms/FormMessage.d.ts +24 -0
  25. package/_types/_module/util/Forms/FormMessage.d.ts.map +1 -0
  26. package/_types/_module/util/Forms/FormModal.d.ts +66 -0
  27. package/_types/_module/util/Forms/FormModal.d.ts.map +1 -0
  28. package/_types/_module/util/Forms/FormRegistry.d.ts +12 -0
  29. package/_types/_module/util/Forms/FormRegistry.d.ts.map +1 -0
  30. package/{util → _types/_module/util}/Math.d.ts +8 -2
  31. package/_types/_module/util/Math.d.ts.map +1 -0
  32. package/_types/_module/util/RawText.d.ts +60 -0
  33. package/_types/_module/util/RawText.d.ts.map +1 -0
  34. package/_types/_module/util/Signal.d.ts +11 -0
  35. package/_types/_module/util/Signal.d.ts.map +1 -0
  36. package/_types/_module/util/System.d.ts +4 -0
  37. package/_types/_module/util/System.d.ts.map +1 -0
  38. package/_types/_module/util/Vector.d.ts +212 -0
  39. package/_types/_module/util/Vector.d.ts.map +1 -0
  40. package/_types/_module/util/Wrapper/Container.d.ts +9 -0
  41. package/_types/_module/util/Wrapper/Container.d.ts.map +1 -0
  42. package/_types/_module/util/Wrapper/IEntity.d.ts +12 -0
  43. package/_types/_module/util/Wrapper/IEntity.d.ts.map +1 -0
  44. package/_types/_module/util/Wrapper/IPlayer.d.ts +12 -0
  45. package/_types/_module/util/Wrapper/IPlayer.d.ts.map +1 -0
  46. package/_types/index.d.ts +3 -0
  47. package/_types/index.d.ts.map +1 -0
  48. package/index.js +303 -215
  49. package/index.ts +2 -21
  50. package/package.json +9 -7
  51. package/tsconfig.types.json +13 -0
  52. package/index.d.ts +0 -20
  53. package/util/Command.d.ts +0 -23
  54. package/util/Form.d.ts +0 -10
  55. package/util/Form.ts +0 -15
  56. package/util/FormAction.d.ts +0 -41
  57. package/util/RawText.d.ts +0 -33
  58. package/util/RawText.ts +0 -76
  59. package/util/Signal.d.ts +0 -9
  60. package/util/Signal.ts +0 -73
  61. package/util/Vector.d.ts +0 -60
  62. package/util/_Form.ts +0 -246
  63. /package/{util → _module/util}/Command.ts +0 -0
  64. /package/{util → _module/util}/Math.ts +0 -0
  65. /package/{util → _module/util}/Vector.ts +0 -0
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "emitDeclarationOnly": true,
5
+ "noEmit": false,
6
+ "outDir": "_types",
7
+ "rootDir": ".",
8
+ "declaration": true,
9
+ "declarationMap": true
10
+ },
11
+ "include": ["**/*.ts"],
12
+ "exclude": ["node_modules", "_types"]
13
+ }
package/index.d.ts DELETED
@@ -1,20 +0,0 @@
1
- import { MathUtils } from "./util/Math";
2
- import { Signal } from "./util/Signal";
3
- import { Vec2, Vec3 } from "./util/Vector";
4
- import { RawText } from "./util/RawText";
5
- import { Command } from "./util/Command";
6
- import { Form } from "./util/Form";
7
- import { ActionFormCtor, IButtonOptions, IActionFormResponse, IActionFormData } from "./util/FormAction";
8
-
9
- export { MathUtils, Signal, Vec2, Vec3, RawText, Command, Form, ActionFormCtor, IButtonOptions, IActionFormResponse, IActionFormData }
10
-
11
- export declare const BlockFactory: {
12
- MathUtils: typeof MathUtils;
13
- Signal: typeof Signal;
14
- Vec2: typeof Vec2;
15
- Vec3: typeof Vec3;
16
- RawText: typeof RawText;
17
- Command: typeof Command;
18
- Form: typeof Form;
19
- IActionFormData: typeof IActionFormData;
20
- };
package/util/Command.d.ts DELETED
@@ -1,23 +0,0 @@
1
- import type {
2
- CustomCommand,
3
- CommandPermissionLevel,
4
- CustomCommandParameter,
5
- CustomCommandOrigin,
6
- CustomCommandResult
7
- } from "@minecraft/server";
8
-
9
- export declare namespace Command {
10
- export abstract class ICustomCommand implements CustomCommand {
11
- public abstract description: string;
12
- public abstract name: string;
13
- public abstract permissionLevel: CommandPermissionLevel;
14
- public cheatsRequired?: boolean;
15
- public mandatoryParameters?: CustomCommandParameter[];
16
- public optionalParameters?: CustomCommandParameter[];
17
- public abstract execute(origin: CustomCommandOrigin, ...args: any[]): CustomCommandResult | undefined;
18
- }
19
-
20
- export const customCommands: ICustomCommand[];
21
-
22
- export function RegisterCustomCommand<T extends { new(): ICustomCommand }>(customCommand: T): void;
23
- }
package/util/Form.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import { Player, RawMessage } from "@minecraft/server";
2
-
3
- export declare abstract class Form {
4
- private static readonly occupiedPlayers: Set<string>;
5
- static returnText: RawMessage | string;
6
-
7
- static isOccupied(player: Player): boolean;
8
-
9
- protected setOccupied(player: Player, occupied: boolean): void;
10
- }
package/util/Form.ts DELETED
@@ -1,15 +0,0 @@
1
- import { Player, RawMessage } from "@minecraft/server";
2
-
3
- export abstract class Form {
4
- private static readonly occupiedPlayers: Set<string> = new Set();
5
- public static returnText: RawMessage | string = "Back";
6
-
7
- public static isOccupied(player: Player): boolean {
8
- return Form.occupiedPlayers.has(player.id);
9
- }
10
-
11
- protected setOccupied(player: Player, occupied: boolean): void {
12
- if (occupied) Form.occupiedPlayers.add(player.id);
13
- else Form.occupiedPlayers.delete(player.id);
14
- }
15
- }
@@ -1,41 +0,0 @@
1
- import { Form } from "@block_factory/lib/util/Form";
2
- import { Player, RawMessage } from "@minecraft/server";
3
- import { ActionFormData, FormCancelationReason } from "@minecraft/server-ui";
4
-
5
- export declare type ActionFormCtor<T extends IActionFormData = IActionFormData> = new () => T;
6
-
7
- export declare interface IButtonOptions {
8
- id?: string | number;
9
- iconPath?: string;
10
- subForm?: ActionFormCtor;
11
- allowReturn?: boolean;
12
- }
13
-
14
- export declare interface IActionFormResponse {
15
- readonly cancelationReason?: FormCancelationReason;
16
- readonly canceled: boolean;
17
- readonly selection?: number;
18
- readonly id?: string | number;
19
- }
20
-
21
- export declare abstract class IActionFormData extends Form {
22
- protected form: ActionFormData;
23
-
24
- protected abstract build(player: Player): void;
25
-
26
- protected abstract onSubmit(player: Player, response: IActionFormResponse): void;
27
-
28
- protected onCancel?(player: Player, response: IActionFormResponse): void;
29
-
30
- title(titleText: RawMessage | string): this;
31
- body(bodyText: RawMessage | string): this;
32
- divider(): this;
33
- header(text: RawMessage | string): this;
34
- label(text: RawMessage | string): this;
35
-
36
- button(text: RawMessage | string, options?: IButtonOptions): this;
37
-
38
- show(player: Player): Promise<IActionFormResponse>;
39
-
40
- protected _showChained(player: Player, returnStack: ActionFormCtor[]): Promise<IActionFormResponse>;
41
- }
package/util/RawText.d.ts DELETED
@@ -1,33 +0,0 @@
1
- import type { RawMessage } from "@minecraft/server";
2
-
3
- export declare namespace RawText {
4
- export const TEXT: (value: string) => RawMessage;
5
- export const TRANSLATE: (key: string, ...params: string[]) => RawMessage;
6
- export const SCORE: (name: string, objective: string) => RawMessage;
7
- export const MESSAGE: (...rawText: RawMessage[]) => RawMessage;
8
- export const FORMAT: {
9
- DarkRed: RawMessage;
10
- Red: RawMessage;
11
- Gold: RawMessage;
12
- Yellow: RawMessage;
13
- Green: RawMessage;
14
- Lime: RawMessage;
15
- Aqua: RawMessage;
16
- Cyan: RawMessage;
17
- DarkBlue: RawMessage;
18
- Blue: RawMessage;
19
- Magenta: RawMessage;
20
- Purple: RawMessage;
21
- White: RawMessage;
22
- Gray: RawMessage;
23
- DarkGray: RawMessage;
24
- Black: RawMessage;
25
- Obfuscated: RawMessage;
26
- Bold: RawMessage;
27
- StrikeThrough: RawMessage;
28
- Italic: RawMessage;
29
- Reset: RawMessage;
30
- NewLine: RawMessage;
31
- };
32
- }
33
- //# sourceMappingURL=RawText.d.ts.map
package/util/RawText.ts DELETED
@@ -1,76 +0,0 @@
1
- import { RawMessage } from '@minecraft/server';
2
-
3
- /**
4
- * Utility helpers for building rawtext messages.
5
- */
6
- export namespace RawText {
7
- /**
8
- * Creates a simple text component.
9
- * @param value - The text value.
10
- * @returns Text component object.
11
- */
12
- export const TEXT = (value: string): RawMessage => ({
13
- text: value
14
- });
15
-
16
- /**
17
- * Creates a translation component with optional 'with' parameters.
18
- * @param key - The translation key.
19
- * @param params - The optional parameters for translation.
20
- * @returns Translation component object.
21
- */
22
- export const TRANSLATE = (key: string, ...params: string[]): RawMessage => ({
23
- translate: key,
24
- with: params.length ? params : undefined
25
- });
26
-
27
- /**
28
- * Creates a score component.
29
- * @param name - The entity's name whose score is being displayed.
30
- * @param objective - The name of the score objective.
31
- * @returns Score component object.
32
- */
33
- export const SCORE = (name: string, objective: string): RawMessage => ({
34
- score: {
35
- name: name,
36
- objective: objective
37
- }
38
- });
39
-
40
- /**
41
- * Wraps various text components into a rawtext structure.
42
- * @param rawText - The raw text components.
43
- * @returns Raw text object.
44
- */
45
- export const MESSAGE = (...rawText: RawMessage[]): RawMessage => ({
46
- rawtext: rawText
47
- });
48
-
49
- /**
50
- * Common formatting and color codes.
51
- */
52
- export const FORMAT = {
53
- DarkRed: TEXT('§4'),
54
- Red: TEXT('§c'),
55
- Gold: TEXT('§6'),
56
- Yellow: TEXT('§e'),
57
- Green: TEXT('§2'),
58
- Lime: TEXT('§a'),
59
- Aqua: TEXT('§b'),
60
- Cyan: TEXT('§3'),
61
- DarkBlue: TEXT('§1'),
62
- Blue: TEXT('§9'),
63
- Magenta: TEXT('§d'),
64
- Purple: TEXT('§5'),
65
- White: TEXT('§f'),
66
- Gray: TEXT('§7'),
67
- DarkGray: TEXT('§8'),
68
- Black: TEXT('§0'),
69
- Obfuscated: TEXT('§k'),
70
- Bold: TEXT('§l'),
71
- StrikeThrough: TEXT('§m'),
72
- Italic: TEXT('§o'),
73
- Reset: TEXT('§r'),
74
- NewLine: TEXT('\n')
75
- }
76
- }
package/util/Signal.d.ts DELETED
@@ -1,9 +0,0 @@
1
- export type Callback<T = void> = (data: T) => void;
2
- export declare class Signal<T = void> {
3
- private listeners;
4
- connect(callback: Callback<T>): void;
5
- disconnect(callback?: Callback<T>): boolean;
6
- once(callback: Callback<T>): void;
7
- emit(data: T): void;
8
- }
9
- //# sourceMappingURL=Signal.d.ts.map
package/util/Signal.ts DELETED
@@ -1,73 +0,0 @@
1
- export type Callback<T = void> = (data: T) => void;
2
-
3
- /**
4
- * Signal class for event handling.
5
- * Allows connecting, disconnecting, and emitting events to listeners.
6
- *
7
- * @template T - Type of data passed to listeners.
8
- *
9
- * @example
10
- * ```typescript
11
- * import { Signal } from "./_lib/signal";
12
- *
13
- * interface ExampleEvent {
14
- * n: number;
15
- * s: string;
16
- * b: boolean;
17
- * }
18
- *
19
- * class TestObject {
20
- * example = new Signal<ExampleEvent>();
21
- * }
22
- *
23
- * const tstObject = new TestObject();
24
- * tstObject.example.connect(({n, s, b}) => console.warn(`n: ${n}, s: ${s}, b: ${b}`));
25
- *
26
- * for (let i = 0; i < 4; i++) {
27
- * let n: number = 1;
28
- * let s: string = "example";
29
- * let b: boolean = true;
30
- * tstObject.example.emit({n, s, b});
31
- * if (i === 3) { // Disconnect after 4th emit
32
- * tstObject.example.disconnect();
33
- * tstObject.example.emit({n, s, b});
34
- * }
35
- * }
36
- * ```
37
- */
38
-
39
- export class Signal<T = void> {
40
- private listeners: Set<Callback<T>> = new Set();
41
-
42
- /** Connect a listener */
43
- public connect(callback: Callback<T>): void {
44
- this.listeners.add(callback);
45
- }
46
-
47
- /** Disconnect a listener */
48
- public disconnect(callback?: Callback<T>): boolean {
49
- if (callback) {
50
- this.listeners.delete(callback);
51
- return true;
52
- } else {
53
- this.listeners.clear();
54
- return false;
55
- }
56
- }
57
-
58
- /** Connect a listener that runs only once */
59
- public once(callback: Callback<T>) {
60
- const wrapper: Callback<T> = (data) => {
61
- this.disconnect(wrapper);
62
- callback(data);
63
- };
64
- this.connect(wrapper);
65
- }
66
-
67
- /** Emit signal to all listeners */
68
- public emit(data: T) {
69
- for (const cb of [...this.listeners]) {
70
- cb(data);
71
- }
72
- }
73
- }
package/util/Vector.d.ts DELETED
@@ -1,60 +0,0 @@
1
- export declare class Vec2 {
2
- x: number;
3
- y: number;
4
- static ZERO: Vec2;
5
- static UP: Vec2;
6
- static DOWN: Vec2;
7
- static LEFT: Vec2;
8
- static RIGHT: Vec2;
9
- constructor(x: number, y: number);
10
- static add(a: Vec2, b: Vec2 | number): Vec2;
11
- static subtract(a: Vec2, b: Vec2 | number): Vec2;
12
- static multiply(a: Vec2, b: Vec2 | number): Vec2;
13
- static divide(a: Vec2, b: Vec2 | number): Vec2;
14
- static lerp(a: Vec2, b: Vec2, t: number): Vec2;
15
- static clamp(v: Vec2, min: Vec2, max: Vec2): Vec2;
16
- static dot(a: Vec2, b: Vec2): number;
17
- static magnitude(a: Vec2): number;
18
- static normalize(a: Vec2): Vec2;
19
- static distance(a: Vec2, b: Vec2): number;
20
- static negate(a: Vec2): Vec2;
21
- static equals(a: Vec2, b: Vec2): boolean;
22
- static approxEquals(a: Vec2, b: Vec2, epsilon?: number): boolean;
23
- static angle(a: Vec2, b: Vec2): number;
24
- static perpendicular(a: Vec2): Vec2;
25
- }
26
- export declare class Vec3 {
27
- x: number;
28
- y: number;
29
- z: number;
30
- static ZERO: Vec3;
31
- static UP: Vec3;
32
- static DOWN: Vec3;
33
- static LEFT: Vec3;
34
- static RIGHT: Vec3;
35
- static FORWARD: Vec3;
36
- static BACK: Vec3;
37
- static WEST: Vec3;
38
- static EAST: Vec3;
39
- static NORTH: Vec3;
40
- static SOUTH: Vec3;
41
- constructor(x: number, y: number, z: number);
42
- static add(a: Vec3, b: Vec3 | number): Vec3;
43
- static subtract(a: Vec3, b: Vec3 | number): Vec3;
44
- static multiply(a: Vec3, b: Vec3 | number): Vec3;
45
- static divide(a: Vec3, b: Vec3 | number): Vec3;
46
- static lerp(a: Vec3, b: Vec3, t: number): Vec3;
47
- static clamp(v: Vec3, min: Vec3, max: Vec3): Vec3;
48
- static dot(a: Vec3, b: Vec3): number;
49
- static cross(a: Vec3, b: Vec3): Vec3;
50
- static magnitude(a: Vec3): number;
51
- static normalize(a: Vec3): Vec3;
52
- static distance(a: Vec3, b: Vec3): number;
53
- static negate(a: Vec3): Vec3;
54
- static equals(a: Vec3, b: Vec3): boolean;
55
- static approxEquals(a: Vec3, b: Vec3, epsilon?: number): boolean;
56
- static angle(a: Vec3, b: Vec3): number;
57
- static project(a: Vec3, onto: Vec3): Vec3;
58
- static cartesian(radius: number, theta: number, phi: number): Vec3;
59
- }
60
- //# sourceMappingURL=Vector.d.ts.map
package/util/_Form.ts DELETED
@@ -1,246 +0,0 @@
1
- /*
2
- **************************************************
3
- Copyright (c) Block Factory - All rights reserved.
4
- **************************************************
5
- Author: Donthedev <https://github.com/voxeldon>
6
- **************************************************
7
- */
8
- import { Player, RawMessage } from "@minecraft/server";
9
- import { ActionFormData, ActionFormResponse, ModalFormData, ModalFormDataDropdownOptions, ModalFormDataSliderOptions, ModalFormDataTextFieldOptions, ModalFormDataToggleOptions, ModalFormResponse } from "@minecraft/server-ui";
10
-
11
- const occupiedPlayers: Set<string> = new Set();
12
-
13
- class Form {
14
- public readonly isOccupied = ((player: Player): boolean => {
15
- return occupiedPlayers.has(player.id);
16
- })
17
- }
18
-
19
- export interface Button {
20
- indexId: string | number;
21
- text: string | RawMessage;
22
- iconPath?: string;
23
- }
24
-
25
- export interface FormActionData {
26
- response: ActionFormResponse;
27
- indexId?: string | number;
28
- }
29
-
30
- export class ActionForm extends Form {
31
- private readonly actionForm: ActionFormData = new ActionFormData();
32
- private buttonMap: Map<number, string | number> = new Map<number, string | number>();
33
- private buttons: Button[] = [];
34
- private titleText: string | RawMessage | undefined;
35
- private bodyText: string | RawMessage | undefined;
36
-
37
- public title(title: string | RawMessage): void {
38
- this.titleText = title;
39
- }
40
-
41
- public body(body: string | RawMessage): void {
42
- this.bodyText = body;
43
- }
44
-
45
- public button(indexId: string, text: string | RawMessage, iconPath?: string): void {
46
- this.buttons.push({ indexId, text, iconPath });
47
- }
48
-
49
- public async showForm(player: Player): Promise<FormActionData> {
50
- if (this.titleText === undefined || this.bodyText === undefined) {
51
- throw Error('Title and body must be set before showing the form');
52
- }
53
- this.actionForm.title(this.titleText);
54
- this.actionForm.body(this.bodyText);
55
- this.generateButtons();
56
-
57
- occupiedPlayers.add(player.id);
58
-
59
- const response: ActionFormResponse = await this.actionForm.show(player).finally(() => {
60
- occupiedPlayers.delete(player.id);
61
- });
62
-
63
- const return_data: FormActionData = {
64
- response: response,
65
- indexId: undefined
66
- };
67
- if (response.selection !== undefined && response.selection !== null) {
68
- const selection: string | number | undefined = this.buttonMap.get(response.selection);
69
- return_data.indexId = selection;
70
- }
71
-
72
- return return_data;
73
- }
74
-
75
- private generateButtons() {
76
- let buttonIndex: number = 0;
77
- this.buttons.forEach(button => {
78
- if (button.iconPath) {
79
- this.actionForm.button(button.text, button.iconPath);
80
- } else {
81
- this.actionForm.button(button.text);
82
- }
83
- this.buttonMap.set(buttonIndex, button.indexId);
84
- buttonIndex++;
85
- });
86
- }
87
- }
88
-
89
- enum WidgetType {
90
- TextField = 'textField',
91
- Dropdown = 'dropdown',
92
- Slider = 'slider',
93
- Toggle = 'toggle'
94
- }
95
-
96
- interface Widget {
97
- typeId: string,
98
- widget: any
99
- };
100
-
101
- export interface TextField {
102
- indexId: string,
103
- label: string | RawMessage,
104
- placeholder: string | RawMessage,
105
- defaultValue?: string
106
- }
107
-
108
- export interface Dropdown {
109
- indexId: string,
110
- label: string | RawMessage,
111
- options: string[],
112
- defaultValueIndex?: number
113
- }
114
-
115
- export interface Slider {
116
- indexId: string,
117
- label: string | RawMessage,
118
- min: number,
119
- max: number,
120
- step: number,
121
- defaultValue?: number
122
- }
123
-
124
- export interface Toggle {
125
- indexId: string,
126
- label: string | RawMessage,
127
- defaultValue?: boolean
128
- }
129
-
130
- export type FormValue = string | boolean | number | undefined;
131
-
132
- export interface FormModalData {
133
- response: ModalFormResponse;
134
- indexMap?: Map<string, FormValue>;
135
- }
136
-
137
- export class ModalForm extends Form {
138
- private readonly modalForm: ModalFormData = new ModalFormData();
139
- private widgitMap: Map<number, string> = new Map<number, string>();
140
- private indexMap: Map<string, FormValue> = new Map<string, FormValue>();
141
- private widgets: Widget[] = [];
142
-
143
- public title(title: string | RawMessage): void {
144
- this.modalForm.title(title);
145
- }
146
-
147
- public label(label: string | RawMessage): void {
148
- this.modalForm.label(label);
149
- }
150
-
151
- public header(header: string | RawMessage): void {
152
- this.modalForm.header(header);
153
- }
154
-
155
- public divider(): void {
156
- this.modalForm.divider();
157
- }
158
-
159
- public textField(indexId: string, label: string | RawMessage, placeholder: string | RawMessage, defaultValue?: string): void {
160
- this.widgets.push({ typeId: WidgetType.TextField, widget: { indexId, label, placeholder, defaultValue } });
161
- }
162
- public dropdown(indexId: string, label: string | RawMessage, options: string[], defaultValueIndex?: number): void {
163
- this.widgets.push({ typeId: WidgetType.Dropdown, widget: { indexId, label, options, defaultValueIndex } });
164
- }
165
-
166
- public slider(indexId: string, label: string | RawMessage, min: number, max: number, step: number, defaultValue?: number): void {
167
- this.widgets.push({ typeId: WidgetType.Slider, widget: { indexId, label, min, max, step, defaultValue } });
168
- }
169
-
170
- public toggle(indexId: string, label: string | RawMessage, defaultValue?: boolean): void {
171
- this.widgets.push({ typeId: WidgetType.Toggle, widget: { indexId, label, defaultValue } });
172
- }
173
-
174
- public async showForm(player: Player): Promise<FormModalData> {
175
- this.processWidgets();
176
- occupiedPlayers.add(player.id);
177
-
178
- const response: ModalFormResponse = await this.modalForm.show(player).finally(() => {
179
- occupiedPlayers.delete(player.id);
180
- });
181
-
182
- const return_data: FormModalData = {
183
- response: response,
184
- indexMap: undefined
185
- }
186
-
187
- if (response.formValues !== null && response.formValues !== undefined) {
188
- this.processValues(response.formValues);
189
- return_data.indexMap = this.indexMap;
190
- }
191
-
192
- return return_data;
193
- }
194
-
195
- private processWidgets(): void {
196
- let widgitIndex: number = 0;
197
-
198
- for (const widget of this.widgets) {
199
- if (widget.typeId === WidgetType.TextField) {
200
- const textField: TextField = widget.widget;
201
- const options: ModalFormDataTextFieldOptions = {
202
- defaultValue: textField?.defaultValue
203
- };
204
- this.modalForm.textField(textField.label, textField.placeholder, options);
205
- this.widgitMap.set(widgitIndex, textField.indexId);
206
- }
207
- else if (widget.typeId === WidgetType.Dropdown) {
208
- const dropdown: Dropdown = widget.widget;
209
- const options: ModalFormDataDropdownOptions = {
210
- defaultValueIndex: dropdown?.defaultValueIndex
211
- };
212
- this.modalForm.dropdown(dropdown.label, dropdown.options, options);
213
- this.widgitMap.set(widgitIndex, dropdown.indexId);
214
-
215
- }
216
- else if (widget.typeId === WidgetType.Slider) {
217
- const slider: Slider = widget.widget;
218
- const options: ModalFormDataSliderOptions = {
219
- defaultValue: slider?.defaultValue,
220
- valueStep: slider.step
221
- };
222
- this.modalForm.slider(slider.label, slider.min, slider.max, options);
223
- this.widgitMap.set(widgitIndex, slider.indexId);
224
- }
225
- else if (widget.typeId === WidgetType.Toggle) {
226
- const toggle: Toggle = widget.widget;
227
- const options: ModalFormDataToggleOptions = {
228
- defaultValue: toggle?.defaultValue
229
- }
230
- this.modalForm.toggle(toggle.label, options);
231
- this.widgitMap.set(widgitIndex, toggle.indexId);
232
- }
233
- widgitIndex += 1;
234
- }
235
- }
236
-
237
- private processValues(formValues: FormValue[]): void {
238
- let widgitIndex: number = 0;
239
- for (const i of formValues) {
240
- const value: FormValue = i?.valueOf();
241
- const indexId: string | undefined = this.widgitMap.get(widgitIndex);
242
- if (indexId) this.indexMap.set(indexId, value);
243
- widgitIndex++;
244
- }
245
- }
246
- }
File without changes
File without changes
File without changes