forked from LolkoPlays/LookUp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.js
More file actions
64 lines (57 loc) · 2.58 KB
/
export.js
File metadata and controls
64 lines (57 loc) · 2.58 KB
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
// === Imports ===
import { world, system } from "@minecraft/server";
import { tabs } from './tabs.js'; //Your tab import path
import { tables } from './tables.js'; //Your table import path
import { entries } from './entries.js'; //Your entry import path
import { recipes } from './recipes.js'; //Your recipe import path
// === Centralized Data Object ===
const data = { tabs, tables, entries, recipes }; //mention all imported data
const CHUNK_SIZE = 1500;
// === Chunked Data Sender ===
function sendLargeEvent(overworld, id, payload) {
const payloadStr = JSON.stringify(payload);
const totalChunks = Math.ceil(payloadStr.length / CHUNK_SIZE);
for (let i = 0; i < totalChunks; i++) {
const chunk = payloadStr.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE);
const packet = { id, index: i, total: totalChunks, chunk };
try {
overworld.runCommand(`scriptevent lookup:chunked_event ${JSON.stringify(packet)}`);
} catch (err) {
console.error(`[LookUp Exporter] Failed to send ${id} chunk ${i}`, err);
}
}
}
// === Export Function ===
function exportLookUpData(overworld) {
for (const [groupKey, group] of Object.entries(data)) {
for (const [id, wrapper] of Object.entries(group)) {
if (wrapper?.tabData) {
sendLargeEvent(overworld, "tab_data", { tabId: id, ...wrapper.tabData });
} else if (wrapper?.tableData) {
sendLargeEvent(overworld, "table_data", { tableId: id, ...wrapper.tableData });
} else if (wrapper?.entryData) {
sendLargeEvent(overworld, "entry_data", { entryId: id, ...wrapper.entryData });
} else if (wrapper?.recipeData) {
const { inputs, pageConfig, ...pureRecipe } = wrapper.recipeData;
sendLargeEvent(overworld, "recipe_data", { typeId: id, ...pureRecipe });
if (inputs) {
sendLargeEvent(overworld, "recipe_input", { typeId: id, input: inputs });
}
if (pageConfig) {
sendLargeEvent(overworld, "page_config", { typeId: id, config: pageConfig });
}
}
}
}
}
// === Respond to LookUp Ping ===
system.afterEvents.scriptEventReceive.subscribe(ev => {
if (ev.id !== "lookup:request_data_ping") return;
try {
const overworld = world.getDimension("overworld");
exportLookUpData(overworld);
console.warn("[LookUp Exporter] Responded to data ping.");
} catch (err) {
console.error("[LookUp Exporter] Error during export", err);
}
});