Skip to content

Commit df6e5c1

Browse files
npermabot174
andauthored
Create math.js (#338)
* Create math.js * Create Readme.md * Create test.js * Update test.js * Rename Readme.md to Readme.md * Update test.js * Update test.js * Update and rename math.js to index.js * Rename test.js to tests.js * Update tests.js * Update Readme.md * Update index.js --------- Co-authored-by: bot174 <91819282+bot174@users.noreply.github.com>
1 parent 2d9988d commit df6e5c1

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

scripts/land-calculate/Readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Description
2+
This Class to Calculate Land [X,Z]
3+
4+
### Method
5+
- testInBox({ start: [x,z], end: [x,z] })
6+
- getTile({ start: [x,z], end: [x,z] })
7+
8+
check `tests.js`
9+
10+
# Credits
11+
@Nperma

scripts/land-calculate/index.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Script example for ScriptAPI
2+
// Author: Nperma <https://github.com/nperma>
3+
// Project: https://github.com/JaylyDev/ScriptAPI
4+
/**
5+
* Class for mathematical and spatial operations related to land usage.
6+
* @param {number[]} value - The initial coordinates [x, z] representing a point in space.
7+
* @constructor
8+
* @writter @Nperma
9+
*/
10+
export class MATH {
11+
constructor(value) {
12+
/**
13+
* Initial coordinates representing a point in space.
14+
* @type {number[]}
15+
*/
16+
this.value = value;
17+
}
18+
19+
/**
20+
* Check if the point is within a specified box.
21+
* @param {Object} box - The box object with start and end points defining the region.
22+
* @param {number[]} box.start - The starting coordinates [x, z] of the box.
23+
* @param {number[]} box.end - The ending coordinates [x, z] of the box.
24+
* @returns {boolean} - True if the point is inside the box, false otherwise.
25+
*/
26+
testInbox(box) {
27+
const pos = this.value;
28+
const { start, end } = box;
29+
return (
30+
pos[0] >= Math.min(start[0], end[0]) &&
31+
pos[0] <= Math.max(start[0], end[0]) &&
32+
pos[1] >= Math.min(start[1], end[1]) &&
33+
pos[1] <= Math.max(start[1], end[1])
34+
);
35+
}
36+
37+
/**
38+
* Get the center point of a specified box.
39+
* @param {Object} box - The box object with start and end points defining the region.
40+
* @param {number[]} box.start - The starting coordinates [x, z] of the box.
41+
* @param {number[]} box.end - The ending coordinates [x, z] of the box.
42+
* @returns {Object} - Object with x and z properties representing the center coordinates.
43+
*/
44+
getCenter(box) {
45+
const { start, end } = box;
46+
const centerX = (start[0] + end[0]) / 2;
47+
const centerZ = (start[1] + end[1]) / 2;
48+
return { x: centerX, z: centerZ };
49+
}
50+
51+
/**
52+
* Get information about the proximity of the point to a specified tile.
53+
* @param {Object} box - The box object with start and end points defining the tile.
54+
* @param {number[]} box.start - The starting coordinates [x, z] of the tile.
55+
* @param {number[]} box.end - The ending coordinates [x, z] of the tile.
56+
* @returns {Object} - Object with status, center, distanceToEnter, and tile properties.
57+
*/
58+
getTile(box) {
59+
const center = this.getCenter(box);
60+
61+
const status = this.testInbox(box);
62+
if (!status) {
63+
const distanceToEnterX = Math.min(
64+
Math.abs(this.value[0] - box.start[0]),
65+
Math.abs(this.value[0] - box.end[0])
66+
);
67+
68+
const distanceToEnterZ = Math.min(
69+
Math.abs(this.value[1] - box.start[1]),
70+
Math.abs(this.value[1] - box.end[1])
71+
);
72+
73+
return {
74+
status,
75+
center,
76+
distanceToEnter: Math.round(
77+
Math.sqrt(distanceToEnterX ** 2 + distanceToEnterZ ** 2)
78+
),
79+
tile: "Tile Information"
80+
};
81+
}
82+
return {
83+
status,
84+
center,
85+
distanceToEnter: 0,
86+
tile: "Tile Information"
87+
};
88+
}
89+
}

scripts/land-calculate/tests.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as mc from "@minecraft/server";
2+
import { MATH } from "./index.js";
3+
4+
const AreaLobby = { start: [200, -200], end: [-200, 200] }; // Center 0,0
5+
6+
let playerLog = {};
7+
8+
mc.system.runInterval(async () => {
9+
mc.world.getPlayers().forEach((ply) => {
10+
if (!playerLog[ply.name]) {
11+
playerLog[ply.name] = {
12+
status: "out land",
13+
};
14+
}
15+
16+
const playerLocation = new MATH([ply.location.x, ply.location.z]);
17+
18+
if (playerLocation.testInbox(AreaLobby) && playerLog[ply.name].status === "out land") {
19+
ply.sendMessage("§7You are inside this Area Lobby");
20+
playerLog[ply.name].status = "in land";
21+
} else if (!playerLocation.testInbox(AreaLobby) && playerLog[ply.name].status === "in land") {
22+
ply.sendMessage("§7You are out of this Area Lobby!!");
23+
playerLog[ply.name].status = "out land";
24+
}
25+
});
26+
});

0 commit comments

Comments
 (0)