Path: blob/master/src/editor/modules/brushes/util.ts
1785 views
import { Player, BlockType, BlockTypes } from "@minecraft/server";1import { Databases } from "@notbeer-api";2import { Database } from "library/@types/classes/databaseBuilder";34class PersistenceGroupItem<T> {5private database: Database;6public readonly key: string;78constructor(database: Database, key: string) {9this.database = database;10this.key = key;11}1213get value(): T {14return this.database.data[this.key];15}1617set value(itemValue: T) {18this.database.data[this.key] = itemValue;19}2021commit() {22this.database.save();23}24}2526class PersistenceGroup {27private database: Database;2829constructor(database: Database) {30this.database = database;31}3233deleteItem(key: string) {34delete this.database.data[key];35this.database.save();36}3738getOrCreateItem<T>(key: string, item: T) {39if (!(key in this.database.data)) this.database.data[key] = item;40const itemImpl = new PersistenceGroupItem<T>(this.database, key);41itemImpl.value = item;42return itemImpl;43}4445fetchItem<T>(key: string) {46if (!(key in this.database.data)) return undefined;47return new PersistenceGroupItem<T>(this.database, key);48}4950listItemNames() {51return Object.keys(this.database.data);52}5354dispose() {55return this.database.unload();56}57}5859export class PersistenceManager {60private player: Player;6162constructor(player: Player) {63this.player = player;64}6566getOrCreateGroup(namespaceName: string) {67return new PersistenceGroup(Databases.load("editor_persistence__" + namespaceName, this.player));68}6970deleteGroup(namespaceName: string) {71return Databases.delete("editor_persistence__" + namespaceName, this.player);72}7374getGroup(namespaceName: string) {75if (Databases.find(new RegExp(`^editor_persistence__${namespaceName}$`), this.player).length === 0) {76return undefined;77}78return this.getOrCreateGroup(namespaceName);79}8081getGroups() {82const groups = Databases.find(new RegExp(`^editor_persistence__`), this.player);83const persistenceGroupList = [];84for (const group of groups) {85persistenceGroupList.push(new PersistenceGroup(Databases.load(group, this.player)));86}87return persistenceGroupList;88}8990disposeAllGroups() {91const groups = Databases.find(new RegExp(`^editor_persistence__`), this.player);92for (const group of groups) Databases.delete(group, this.player);93}94}9596export function convertBlockStringsToBlockType(blockString: Readonly<string[]>) {97const blockTypes: BlockType[] = [];98for (let blockStringElement of blockString) {99if (!blockStringElement.includes(":")) {100blockStringElement = "minecraft:" + blockStringElement;101}102const blockType = BlockTypes.get(blockStringElement);103if (blockType) {104blockTypes.push(blockType);105}106}107return blockTypes;108}109110export function convertBlockTypesToBlockStrings(blockTypes: Readonly<BlockType[]>) {111const blockStrings: string[] = [];112for (const blockType of blockTypes) {113let blockName = blockType.id;114if (blockName.startsWith("minecraft:")) {115blockName = blockName.substring(10);116}117blockStrings.push(blockName);118}119return blockStrings;120}121122interface InputMarkupProperties {123showUnset?: boolean;124ignoreFormat?: boolean;125prefix?: string;126contextId?: string;127}128129export function getInputMarkup(id: string, props?: InputMarkupProperties) {130let markupStart = "[~*input|$id=" + id;131if (props?.showUnset) {132markupStart = markupStart.concat("|showUnset");133}134if (!props || !props.ignoreFormat) {135markupStart = markupStart.concat("|$format=", props?.prefix ?? "[", "*input");136markupStart = markupStart.concat(props?.prefix ?? "]");137}138if (props?.contextId) {139markupStart = markupStart.concat("|$contextId=", props.contextId);140}141return markupStart.concat("~]");142}143144export enum RelativeDirection {145Forward,146Right,147Back,148Left,149Up,150Down,151}152153const directionLookup = {154[RelativeDirection.Forward]: { x: 0, y: 0, z: 1 },155[RelativeDirection.Right]: { x: -1, y: 0, z: 0 },156[RelativeDirection.Back]: { x: 0, y: 0, z: -1 },157[RelativeDirection.Left]: { x: 1, y: 0, z: 0 },158[RelativeDirection.Up]: { x: 0, y: 1, z: 0 },159[RelativeDirection.Down]: { x: 0, y: -1, z: 0 },160};161162export function getRotationCorrectedDirection(rotationY: number, realDirection: RelativeDirection) {163if (realDirection === RelativeDirection.Up || realDirection === RelativeDirection.Down) {164return realDirection;165}166const directionQuadrant = Math.floor(((rotationY + 405 + realDirection * 90) % 360) / 90);167return directionQuadrant;168}169170export function getRotationCorrectedDirectionVector(rotationY: number, realDirection: RelativeDirection) {171const relativeDirection = getRotationCorrectedDirection(rotationY, realDirection);172return directionLookup[relativeDirection];173}174175176