Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sisilicon
GitHub Repository: sisilicon/worldedit-be
Path: blob/master/src/editor/modules/brushes/util.ts
1785 views
1
import { Player, BlockType, BlockTypes } from "@minecraft/server";
2
import { Databases } from "@notbeer-api";
3
import { Database } from "library/@types/classes/databaseBuilder";
4
5
class PersistenceGroupItem<T> {
6
private database: Database;
7
public readonly key: string;
8
9
constructor(database: Database, key: string) {
10
this.database = database;
11
this.key = key;
12
}
13
14
get value(): T {
15
return this.database.data[this.key];
16
}
17
18
set value(itemValue: T) {
19
this.database.data[this.key] = itemValue;
20
}
21
22
commit() {
23
this.database.save();
24
}
25
}
26
27
class PersistenceGroup {
28
private database: Database;
29
30
constructor(database: Database) {
31
this.database = database;
32
}
33
34
deleteItem(key: string) {
35
delete this.database.data[key];
36
this.database.save();
37
}
38
39
getOrCreateItem<T>(key: string, item: T) {
40
if (!(key in this.database.data)) this.database.data[key] = item;
41
const itemImpl = new PersistenceGroupItem<T>(this.database, key);
42
itemImpl.value = item;
43
return itemImpl;
44
}
45
46
fetchItem<T>(key: string) {
47
if (!(key in this.database.data)) return undefined;
48
return new PersistenceGroupItem<T>(this.database, key);
49
}
50
51
listItemNames() {
52
return Object.keys(this.database.data);
53
}
54
55
dispose() {
56
return this.database.unload();
57
}
58
}
59
60
export class PersistenceManager {
61
private player: Player;
62
63
constructor(player: Player) {
64
this.player = player;
65
}
66
67
getOrCreateGroup(namespaceName: string) {
68
return new PersistenceGroup(Databases.load("editor_persistence__" + namespaceName, this.player));
69
}
70
71
deleteGroup(namespaceName: string) {
72
return Databases.delete("editor_persistence__" + namespaceName, this.player);
73
}
74
75
getGroup(namespaceName: string) {
76
if (Databases.find(new RegExp(`^editor_persistence__${namespaceName}$`), this.player).length === 0) {
77
return undefined;
78
}
79
return this.getOrCreateGroup(namespaceName);
80
}
81
82
getGroups() {
83
const groups = Databases.find(new RegExp(`^editor_persistence__`), this.player);
84
const persistenceGroupList = [];
85
for (const group of groups) {
86
persistenceGroupList.push(new PersistenceGroup(Databases.load(group, this.player)));
87
}
88
return persistenceGroupList;
89
}
90
91
disposeAllGroups() {
92
const groups = Databases.find(new RegExp(`^editor_persistence__`), this.player);
93
for (const group of groups) Databases.delete(group, this.player);
94
}
95
}
96
97
export function convertBlockStringsToBlockType(blockString: Readonly<string[]>) {
98
const blockTypes: BlockType[] = [];
99
for (let blockStringElement of blockString) {
100
if (!blockStringElement.includes(":")) {
101
blockStringElement = "minecraft:" + blockStringElement;
102
}
103
const blockType = BlockTypes.get(blockStringElement);
104
if (blockType) {
105
blockTypes.push(blockType);
106
}
107
}
108
return blockTypes;
109
}
110
111
export function convertBlockTypesToBlockStrings(blockTypes: Readonly<BlockType[]>) {
112
const blockStrings: string[] = [];
113
for (const blockType of blockTypes) {
114
let blockName = blockType.id;
115
if (blockName.startsWith("minecraft:")) {
116
blockName = blockName.substring(10);
117
}
118
blockStrings.push(blockName);
119
}
120
return blockStrings;
121
}
122
123
interface InputMarkupProperties {
124
showUnset?: boolean;
125
ignoreFormat?: boolean;
126
prefix?: string;
127
contextId?: string;
128
}
129
130
export function getInputMarkup(id: string, props?: InputMarkupProperties) {
131
let markupStart = "[~*input|$id=" + id;
132
if (props?.showUnset) {
133
markupStart = markupStart.concat("|showUnset");
134
}
135
if (!props || !props.ignoreFormat) {
136
markupStart = markupStart.concat("|$format=", props?.prefix ?? "[", "*input");
137
markupStart = markupStart.concat(props?.prefix ?? "]");
138
}
139
if (props?.contextId) {
140
markupStart = markupStart.concat("|$contextId=", props.contextId);
141
}
142
return markupStart.concat("~]");
143
}
144
145
export enum RelativeDirection {
146
Forward,
147
Right,
148
Back,
149
Left,
150
Up,
151
Down,
152
}
153
154
const directionLookup = {
155
[RelativeDirection.Forward]: { x: 0, y: 0, z: 1 },
156
[RelativeDirection.Right]: { x: -1, y: 0, z: 0 },
157
[RelativeDirection.Back]: { x: 0, y: 0, z: -1 },
158
[RelativeDirection.Left]: { x: 1, y: 0, z: 0 },
159
[RelativeDirection.Up]: { x: 0, y: 1, z: 0 },
160
[RelativeDirection.Down]: { x: 0, y: -1, z: 0 },
161
};
162
163
export function getRotationCorrectedDirection(rotationY: number, realDirection: RelativeDirection) {
164
if (realDirection === RelativeDirection.Up || realDirection === RelativeDirection.Down) {
165
return realDirection;
166
}
167
const directionQuadrant = Math.floor(((rotationY + 405 + realDirection * 90) % 360) / 90);
168
return directionQuadrant;
169
}
170
171
export function getRotationCorrectedDirectionVector(rotationY: number, realDirection: RelativeDirection) {
172
const relativeDirection = getRotationCorrectedDirection(rotationY, realDirection);
173
return directionLookup[relativeDirection];
174
}
175
176