diff --git a/scripts/land-calculate/Readme.md b/scripts/land-calculate/Readme.md new file mode 100644 index 00000000..8f49f2ab --- /dev/null +++ b/scripts/land-calculate/Readme.md @@ -0,0 +1,11 @@ +# Description +This Class to Calculate Land [X,Z] + +### Method +- testInBox({ start: [x,z], end: [x,z] }) +- getTile({ start: [x,z], end: [x,z] }) + +check `tests.js` + +# Credits +@Nperma diff --git a/scripts/land-calculate/index.js b/scripts/land-calculate/index.js new file mode 100644 index 00000000..8a119bae --- /dev/null +++ b/scripts/land-calculate/index.js @@ -0,0 +1,89 @@ +// Script example for ScriptAPI +// Author: Nperma +// Project: https://github.com/JaylyDev/ScriptAPI +/** + * Class for mathematical and spatial operations related to land usage. + * @param {number[]} value - The initial coordinates [x, z] representing a point in space. + * @constructor + * @writter @Nperma + */ +export class MATH { + constructor(value) { + /** + * Initial coordinates representing a point in space. + * @type {number[]} + */ + this.value = value; + } + + /** + * Check if the point is within a specified box. + * @param {Object} box - The box object with start and end points defining the region. + * @param {number[]} box.start - The starting coordinates [x, z] of the box. + * @param {number[]} box.end - The ending coordinates [x, z] of the box. + * @returns {boolean} - True if the point is inside the box, false otherwise. + */ + testInbox(box) { + const pos = this.value; + const { start, end } = box; + return ( + pos[0] >= Math.min(start[0], end[0]) && + pos[0] <= Math.max(start[0], end[0]) && + pos[1] >= Math.min(start[1], end[1]) && + pos[1] <= Math.max(start[1], end[1]) + ); + } + + /** + * Get the center point of a specified box. + * @param {Object} box - The box object with start and end points defining the region. + * @param {number[]} box.start - The starting coordinates [x, z] of the box. + * @param {number[]} box.end - The ending coordinates [x, z] of the box. + * @returns {Object} - Object with x and z properties representing the center coordinates. + */ + getCenter(box) { + const { start, end } = box; + const centerX = (start[0] + end[0]) / 2; + const centerZ = (start[1] + end[1]) / 2; + return { x: centerX, z: centerZ }; + } + + /** + * Get information about the proximity of the point to a specified tile. + * @param {Object} box - The box object with start and end points defining the tile. + * @param {number[]} box.start - The starting coordinates [x, z] of the tile. + * @param {number[]} box.end - The ending coordinates [x, z] of the tile. + * @returns {Object} - Object with status, center, distanceToEnter, and tile properties. + */ + getTile(box) { + const center = this.getCenter(box); + + const status = this.testInbox(box); + if (!status) { + const distanceToEnterX = Math.min( + Math.abs(this.value[0] - box.start[0]), + Math.abs(this.value[0] - box.end[0]) + ); + + const distanceToEnterZ = Math.min( + Math.abs(this.value[1] - box.start[1]), + Math.abs(this.value[1] - box.end[1]) + ); + + return { + status, + center, + distanceToEnter: Math.round( + Math.sqrt(distanceToEnterX ** 2 + distanceToEnterZ ** 2) + ), + tile: "Tile Information" + }; + } + return { + status, + center, + distanceToEnter: 0, + tile: "Tile Information" + }; + } + } diff --git a/scripts/land-calculate/tests.js b/scripts/land-calculate/tests.js new file mode 100644 index 00000000..975a7ac9 --- /dev/null +++ b/scripts/land-calculate/tests.js @@ -0,0 +1,26 @@ +import * as mc from "@minecraft/server"; +import { MATH } from "./index.js"; + +const AreaLobby = { start: [200, -200], end: [-200, 200] }; // Center 0,0 + +let playerLog = {}; + +mc.system.runInterval(async () => { + mc.world.getPlayers().forEach((ply) => { + if (!playerLog[ply.name]) { + playerLog[ply.name] = { + status: "out land", + }; + } + + const playerLocation = new MATH([ply.location.x, ply.location.z]); + + if (playerLocation.testInbox(AreaLobby) && playerLog[ply.name].status === "out land") { + ply.sendMessage("§7You are inside this Area Lobby"); + playerLog[ply.name].status = "in land"; + } else if (!playerLocation.testInbox(AreaLobby) && playerLog[ply.name].status === "in land") { + ply.sendMessage("§7You are out of this Area Lobby!!"); + playerLog[ply.name].status = "out land"; + } + }); +});