@starktma/minecraft-utils 1.2.0 → 1.2.1

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 CHANGED
@@ -0,0 +1,77 @@
1
+ # Database Management in Minecraft: TypeScript Edition
2
+
3
+ PropertyDatabase is a TypeScript-based database management system designed for use within Minecraft. The system consists of two main classes: `DatabaseManager` and `SimpleDatabase`, which handle JSON database operations in the Minecraft world.
4
+
5
+ ## Features
6
+
7
+ - **Entity & World Targeting**: Ability to target either an entity or the Minecraft world for storing data.
8
+ - **JSON Database Operations**: Supports basic CRUD (Create, Read, Update, Delete) operations for JSON databases.
9
+ - **Custom Object Management**: Facilitates storing and managing custom objects with unique IDs.
10
+
11
+ ## Installation
12
+
13
+ 1. Ensure you have the Minecraft server set up with the appropriate scripting API support.
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.
16
+
17
+ ## Usage
18
+
19
+ ### DatabaseManager Class
20
+
21
+ Handles JSON databases in Minecraft's world properties. Can target an entity or the world itself.
22
+
23
+ #### Methods:
24
+
25
+ - `hasJSONDatabase(databaseName: string)`: Checks if a database exists.
26
+ - `addJSONDatabase(databaseName: string, database: object)`: Adds a new database.
27
+ - `removeJSONDatabase(databaseName: string)`: Removes a database.
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
+ ```
@@ -1 +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;;;;O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B,IAAI,mBAAmB,IACoD,yBAAyB,CACnG;IAED,IAAI,kBAAkB,IACoD,wBAAwB,CACjG;IAED,eAAe,IAAI,SAAS,GAAG,SAAS;IAIxC,KAAK;IAML,IAAI;CAmBJ;AAED,cAAM,aAAa,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY;IACxD,OAAO,CAAC,OAAO,CAAwB;IACvC,OAAO,CAAC,MAAM,CAAoC;IAElD,SAAS;IAwBT,SAAS,CAAC,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC;IAIhD,SAAS,CAAC,EAAE,EAAE,MAAM;IAIpB,YAAY,CAAC,EAAE,EAAE,MAAM;IAIvB,SAAS,CAAC,MAAM,EAAE,MAAM;IAKxB,cAAc,CAAC,OAAO,EAAE,MAAM;;;IAM9B,SAAS,CAAC,OAAO,EAAE,MAAM;IAIzB,IAAI;CAWJ;AAED,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,aAAa,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@starktma/minecraft-utils",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -167,7 +167,7 @@ export class SimpleDatabase<T extends SimpleObject> {
167
167
  * @param databaseName The name of the database.
168
168
  * @param target The target entity to store the database in. If undefined, the database is stored in the world.
169
169
  */
170
- protected constructor(databaseName: string, target: Entity | undefined) {
170
+ protected constructor(databaseName: string, target?: Entity | undefined) {
171
171
  this.mainDB = new DatabaseManager(target);
172
172
  this.databaseName = databaseName;
173
173
 
@@ -13,7 +13,7 @@ import {
13
13
  EquipmentSlot,
14
14
  ItemStack,
15
15
  } from "@minecraft/server";
16
- import { calculateDistance, toRadians, toUnsigned } from "../math";
16
+ import { calculateDistance, toRadians, toSigned, toUnsigned } from "../math";
17
17
 
18
18
  export function getBlocksInASphere(centerBlock: Block | Entity, radius: number, innerRadius?: number) {
19
19
  if (centerBlock) {
@@ -62,7 +62,9 @@ export function displayActionbar(player: Player | undefined, ...message: any) {
62
62
  */
63
63
  export function snapYawToGrid(angle: number): number {
64
64
  const gridSize = 90;
65
- return Math.round(toUnsigned(angle) / gridSize) * gridSize;
65
+ // Round to nearest grid step, then normalize into [0, 360)
66
+ const snapped = Math.round(toUnsigned(angle) / gridSize) * gridSize;
67
+ return ((snapped % 360) + 360) % 360;
66
68
  }
67
69
 
68
70
  /**
@@ -83,12 +85,13 @@ export function snapLocationToGrid(location: Vector3, yaw: Vector2, gridSize: nu
83
85
  };
84
86
  }
85
87
 
86
- export function getStructureRotationEnum(angle: number, saveYaw?: number): StructureRotation {
87
- const EPS = 1e-4;
88
- const diff = snapYawToGrid(angle) - snapYawToGrid(saveYaw ?? 0);
89
- if (Math.abs(diff - 90) < EPS) return StructureRotation.Rotate90;
90
- if (Math.abs(diff - 180) < EPS) return StructureRotation.Rotate180;
91
- if (Math.abs(diff - 270) < EPS) return StructureRotation.Rotate270;
88
+ export function getStructureRotationEnum(angle: number, offset?: number): StructureRotation {
89
+ const diff = snapYawToGrid(toUnsigned(angle - (offset ?? 0)));
90
+ console.log(diff);
91
+
92
+ if (Math.abs(diff - 90) < Number.EPSILON) return StructureRotation.Rotate90;
93
+ if (Math.abs(diff - 180) < Number.EPSILON) return StructureRotation.Rotate180;
94
+ if (Math.abs(diff - 270) < Number.EPSILON) return StructureRotation.Rotate270;
92
95
  return StructureRotation.None;
93
96
  }
94
97
 
@@ -9,9 +9,9 @@ import {
9
9
  InputButton,
10
10
  ItemStack,
11
11
  } from "@minecraft/server";
12
- import { NAMESPACE } from "./constants";
13
12
 
14
13
  type EventCallback<T = CustomPlayer> = (player: T) => void;
14
+ type PlayerEventType = CustomPlayerEvents | string;
15
15
 
16
16
  enum CustomPlayerEvents {
17
17
  Tick = "tick",
@@ -23,12 +23,6 @@ enum CustomPlayerEvents {
23
23
  SneakEnd = "sneakEnd",
24
24
  }
25
25
 
26
- enum PropertyIDs {
27
- OriginalNameTag = `${NAMESPACE}:original_name_tag`,
28
- }
29
-
30
- type PlayerEventType = CustomPlayerEvents | string;
31
-
32
26
  class EventGroup<T = CustomPlayer> {
33
27
  private callbacks = new Map<PlayerEventType, EventCallback<T>[]>();
34
28
 
@@ -93,7 +87,6 @@ class CustomPlayer {
93
87
  }
94
88
 
95
89
  reset() {
96
- this.player.nameTag = (this.player.getDynamicProperty(PropertyIDs.OriginalNameTag) as string | undefined) ?? this.player.nameTag;
97
90
  this.stateTick = 0;
98
91
  this.JumpTick = 0;
99
92
  this.player.camera.clear();
@@ -101,8 +94,8 @@ class CustomPlayer {
101
94
 
102
95
  tick() {
103
96
  this.stateTick++;
104
-
105
- if (this.player.inputInfo.getButtonState(InputButton.Jump) === ButtonState.Pressed && !this.player.isOnGround) {
97
+
98
+ if (this.player.inputInfo.getButtonState(InputButton.Jump) === ButtonState.Pressed) {
106
99
  this.JumpTick++;
107
100
  } else if (this.JumpTick > 0) {
108
101
  this.JumpTick = -1;
@@ -142,9 +135,6 @@ class PlayerManager<T extends CustomPlayer = CustomPlayer> {
142
135
 
143
136
  world.afterEvents.worldLoad.subscribe(() => {
144
137
  world.getAllPlayers().forEach((player) => this.addPlayer(player));
145
- });
146
-
147
- world.afterEvents.worldLoad.subscribe(() => {
148
138
  system.runInterval(() => {
149
139
  this.tick();
150
140
  });
@@ -191,4 +181,4 @@ class PlayerManager<T extends CustomPlayer = CustomPlayer> {
191
181
  }
192
182
  }
193
183
 
194
- export { CustomPlayerEvents, PropertyIDs, CustomPlayer, PlayerManager };
184
+ export { CustomPlayerEvents, CustomPlayer, PlayerManager };
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/player-event/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,iBAAiB,CAAC"}
@@ -1 +0,0 @@
1
- export const NAMESPACE = 'player-event';