-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive-pack.js
More file actions
101 lines (86 loc) · 3.06 KB
/
Copy patharchive-pack.js
File metadata and controls
101 lines (86 loc) · 3.06 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
window.ArchivePack = (function () {
function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = Math.random() * 16 | 0;
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});}
function buildManifest(structureNames) {
const description = structureNames.length
? 'Structures: ' + structureNames.join(', ')
: 'FireBee Archive Pack';
return JSON.stringify({
format_version: 2,
header: {
name: 'FireBee Archive Pack',
description: description,
uuid: generateUUID(),
version: [1, 0, 0],
min_engine_version: [1, 21, 0]
},
modules: [
{
type: 'data',
uuid: generateUUID(),
version: [1, 0, 0]
},
{
type: 'script',
uuid: generateUUID(),
version: [1, 0, 0],
entry: 'scripts/main.js'
}
],
dependencies: [
{ module_name: '@minecraft/server', version: 'beta' },
{ module_name: '@minecraft/debug-utilities', version: 'beta' }
],
metadata: { product_type: 'addon' }
}, null, 2);}
async function loadJSZip() {
if (typeof JSZip !== 'undefined') return;
await new Promise((resolve, reject) => {
const s = document.createElement('script');
s.src = 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js';
s.onload = resolve;
s.onerror = reject;
document.head.appendChild(s);
});}
async function downloadFromSelection(selection, btn) {
btn.disabled = true;
btn.textContent = 'Preparing…';
try {
await loadJSZip();
const zip = new JSZip();
const packBase = '/Archive/Archive pack/';
zip.file('manifest.json', buildManifest(selection.map(s => s.name)));
const iconRes = await fetch(packBase + 'pack_icon.png');
if (iconRes.ok) zip.file('pack_icon.png', await iconRes.blob());
const mainRes = await fetch(packBase + 'scripts/main.js');
if (mainRes.ok) zip.file('scripts/main.js', await mainRes.blob());
await Promise.all(selection.map(async item => {
try {
const url = new URL(item.file, window.location.origin + item.base).href;
const res = await fetch(url);
if (!res.ok) throw new Error('fetch failed: ' + url);
zip.file('structures/' + item.name, await res.blob());
} catch (e) {
console.warn('Could not fetch structure:', item.name, e);
}
}));
const content = await zip.generateAsync({ type: 'blob' });
const a = document.createElement('a');
a.href = URL.createObjectURL(content);
a.download = 'BeeArchive.mcpack';
a.click();
URL.revokeObjectURL(a.href);
} catch (e) {
console.error('Pack build failed:', e);
btn.textContent = 'Failed';
setTimeout(() => { btn.disabled = false; btn.textContent = '↓ Download as Pack'; }, 2000);
return;
}
btn.disabled = false;
btn.textContent = '↓ Download as Pack';
}
return { downloadFromSelection };
})();