UNPKG

@tendrock/lib

Version:

A lib under the Tendrock system for Minecraft Bedrock Script API

125 lines (124 loc) 4.79 kB
import { BlockStates, Direction, ItemStack, world } from "@minecraft/server"; import { DirectionUtils, Utils } from "../util"; import { LoopOperator } from "../enum"; import { TendrockVector3 } from "../vector"; export class ServerWorld { constructor() { this._neighborOffsetMap = new Map([ [Direction.West, TendrockVector3.create(-1, 0, 0)], [Direction.East, TendrockVector3.create(1, 0, 0)], [Direction.North, TendrockVector3.create(0, 0, -1)], [Direction.South, TendrockVector3.create(0, 0, 1)], [Direction.Down, TendrockVector3.create(0, -1, 0)], [Direction.Up, TendrockVector3.create(0, 1, 0)], ]); } setBlockState(block, stateName, state) { const permutation = block.permutation.withState(stateName, state); block.setPermutation(permutation); } neighborBlock(block, direction) { return block[DirectionUtils.getDirectionMethodName(direction)](); } neighborLocation(sourceLocation, direction) { // 4: x-- // 5: x++ // 2: z-- // 3: z++ return this._neighborOffsetMap.get(direction).addVec(sourceLocation); } /** * @static * @remarks * 获取两个位置之间的所有位置并对其执行回调 * Get all locations between two locations and execute a callback on them * @param pos1 * 第一个位置 * First position * @param pos2 * 第二个位置 * Second position * @param callback * 对每个位置执行的回调函数 * Callback function executed for each position */ forEachLocationBetween(pos1, pos2, callback) { const xStart = Math.min(pos1.x, pos2.x); const yStart = Math.min(pos1.y, pos2.y); const zStart = Math.min(pos1.z, pos2.z); const xEnd = xStart + Math.abs(pos2.x - pos1.x); const yEnd = yStart + Math.abs(pos2.y - pos1.y); const zEnd = zStart + Math.abs(pos2.z - pos1.z); for (let x = xStart; x <= xEnd; x++) { for (let y = yStart; y <= yEnd; y++) { for (let z = zStart; z <= zEnd; z++) { if (callback(new TendrockVector3(x, y, z))) return; } } } } forEachNeighborBlock(block, callback) { for (const direction of DirectionUtils.AllDirections) { const neighborBlock = block[DirectionUtils.getDirectionMethodName(direction)](); if (!(neighborBlock === null || neighborBlock === void 0 ? void 0 : neighborBlock.isValid)) continue; const loopOperator = callback(neighborBlock, direction); if (loopOperator === LoopOperator.Break) break; } } cycleBlockState(block, propertyName) { const validStates = BlockStates.get(propertyName).validValues; const currentState = block.permutation.getState(propertyName); let nextStateIndex = validStates.indexOf(currentState) + 1; if (nextStateIndex >= validStates.length) nextStateIndex = 0; this.setBlockState(block, propertyName, validStates[nextStateIndex]); } cycleBlockStateReverse(block, propertyName) { const validStates = BlockStates.get(propertyName).validValues; const currentState = block.permutation.getState(propertyName); let nextStateIndex = validStates.indexOf(currentState) - 1; if (nextStateIndex <= 0) nextStateIndex = validStates.length - 1; this.setBlockState(block, propertyName, validStates[nextStateIndex]); } spawnItem(dimension, location, itemType, amount = 1) { return dimension.spawnItem(new ItemStack(itemType, amount), location); } hasPlayerNearby(block, maxDistance) { return block.dimension.getPlayers({ location: block.location, maxDistance }).length > 0; } isAbsoluteTimeBetween(min, max) { return Utils.isBetween(world.getAbsoluteTime(), min, max); } isDayTimeBetween(min, max) { return Utils.isBetween(world.getTimeOfDay(), min, max); } isSunrise() { return this.isDayTimeBetween(22200, 24000); } isDay() { return this.isDayTimeBetween(0, 12000); } isMorning() { return this.isDayTimeBetween(0, 6000); } isAfternoon() { return this.isDayTimeBetween(6000, 12000); } isSunset() { return this.isDayTimeBetween(12000, 13800); } isNight() { return this.isDayTimeBetween(13800, 22000); } isPreMidnight() { return this.isDayTimeBetween(13800, 18000); } isAfterMidnight() { return world.getTimeOfDay() > 18000; } } export const serverWorld = new ServerWorld();