-
Notifications
You must be signed in to change notification settings - Fork 0
/
warplist.ts
248 lines (212 loc) · 10.5 KB
/
warplist.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
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
import { bedrockServer, command, DimensionId } from 'bdsx';
import { FormButton, SimpleForm } from 'bdsx/bds/form';
import { CxxString } from 'bdsx/nativetype';
import fs = require('fs');
import { connectionList } from './playerlist'; /* found @ https://github.com/randommouse/bdsx-scripts/blob/bdsx2/playerlist.ts */
import { tdTeleport } from './tdtp'; /* found @ https://github.com/randommouse/bdsx-scripts/blob/bdsx2/tdtp.ts */
let warpListGUI: boolean = true; //if 'true', uses a form-based GUI for '/warp list' command response
let dbFile = "warplist.json"; //database file location
let warpDB: any = []
let system = server.registerSystem(0,0);
let homename: string = '§5HOME';
// Load Database File on Server Start
fs.readFile(dbFile, (err, data: any) =>{
console.log('[WARP LIST] Database ' + dbFile + ' LOADED');
if (data){
warpDB = JSON.parse(data);
let _filedata = JSON.stringify(warpDB, null, 2);
// console.log(_filedata);
}
});
// Save Database File on Server Shutdown
system.shutdown = function(){
saveToFile();
}
bedrockServer.close.on(() => {
saveToFile();
});
// Register Commands
command.register('warp', 'Set, Delete, List, or Teleport to Warp Points.').overload((param, origin, output)=>{
let playerName = origin.getName();
// /warp list
if (param.warpCmd == 'list'){
if (warpListGUI == true) {
warpListForm(playerName);
} else {
warpList(playerName);
}
}
// /warp set "warpName" (use "" if name includes spaces)
if (param.warpCmd == 'set' && param.warpName !== undefined){ warpSet(playerName, param.warpName) };
// /warp del "warpName" (use "" if name includes spaces)
if (param.warpCmd == 'del' && param.warpName !== undefined){ warpDel(playerName, param.warpName) };
// /warp to "warpName" (use "" if name includes spaces)
if (param.warpCmd == 'to' && param.warpName !== undefined){ warpTo(playerName, param.warpName) };
}, {warpCmd: CxxString, warpName: [CxxString, true]});
command.register('warp set', '§eSet§7 a Warp Point.');
command.register('warp del', '§eDelete§7 a Warp Point.');
command.register('warp to', '§eTeleport§7 to a Warp Point.');
command.register('warp list', '§eList§7 your Warp Points.');
// /sethome
command.register('sethome', `§eSet§7 your ${homename}§r§o§7 Warp Point.`).overload((param, origin, output)=>{
warpSet(origin.getName(), homename);
},{});
// /home
command.register('home', `§eTeleport§7 to your ${homename}§r§o§7 Warp Point.`).overload((param, origin, output)=>{
warpTo(origin.getName(), homename);
},{});;
// Functions
function tellRaw(playerName: string, text: string){
system.executeCommand(`/tellraw ${playerName} {"rawtext":[{"text":"${text}"}]}`, () => {});
}
function saveToFile(dbObject: object = warpDB, file: string = dbFile){
let filedata = JSON.stringify(dbObject, null, 2);
fs.writeFile(file, filedata, () => {
console.log('[WARP LIST] Database ' + dbFile + ' SAVED');
});
}
function warpSet(playerName: string, warpName: string){
let originActor = connectionList.nXNet.get(playerName).getActor();
let originEntity = connectionList.n2Ent.get(playerName);
let originPosition = system.getComponent(originEntity, "minecraft:position");
let originXuid = connectionList.nXXid.get(playerName);
let dimId = originActor.getDimension();
let xPos = originPosition!.data.x;
let yPos = originPosition!.data.y;
let zPos = originPosition!.data.z;
let dbObject = warpDB.find((obj: { xuid: string; }) => obj.xuid == originXuid);
let warpEntry = new WarpDBEntry(warpName, dimId, xPos, yPos, zPos);
if (warpName != undefined && warpName != '' && warpName != null ) {
tellRaw(playerName, '§e§l[WARP LIST]');
if (dbObject != undefined){
let dbIndex = warpDB.indexOf(dbObject);
let warpObject = dbObject.warp.find((obj: { name: string; }) => obj.name == warpName);
if (warpObject != undefined){
tellRaw(playerName, `§eExisting §3§o${warpObject.name}§r§e\n [§f${DimensionId[warpObject.dimId]} §e@ §4${warpObject.x.toFixed(1)} §a${warpObject.y.toFixed(1)} §9${warpObject.z.toFixed(1)}§e]`);
tellRaw(playerName, `§cOverwriting §3§o${warpName}§r§e\n [§f${DimensionId[dimId]} §e@ §4${xPos.toFixed(1)} §a${yPos.toFixed(1)} §9${zPos.toFixed(1)}§e]`);
let warpIndex = warpDB[dbIndex].warp.indexOf(warpObject);
warpDB[dbIndex].warp[warpIndex] = warpEntry;
} else {
tellRaw(playerName, `§eSet §3§o${warpName}§r§e\n [§f${DimensionId[dimId]} §e@ §4${xPos.toFixed(1)} §a${yPos.toFixed(1)} §9${zPos.toFixed(1)}§e]`);
if (warpName == homename){
warpDB[dbIndex].warp.unshift(warpEntry);
} else {
warpDB[dbIndex].warp.push(warpEntry);
}
}
} else {
tellRaw(playerName, `§eSet §3§o${warpName}§r§e\n [§f${DimensionId[dimId]} §e@ §4${xPos.toFixed(1)} §a${yPos.toFixed(1)} §9${zPos.toFixed(1)}§e]`);
warpDB.push(new PlayerDBEntry(originXuid, playerName, warpEntry));
}
tellRaw(playerName, '§e§l* * * * * * *');
// Save warpDB to dbFile
saveToFile();
}
}
function warpDel(playerName: string, warpName: string){
let originXuid = connectionList.nXXid.get(playerName);
let dbObject = warpDB.find((obj: { xuid: string; }) => obj.xuid == originXuid);
if (warpName != undefined && warpName != '' && warpName != null ) {
tellRaw(playerName, '§e§l[WARP LIST]');
if (dbObject != undefined){
let warpObject = dbObject.warp.find((obj: { name: string; }) => obj.name == warpName);
let dbIndex = warpDB.indexOf(dbObject);
if (warpObject != undefined){
let warpIndex: number = warpDB[dbIndex].warp.indexOf(warpObject);
warpDB[dbIndex].warp.splice(warpIndex, 1);
tellRaw(playerName, `§eDeleted §3§o${warpObject.name}§r§e\n [§f${DimensionId[warpObject.dimId]} §e@ §4${warpObject.x.toFixed(1)} §a${warpObject.y.toFixed(1)} §9${warpObject.z.toFixed(1)}§e]`);
} else {
tellRaw(playerName, `§eNo warp called: §3§o${warpName}`);
}
} else {
tellRaw(playerName, '§c0 §gWarp points set');
}
tellRaw(playerName, '§e§l* * * * * * *');
// Save warpDB to dbFile
saveToFile();
}
}
function warpTo(playerName: string, warpName: string){
let originXuid = connectionList.nXXid.get(playerName);
let dbObject = warpDB.find((obj: { xuid: string; }) => obj.xuid == originXuid);
if (warpName != undefined && warpName != '' && warpName != null ) {
tellRaw(playerName, '§e§l[WARP LIST]');
if (dbObject != undefined){
let warpObject = dbObject.warp.find((obj: { name: string; }) => obj.name == warpName);
if (warpObject != undefined){
tdTeleport(playerName, warpObject.dimId, warpObject.x, warpObject.y, warpObject.z);
tellRaw(playerName, `§eWarped to §3§o${warpObject.name}§r§e\n [§f${DimensionId[warpObject.dimId]} §e@ §4${warpObject.x.toFixed(1)} §a${warpObject.y.toFixed(1)} §9${warpObject.z.toFixed(1)}§e]`);
} else {
tellRaw(playerName, `§eNo warp called: §3§o${warpName}`);
}
} else {
tellRaw(playerName, '§c0 §gWarp points set');
}
tellRaw(playerName, '§e§l* * * * * * *');
}
}
function warpList(playerName: string){
let originXuid = connectionList.nXXid.get(playerName);
let dbObject = warpDB.find((obj: { xuid: string; }) => obj.xuid == originXuid);
tellRaw(playerName, '§e§l[WARP LIST]');
if (dbObject != undefined){
if (dbObject.warp.length > 0){
for (let i = 0; i < dbObject.warp.length; i++) {
tellRaw(playerName , `§e[${i + 1}] §3§o${dbObject.warp[i].name}§r§e\n [§f${DimensionId[dbObject.warp[i].dimId]} §e@ §4${dbObject.warp[i].x.toFixed(1)} §a${dbObject.warp[i].y.toFixed(1)} §9${dbObject.warp[i].z.toFixed(1)}§e]`);
}
} else {
tellRaw(playerName, '§c0 §gWarp points set');
}
} else {
tellRaw(playerName, '§c0 §gWarp points set');
}
tellRaw(playerName, '§e§l* * * * * * *');
}
function warpListForm(playerName: string) {
let playerXuid = connectionList.nXXid.get(playerName);
let playerNetID = connectionList.nXNet.get(playerName);
let dbObject = warpDB.find((obj: { xuid: string; }) => obj.xuid == playerXuid);
let warpListForm = new SimpleForm('§0§l[WARP LIST]')
if (dbObject != undefined) {
if (dbObject.warp.length >= 0) {
for (let i = 0; i < dbObject.warp.length; i++) {
warpListForm.addButton(new FormButton(`§1§o${dbObject.warp[i].name}§r§8\n[§0${DimensionId[dbObject.warp[i].dimId]} §8@ §4${dbObject.warp[i].x.toFixed(1)} §2${dbObject.warp[i].y.toFixed(1)} §9${dbObject.warp[i].z.toFixed(1)}§8]`));
}
warpListForm.sendTo(playerNetID, (data, playerNetID) => {
if (data.response !== undefined && data.response !== null){
warpTo(playerName, dbObject.warp[data.response].name);
}
})
} else {
tellRaw(playerName, '§e§l[WARP LIST]');
tellRaw(playerName, '§c0 §gWarp points set');
tellRaw(playerName, '§e§l* * * * * * *');
}
} else {
tellRaw(playerName, '§e§l[WARP LIST]');
tellRaw(playerName, '§c0 §gWarp points set');
tellRaw(playerName, '§e§l* * * * * * *');
}
}
// Database Entry Classes
class PlayerDBEntry {
xuid: string;
name: string;
warp: WarpDBEntry;
constructor(xuid: string, name: string, warp?: WarpDBEntry ){
this.xuid = xuid;
this.name = name;
this.warp = [];
if (warp != undefined){this.warp.push(warp)}
}
}
class WarpDBEntry {
[key: string]: any
constructor(warpName: string, dimensionId: number, xPos: number, yPos: number, zPos: number){
this.name = warpName;
this.dimId = dimensionId;
this.x = xPos;
this.y = yPos;
this.z = zPos;
}
}