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
123 lines (123 loc) · 4.25 KB
/
index.js
File metadata and controls
123 lines (123 loc) · 4.25 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
// Script example for ScriptAPI
// Author: iBlqzed <https://github.com/iBlqzed>
// Project: https://github.com/JaylyDev/ScriptAPI
import { world } from "@minecraft/server";
const names = [];
/**
* Database
*/
export class Database {
/**
* Create a new database!
*/
constructor(name) {
this.data = new Map();
this.name = JSON.stringify(name).slice(1, -1).replaceAll(/"/g, '\\"');
if (names.includes(this.name))
throw new Error(`You can't have 2 of the same databases`);
if (this.name.includes('"'))
throw new TypeError(`Database names can't include "!`);
if (this.name.length > 13 || this.name.length === 0)
throw new Error(`Database names can't be more than 13 characters long, and it can't be nothing!`);
names.push(this.name);
runCommand(`scoreboard objectives add "DB_${this.name}" dummy`);
world.scoreboard.getObjective(`DB_${this.name}`).getParticipants().forEach(e => this.data.set(e.displayName.split("_")[0].replaceAll(/\\"/g, '"'), JSON.parse(e.displayName.split("_").filter((v, i) => i > 0).join("_").replaceAll(/\\"/g, '"'))));
}
/**
* The length of the database
*/
get length() {
return this.data.size;
}
/**
* Set a value from a key
* @param {string} key Key to set
* @param {any} value The value
*/
set(key, value) {
if (key.includes('_'))
throw new TypeError(`Database keys can't include "_"`);
if ((JSON.stringify(value).replaceAll(/"/g, '\\"').length + key.replaceAll(/"/g, '\\"').length + 1) > 32000)
throw new Error(`Database setter to long... somehow`);
if (this.data.has(key))
runCommand(`scoreboard players reset "${key.replaceAll(/"/g, '\\"')}_${JSON.stringify(this.data.get(key)).replaceAll(/"/g, '\\"')}" "DB_${this.name}"`);
runCommand(`scoreboard players set "${key.replaceAll(/"/g, '\\"')}_${JSON.stringify(value).replaceAll(/"/g, '\\"')}" "DB_${this.name}" 0`);
this.data.set(key, value);
}
/**
* Get a value from a key
* @param {string} key Key to get
* @returns {any} The value that was set for the key (or undefined)
*/
get(key) {
if (this.data.has(key))
return this.data.get(key);
return undefined;
}
/**
* Test for whether or not the database has the key
* @param {string} key Key to test for
* @returns {boolean} Whether or not the database has the key
*/
has(key) {
if (!this.data.has(key))
return false;
return true;
}
/**
* Delete a key from the database
* @param {string} key Key to delete from the database
*/
delete(key) {
if (!this.data.has(key))
return;
runCommand(`scoreboard players reset "${key.replaceAll(/"/g, '\\"')}_${JSON.stringify(this.data.get(key)).replaceAll(/"/g, '\\"')}" "DB_${this.name}"`);
this.data.delete(key);
}
/**
* Get an array of all keys in the database
* @returns {string[]} An array of all keys in the database
*/
keys() {
return [...this.data.keys()];
}
/**
* Get an array of all values in the database
* @returns {any[]} An array of all values in the database
*/
values() {
return [...this.data.values()];
}
/**
* Clears all values in the database
*/
clear() {
runCommand(`scoreboard objectives remove "DB_${this.name}"`);
runCommand(`scoreboard objectives add "DB_${this.name}" dummy`);
this.data.clear();
}
/**
* Loop through all keys and values of the database
* @param {(key: string, value: any) => void} callback Code to run per loop
*/
forEach(callback) {
this.data.forEach((v, k) => callback(k, v));
}
*[Symbol.iterator]() {
yield* this.data.entries();
}
}
/**
* Run a command!
* @param {string} cmd Command to run
* @returns {{ error: boolean, data: any }} Whether or not the command errors, and command data
* @example runCommand(`give @a diamond`)
*/
function runCommand(cmd) {
try {
return { error: false, data: world.getDimension('overworld').runCommand(cmd) };
}
catch {
return { error: true, data: undefined };
}
}