@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
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts an angle in degrees to radians.
|
|
3
|
+
*
|
|
4
|
+
* @param degrees - The angle in degrees to be converted.
|
|
5
|
+
* @returns The angle in radians.
|
|
6
|
+
*/
|
|
7
|
+
export function toRadians(degrees) {
|
|
8
|
+
return degrees * (Math.PI / 180);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Converts an angle in radians to degrees.
|
|
12
|
+
*
|
|
13
|
+
* @param radians - The angle in radians to be converted.
|
|
14
|
+
* @returns The angle in degrees.
|
|
15
|
+
*/
|
|
16
|
+
export function calculateDistance(position1, position2) {
|
|
17
|
+
return Math.sqrt(Math.pow(position1.x - position2.x, 2) + Math.pow(position1.y - position2.y, 2) + Math.pow(position1.z - position2.z, 2));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Converts an angle in radians to degrees.
|
|
21
|
+
*
|
|
22
|
+
* @param radians - The angle in radians to be converted.
|
|
23
|
+
* @returns The angle in degrees.
|
|
24
|
+
*/
|
|
25
|
+
export function toDegrees(radians) {
|
|
26
|
+
return radians * (180 / Math.PI);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Converts a hex color string to an RGBA object.
|
|
30
|
+
*
|
|
31
|
+
* @param hex - The hex color string, which can be in the formats #RGB, #RGBA, #RRGGBB, or #RRGGBBAA.
|
|
32
|
+
* @returns An object containing the red, green, blue, and alpha components as numbers between 0 and 1.
|
|
33
|
+
* @throws Error if the hex string is invalid.
|
|
34
|
+
*/
|
|
35
|
+
export function hexToRgba(hex, stripAlpha = false) {
|
|
36
|
+
if (!/^#([a-fA-F0-9]{4}|[a-fA-F0-9]{8}|[a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(hex)) {
|
|
37
|
+
throw new Error("Invalid hex color");
|
|
38
|
+
}
|
|
39
|
+
let normalized = hex.slice(1);
|
|
40
|
+
if (normalized.length === 3 || normalized.length === 4) {
|
|
41
|
+
normalized = normalized
|
|
42
|
+
.split("")
|
|
43
|
+
.map((c) => c + c)
|
|
44
|
+
.join("");
|
|
45
|
+
}
|
|
46
|
+
const red = parseInt(normalized.substring(0, 2), 16) / 255;
|
|
47
|
+
const green = parseInt(normalized.substring(2, 4), 16) / 255;
|
|
48
|
+
const blue = parseInt(normalized.substring(4, 6), 16) / 255;
|
|
49
|
+
const alpha = (normalized.length === 8 ? parseInt(normalized.substring(6, 8), 16) : 255) / 255;
|
|
50
|
+
if (stripAlpha) {
|
|
51
|
+
return { red, green, blue };
|
|
52
|
+
}
|
|
53
|
+
return { red, green, blue, alpha };
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Clamps a value between a minimum and maximum range.
|
|
57
|
+
*
|
|
58
|
+
* @param value - The value to be clamped.
|
|
59
|
+
* @param min - The minimum value of the range.
|
|
60
|
+
* @param max - The maximum value of the range.
|
|
61
|
+
* @returns The clamped value, which will be between `min` and `max`.
|
|
62
|
+
*/
|
|
63
|
+
export function clamp(value, min, max) {
|
|
64
|
+
return Math.max(min, Math.min(max, value));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Converts an angle in degrees to its signed equivalent in the range [-180, 180].
|
|
68
|
+
*
|
|
69
|
+
* @param angle - The angle in degrees to be converted.
|
|
70
|
+
* @returns The signed angle in degrees, normalized to the range [-180, 180].
|
|
71
|
+
*/
|
|
72
|
+
export function toSigned(angle) {
|
|
73
|
+
const wrapped = ((angle % 360) + 360) % 360;
|
|
74
|
+
return wrapped > 180 ? wrapped - 360 : wrapped;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Converts an angle in degrees to its unsigned equivalent in the range [0, 360].
|
|
78
|
+
*
|
|
79
|
+
* @param angle - The angle in degrees to be converted.
|
|
80
|
+
* @returns The unsigned angle in degrees, normalized to the range [0, 360].
|
|
81
|
+
*/
|
|
82
|
+
export function toUnsigned(angle) {
|
|
83
|
+
return ((angle % 360) + 360) % 360;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Calculates the shortest signed arc between two angles in degrees.
|
|
87
|
+
*
|
|
88
|
+
* @param from - The starting angle in degrees.
|
|
89
|
+
* @param to - The target angle in degrees.
|
|
90
|
+
* @returns The shortest arc in degrees from `from` to `to`.
|
|
91
|
+
*/
|
|
92
|
+
export function shortestArc(from, to) {
|
|
93
|
+
return -toSigned((to - from + 360) % 360);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Checks if two positions are equal within a specified tolerance.
|
|
97
|
+
*
|
|
98
|
+
* @param a - The first position.
|
|
99
|
+
* @param b - The second position.
|
|
100
|
+
* @param tolerance - The tolerance for comparison (default is 0.5).
|
|
101
|
+
* @returns `true` if the positions are equal within the tolerance, otherwise `false`.
|
|
102
|
+
*/
|
|
103
|
+
export function positionsEqual(a, b, tolerance = 0.5) {
|
|
104
|
+
return Math.abs(a.x - b.x) < tolerance && Math.abs(a.y - b.y) < tolerance && Math.abs(a.z - b.z) < tolerance;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Checks if a value is within a specified range.
|
|
108
|
+
*
|
|
109
|
+
* @param value - The value to check.
|
|
110
|
+
* @param min - The minimum value of the range.
|
|
111
|
+
* @param max - The maximum value of the range.
|
|
112
|
+
* @returns `true` if the value is within the range [min, max], otherwise `false`.
|
|
113
|
+
*/
|
|
114
|
+
export function inRange(value, min, max) {
|
|
115
|
+
return value >= min && value <= max;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/math/index.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,OAAe;IACxC,OAAO,OAAO,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,CAAC,CAAC;AAClC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,SAAkB,EAAE,SAAkB;IACvE,OAAO,IAAI,CAAC,IAAI,CACf,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CACxH,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SA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G,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;AAC9G,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,OAAO,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW;IAC9D,OAAO,KAAK,IAAI,GAAG,IAAI,KAAK,IAAI,GAAG,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Player, Entity, Block, Vector3, StructureRotation, Vector2 } from "@minecraft/server";
|
|
2
|
+
export declare function getBlocksInASphere(centerBlock: Block | Entity, radius: number, innerRadius?: number): Block[];
|
|
3
|
+
export declare function displayActionbar(player: Player | undefined, ...message: any): void;
|
|
4
|
+
/**
|
|
5
|
+
* Snaps an angle to the nearest grid size.
|
|
6
|
+
*
|
|
7
|
+
* @param angle - The angle in degrees to be snapped.
|
|
8
|
+
* @returns The snapped unsigned angle in degrees.
|
|
9
|
+
*/
|
|
10
|
+
export declare function snapYawToGrid(angle: number): number;
|
|
11
|
+
/**
|
|
12
|
+
* Snaps a vector to the nearest integer coordinates.
|
|
13
|
+
*
|
|
14
|
+
* @param v - The vector to be snapped.
|
|
15
|
+
* @returns A new vector with each component rounded to the nearest integer.
|
|
16
|
+
*/
|
|
17
|
+
export declare function snapLocationToGrid(location: Vector3, yaw: Vector2, gridSize?: number): Vector3;
|
|
18
|
+
export declare function getStructureRotationEnum(angle: number, offset?: number): StructureRotation;
|
|
19
|
+
export declare function getRelativeMovementDirection(player: Player, round: boolean): Vector3;
|
|
20
|
+
export declare function restoreInventory(entity: Entity, id: string): void;
|
|
21
|
+
export declare function saveInventory(entity: Entity, id: string, clearAll?: boolean): number;
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { Block, world, StructureRotation, BlockInventoryComponent, BlockPermutation, EntityEquippableComponent, EntityInventoryComponent, EquipmentSlot, ItemStack, } from "@minecraft/server";
|
|
2
|
+
import { calculateDistance, toRadians, toUnsigned } from "../math";
|
|
3
|
+
export function getBlocksInASphere(centerBlock, radius, innerRadius) {
|
|
4
|
+
if (centerBlock) {
|
|
5
|
+
if (centerBlock instanceof Block && !centerBlock.isValid)
|
|
6
|
+
return [];
|
|
7
|
+
let center = centerBlock.location;
|
|
8
|
+
let blocks = [];
|
|
9
|
+
for (let x = center.x - radius; x < center.x + radius; x++) {
|
|
10
|
+
for (let y = center.y - radius; y < center.y + radius; y++) {
|
|
11
|
+
for (let z = center.z - radius; z < center.z + radius; z++) {
|
|
12
|
+
try {
|
|
13
|
+
let block = centerBlock.dimension.getBlock({ x: x, y: y, z: z });
|
|
14
|
+
if (block &&
|
|
15
|
+
block.isValid &&
|
|
16
|
+
!block.isAir &&
|
|
17
|
+
!block.permutation.matches("minecraft:bedrock") &&
|
|
18
|
+
!block.permutation.matches("minecraft:barrier") &&
|
|
19
|
+
calculateDistance(center, block.location) <= radius &&
|
|
20
|
+
(!innerRadius || calculateDistance(center, block.location) >= innerRadius)) {
|
|
21
|
+
blocks.push(block);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch (e) { }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return blocks;
|
|
29
|
+
}
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
export function displayActionbar(player, ...message) {
|
|
33
|
+
const text = message.join(" ");
|
|
34
|
+
let target;
|
|
35
|
+
let selector;
|
|
36
|
+
target = player && player.isValid ? player : world.getDimension("overworld");
|
|
37
|
+
selector = player ? `@s` : `@a`;
|
|
38
|
+
target.runCommand(`title ${selector} actionbar ${JSON.stringify(text)}`);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Snaps an angle to the nearest grid size.
|
|
42
|
+
*
|
|
43
|
+
* @param angle - The angle in degrees to be snapped.
|
|
44
|
+
* @returns The snapped unsigned angle in degrees.
|
|
45
|
+
*/
|
|
46
|
+
export function snapYawToGrid(angle) {
|
|
47
|
+
const gridSize = 90;
|
|
48
|
+
// Round to nearest grid step, then normalize into [0, 360)
|
|
49
|
+
const snapped = Math.round(toUnsigned(angle) / gridSize) * gridSize;
|
|
50
|
+
return ((snapped % 360) + 360) % 360;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Snaps a vector to the nearest integer coordinates.
|
|
54
|
+
*
|
|
55
|
+
* @param v - The vector to be snapped.
|
|
56
|
+
* @returns A new vector with each component rounded to the nearest integer.
|
|
57
|
+
*/
|
|
58
|
+
export function snapLocationToGrid(location, yaw, gridSize = 1) {
|
|
59
|
+
const snappedYaw = snapYawToGrid(yaw.y);
|
|
60
|
+
const dx = Math.round(Math.sin(snappedYaw));
|
|
61
|
+
const dz = Math.round(Math.cos(snappedYaw));
|
|
62
|
+
return {
|
|
63
|
+
x: Math.floor(location.x / gridSize) * gridSize,
|
|
64
|
+
y: Math.floor(location.y / gridSize) * gridSize,
|
|
65
|
+
z: Math.floor(location.z / gridSize) * gridSize,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export function getStructureRotationEnum(angle, offset) {
|
|
69
|
+
const diff = snapYawToGrid(toUnsigned(angle - (offset ?? 0)));
|
|
70
|
+
console.log(diff);
|
|
71
|
+
if (Math.abs(diff - 90) < Number.EPSILON)
|
|
72
|
+
return StructureRotation.Rotate90;
|
|
73
|
+
if (Math.abs(diff - 180) < Number.EPSILON)
|
|
74
|
+
return StructureRotation.Rotate180;
|
|
75
|
+
if (Math.abs(diff - 270) < Number.EPSILON)
|
|
76
|
+
return StructureRotation.Rotate270;
|
|
77
|
+
return StructureRotation.None;
|
|
78
|
+
}
|
|
79
|
+
export function getRelativeMovementDirection(player, round) {
|
|
80
|
+
const playerYaw = toRadians(player.getRotation().y);
|
|
81
|
+
const { sin, cos } = { sin: Math.sin(playerYaw), cos: Math.cos(playerYaw) };
|
|
82
|
+
const playerMovement = player.inputInfo.getMovementVector();
|
|
83
|
+
const dx = -sin * playerMovement.y + cos * playerMovement.x;
|
|
84
|
+
const dz = cos * playerMovement.y + sin * playerMovement.x;
|
|
85
|
+
if (round) {
|
|
86
|
+
return { x: Math.round(dx), y: 0, z: Math.round(dz) };
|
|
87
|
+
}
|
|
88
|
+
return { x: dx, y: 0, z: dz };
|
|
89
|
+
}
|
|
90
|
+
export function restoreInventory(entity, id) {
|
|
91
|
+
const blockLocation = entity.dimension.getBlockFromRay(entity.location, { x: 0, y: -1, z: 0 }).block.location;
|
|
92
|
+
blockLocation.y++;
|
|
93
|
+
const blockLocation2 = { ...blockLocation, y: blockLocation.y + 1 };
|
|
94
|
+
const savedStructure = world.structureManager.get(id);
|
|
95
|
+
if (savedStructure)
|
|
96
|
+
world.structureManager.place(savedStructure, entity.dimension, blockLocation);
|
|
97
|
+
world.structureManager.delete(id);
|
|
98
|
+
const block = entity.dimension.getBlock(blockLocation);
|
|
99
|
+
const block2 = entity.dimension.getBlock(blockLocation2);
|
|
100
|
+
const equipment = entity.getComponent(EntityEquippableComponent.componentId);
|
|
101
|
+
const invComponent = entity.getComponent(EntityInventoryComponent.componentId);
|
|
102
|
+
const blockInventory = block.getComponent(BlockInventoryComponent.componentId);
|
|
103
|
+
const blockInventory2 = block2.getComponent(BlockInventoryComponent.componentId);
|
|
104
|
+
const slots = Object.values(EquipmentSlot);
|
|
105
|
+
slots.forEach((slot) => {
|
|
106
|
+
equipment.setEquipment(slot, blockInventory.container.getItem(slots.indexOf(slot)));
|
|
107
|
+
});
|
|
108
|
+
Array(41)
|
|
109
|
+
.fill(0)
|
|
110
|
+
.forEach((_, i) => {
|
|
111
|
+
if (i < invComponent.container.size) {
|
|
112
|
+
if (i < 9) {
|
|
113
|
+
// First chest: starts filling from slot 5
|
|
114
|
+
blockInventory.container.moveItem(i + 5, i, invComponent.container);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
// Second chest: adjusts index for 0-26 range starting from slot 0
|
|
118
|
+
blockInventory2.container.moveItem(i - 9, i, invComponent.container);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
blockInventory.container.clearAll();
|
|
123
|
+
blockInventory2.container.clearAll();
|
|
124
|
+
block.setPermutation(BlockPermutation.resolve("minecraft:air"));
|
|
125
|
+
block2.setPermutation(BlockPermutation.resolve("minecraft:air"));
|
|
126
|
+
}
|
|
127
|
+
export function saveInventory(entity, id, clearAll = false) {
|
|
128
|
+
const blockLocation = entity.dimension.getTopmostBlock(entity.location).location;
|
|
129
|
+
blockLocation.y++;
|
|
130
|
+
const blockLocation2 = { ...blockLocation, y: blockLocation.y + 1 };
|
|
131
|
+
const block = entity.dimension.getBlock(blockLocation);
|
|
132
|
+
const block2 = entity.dimension.getBlock(blockLocation2);
|
|
133
|
+
block.setPermutation(BlockPermutation.resolve("minecraft:chest"));
|
|
134
|
+
block2.setPermutation(BlockPermutation.resolve("minecraft:chest"));
|
|
135
|
+
const equipment = entity.getComponent(EntityEquippableComponent.componentId);
|
|
136
|
+
const invComponent = entity.getComponent(EntityInventoryComponent.componentId);
|
|
137
|
+
const blockInventory = block.getComponent(BlockInventoryComponent.componentId);
|
|
138
|
+
const blockInventory2 = block2.getComponent(BlockInventoryComponent.componentId);
|
|
139
|
+
const itemCount = invComponent.container.size - invComponent.container.emptySlotsCount;
|
|
140
|
+
Object.values(EquipmentSlot).forEach((slot) => {
|
|
141
|
+
if (clearAll) {
|
|
142
|
+
equipment.setEquipment(slot);
|
|
143
|
+
}
|
|
144
|
+
blockInventory.container.addItem(equipment.getEquipment(slot) ?? new ItemStack("minecraft:air", 1));
|
|
145
|
+
});
|
|
146
|
+
if (clearAll) {
|
|
147
|
+
}
|
|
148
|
+
Array(41)
|
|
149
|
+
.fill(0)
|
|
150
|
+
.forEach((_, i) => {
|
|
151
|
+
if (i < invComponent.container.size) {
|
|
152
|
+
if (i < 9) {
|
|
153
|
+
// First chest: starts filling from slot 5
|
|
154
|
+
invComponent.container.moveItem(i, 5 + i, blockInventory.container);
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
// Second chest: adjusts index for 0-26 range starting from slot 0
|
|
158
|
+
invComponent.container.moveItem(i, i - 9, blockInventory2.container);
|
|
159
|
+
}
|
|
160
|
+
if (clearAll) {
|
|
161
|
+
invComponent.container.setItem(i);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
world.structureManager.delete(id);
|
|
166
|
+
world.structureManager.createFromWorld(id, entity.dimension, blockLocation, blockLocation2);
|
|
167
|
+
blockInventory.container.clearAll();
|
|
168
|
+
blockInventory2.container.clearAll();
|
|
169
|
+
block.setPermutation(BlockPermutation.resolve("minecraft:air"));
|
|
170
|
+
block2.setPermutation(BlockPermutation.resolve("minecraft:air"));
|
|
171
|
+
return itemCount;
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/minecraft/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,EAEL,KAAK,EACL,iBAAiB,EAEjB,uBAAuB,EACvB,gBAAgB,EAChB,yBAAyB,EACzB,wBAAwB,EACxB,aAAa,EACb,SAAS,GACT,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAY,UAAU,EAAE,MAAM,SAAS,CAAC;AAE7E,MAAM,UAAU,kBAAkB,CAAC,WAA2B,EAAE,MAAc,EAAE,WAAoB;IACnG,IAAI,WAAW,EAAE,CAAC;QACjB,IAAI,WAAW,YAAY,KAAK,IAAI,CAAC,WAAW,CAAC,OAAO;YAAE,OAAO,EAAE,CAAC;QACpE,IAAI,MAAM,GAAG,WAAW,CAAC,QAAQ,CAAC;QAClC,IAAI,MAAM,GAAY,EAAE,CAAC;QACzB,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,MAAM,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,MAAM,EAAE,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E,CAAC;IACrC,eAAe,CAAC,SAAU,CAAC,QAAQ,EAAE,CAAC;IAEtC,KAAK,CAAC,cAAc,CAAC,gBAAgB,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC;IAChE,MAAM,CAAC,cAAc,CAAC,gBAAgB,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC;IAEjE,OAAO,SAAS,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Player, EntityEquippableComponent, EntityInventoryComponent, ItemStack } from "@minecraft/server";
|
|
2
|
+
type EventCallback<T = CustomPlayer> = (player: T) => void;
|
|
3
|
+
type PlayerEventType = CustomPlayerEvents | string;
|
|
4
|
+
declare enum CustomPlayerEvents {
|
|
5
|
+
Tick = "tick",
|
|
6
|
+
JumpStart = "jumpStart",
|
|
7
|
+
JumpHold = "jumpHold",
|
|
8
|
+
JumpEnd = "jumpEnd",
|
|
9
|
+
SneakStart = "sneakStart",
|
|
10
|
+
SneakHold = "sneakHold",
|
|
11
|
+
SneakEnd = "sneakEnd"
|
|
12
|
+
}
|
|
13
|
+
declare class EventGroup<T = CustomPlayer> {
|
|
14
|
+
private callbacks;
|
|
15
|
+
constructor();
|
|
16
|
+
private run;
|
|
17
|
+
trigger(type: PlayerEventType, player: T): void;
|
|
18
|
+
on(type: PlayerEventType, callback: EventCallback<T>): void;
|
|
19
|
+
off(type: PlayerEventType, callback: EventCallback<T>): void;
|
|
20
|
+
}
|
|
21
|
+
declare class CustomPlayer {
|
|
22
|
+
player: Player;
|
|
23
|
+
stateTick: number;
|
|
24
|
+
JumpTick: number;
|
|
25
|
+
SneakTick: number;
|
|
26
|
+
constructor(player: Player);
|
|
27
|
+
get equippableComponent(): EntityEquippableComponent;
|
|
28
|
+
get inventoryComponent(): EntityInventoryComponent;
|
|
29
|
+
getEquippedItem(): ItemStack | undefined;
|
|
30
|
+
reset(): void;
|
|
31
|
+
tick(): void;
|
|
32
|
+
}
|
|
33
|
+
declare class PlayerManager<T extends CustomPlayer = CustomPlayer> {
|
|
34
|
+
private players;
|
|
35
|
+
private events;
|
|
36
|
+
protected constructor();
|
|
37
|
+
protected createPlayerManager(player: Player): T;
|
|
38
|
+
getPlayer(id: string): T | undefined;
|
|
39
|
+
removePlayer(id: string): void;
|
|
40
|
+
addPlayer(player: Player): void;
|
|
41
|
+
registerEvents(eventID: string): {
|
|
42
|
+
event: EventGroup<T>;
|
|
43
|
+
};
|
|
44
|
+
getEvents(eventID: string): EventGroup<T> | undefined;
|
|
45
|
+
tick(): void;
|
|
46
|
+
}
|
|
47
|
+
export { CustomPlayerEvents, CustomPlayer, PlayerManager };
|
|
48
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { system, world, ButtonState, EntityEquippableComponent, EntityInventoryComponent, EquipmentSlot, InputButton, } from "@minecraft/server";
|
|
2
|
+
var CustomPlayerEvents;
|
|
3
|
+
(function (CustomPlayerEvents) {
|
|
4
|
+
CustomPlayerEvents["Tick"] = "tick";
|
|
5
|
+
CustomPlayerEvents["JumpStart"] = "jumpStart";
|
|
6
|
+
CustomPlayerEvents["JumpHold"] = "jumpHold";
|
|
7
|
+
CustomPlayerEvents["JumpEnd"] = "jumpEnd";
|
|
8
|
+
CustomPlayerEvents["SneakStart"] = "sneakStart";
|
|
9
|
+
CustomPlayerEvents["SneakHold"] = "sneakHold";
|
|
10
|
+
CustomPlayerEvents["SneakEnd"] = "sneakEnd";
|
|
11
|
+
})(CustomPlayerEvents || (CustomPlayerEvents = {}));
|
|
12
|
+
class EventGroup {
|
|
13
|
+
callbacks = new Map();
|
|
14
|
+
constructor() {
|
|
15
|
+
// Initialize with core events
|
|
16
|
+
Object.values(CustomPlayerEvents).forEach((event) => {
|
|
17
|
+
this.callbacks.set(event, []);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
run(callbacks, player) {
|
|
21
|
+
for (const cb of callbacks)
|
|
22
|
+
cb(player);
|
|
23
|
+
}
|
|
24
|
+
trigger(type, player) {
|
|
25
|
+
const callbacks = this.callbacks.get(type);
|
|
26
|
+
if (callbacks) {
|
|
27
|
+
this.run(callbacks, player);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
on(type, callback) {
|
|
31
|
+
if (!this.callbacks.has(type)) {
|
|
32
|
+
this.callbacks.set(type, []);
|
|
33
|
+
}
|
|
34
|
+
this.callbacks.get(type).push(callback);
|
|
35
|
+
}
|
|
36
|
+
// Simple way to remove listeners
|
|
37
|
+
off(type, callback) {
|
|
38
|
+
const callbacks = this.callbacks.get(type);
|
|
39
|
+
if (callbacks) {
|
|
40
|
+
const index = callbacks.indexOf(callback);
|
|
41
|
+
if (index > -1) {
|
|
42
|
+
callbacks.splice(index, 1);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
class CustomPlayer {
|
|
48
|
+
player;
|
|
49
|
+
stateTick = 0;
|
|
50
|
+
JumpTick = 0;
|
|
51
|
+
SneakTick = 0;
|
|
52
|
+
constructor(player) {
|
|
53
|
+
this.player = player;
|
|
54
|
+
this.reset();
|
|
55
|
+
}
|
|
56
|
+
get equippableComponent() {
|
|
57
|
+
return this.player.getComponent(EntityEquippableComponent.componentId);
|
|
58
|
+
}
|
|
59
|
+
get inventoryComponent() {
|
|
60
|
+
return this.player.getComponent(EntityInventoryComponent.componentId);
|
|
61
|
+
}
|
|
62
|
+
getEquippedItem() {
|
|
63
|
+
return this.equippableComponent.getEquipment(EquipmentSlot.Mainhand);
|
|
64
|
+
}
|
|
65
|
+
reset() {
|
|
66
|
+
this.stateTick = 0;
|
|
67
|
+
this.JumpTick = 0;
|
|
68
|
+
this.player.camera.clear();
|
|
69
|
+
}
|
|
70
|
+
tick() {
|
|
71
|
+
this.stateTick++;
|
|
72
|
+
if (this.player.inputInfo.getButtonState(InputButton.Jump) === ButtonState.Pressed) {
|
|
73
|
+
this.JumpTick++;
|
|
74
|
+
}
|
|
75
|
+
else if (this.JumpTick > 0) {
|
|
76
|
+
this.JumpTick = -1;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
this.JumpTick = 0;
|
|
80
|
+
}
|
|
81
|
+
if (this.player.inputInfo.getButtonState(InputButton.Sneak) === ButtonState.Pressed) {
|
|
82
|
+
this.SneakTick++;
|
|
83
|
+
}
|
|
84
|
+
else if (this.SneakTick > 0) {
|
|
85
|
+
this.SneakTick = -1;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
this.SneakTick = 0;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
class PlayerManager {
|
|
93
|
+
players = new Map();
|
|
94
|
+
events = new Map();
|
|
95
|
+
constructor() {
|
|
96
|
+
world.afterEvents.playerSpawn.subscribe(({ player, initialSpawn }) => {
|
|
97
|
+
const id = player.id;
|
|
98
|
+
const existing = this.getPlayer(id);
|
|
99
|
+
if (initialSpawn || !existing) {
|
|
100
|
+
this.addPlayer(player);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
existing.reset();
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
world.afterEvents.playerLeave.subscribe(({ playerId }) => {
|
|
107
|
+
this.removePlayer(playerId);
|
|
108
|
+
});
|
|
109
|
+
world.afterEvents.worldLoad.subscribe(() => {
|
|
110
|
+
world.getAllPlayers().forEach((player) => this.addPlayer(player));
|
|
111
|
+
system.runInterval(() => {
|
|
112
|
+
this.tick();
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
createPlayerManager(player) {
|
|
117
|
+
return new CustomPlayer(player);
|
|
118
|
+
}
|
|
119
|
+
getPlayer(id) {
|
|
120
|
+
return this.players.get(id);
|
|
121
|
+
}
|
|
122
|
+
removePlayer(id) {
|
|
123
|
+
this.players.delete(id);
|
|
124
|
+
}
|
|
125
|
+
addPlayer(player) {
|
|
126
|
+
if (this.players.has(player.id))
|
|
127
|
+
return;
|
|
128
|
+
this.players.set(player.id, this.createPlayerManager(player));
|
|
129
|
+
}
|
|
130
|
+
registerEvents(eventID) {
|
|
131
|
+
const eventGroup = new EventGroup();
|
|
132
|
+
this.events.set(eventID, eventGroup);
|
|
133
|
+
return { event: eventGroup };
|
|
134
|
+
}
|
|
135
|
+
getEvents(eventID) {
|
|
136
|
+
return this.events.get(eventID);
|
|
137
|
+
}
|
|
138
|
+
tick() {
|
|
139
|
+
for (const manager of this.players.values()) {
|
|
140
|
+
const { player } = manager;
|
|
141
|
+
if (!player.isValid)
|
|
142
|
+
continue;
|
|
143
|
+
manager.tick();
|
|
144
|
+
this.events.forEach((eventGroup) => {
|
|
145
|
+
eventGroup.trigger(CustomPlayerEvents.Tick, manager);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
export { CustomPlayerEvents, CustomPlayer, PlayerManager };
|
|
151
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/player-event/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,MAAM,EACN,KAAK,EAEL,WAAW,EACX,yBAAyB,EACzB,wBAAwB,EACxB,aAAa,EACb,WAAW,GAEX,MAAM,mBAAmB,CAAC;AAK3B,IAAK,kBAQJ;AARD,WAAK,kBAAkB;IACtB,mCAAa,CAAA;IACb,6CAAuB,CAAA;IACvB,2CAAqB,CAAA;IACrB,yCAAmB,CAAA;IACnB,+CAAyB,CAAA;IACzB,6CAAuB,CAAA;IACvB,2CAAqB,CAAA;AACtB,CAAC,EARI,kBAAkB,KAAlB,kBAAkB,QAQtB;AAED,MAAM,UAAU;IACP,SAAS,GAAG,IAAI,GAAG,EAAuC,CAAC;IAEnE;QACC,8BAA8B;QAC9B,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;YACnD,IAAI,C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AAE,aAAa,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@starktma/minecraft-utils",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"license": "ISC",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"scripts": {
|
|
12
|
-
"build": "npx -p typescript tsc --project tsconfig.json"
|
|
12
|
+
"build": "npx -p typescript tsc --project tsconfig.json",
|
|
13
|
+
"prepare": "npm run build"
|
|
13
14
|
},
|
|
14
15
|
"exports": {
|
|
15
16
|
".": {
|
|
@@ -36,6 +37,7 @@
|
|
|
36
37
|
"publishConfig": {
|
|
37
38
|
"access": "public"
|
|
38
39
|
},
|
|
40
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
39
41
|
"devDependencies": {
|
|
40
42
|
"@minecraft/server": "^2.1.0",
|
|
41
43
|
"@minecraft/server-ui": "^2.0.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const NAMESPACE = 'inventory-manager';
|