forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
125 lines (110 loc) · 3.69 KB
/
index.ts
File metadata and controls
125 lines (110 loc) · 3.69 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
// Script example for ScriptAPI
// Author: iBlqzed <https://github.com/iBlqzed>
// Project: https://github.com/JaylyDev/ScriptAPI
import { world, system } from "@minecraft/server";
export class Database<V = any> {
static readonly databases = new Array<Database<any>>()
private cache = Database.getAll(this.name, this.defaultValue)
public constructor(readonly name: string, private readonly defaultValue: string = "{}") {
Database.databases.push(this)
}
/**
* Set a value from a key
* @remarks Doesn't save instantly, call .save() or wait 1 minute to save automatically
* @param {string} property Key to set
* @param {V} value The value
*/
public set(property: string, value: V): void {
this.cache[property] = value
}
/**
* Get a value from a key
* @param {string} property Key to get
* @returns {V} The value that was set for the key (or undefined)
*/
public get(property: string): V {
return this.cache[property]
}
/**
* Test for whether or not the database has the key
* @param {string} property Key to test for
* @returns {boolean} Whether or not the database has the key
*/
public has(property: string): boolean {
return (property in this.cache)
}
/**
* Delete a key from the database
* @remarks Doesn't save instantly, call .save() or wait 1 minute to save automatically
* @param {string} property Key to delete from the database
* @returns {boolean} Whether the database had the key to begin with
*/
public delete(property: string): boolean {
return delete this.cache[property]
}
/**
* Get an array of all keys in the database
* @returns {string[]} An array of all keys in the database
*/
public keys(): string[] {
return Object.keys(this.cache);
}
/**
* Get an array of all values in the database
* @returns {V[]} An array of all values in the database
*/
public values(): V[] {
return Object.values(this.cache)
}
/**
* Clears all values in the database
* @remarks Saves instantly
*/
public clear() {
this.cache = {}
this.save()
}
/**
* Get an object with all keys and values
* @remarks All changes will save
* @returns {Record<string, V>} An object of all keys and values
*/
public getAll(): Record<string, V> {
return this.cache ??= Database.getAll(this.name, this.defaultValue)
}
/**
* Save the database instantly
*/
public save(): void {
const stringified = JSON.stringify(this.cache)
const index = Math.ceil(stringified.length / 32000)
world.setDynamicProperty(`${this.name}Index`, index)
for (let i = 0; i < index; i++) {
world.setDynamicProperty(`${this.name}:${i}`, stringified.slice(i * 32000, (i + 1) * 32000))
}
}
protected static save() {
this.databases.forEach((database) => {
database.save()
})
}
protected static getAll(name: string, defaultValue: string): Record<string, any> {
let stringified = ""
const index = world.getDynamicProperty(`${name}Index`) as number
if (!index) {
world.setDynamicProperty(`${name}Index`, 1)
world.setDynamicProperty(`${name}:0`, defaultValue)
stringified = defaultValue
} else {
for (let i = 0; i < index; i++) {
const value = world.getDynamicProperty(`${this.name}:${i}`)
stringified += value
}
}
return JSON.parse(stringified)
}
}
system.runInterval(() => {
//@ts-ignore
Database.save()
}, 1200)