This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFpmApiTemplate.js
288 lines (261 loc) · 9.03 KB
/
FpmApiTemplate.js
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* eslint-disable no-undef */
const PluginIdentifier = "FpmApiTemplate";
/**
* async version of setTimeout
* @param {Number} ms milliseconds to wait
* @returns {Promise<void>}
*/
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
/////////////////////////////////////// FakePlayerManager API START ///////////////////////////////////////
const FPM_API_PREFIX = "FakePlayerManagerAPI";
const FPM_API_DEBUG = false;
/**
* @type {boolean} whether the FakePlayerManager is installed
*/
const FPM_INSTALLED = lxl.require("FakePlayerManager.lxl.js");
if(!FPM_INSTALLED){
// TODO when FakePlayerManager.lxl.js is not found
}
/**
* Import a FakePlayerManager API by name
* @param {string} name the name of the API
* @returns {(...args: any[]) => any} the API
*/
function fpmImport(name){
if(FPM_INSTALLED){
if(FPM_API_DEBUG) logger.info(`Importing FakePlayerManager API ${name}`);
return lxl.import(`${FPM_API_PREFIX}_${name}`);
}else{
return ()=>{
logger.error(`Can't call ${name} because FakePlayerManager is not installed.`);
logger.error(`Please install FakePlayerManager.lxl.js first.`);
logger.error(`You can download it from https://www.minebbs.com/resources/fakeplayermanager-gui.2945/`);
if(FPM_API_DEBUG) throw new Error("FakePlayerManager is not installed.");
};
}
}
/**
* @param {string} identifier the identifier of the API
* @param {string} resolveFuncName the name of the resolve function
* @param {string} rejectFuncName the name of the reject function
* @returns {void}
* @type {(identifier: string, resolveFuncName: string, rejectFuncName: string) => void}
*/
const fpmResiterAsync = fpmImport("registerAsync");
/**
* Import a async FakePlayerManager API by name
* @param {string} name the name of the API, e.g. "list"
* @returns {(...args: any[]) => Promise<any>} the API
*/
function fpmImportAsync(name){
if(!FPM_INSTALLED){
return async ()=>{
logger.error(`Can't call ${name} because FakePlayerManager is not installed.`);
logger.error(`Please install FakePlayerManager.lxl.js first.`);
logger.error(`You can download it from https://www.minebbs.com/resources/fakeplayermanager-gui.2945/`);
if(FPM_API_DEBUG) throw new Error("FakePlayerManager is not installed.");
};
}
const fullName = `${FPM_API_PREFIX}_async_${PluginIdentifier}_${name}`;
let resolveFunc = {};
let rejectFunc = {};
lxl.export((id, ...args)=>{
if(Object.prototype.hasOwnProperty.call(resolveFunc, id)){
resolveFunc[id](...args);
delete resolveFunc[id];
}
}, `${fullName}_resolve`);
lxl.export((id, ...args)=>{
if(Object.prototype.hasOwnProperty.call(rejectFunc, id)){
rejectFunc[id](...args);
delete rejectFunc[id];
}
}, `${fullName}_reject`);
if(FPM_API_DEBUG) logger.info(`fpmResiterAsync ${fullName}`);
setTimeout(() => {
fpmResiterAsync(`${fullName}`,`${fullName}_resolve`,`${fullName}_reject`);
}, 0);
if(FPM_API_DEBUG) logger.info(`Importing FakePlayerManager API async_${name}`);
const syncFunc = fpmImport(`async_${name}`);
return async(...args) => {
if(!FpmApi.inited){
await wait(0);
FpmApi.inited = true;
}
return new Promise((resolve, reject) => {
const id = system.randomGuid();
resolveFunc[id] = resolve;
rejectFunc[id] = reject;
try{
syncFunc(fullName, id, ...args);
}catch(e){
reject(e);
}
});
};
}
/**
* FakePlayerManager API for LiteLoader Script Engine
* @author xiaoqch
* @version 1.2.0
* @license MIT
* @see https://www.minebbs.com/resources/fakeplayermanager-gui.2945/
*/
// eslint-disable-next-line no-unused-vars
class FpmApi{
/**
* @type {boolean} whether the FakePlayerManager is installed
*/
inited = false;
/**
* get the list of all the fake players
* @type {() => Promise<string[]>}
* @example
* FpmApi.list().then(list=>{
* logger.info(`${list.length} players found.`);
* }).catch(err=>{
* logger.error(`${err.name}: ${err.message}`);
* logger.error(err.stack);
* });
* @example
* try{
* const list = await FpmApi.list();
* logger.info(`${list.length} players found.`);
* }catch(err){
* logger.error(`${err.name}: ${err.message}`);
* logger.error(err.stack);
* }
*/
static list = fpmImportAsync("list");
/**
* add a fake player
* @type {(name: string, allowChatControl: boolean?, skin: string?) => Promise<{success: boolean, reason: string}>}
* @example:
* const { success, reason } = await FpmApi.add("Player");
*/
static add = fpmImportAsync("add");
/**
* connect to a fake player
* @type {(name: string) => Promise<{success: boolean, reason: string}>}
* @example
* const { success, reason } = await FpmApi.connect("Player");
*/
static connect = fpmImportAsync("connect");
/**
* disconnect a fake player
* @type {(name: string) => Promise<{name:string, success:boolean, reason:string}>}
*/
static disconnect = fpmImportAsync("disconnect");
/**
* get state of a fake player
* @type {(name: string) => Promise<{name:string, success:boolean, resaon: string, state: string}>}
* @example
* const { success, reason, state } = await FpmApi.getState("Player");
*/
static getState = fpmImportAsync("getState");
/**
* get states of all fake players
* @type {() => Promise<object>}
* @example
* const states = await FpmApi.getStates();
* const playerNames = Object.keys(states);
* for(const name of playerNames){
* const state = states[name].state;
* const allowChatControl = states[name].allowChatControl;
* logger.info(`${name} is ${state}`);
* }
*/
static getAllState = fpmImportAsync("getAllState");
/**
* remove a fake player
* @type {(name: string) => Promise<{success: boolean, reason: string}>}
* @example
* const { success, reason } = await FpmApi.remove("Player");
*/
static remove = fpmImportAsync("remove");
/**
* remove all fake players
* @type {() => Promise<string[]>}
* @example
* const list = await FpmApi.removeAll();
* logger.info(`${list.length} players removed.`);
*/
static removeAll = fpmImportAsync("removeAll");
/**
* connect all fake players
* @type {() => Promise<string[]>}
* @example
* const list = await FpmApi.connectAll();
* logger.info(`${list.length} players connected.`);
*/
static connectAll = fpmImportAsync("connectAll");
/**
* disconnect all fake players
* @type {() => Promise<string[]>}
* @example
* const list = await FpmApi.disconnectAll();
* logger.info(`${list.length} players disconnected.`);
*/
static disconnectAll = fpmImportAsync("disconnectAll");
/**
* set chat control of a fake player
* @type {(name: string, allowChatControl: boolean) => Promise<{success: boolean, reason: string}>}
* @example
* const { success, reason } = await FpmApi.setChatControl("Player", true);
* @example
* const { success, reason } = await FpmApi.setChatControl("Player", false);
*/
static setChatControl = fpmImportAsync("setChatControl");
/**
* reconnect FakePlayerManager WebSocket
* @type {() => Promise<boolean>}
*/
static reconnect = fpmImportAsync("reconnect");
/**
* whether the FakePlayerManager is ready
* @type {() => boolean}
*/
static _isReady = fpmImport("isReady");
/**
* FakePlayerManager version
* @type {() => number[]}
*/
static _version = fpmImport("version");
/**
* whether the FakePlayerManager is ready
* @type {() => boolean}
*/
static get ready(){ return FPM_INSTALLED && FpmApi._isReady(); }
/**
* FakePlayerManager version
* @type {() => number[]}
*/
static get version(){ return FpmApi._version(); }
static get versionString(){ return FpmApi.version.join("."); }
/**
* disable the constructor
* @private
* @constructor
*/
constructor(){
throw new Error("This is a static class.");
}
}
/////////////////////////////////////// FakePlayerManager API END ///////////////////////////////////////
if(FPM_API_DEBUG) {
(async () => {
const list = await FpmApi.list();
logger.info(`${list.length} players found.`);
logger.info(`list: ${list.join(", ")}`);
for (const name of list) {
if (!name.startsWith("test_"))
continue;
const { success, reason } = await FpmApi.connect(name);
if (!success) {
logger.error(`${name} connect failed: ${reason}`);
}
}
})().catch(err => {
logger.error(err.stack);
});
}