Skip to content

Commit 54a8125

Browse files
committed
test forms api
1 parent e1ebf6a commit 54a8125

File tree

2 files changed

+111
-3
lines changed

2 files changed

+111
-3
lines changed

src/bricklib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * as command from './command.js';
1212
export * as database from './database.js';
1313
export * as events from './events.js';
1414
export * as format from './format.js';
15+
export * as forms from './forms.js';
1516
export * as glyphs from './glyphs.js';
1617
export * as locale from './locale.js';
1718
export * as rawtext from './rawtext.js';

src/index.ts

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import { FormCancelationReason } from '@minecraft/server-ui';
12
import * as bricklib from './bricklib/index.js';
3+
24
const mgr = new bricklib.command.CommandManager();
3-
bricklib.command.enableCustomChatCmds(mgr, '!');
5+
bricklib.command.enableCustomChatCmds(mgr, '\\');
6+
7+
48

59
mgr.registerCommand([ 'hello', 'hi' ], (src, args) => {
610
src.sendMessage(src.name + '\n' + args.join('\n') + '\nEND OF ARGS');
711
return 0;
812
});
913

10-
11-
1214
const def = {
1315
id: 'echo',
1416
name: 'echo',
@@ -24,3 +26,108 @@ mgr.registerCommand(...bricklib.args.makeCommand(def, (args, src) => {
2426
src.sendMessage(args.text.join(' '));
2527
return 0;
2628
}));
29+
30+
31+
32+
33+
const { registerForm, showForm } = bricklib.forms;
34+
const nextTick = bricklib.ticks.setTickTimeout;
35+
36+
nextTick(() => {
37+
38+
39+
mgr.registerCommand([ 'form' ], (src) => {
40+
src.sendMessage('please close chats');
41+
nextTick(() => showForm('action-frm', src));
42+
return 0;
43+
})
44+
45+
46+
47+
registerForm('action-frm', {
48+
title: 'Action Form Title',
49+
body: 'Body Text',
50+
cancel: (ctx, r) => {
51+
if (r == FormCancelationReason.UserBusy)
52+
return nextTick(() => ctx.goto('action-frm'));
53+
ctx.user.sendMessage('canceled action-frm bc: ' + r)
54+
},
55+
buttons: [
56+
{
57+
text: 'btn1 -> msg-frm',
58+
action: (ctx) => ctx.goto('msg-frm'),
59+
/* no icon */
60+
},
61+
{
62+
text: 'btn2 -> modal-frm',
63+
action: (ctx) => ctx.goto('modal-frm'),
64+
icon: 'textures/items/diamond',
65+
},
66+
{
67+
text: 'btn3 -> itself',
68+
action: (ctx) => ctx.goto('action-frm'),
69+
}
70+
],
71+
});
72+
73+
registerForm('msg-frm', {
74+
title: 'Message Form Title',
75+
message: 'The message',
76+
cancel: (ctx, r) => {
77+
if (r == FormCancelationReason.UserBusy)
78+
return nextTick(() => ctx.goto('msg-frm'));
79+
ctx.user.sendMessage('canceled msg-frm bc: ' + r)
80+
},
81+
btn1: {
82+
text: 'btn1 -> action-frm',
83+
action: (ctx) => ctx.goto('action-frm'),
84+
},
85+
btn2: {
86+
text: 'btn2 -> modal-frm',
87+
action: (ctx) => ctx.goto('modal-frm'),
88+
}
89+
});
90+
91+
registerForm('modal-frm', {
92+
title: 'Modal Form Title',
93+
cancel: (ctx, r) => {
94+
if (r == FormCancelationReason.UserBusy)
95+
return nextTick(() => ctx.goto('modal-frm'));
96+
ctx.user.sendMessage('canceled modal-frm bc: ' + r)
97+
},
98+
inputs: [
99+
{
100+
id: 'text-field',
101+
type: 'text',
102+
label: 'Text Label',
103+
default: 'Default Value',
104+
placeholder: 'Placeholder for text',
105+
},
106+
{
107+
id: 'toggle-field',
108+
type: 'toggle',
109+
label: 'Label For Toggle',
110+
default: true,
111+
},
112+
{
113+
id: 'slider-field',
114+
type: 'slider',
115+
label: 'Label For Slider',
116+
default: 50,
117+
min: 10,
118+
max: 90,
119+
step: 2,
120+
},
121+
{
122+
id: 'dropdown-field',
123+
type: 'dropdown',
124+
label: 'Label For Dropdown',
125+
default: 1,
126+
options: ['prev', 'the default', 'another value', 'extra vals'],
127+
}
128+
],
129+
submit: (ctx, result) => ctx.user.sendMessage(JSON.stringify(result)),
130+
});
131+
132+
133+
});

0 commit comments

Comments
 (0)