@starktma/minecraft-utils 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
File without changes
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/database/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,sBAAsB,CAAC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../src/database/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAgB,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AA0I5C;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,cAAc,CAAC,CAAC,SAAS,YAAY;IACjD,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,OAAO,CAAM;IAErB,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC;IAE/B;;;;OAIG;IACH,SAAS,aAAa,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS;IAYtE;;;;OAIG;IACH,OAAO,CAAC,YAAY;IAIpB;;;;OAIG;IACH,OAAO,CAAC,SAAS;IAIjB;;;OAGG;IACH,SAAS,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI;IAK1B;;;;OAIG;IACH,YAAY,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI;IAO7B;;;;OAIG;IACH,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO;IAI9B;;;;OAIG;IACH,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAIpC;;;OAGG;IACH,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAK9B;;;OAGG;IACH,aAAa,IAAI,CAAC,EAAE;IAIpB;;OAEG;IACH,eAAe,IAAI,IAAI;IAKvB;;;OAGG;IACH,OAAO,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI;CAGtE"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../../src/database/interfaces.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,YAAY;IAC5B,EAAE,EAAE,MAAM,CAAC;CACX"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/math/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,GAAG,MAAM,CAIhF;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAmBlG;AAED;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAErE;AAED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,MAAM,CAG9C;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,CAE5D;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,SAAS,GAAE,MAAY,GAAG,OAAO,CAEvF;AAED;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAExE"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/minecraft/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAS,MAAM,mBAAmB,CAAC;AAG1E,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,KAAK,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,WA4BnG;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,EAAE,GAAG,OAAO,EAAE,GAAG,QAO3E;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAGxD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAEtD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/minecraft/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,MAAM,EACN,MAAM,EACN,KAAK,EACL,OAAO,EAEP,iBAAiB,EACjB,OAAO,EAOP,MAAM,mBAAmB,CAAC;AAG3B,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,KAAK,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,WA4BnG;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,EAAE,GAAG,OAAO,EAAE,GAAG,QAO3E;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAGnD;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,QAAQ,GAAE,MAAU,GAAG,OAAO,CAUjG;AAED,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAO3F;AAED,wBAAgB,4BAA4B,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAapF;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAwC1D;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,GAAE,OAAe,GAAG,MAAM,CAuD3F"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/player-event/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,iBAAiB,CAAC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/player-event/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,MAAM,EAEN,yBAAyB,EACzB,wBAAwB,EAGxB,SAAS,EACT,MAAM,mBAAmB,CAAC;AAG3B,KAAK,aAAa,CAAC,CAAC,GAAG,YAAY,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,IAAI,CAAC;AAE3D,aAAK,kBAAkB;IACtB,IAAI,SAAS;IACb,SAAS,cAAc;IACvB,QAAQ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AAE,aAAa,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,36 +1,44 @@
1
- {
2
- "name": "@starktma/minecraft-utils",
3
- "version": "1.0.1",
4
- "description": "",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "keywords": [],
8
- "author": "",
9
- "license": "ISC",
10
- "type": "module",
11
- "scripts": {
12
- "build": "npx -p typescript tsc --project tsconfig.json"
13
- },
14
- "exports": {
15
- ".": {
16
- "import": "./dist/index.js",
17
- "types": "./dist/index.d.ts"
18
- },
19
- "./math": {
20
- "import": "./dist/math/index.js",
21
- "types": "./dist/math/index.d.ts"
22
- },
23
- "./minecraft": {
24
- "import": "./dist/minecraft/index.js",
25
- "types": "./dist/minecraft/index.d.ts"
26
- }
27
- },
28
- "publishConfig": {
29
- "access": "public"
30
- },
31
- "devDependencies": {
32
- "@minecraft/server": "^2.1.0",
33
- "@minecraft/server-ui": "^2.0.0",
34
- "typescript": "^5.9.2"
35
- }
36
- }
1
+ {
2
+ "name": "@starktma/minecraft-utils",
3
+ "version": "1.2.0",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "keywords": [],
8
+ "author": "",
9
+ "license": "ISC",
10
+ "type": "module",
11
+ "scripts": {
12
+ "build": "npx -p typescript tsc --project tsconfig.json"
13
+ },
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.js",
17
+ "types": "./dist/index.d.ts"
18
+ },
19
+ "./math": {
20
+ "import": "./dist/math/index.js",
21
+ "types": "./dist/math/index.d.ts"
22
+ },
23
+ "./minecraft": {
24
+ "import": "./dist/minecraft/index.js",
25
+ "types": "./dist/minecraft/index.d.ts"
26
+ },
27
+ "./database": {
28
+ "import": "./dist/database/index.js",
29
+ "types": "./dist/database/index.d.ts"
30
+ },
31
+ "./player-events": {
32
+ "import": "./dist/player-event/index.js",
33
+ "types": "./dist/player-event/index.d.ts"
34
+ }
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "devDependencies": {
40
+ "@minecraft/server": "^2.1.0",
41
+ "@minecraft/server-ui": "^2.0.0",
42
+ "typescript": "^5.9.2"
43
+ }
44
+ }
@@ -0,0 +1 @@
1
+ export const NAMESPACE = 'inventory-manager';
@@ -0,0 +1,271 @@
1
+ import { Entity, world, World } from "@minecraft/server";
2
+ import { SimpleObject } from "./interfaces";
3
+
4
+ //@ts-ignore
5
+ import { NAMESPACE } from "./constants";
6
+
7
+ /**
8
+ * DatabaseManager is a class that manages databases stored in Minecraft's world properties.
9
+ * Currently only supports JSON databases.
10
+ */
11
+ class DatabaseManager {
12
+ private static readonly DYNAMIC_PROP_MAX_LENGTH = 32767;
13
+ private static readonly CHUNK_KEY = "__SPLIT__";
14
+
15
+ private target: Entity | World;
16
+
17
+ constructor(target: Entity | undefined) {
18
+ if (target instanceof Entity) {
19
+ this.target = target;
20
+ } else {
21
+ this.target = world;
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Checks if a JSON database with the given name exists.
27
+ * @param databaseName The name of the database.
28
+ * @returns True if the database exists, false otherwise.
29
+ */
30
+ hasJSONDatabase(databaseName: string) {
31
+ return this.target.getDynamicProperty(NAMESPACE + ":" + databaseName) !== undefined;
32
+ }
33
+
34
+ /**
35
+ * Adds a new JSON database with the given name and data.
36
+ * @param databaseName The name of the database.
37
+ * @param database The data to be stored in the database.
38
+ */
39
+ addJSONDatabase(databaseName: string, database: object) {
40
+ const propertyName = `${NAMESPACE}:${databaseName}`;
41
+ const jsonString = JSON.stringify(database);
42
+ const existingProp = this.target.getDynamicProperty(propertyName) as string | undefined;
43
+ let existingChunks = 0;
44
+
45
+ if (existingProp) {
46
+ try {
47
+ const propObj = JSON.parse(existingProp);
48
+ if (propObj && typeof propObj === "object" && DatabaseManager.CHUNK_KEY in propObj) {
49
+ existingChunks = propObj[DatabaseManager.CHUNK_KEY];
50
+ }
51
+ } catch {}
52
+ }
53
+ if (jsonString.length <= DatabaseManager.DYNAMIC_PROP_MAX_LENGTH || jsonString.length === 0) {
54
+ if (existingChunks > 0) {
55
+ for (let i = 0; i < existingChunks; i++) {
56
+ const partName = `${propertyName}_${i}`;
57
+ this.target.setDynamicProperty(partName, undefined);
58
+ }
59
+ }
60
+ this.target.setDynamicProperty(propertyName, jsonString);
61
+ } else {
62
+ const chunkSize = DatabaseManager.DYNAMIC_PROP_MAX_LENGTH;
63
+ const chunkCount = Math.ceil(jsonString.length / chunkSize);
64
+ if (existingChunks > 0) {
65
+ for (let i = 0; i < existingChunks; i++) {
66
+ const oldPartName = `${propertyName}_${i}`;
67
+ this.target.setDynamicProperty(oldPartName, undefined);
68
+ }
69
+ }
70
+ for (let i = 0; i < chunkCount; i++) {
71
+ const start = i * chunkSize;
72
+ const end = start + chunkSize;
73
+ const chunk = jsonString.slice(start, end);
74
+ const partName = `${propertyName}_${i}`;
75
+ this.target.setDynamicProperty(partName, chunk);
76
+ }
77
+ const meta = { [DatabaseManager.CHUNK_KEY]: chunkCount };
78
+ this.target.setDynamicProperty(propertyName, JSON.stringify(meta));
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Removes a JSON database with the given name.
84
+ * @param databaseName The name of the database.
85
+ */
86
+ removeJSONDatabase(databaseName: string) {
87
+ const propertyName = `${NAMESPACE}:${databaseName}`;
88
+ const propString = this.target.getDynamicProperty(propertyName) as string | undefined;
89
+ if (propString !== undefined) {
90
+ try {
91
+ const propObj = JSON.parse(propString);
92
+ if (propObj && typeof propObj === "object" && DatabaseManager.CHUNK_KEY in propObj) {
93
+ const chunkCount = propObj[DatabaseManager.CHUNK_KEY];
94
+ for (let i = 0; i < chunkCount; i++) {
95
+ const partName = `${propertyName}_${i}`;
96
+ this.target.setDynamicProperty(partName, undefined);
97
+ }
98
+ }
99
+ } catch {}
100
+ this.target.setDynamicProperty(propertyName, undefined);
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Retrieves a JSON database with the given name.
106
+ * @param databaseName The name of the database.
107
+ * @returns The data stored in the database.
108
+ * @throws An error if the database does not exist.
109
+ */
110
+ getJSONDatabase(databaseName: string) {
111
+ const propertyName = `${NAMESPACE}:${databaseName}`;
112
+ const propString = this.target.getDynamicProperty(propertyName) as string | undefined;
113
+ if (propString === undefined) {
114
+ throw new Error("Database does not exist");
115
+ }
116
+ try {
117
+ const propObj = JSON.parse(propString);
118
+ if (propObj && typeof propObj === "object" && DatabaseManager.CHUNK_KEY in propObj) {
119
+ const chunkCount: number = propObj[DatabaseManager.CHUNK_KEY];
120
+ let combined = "";
121
+ for (let i = 0; i < chunkCount; i++) {
122
+ const partName = `${propertyName}_${i}`;
123
+ const part = this.target.getDynamicProperty(partName) as string | undefined;
124
+ if (typeof part === "string") {
125
+ combined += part;
126
+ } else {
127
+ combined += "";
128
+ }
129
+ }
130
+ return JSON.parse(combined);
131
+ } else {
132
+ return JSON.parse(propString);
133
+ }
134
+ } catch {
135
+ throw new Error("Failed to parse database JSON");
136
+ }
137
+ }
138
+ }
139
+
140
+ /**
141
+ * SimpleDatabase is a base class for databases that store custom objects with an id property.
142
+ * It provides methods for adding, updating, removing and retrieving objects from the database.
143
+ * A singleton pattern is used to ensure that only one instance of the database exists.
144
+ *
145
+ * @example
146
+ * class MyDatabase extends SimpleDatabase<PlayerObject> {
147
+ * protected static instance: MyDatabase;
148
+ * constructor() {
149
+ * super("myDatabase", undefined);
150
+ * }
151
+ *
152
+ * static getInstance(): MyDatabase {
153
+ * if (!MyDatabase.instance) {
154
+ * MyDatabase.instance = new MyDatabase();
155
+ * }
156
+ * return MyDatabase.instance;
157
+ * }
158
+ */
159
+ export class SimpleDatabase<T extends SimpleObject> {
160
+ private mainDB: DatabaseManager;
161
+ private localDB: T[];
162
+
163
+ protected databaseName: string;
164
+
165
+ /**
166
+ * The constructor initializes the local database and syncs it with the main database.
167
+ * @param databaseName The name of the database.
168
+ * @param target The target entity to store the database in. If undefined, the database is stored in the world.
169
+ */
170
+ protected constructor(databaseName: string, target: Entity | undefined) {
171
+ this.mainDB = new DatabaseManager(target);
172
+ this.databaseName = databaseName;
173
+
174
+ if (this.mainDB.hasJSONDatabase(this.databaseName)) {
175
+ this.localDB = this.getMainDB();
176
+ } else {
177
+ this.localDB = [];
178
+ this.updateMainDB();
179
+ }
180
+ }
181
+
182
+ /**
183
+ * Updates the main database with the local database.
184
+ * This method is called automatically when an object is added, updated or removed.
185
+ * @private
186
+ */
187
+ private updateMainDB() {
188
+ this.mainDB.addJSONDatabase(this.databaseName, this.localDB);
189
+ }
190
+
191
+ /**
192
+ * Retrieves the main database.
193
+ * @private
194
+ * @returns The main database.
195
+ */
196
+ private getMainDB() {
197
+ return this.mainDB.getJSONDatabase(this.databaseName);
198
+ }
199
+
200
+ /**
201
+ * Adds an object to the local database and updates the main database.
202
+ * @param object The object to be added.
203
+ */
204
+ addObject(object: T): void {
205
+ this.localDB.push(object);
206
+ this.updateMainDB();
207
+ }
208
+
209
+ /**
210
+ * Updates an object in the local database and the main database.
211
+ * If the object does not exist, it is added.
212
+ * @param object The object to be updated.
213
+ */
214
+ updateObject(object: T): void {
215
+ if (this.hasObject(object.id)) {
216
+ this.removeObject(object.id);
217
+ }
218
+ this.addObject(object);
219
+ }
220
+
221
+ /**
222
+ * Checks if an object with the given id exists in the local database.
223
+ * @param id The id of the object.
224
+ * @returns True if the object exists, false otherwise.
225
+ */
226
+ hasObject(id: string): boolean {
227
+ return this.localDB.map((object) => object.id).includes(id);
228
+ }
229
+
230
+ /**
231
+ * Retrieves an object with the given id from the local database.
232
+ * @param id The id of the object.
233
+ * @returns The object if it exists, undefined otherwise.
234
+ */
235
+ getObject(id: string): T | undefined {
236
+ return this.localDB.filter((object) => object.id === id)[0];
237
+ }
238
+
239
+ /**
240
+ * Removes an object with the given id from the local database and updates the main database.
241
+ * @param id The id of the object.
242
+ */
243
+ removeObject(id: string): void {
244
+ this.localDB = this.localDB.filter((object) => object.id !== id);
245
+ this.updateMainDB();
246
+ }
247
+
248
+ /**
249
+ * Retrieves all objects from the local database.
250
+ * @returns An array of all objects in the local database.
251
+ */
252
+ getAllObjects(): T[] {
253
+ return this.localDB;
254
+ }
255
+
256
+ /**
257
+ * Removes all objects from the local database and updates the main database.
258
+ */
259
+ eraseAllObjects(): void {
260
+ this.localDB = [];
261
+ this.updateMainDB();
262
+ }
263
+
264
+ /**
265
+ * Iterates over all objects in the local database.
266
+ * @param callback The function to be called for each object.
267
+ */
268
+ forEach(callback: (object: SimpleObject, index: number) => void): void {
269
+ this.localDB.forEach((object, index) => callback(object, index));
270
+ }
271
+ }
@@ -0,0 +1,4 @@
1
+ import { SimpleDatabase } from "./database";
2
+ import { SimpleObject } from "./interfaces";
3
+
4
+ export { SimpleDatabase, SimpleObject };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * SimpleObject is an interface for objects with an id property.
3
+ */
4
+ export interface SimpleObject {
5
+ id: string;
6
+ }