@starktma/minecraft-utils 1.2.0 → 1.2.2
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 +26 -0
- package/dist/database/database.d.ts.map +1 -1
- package/dist/minecraft/index.d.ts.map +1 -1
- package/dist/player-event/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/database/database.ts +1 -1
- package/src/minecraft/index.ts +11 -8
- package/src/player-event/index.ts +4 -14
- package/dist/player-event/constants.d.ts.map +0 -1
- package/src/player-event/constants.ts +0 -1
package/README.md
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @starktma/minecraft-utils — utilities for Minecraft scripting
|
|
2
|
+
|
|
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
|
+
|
|
5
|
+
## Quick summary
|
|
6
|
+
|
|
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`.
|
|
11
|
+
|
|
12
|
+
## Short database note
|
|
13
|
+
|
|
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`.
|
|
15
|
+
|
|
16
|
+
## Installation & quick dev
|
|
17
|
+
|
|
18
|
+
- **To use the published package**: `npm install @starktma/minecraft-utils` (if published to npm or a registry).
|
|
19
|
+
|
|
20
|
+
## Where to look next
|
|
21
|
+
|
|
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
|
|
@@ -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;
|
|
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,CAAC,EAAE,MAAM,GAAG,SAAS;IAYvE;;;;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"}
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
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,CAKnD;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,MAAM,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAQ1F;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"}
|
|
@@ -1 +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;
|
|
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;AAE3B,KAAK,aAAa,CAAC,CAAC,GAAG,YAAY,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,IAAI,CAAC;AAC3D,KAAK,eAAe,GAAG,kBAAkB,GAAG,MAAM,CAAC;AAEnD,aAAK,kBAAkB;IACtB,IAAI,SAAS;IACb,SAAS,cAAc;IACvB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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
|
|
170
|
+
protected constructor(databaseName: string, target?: Entity | undefined) {
|
|
171
171
|
this.mainDB = new DatabaseManager(target);
|
|
172
172
|
this.databaseName = databaseName;
|
|
173
173
|
|
package/src/minecraft/index.ts
CHANGED
|
@@ -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
|
-
|
|
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,
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (Math.abs(diff -
|
|
91
|
-
if (Math.abs(diff -
|
|
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
|
|
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,
|
|
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';
|