This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.ts
202 lines (181 loc) · 5.8 KB
/
form.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { MinecraftPacketIds } from "bdsx/bds/packetids";
import { NetworkIdentifier } from "bdsx/bds/networkidentifier";
import { ServerSettingsResponsePacket, ShowModalFormPacket } from "bdsx/bds/packets";
import { events } from "bdsx/event";
export let FormData = new Map<NetworkIdentifier, {Id:number;func:(data:any)=>void}[]>();
class formJSONTYPE {
type:"form"|"custom_form"|"modal";
title:string;
content:string|any[];
buttons?:{text:string; image?:any}[];
button1?:string;
button2?:string;
}
class formJSON {
type:"form" = "form";
title:string = "";
content:string = "";
buttons:{text:string; image?:any}[] = [];
}
class CustomformJSON {
type:"custom_form" = "custom_form";
title:string = "";
content:any[] = [];
}
class modalJSON {
type:"modal" = "modal";
title:string = "";
content:string = "";
button1?:string = "";
button2?:string = "";
}
class formSetting extends CustomformJSON {
icon?:{type:"url",data:string}
}
class FormFile {
json: formJSON = new formJSON();
handler?: (data: any) => void;
target: NetworkIdentifier;
setTitle(title:string) {
this.json.title = title;
}
setContent(content:string) {
this.json.content = content;
}
addButton(text:string, image?:object) {
return this.json.buttons.push({
text: text,
image: image
});
}
addHandler(handler?:(data:number)=>void){
this.handler = handler;
}
send(){
formSend(this.target, this.json, this.handler);
}
}
class CustomFormFile {
json: CustomformJSON = new CustomformJSON();
handler?: (data: any) => void;
target: NetworkIdentifier;
setTitle(title:string) {
this.json.title = title;
}
addContent(content:object[]) {
this.json.content = content;
}
addHandler(handler?:(data:any)=>void){
this.handler = handler;
}
send(){
formSend(this.target, this.json, this.handler);
}
}
class ModalFile {
json: modalJSON = new modalJSON();
handler?: (data: any) => void;
target: NetworkIdentifier;
setTitle(title:string) {
this.json.title = title;
}
setContent(content:string) {
this.json.content = content;
}
setButton1(button:string) {
this.json.button1 = button;
}
setButton2(button:string) {
this.json.button2 = button;
}
addHandler(handler?:(data:boolean)=>void){
this.handler = handler;
}
send(){
formSend(this.target, this.json, this.handler);
}
}
/**
*JsonType example : https://github.com/NLOGPlugins/Form_Json You can use form.write instead of this
*/
export function formSend(target: NetworkIdentifier, form: formJSONTYPE|object, handler?: (data: any) => void, id?:number) {
try {
const modalPacket = ShowModalFormPacket.create();
let formId = Math.floor(Math.random() * 1147483647) + 1000000000;
if (typeof id === "number") formId = id;
modalPacket.setUint32(formId, 0x30);
modalPacket.setCxxString(JSON.stringify(form), 0x38);
modalPacket.sendTo(target, 0);
if (handler === undefined) handler = ()=>{}
if (!FormData.has(target)) {
FormData.set(target, [
{
Id: formId,
func: handler
}
])
} else {
let f = FormData.get(target)!;
f.push({
Id: formId,
func: handler
})
FormData.set(target, f);
}
modalPacket.dispose();
} catch (err) {}
}
export namespace Form {
export const create = {
form:(target: NetworkIdentifier): FormFile => {
let form = new FormFile();
form.target = target;
return form;
},
custom_form:(target: NetworkIdentifier): CustomFormFile => {
let form = new CustomFormFile();
form.target = target;
return form;
},
modal:(target: NetworkIdentifier): ModalFile => {
let form = new ModalFile();
form.target = target;
return form;
}
}
export const write = formSend;
export function setSettingForm(form: (target:NetworkIdentifier)=>formSetting|object, handler = (data: any, target:NetworkIdentifier, json:formSetting) => {}) {
settingForm = form;
settingHandler = handler;
}
}
let settingForm: ((target:NetworkIdentifier)=>formSetting|object)|undefined;
let settingHandler = (data: any, target:NetworkIdentifier, json:formSetting|object) => {};
events.packetAfter(MinecraftPacketIds.ServerSettingsRequest).on((ptr, target) => {
if (settingForm === undefined) return;
setTimeout(()=>{
const packet = ServerSettingsResponsePacket.create();
packet.setUint32(5928, 0x30);
packet.setCxxString(JSON.stringify(settingForm!(target)), 0x38);
packet.sendTo(target);
packet.dispose();
}, 2000);
});
events.packetRaw(MinecraftPacketIds.ModalFormResponse).on((ptr, size, target) => {
ptr.move(1);
let formId = ptr.readVarUint();
let formData = ptr.readVarString();
let data = JSON.parse(formData.replace("\n",""));
if (formId === 5928) {
let f = {};
if (settingForm !== undefined) f = settingForm(target);
settingHandler(data, target, f);
return;
}
let dataValue = FormData.get(target)!.find((v)=> v.Id === formId)!;
if (dataValue === undefined) return;
dataValue.func(data);
let f = FormData.get(target)!;
f.splice(f.indexOf(dataValue), 1);
FormData.set(target, f);
});