forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
165 lines (165 loc) · 6.23 KB
/
index.js
File metadata and controls
165 lines (165 loc) · 6.23 KB
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
// Script example for ScriptAPI
// Author: Jayly <https://github.com/JaylyDev>
// Project: https://github.com/JaylyDev/ScriptAPI
import { world } from '@minecraft/server';
import { b64_to_utf8, utf8_to_b64 } from './encoding';
;
const overworld = world.getDimension('overworld');
export class RESTError extends Error {
}
;
;
globalThis.KEY = 'jayly-restful';
export class Table {
/**
* @internal
*/
toRawtext() {
const rawData = JSON.stringify({ route: this.route, data: this.data });
return utf8_to_b64(rawData);
}
;
constructor(rawtext) {
try {
const { route, data } = JSON.parse(b64_to_utf8(rawtext));
this.route = route;
this.data = data;
}
catch (error) {
const { route, data } = JSON.parse(rawtext);
this.route = route;
this.data = data;
}
}
;
}
;
export class Member {
constructor(key, value) {
this.key = key;
this.value = value;
}
;
}
;
/**
* Request methods available with Rest API
*/
export var RequestMethod;
(function (RequestMethod) {
/**
* POST requests are commonly used to create a new resource that is a
* subordinate of the specified route.
*/
RequestMethod["POST"] = "POST";
/**
* GET requests are commonly used to retrieve information about a resource
* at the specified route.
*/
RequestMethod["GET"] = "GET";
/**
* PUT requests are commonly used to update a single resource that already
* exists in a resource collection.
*/
RequestMethod["PUT"] = "PUT";
/**
* PATCH requests are commonly used to update partial of an already
* existed resource collection.
*/
RequestMethod["PATCH"] = "PATCH";
/**
* POST requests are commonly used to remove an existing resource that is a
* subordinate of the specified route.
*/
RequestMethod["DELETE"] = "DELETE";
})(RequestMethod || (RequestMethod = {}));
;
export class REST {
constructor(id) {
const regex = /^[a-z]+$/;
if (!regex.test(id))
throw new RESTError('Invalid id.');
const objectiveId = id;
this.scoreboard = world.scoreboard.getObjective(objectiveId) ?? world.scoreboard.addObjective(objectiveId, id);
}
;
request(route, options) {
const participant = this.scoreboard.getParticipants().find((value) => new Table(value.displayName).route === route);
// delete route
if (options.method === RequestMethod.DELETE) {
if (!participant)
throw new RESTError('Route not found.');
const parsedOption = options;
if (typeof parsedOption.key !== 'string')
this.scoreboard.removeParticipant(participant);
else {
const table = new Table(participant.displayName);
const memberIndex = table.data.findIndex((value) => value.key === parsedOption.key);
if (memberIndex === -1)
throw new RESTError('Member not found in route ' + route);
// set value
table.data.splice(memberIndex, 1);
const encrypted = table.toRawtext();
// version increment
const version = this.scoreboard.getScore(participant);
overworld.runCommand(`scoreboard players set ${JSON.stringify(encrypted)} ${JSON.stringify(this.scoreboard.id)} ${version + 1}`);
}
;
}
// get member value from route
else if (options.method === RequestMethod.GET) {
const parsedOption = options;
if (!participant)
throw new RESTError('Route not found.');
const { data: members } = new Table(participant.displayName);
const data = members.find((v) => v.key === parsedOption.key);
if (!data)
throw new RESTError('Member not found in route ' + route);
return data.value;
}
// modify member value from route
else if (options.method === RequestMethod.PATCH) {
const parsedOption = options;
if (!participant)
throw new RESTError('Route not found.');
const table = new Table(participant.displayName);
const member = table.data.find((value) => value.key === parsedOption.key);
if (!member)
throw new RESTError('Member not found in route ' + route);
// set value
member.value = parsedOption.value;
const encrypted = table.toRawtext();
// version increment
const version = this.scoreboard.getScore(participant);
overworld.runCommand(`scoreboard players set ${JSON.stringify(encrypted)} ${JSON.stringify(this.scoreboard.id)} ${version + 1}`);
return version + 1;
}
// create a new route
else if (options.method === RequestMethod.POST) {
if (!!participant)
throw new RESTError('Cannot create new route. Route ' + JSON.stringify(route) + ' already exists.');
const rawtext = JSON.stringify({ route, data: [] });
const table = new Table(rawtext.toString());
overworld.runCommand(`scoreboard players set ${JSON.stringify(table.toRawtext())} ${JSON.stringify(this.scoreboard.id)} 1`);
}
// create a new member from route
else if (options.method === RequestMethod.PUT) {
const parsedOption = options;
if (!participant)
throw new RESTError('Route not found.');
const table = new Table(participant.displayName);
if (table.data.findIndex((value) => value.key === parsedOption.key) > -1)
throw new RESTError('Member exist in route ' + route);
// set value
const member = new Member(parsedOption.key, parsedOption.value);
table.data.push(member);
const encrypted = table.toRawtext();
// version increment
overworld.runCommand(`scoreboard players set ${JSON.stringify(encrypted)} ${JSON.stringify(this.scoreboard.id)} 1`);
}
else
throw new RESTError('Request method ' + options.method + ' not acceptable.');
}
;
}
;