-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
69 lines (55 loc) · 1.84 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { BlocksMap, type McFC } from "./types.js";
import { Rcon } from "rcon-client";
const deduplicateBlocks = (inputBlocks: BlocksMap): BlocksMap => {
const blocks: BlocksMap = [];
for (const block of inputBlocks) {
const existingBlock = blocks.findIndex(
(b) => b.x === block.x && b.y === block.y && b.z === block.z,
);
if (existingBlock !== -1) {
blocks.splice(existingBlock, 1);
}
blocks.push(block);
}
return blocks;
};
const sortBlocks = (inputBlocks: BlocksMap): BlocksMap =>
inputBlocks.sort((a, b) => a.y - b.y || a.z - b.z || a.x - b.x);
export const mc = (
type: McFC,
passedProps = null,
...children: BlocksMap
): BlocksMap => {
const flatChildren = children.flat(Infinity).filter(Boolean);
for (const child of flatChildren) {
if (typeof child !== "object") {
console.error(child);
throw new Error(`Invalid child of type ${typeof child}`);
}
}
const refinedChildren = sortBlocks(deduplicateBlocks(flatChildren));
const props = passedProps || {};
props.children = refinedChildren;
return [...type(props)];
};
export const clearSpace = async (rcon: Rcon) => {
const startTime = Date.now();
const promises: Promise<string>[] = [];
for (let z = -100; z <= 100; z += 10) {
for (let x = -100; x <= 100; x += 10) {
promises.push(rcon.send(`fill ${x} 0 ${z} ${x + 10} 255 ${z + 10} air`));
}
}
await Promise.all(promises);
console.log(`Cleared in ${Date.now() - startTime}ms`);
};
export const applyBuild = async (rcon: Rcon, build: BlocksMap) => {
const filteredBlocks = build.filter((block) => block.id !== "air");
const promises = filteredBlocks.map((block) =>
rcon.send(`setblock ${block.x} ${block.y} ${block.z} ${block.id}`),
);
const results = await Promise.all(promises);
for (const message of results) {
console.log(message);
}
};