@starktma/minecraft-utils 1.2.1 → 1.2.3
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 +17 -68
- package/dist/database/constants.d.ts +2 -0
- package/dist/database/constants.js +2 -0
- package/dist/database/constants.js.map +1 -0
- package/dist/database/database.d.ts +87 -0
- package/dist/database/database.js +256 -0
- package/dist/database/database.js.map +1 -0
- package/{src/database/index.ts → dist/database/index.d.ts} +1 -1
- package/dist/database/index.js +3 -0
- package/dist/database/index.js.map +1 -0
- package/{src/database/interfaces.ts → dist/database/interfaces.d.ts} +2 -1
- package/dist/database/interfaces.js +2 -0
- package/dist/database/interfaces.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js.map +1 -0
- package/{src/math/index.ts → dist/math/index.d.ts} +11 -63
- package/dist/math/index.js +117 -0
- package/dist/math/index.js.map +1 -0
- package/dist/minecraft/index.d.ts +22 -0
- package/dist/minecraft/index.js +173 -0
- package/dist/minecraft/index.js.map +1 -0
- package/dist/player-event/index.d.ts +48 -0
- package/dist/player-event/index.js +151 -0
- package/dist/player-event/index.js.map +1 -0
- package/package.json +4 -2
- package/src/database/constants.ts +0 -1
- package/src/database/database.ts +0 -271
- package/src/index.ts +0 -0
- package/src/minecraft/index.ts +0 -210
- package/src/player-event/index.ts +0 -184
- package/tsconfig.json +0 -22
package/README.md
CHANGED
|
@@ -1,77 +1,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @starktma/minecraft-utils — utilities for Minecraft scripting
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This package provides a small collection of TypeScript utilities. The code is split into focused modules; this README gives a short summary and points to the source files for details.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Quick summary
|
|
6
6
|
|
|
7
|
-
- **
|
|
8
|
-
- **
|
|
9
|
-
- **
|
|
7
|
+
- **database** — lightweight JSON storage helpers for Minecraft's dynamic properties. Implements a `DatabaseManager` (low-level serialization/partitioning) and `SimpleDatabase` (convenience base class for storing objects by id). See `src/database` for full details and examples.
|
|
8
|
+
- **math** — common math utilities used across the packages. See `src/math`.
|
|
9
|
+
- **minecraft** — helpers for Minecraft-specific operations and types. See `src/minecraft`.
|
|
10
|
+
- **player-event** — convenience helpers to handle player-related events and payloads. See `src/player-event`.
|
|
10
11
|
|
|
11
|
-
##
|
|
12
|
+
## Short database note
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
2. Install the PropertyDatabase module `@starktma/minecraft-utils` using your preferred method (e.g., npm, yarn).
|
|
15
|
-
3. Place the PropertyDatabase module in your server's script directory or in the assets/javascript directory if you're using Anvil.
|
|
14
|
+
The database utilities are intentionally small: `DatabaseManager` handles serializing/partitioning JSON so it can be stored within Minecraft dynamic property limits, and `SimpleDatabase` provides a straightforward API for storing objects keyed by `id`. For full API and examples, open `src/database`.
|
|
16
15
|
|
|
17
|
-
##
|
|
16
|
+
## Installation & quick dev
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
- **To use the published package**: `npm install @starktma/minecraft-utils` (if published to npm or a registry).
|
|
20
19
|
|
|
21
|
-
|
|
20
|
+
## Where to look next
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
- `
|
|
26
|
-
- `
|
|
27
|
-
- `
|
|
28
|
-
- `getJSONDatabase(databaseName: string)`: Retrieves a database.
|
|
29
|
-
|
|
30
|
-
### SimpleDatabase Class
|
|
31
|
-
|
|
32
|
-
A base class for managing databases of custom objects.
|
|
33
|
-
|
|
34
|
-
#### Methods:
|
|
35
|
-
|
|
36
|
-
- `addObject(object: SimpleObject)`: Adds a new object to the database.
|
|
37
|
-
- `updateObject(object: SimpleObject)`: Updates an existing object.
|
|
38
|
-
- `hasObject(id: string)`: Checks if an object exists.
|
|
39
|
-
- `getObject(id: string)`: Retrieves an object by ID.
|
|
40
|
-
- `removeObject(id: string)`: Removes an object.
|
|
41
|
-
- `getAllObjects()`: Retrieves all objects.
|
|
42
|
-
- `eraseAllObjects()`: Clears the database.
|
|
43
|
-
- `forEach(callback: Function)`: Iterates over objects.
|
|
44
|
-
|
|
45
|
-
### SimpleObject Interface
|
|
46
|
-
|
|
47
|
-
A basic interface for objects managed by `SimpleDatabase`. Must include an `id` property.
|
|
48
|
-
|
|
49
|
-
## NOTE
|
|
50
|
-
|
|
51
|
-
The `DatabaseManager` class is designed to handle JSON databases in Minecraft's world properties and cannot be used directly. Its main job is to convert your database to string formats that can be stored on dynamic properties and back.
|
|
52
|
-
|
|
53
|
-
The `DatabaseManager` class is responsible for deconstructing and reconstructing the database from JSON strings to work around the dynamic property size limits in Minecraft [`32767`].
|
|
54
|
-
|
|
55
|
-
The `SimpleDatabase` class uses the `DatabaseManager` to manage custom objects. It provides methods to add, update, retrieve, and remove objects, as well as to iterate over all objects in the database.
|
|
56
|
-
|
|
57
|
-
The `SimpleDatabase` is intended to be inherited by custom object classes. It requires an `id` property to uniquely identify each object and can be extended with additional properties as needed.
|
|
58
|
-
|
|
59
|
-
The `SimpleObject` interface defines the structure of objects that can be managed by `SimpleDatabase`. It includes an `id` property, which is essential for identifying objects within the database, and can be extended with additional properties to structure your own database objects.
|
|
60
|
-
|
|
61
|
-
## Examples
|
|
62
|
-
|
|
63
|
-
### Creating a Custom Database
|
|
64
|
-
|
|
65
|
-
```typescript
|
|
66
|
-
class MyDatabase extends SimpleDatabase {
|
|
67
|
-
constructor() {
|
|
68
|
-
super("my_database");
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
addObject(object: MyObject) {
|
|
72
|
-
super.addObject(object);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Additional custom methods...
|
|
76
|
-
}
|
|
77
|
-
```
|
|
22
|
+
- See the module source for exact types, exported functions, and examples:
|
|
23
|
+
- `src/database` — database manager, simple database base class, and interfaces
|
|
24
|
+
- `src/math` — math helpers
|
|
25
|
+
- `src/minecraft` — Minecraft helpers
|
|
26
|
+
- `src/player-event` — player event helpers
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/database/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Entity } from "@minecraft/server";
|
|
2
|
+
import { SimpleObject } from "./interfaces";
|
|
3
|
+
/**
|
|
4
|
+
* SimpleDatabase is a base class for databases that store custom objects with an id property.
|
|
5
|
+
* It provides methods for adding, updating, removing and retrieving objects from the database.
|
|
6
|
+
* A singleton pattern is used to ensure that only one instance of the database exists.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* class MyDatabase extends SimpleDatabase<PlayerObject> {
|
|
10
|
+
* protected static instance: MyDatabase;
|
|
11
|
+
* constructor() {
|
|
12
|
+
* super("myDatabase", undefined);
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* static getInstance(): MyDatabase {
|
|
16
|
+
* if (!MyDatabase.instance) {
|
|
17
|
+
* MyDatabase.instance = new MyDatabase();
|
|
18
|
+
* }
|
|
19
|
+
* return MyDatabase.instance;
|
|
20
|
+
* }
|
|
21
|
+
*/
|
|
22
|
+
export declare class SimpleDatabase<T extends SimpleObject> {
|
|
23
|
+
private mainDB;
|
|
24
|
+
private localDB;
|
|
25
|
+
protected databaseName: string;
|
|
26
|
+
/**
|
|
27
|
+
* The constructor initializes the local database and syncs it with the main database.
|
|
28
|
+
* @param databaseName The name of the database.
|
|
29
|
+
* @param target The target entity to store the database in. If undefined, the database is stored in the world.
|
|
30
|
+
*/
|
|
31
|
+
protected constructor(databaseName: string, target?: Entity | undefined);
|
|
32
|
+
/**
|
|
33
|
+
* Updates the main database with the local database.
|
|
34
|
+
* This method is called automatically when an object is added, updated or removed.
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
private updateMainDB;
|
|
38
|
+
/**
|
|
39
|
+
* Retrieves the main database.
|
|
40
|
+
* @private
|
|
41
|
+
* @returns The main database.
|
|
42
|
+
*/
|
|
43
|
+
private getMainDB;
|
|
44
|
+
/**
|
|
45
|
+
* Adds an object to the local database and updates the main database.
|
|
46
|
+
* @param object The object to be added.
|
|
47
|
+
*/
|
|
48
|
+
addObject(object: T): void;
|
|
49
|
+
/**
|
|
50
|
+
* Updates an object in the local database and the main database.
|
|
51
|
+
* If the object does not exist, it is added.
|
|
52
|
+
* @param object The object to be updated.
|
|
53
|
+
*/
|
|
54
|
+
updateObject(object: T): void;
|
|
55
|
+
/**
|
|
56
|
+
* Checks if an object with the given id exists in the local database.
|
|
57
|
+
* @param id The id of the object.
|
|
58
|
+
* @returns True if the object exists, false otherwise.
|
|
59
|
+
*/
|
|
60
|
+
hasObject(id: string): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Retrieves an object with the given id from the local database.
|
|
63
|
+
* @param id The id of the object.
|
|
64
|
+
* @returns The object if it exists, undefined otherwise.
|
|
65
|
+
*/
|
|
66
|
+
getObject(id: string): T | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Removes an object with the given id from the local database and updates the main database.
|
|
69
|
+
* @param id The id of the object.
|
|
70
|
+
*/
|
|
71
|
+
removeObject(id: string): void;
|
|
72
|
+
/**
|
|
73
|
+
* Retrieves all objects from the local database.
|
|
74
|
+
* @returns An array of all objects in the local database.
|
|
75
|
+
*/
|
|
76
|
+
getAllObjects(): T[];
|
|
77
|
+
/**
|
|
78
|
+
* Removes all objects from the local database and updates the main database.
|
|
79
|
+
*/
|
|
80
|
+
eraseAllObjects(): void;
|
|
81
|
+
/**
|
|
82
|
+
* Iterates over all objects in the local database.
|
|
83
|
+
* @param callback The function to be called for each object.
|
|
84
|
+
*/
|
|
85
|
+
forEach(callback: (object: SimpleObject, index: number) => void): void;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=database.d.ts.map
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { Entity, world } from "@minecraft/server";
|
|
2
|
+
//@ts-ignore
|
|
3
|
+
import { NAMESPACE } from "./constants";
|
|
4
|
+
/**
|
|
5
|
+
* DatabaseManager is a class that manages databases stored in Minecraft's world properties.
|
|
6
|
+
* Currently only supports JSON databases.
|
|
7
|
+
*/
|
|
8
|
+
class DatabaseManager {
|
|
9
|
+
static DYNAMIC_PROP_MAX_LENGTH = 32767;
|
|
10
|
+
static CHUNK_KEY = "__SPLIT__";
|
|
11
|
+
target;
|
|
12
|
+
constructor(target) {
|
|
13
|
+
if (target instanceof Entity) {
|
|
14
|
+
this.target = target;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
this.target = world;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Checks if a JSON database with the given name exists.
|
|
22
|
+
* @param databaseName The name of the database.
|
|
23
|
+
* @returns True if the database exists, false otherwise.
|
|
24
|
+
*/
|
|
25
|
+
hasJSONDatabase(databaseName) {
|
|
26
|
+
return this.target.getDynamicProperty(NAMESPACE + ":" + databaseName) !== undefined;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Adds a new JSON database with the given name and data.
|
|
30
|
+
* @param databaseName The name of the database.
|
|
31
|
+
* @param database The data to be stored in the database.
|
|
32
|
+
*/
|
|
33
|
+
addJSONDatabase(databaseName, database) {
|
|
34
|
+
const propertyName = `${NAMESPACE}:${databaseName}`;
|
|
35
|
+
const jsonString = JSON.stringify(database);
|
|
36
|
+
const existingProp = this.target.getDynamicProperty(propertyName);
|
|
37
|
+
let existingChunks = 0;
|
|
38
|
+
if (existingProp) {
|
|
39
|
+
try {
|
|
40
|
+
const propObj = JSON.parse(existingProp);
|
|
41
|
+
if (propObj && typeof propObj === "object" && DatabaseManager.CHUNK_KEY in propObj) {
|
|
42
|
+
existingChunks = propObj[DatabaseManager.CHUNK_KEY];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch { }
|
|
46
|
+
}
|
|
47
|
+
if (jsonString.length <= DatabaseManager.DYNAMIC_PROP_MAX_LENGTH || jsonString.length === 0) {
|
|
48
|
+
if (existingChunks > 0) {
|
|
49
|
+
for (let i = 0; i < existingChunks; i++) {
|
|
50
|
+
const partName = `${propertyName}_${i}`;
|
|
51
|
+
this.target.setDynamicProperty(partName, undefined);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
this.target.setDynamicProperty(propertyName, jsonString);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const chunkSize = DatabaseManager.DYNAMIC_PROP_MAX_LENGTH;
|
|
58
|
+
const chunkCount = Math.ceil(jsonString.length / chunkSize);
|
|
59
|
+
if (existingChunks > 0) {
|
|
60
|
+
for (let i = 0; i < existingChunks; i++) {
|
|
61
|
+
const oldPartName = `${propertyName}_${i}`;
|
|
62
|
+
this.target.setDynamicProperty(oldPartName, undefined);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
for (let i = 0; i < chunkCount; i++) {
|
|
66
|
+
const start = i * chunkSize;
|
|
67
|
+
const end = start + chunkSize;
|
|
68
|
+
const chunk = jsonString.slice(start, end);
|
|
69
|
+
const partName = `${propertyName}_${i}`;
|
|
70
|
+
this.target.setDynamicProperty(partName, chunk);
|
|
71
|
+
}
|
|
72
|
+
const meta = { [DatabaseManager.CHUNK_KEY]: chunkCount };
|
|
73
|
+
this.target.setDynamicProperty(propertyName, JSON.stringify(meta));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Removes a JSON database with the given name.
|
|
78
|
+
* @param databaseName The name of the database.
|
|
79
|
+
*/
|
|
80
|
+
removeJSONDatabase(databaseName) {
|
|
81
|
+
const propertyName = `${NAMESPACE}:${databaseName}`;
|
|
82
|
+
const propString = this.target.getDynamicProperty(propertyName);
|
|
83
|
+
if (propString !== undefined) {
|
|
84
|
+
try {
|
|
85
|
+
const propObj = JSON.parse(propString);
|
|
86
|
+
if (propObj && typeof propObj === "object" && DatabaseManager.CHUNK_KEY in propObj) {
|
|
87
|
+
const chunkCount = propObj[DatabaseManager.CHUNK_KEY];
|
|
88
|
+
for (let i = 0; i < chunkCount; i++) {
|
|
89
|
+
const partName = `${propertyName}_${i}`;
|
|
90
|
+
this.target.setDynamicProperty(partName, undefined);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch { }
|
|
95
|
+
this.target.setDynamicProperty(propertyName, undefined);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Retrieves a JSON database with the given name.
|
|
100
|
+
* @param databaseName The name of the database.
|
|
101
|
+
* @returns The data stored in the database.
|
|
102
|
+
* @throws An error if the database does not exist.
|
|
103
|
+
*/
|
|
104
|
+
getJSONDatabase(databaseName) {
|
|
105
|
+
const propertyName = `${NAMESPACE}:${databaseName}`;
|
|
106
|
+
const propString = this.target.getDynamicProperty(propertyName);
|
|
107
|
+
if (propString === undefined) {
|
|
108
|
+
throw new Error("Database does not exist");
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const propObj = JSON.parse(propString);
|
|
112
|
+
if (propObj && typeof propObj === "object" && DatabaseManager.CHUNK_KEY in propObj) {
|
|
113
|
+
const chunkCount = propObj[DatabaseManager.CHUNK_KEY];
|
|
114
|
+
let combined = "";
|
|
115
|
+
for (let i = 0; i < chunkCount; i++) {
|
|
116
|
+
const partName = `${propertyName}_${i}`;
|
|
117
|
+
const part = this.target.getDynamicProperty(partName);
|
|
118
|
+
if (typeof part === "string") {
|
|
119
|
+
combined += part;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
combined += "";
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return JSON.parse(combined);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
return JSON.parse(propString);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
throw new Error("Failed to parse database JSON");
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* SimpleDatabase is a base class for databases that store custom objects with an id property.
|
|
138
|
+
* It provides methods for adding, updating, removing and retrieving objects from the database.
|
|
139
|
+
* A singleton pattern is used to ensure that only one instance of the database exists.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* class MyDatabase extends SimpleDatabase<PlayerObject> {
|
|
143
|
+
* protected static instance: MyDatabase;
|
|
144
|
+
* constructor() {
|
|
145
|
+
* super("myDatabase", undefined);
|
|
146
|
+
* }
|
|
147
|
+
*
|
|
148
|
+
* static getInstance(): MyDatabase {
|
|
149
|
+
* if (!MyDatabase.instance) {
|
|
150
|
+
* MyDatabase.instance = new MyDatabase();
|
|
151
|
+
* }
|
|
152
|
+
* return MyDatabase.instance;
|
|
153
|
+
* }
|
|
154
|
+
*/
|
|
155
|
+
export class SimpleDatabase {
|
|
156
|
+
mainDB;
|
|
157
|
+
localDB;
|
|
158
|
+
databaseName;
|
|
159
|
+
/**
|
|
160
|
+
* The constructor initializes the local database and syncs it with the main database.
|
|
161
|
+
* @param databaseName The name of the database.
|
|
162
|
+
* @param target The target entity to store the database in. If undefined, the database is stored in the world.
|
|
163
|
+
*/
|
|
164
|
+
constructor(databaseName, target) {
|
|
165
|
+
this.mainDB = new DatabaseManager(target);
|
|
166
|
+
this.databaseName = databaseName;
|
|
167
|
+
if (this.mainDB.hasJSONDatabase(this.databaseName)) {
|
|
168
|
+
this.localDB = this.getMainDB();
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
this.localDB = [];
|
|
172
|
+
this.updateMainDB();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Updates the main database with the local database.
|
|
177
|
+
* This method is called automatically when an object is added, updated or removed.
|
|
178
|
+
* @private
|
|
179
|
+
*/
|
|
180
|
+
updateMainDB() {
|
|
181
|
+
this.mainDB.addJSONDatabase(this.databaseName, this.localDB);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Retrieves the main database.
|
|
185
|
+
* @private
|
|
186
|
+
* @returns The main database.
|
|
187
|
+
*/
|
|
188
|
+
getMainDB() {
|
|
189
|
+
return this.mainDB.getJSONDatabase(this.databaseName);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Adds an object to the local database and updates the main database.
|
|
193
|
+
* @param object The object to be added.
|
|
194
|
+
*/
|
|
195
|
+
addObject(object) {
|
|
196
|
+
this.localDB.push(object);
|
|
197
|
+
this.updateMainDB();
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Updates an object in the local database and the main database.
|
|
201
|
+
* If the object does not exist, it is added.
|
|
202
|
+
* @param object The object to be updated.
|
|
203
|
+
*/
|
|
204
|
+
updateObject(object) {
|
|
205
|
+
if (this.hasObject(object.id)) {
|
|
206
|
+
this.removeObject(object.id);
|
|
207
|
+
}
|
|
208
|
+
this.addObject(object);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Checks if an object with the given id exists in the local database.
|
|
212
|
+
* @param id The id of the object.
|
|
213
|
+
* @returns True if the object exists, false otherwise.
|
|
214
|
+
*/
|
|
215
|
+
hasObject(id) {
|
|
216
|
+
return this.localDB.map((object) => object.id).includes(id);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Retrieves an object with the given id from the local database.
|
|
220
|
+
* @param id The id of the object.
|
|
221
|
+
* @returns The object if it exists, undefined otherwise.
|
|
222
|
+
*/
|
|
223
|
+
getObject(id) {
|
|
224
|
+
return this.localDB.filter((object) => object.id === id)[0];
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Removes an object with the given id from the local database and updates the main database.
|
|
228
|
+
* @param id The id of the object.
|
|
229
|
+
*/
|
|
230
|
+
removeObject(id) {
|
|
231
|
+
this.localDB = this.localDB.filter((object) => object.id !== id);
|
|
232
|
+
this.updateMainDB();
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Retrieves all objects from the local database.
|
|
236
|
+
* @returns An array of all objects in the local database.
|
|
237
|
+
*/
|
|
238
|
+
getAllObjects() {
|
|
239
|
+
return this.localDB;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Removes all objects from the local database and updates the main database.
|
|
243
|
+
*/
|
|
244
|
+
eraseAllObjects() {
|
|
245
|
+
this.localDB = [];
|
|
246
|
+
this.updateMainDB();
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Iterates over all objects in the local database.
|
|
250
|
+
* @param callback The function to be called for each object.
|
|
251
|
+
*/
|
|
252
|
+
forEach(callback) {
|
|
253
|
+
this.localDB.forEach((object, index) => callback(object, index));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
//# sourceMappingURL=database.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.js","sourceRoot":"","sources":["../../src/database/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAS,MAAM,mBAAmB,CAAC;AAGzD,YAAY;AACZ,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC;;;GAGG;AACH,MAAM,eAAe;IACZ,MAAM,CAAU,uBAAuB,GAAG,KAAK,CAAC;IAChD,MAAM,CAAU,SAAS,GAAG,WAAW,CAAC;IAExC,MAAM,CAAiB;IAE/B,YAAY,MAA0B;QACrC,IAAI,MAAM,YAAY,MAAM,EAAE,CAAC;YAC9B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACtB,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACrB,CAAC;IACF,CAAC;IAED;;;;OAIG;IACH,eAAe,CAAC,YAAoB;QACnC,OAAO,IAAI,CAAC,MAAM,CAAC,kBAAkB,CAAC,SAAS,GAAG,GAAG,GAAG,YAAY,CAAC,KAAK,SAAS,CAAC;IACrF,CAAC;IAED;;;;OAIG;IACH,eAAe,CAAC,YAAoB,EAAE,QAAgB;QACrD,MAAM,YAAY,GAAG,GAAG,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I,CAAC,YAAY,EAAE,CAAC;IACrB,CAAC;IAED;;;OAGG;IACH,OAAO,CAAC,QAAuD;QAC9D,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;IAClE,CAAC;CACD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAG5C,OAAO,EAAE,cAAc,EAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../src/database/interfaces.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
|
@@ -1,37 +1,25 @@
|
|
|
1
1
|
import { RGB, RGBA, Vector3 } from "@minecraft/server";
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* Converts an angle in degrees to radians.
|
|
5
4
|
*
|
|
6
5
|
* @param degrees - The angle in degrees to be converted.
|
|
7
6
|
* @returns The angle in radians.
|
|
8
7
|
*/
|
|
9
|
-
export function toRadians(degrees: number): number
|
|
10
|
-
return degrees * (Math.PI / 180);
|
|
11
|
-
}
|
|
12
|
-
|
|
8
|
+
export declare function toRadians(degrees: number): number;
|
|
13
9
|
/**
|
|
14
10
|
* Converts an angle in radians to degrees.
|
|
15
11
|
*
|
|
16
12
|
* @param radians - The angle in radians to be converted.
|
|
17
13
|
* @returns The angle in degrees.
|
|
18
14
|
*/
|
|
19
|
-
export function calculateDistance(position1: Vector3, position2: Vector3): number
|
|
20
|
-
return Math.sqrt(
|
|
21
|
-
Math.pow(position1.x - position2.x, 2) + Math.pow(position1.y - position2.y, 2) + Math.pow(position1.z - position2.z, 2)
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
|
|
15
|
+
export declare function calculateDistance(position1: Vector3, position2: Vector3): number;
|
|
25
16
|
/**
|
|
26
17
|
* Converts an angle in radians to degrees.
|
|
27
18
|
*
|
|
28
19
|
* @param radians - The angle in radians to be converted.
|
|
29
20
|
* @returns The angle in degrees.
|
|
30
21
|
*/
|
|
31
|
-
export function toDegrees(radians: number): number
|
|
32
|
-
return radians * (180 / Math.PI);
|
|
33
|
-
}
|
|
34
|
-
|
|
22
|
+
export declare function toDegrees(radians: number): number;
|
|
35
23
|
/**
|
|
36
24
|
* Converts a hex color string to an RGBA object.
|
|
37
25
|
*
|
|
@@ -39,30 +27,7 @@ export function toDegrees(radians: number): number {
|
|
|
39
27
|
* @returns An object containing the red, green, blue, and alpha components as numbers between 0 and 1.
|
|
40
28
|
* @throws Error if the hex string is invalid.
|
|
41
29
|
*/
|
|
42
|
-
export function hexToRgba(hex: string, stripAlpha
|
|
43
|
-
if (!/^#([a-fA-F0-9]{4}|[a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(hex)) {
|
|
44
|
-
throw new Error("Invalid hex color");
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
let normalized = hex.slice(1);
|
|
48
|
-
if (normalized.length === 3 || normalized.length === 4) {
|
|
49
|
-
normalized = normalized
|
|
50
|
-
.split("")
|
|
51
|
-
.map((c) => c + c)
|
|
52
|
-
.join("");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const red = parseInt(normalized.substring(0, 2), 16) / 255;
|
|
56
|
-
const green = parseInt(normalized.substring(2, 4), 16) / 255;
|
|
57
|
-
const blue = parseInt(normalized.substring(4, 6), 16) / 255;
|
|
58
|
-
const alpha = (normalized.length === 8 ? parseInt(normalized.substring(6, 8), 16) : 255) / 255;
|
|
59
|
-
|
|
60
|
-
if (stripAlpha) {
|
|
61
|
-
return { red, green, blue };
|
|
62
|
-
}
|
|
63
|
-
return { red, green, blue, alpha };
|
|
64
|
-
}
|
|
65
|
-
|
|
30
|
+
export declare function hexToRgba(hex: string, stripAlpha?: boolean): RGB | RGBA;
|
|
66
31
|
/**
|
|
67
32
|
* Clamps a value between a minimum and maximum range.
|
|
68
33
|
*
|
|
@@ -71,31 +36,21 @@ export function hexToRgba(hex: string, stripAlpha: boolean = false): RGB | RGBA
|
|
|
71
36
|
* @param max - The maximum value of the range.
|
|
72
37
|
* @returns The clamped value, which will be between `min` and `max`.
|
|
73
38
|
*/
|
|
74
|
-
export function clamp(value: number, min: number, max: number): number
|
|
75
|
-
return Math.max(min, Math.min(max, value));
|
|
76
|
-
}
|
|
77
|
-
|
|
39
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
|
78
40
|
/**
|
|
79
41
|
* Converts an angle in degrees to its signed equivalent in the range [-180, 180].
|
|
80
42
|
*
|
|
81
43
|
* @param angle - The angle in degrees to be converted.
|
|
82
44
|
* @returns The signed angle in degrees, normalized to the range [-180, 180].
|
|
83
45
|
*/
|
|
84
|
-
export function toSigned(angle: number): number
|
|
85
|
-
const wrapped = ((angle % 360) + 360) % 360;
|
|
86
|
-
return wrapped > 180 ? wrapped - 360 : wrapped;
|
|
87
|
-
}
|
|
88
|
-
|
|
46
|
+
export declare function toSigned(angle: number): number;
|
|
89
47
|
/**
|
|
90
48
|
* Converts an angle in degrees to its unsigned equivalent in the range [0, 360].
|
|
91
49
|
*
|
|
92
50
|
* @param angle - The angle in degrees to be converted.
|
|
93
51
|
* @returns The unsigned angle in degrees, normalized to the range [0, 360].
|
|
94
52
|
*/
|
|
95
|
-
export function toUnsigned(angle: number): number
|
|
96
|
-
return ((angle % 360) + 360) % 360;
|
|
97
|
-
}
|
|
98
|
-
|
|
53
|
+
export declare function toUnsigned(angle: number): number;
|
|
99
54
|
/**
|
|
100
55
|
* Calculates the shortest signed arc between two angles in degrees.
|
|
101
56
|
*
|
|
@@ -103,10 +58,7 @@ export function toUnsigned(angle: number): number {
|
|
|
103
58
|
* @param to - The target angle in degrees.
|
|
104
59
|
* @returns The shortest arc in degrees from `from` to `to`.
|
|
105
60
|
*/
|
|
106
|
-
export function shortestArc(from: number, to: number): number
|
|
107
|
-
return -toSigned((to - from + 360) % 360);
|
|
108
|
-
}
|
|
109
|
-
|
|
61
|
+
export declare function shortestArc(from: number, to: number): number;
|
|
110
62
|
/**
|
|
111
63
|
* Checks if two positions are equal within a specified tolerance.
|
|
112
64
|
*
|
|
@@ -115,10 +67,7 @@ export function shortestArc(from: number, to: number): number {
|
|
|
115
67
|
* @param tolerance - The tolerance for comparison (default is 0.5).
|
|
116
68
|
* @returns `true` if the positions are equal within the tolerance, otherwise `false`.
|
|
117
69
|
*/
|
|
118
|
-
export function positionsEqual(a: Vector3, b: Vector3, tolerance
|
|
119
|
-
return Math.abs(a.x - b.x) < tolerance && Math.abs(a.y - b.y) < tolerance && Math.abs(a.z - b.z) < tolerance;
|
|
120
|
-
}
|
|
121
|
-
|
|
70
|
+
export declare function positionsEqual(a: Vector3, b: Vector3, tolerance?: number): boolean;
|
|
122
71
|
/**
|
|
123
72
|
* Checks if a value is within a specified range.
|
|
124
73
|
*
|
|
@@ -127,6 +76,5 @@ export function positionsEqual(a: Vector3, b: Vector3, tolerance: number = 0.5):
|
|
|
127
76
|
* @param max - The maximum value of the range.
|
|
128
77
|
* @returns `true` if the value is within the range [min, max], otherwise `false`.
|
|
129
78
|
*/
|
|
130
|
-
export function inRange(value: number, min: number, max: number): boolean
|
|
131
|
-
|
|
132
|
-
}
|
|
79
|
+
export declare function inRange(value: number, min: number, max: number): boolean;
|
|
80
|
+
//# sourceMappingURL=index.d.ts.map
|