-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfakeschem.html
More file actions
366 lines (311 loc) · 11.7 KB
/
fakeschem.html
File metadata and controls
366 lines (311 loc) · 11.7 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<!--
This webpage uses Prismarine NBT (https://github.com/PrismarineJS/prismarine-nbt)
and pako (https://github.com/nodeca/pako)
Pako is licensed as:
The MIT License
Copyright (C) 2014-2017 by Vitaly Puzrin and Andrei Tuputcyn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fake Schematic Creator</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="./pnbt.js"></script>
<script>
const nbt = require('prismarine-nbt');
</script>
<script src="./pako.min.js"></script>
<style>
.hidden {
display: none;
}
textarea {
resize: none;
}
</style>
</head>
<body>
<header class="w3-container w3-dark-grey">
<h2>Fake Schematic Creator</h2>
</header>
<div class="w3-container">
<h3>Material list</h3>
<div class="w3-panel w3-pale-green">
<p>This page creates a .schem to integrate with Litematica's Material List
<br>However, materials that are not blocks (like Eggs) will be ignored</p>
</div>
<button id="download" disabled="disabled"><i class="fa fa-download"></i> Download as .schem</button>
<div class="w3-row">
<div class="w3-col s6">
<h4>Input</h4>
<textarea id="materials" class="w3-input w3-monospace w3-border" rows="16" placeholder="Material 2B = 1728"></textarea>
</div>
<div class="w3-col s6">
<h4>Intermediate (double check this)</h4>
<textarea id="results" class="w3-input w3-monospace w3-border" rows="16" readonly="readonly"></textarea>
</div>
</div>
</div>
<script type="module">
import { xyzToKey, convertToSchem} from './js/schematics.js';
const materials = document.getElementById("materials");
const results = document.getElementById("results");
const downloadBtn = document.getElementById("download");
let schematic;
downloadBtn.addEventListener("click", function(event) {
if (schematic) {
const filename = "fake.schem"
const buffer = convertToSchem(schematic, undefined);
download(filename, buffer);
}
});
const nbt = require('prismarine-nbt')
const { Buffer } = require('buffer');
const MC_PREFIX = "minecraft:";
const NBSP = String.fromCharCode(160); //non-breaking space
const CRLF = String.fromCharCode(13, 10); //new line
function getMaxStack(item) {
if (item.includes("shulker_box")) {
return 1;
}
else if (item.includes("sign")) {
return 16;
}
else if (item.includes("banner")) {
return 16;
}
else if (item.includes("bed")) {
return 1;
}
else if (item.includes("boat")) {
return 1;
}
switch (item) {
case 'cake':
case 'water_bucket':
case 'lava_bucket':
return 1;
case 'armor_stand':
return 16;
}
return 64;
}
function insertResult(textareaContent, schematic) {
results.innerHTML = textareaContent.join(CRLF);
if (schematic)
downloadBtn.removeAttribute("disabled");
}
function run() {
let newMaterials = [];
let fail = false;
let totalCount = 0;
let palette = new Map();
// Possible syntaxes:
// stone_bricks = 4321
// Stone Bricks = 4321
// diorite_slab 2B 3I
// White Terracotta 2 B 4 S
// white_terracotta 2 B 4 S = 3712
// 2B 3I diorite_slab = 3459
// White Terracotta x 11186 (6b 12s 50i)
for (const line of materials.value.split('\n')) {
if (line === "") continue;
let result = parseLine(line);
if (result === false) {
console.log(" line = " + line);
newMaterials.push('?');
fail = true;
continue;
}
let [material, boxes, stacks, items, count] = result;
let calcCount = count;
if (boxes != 0 || stacks != 0 || items != 0) {
let maxStack = getMaxStack(material);
calcCount = boxes * 27 * maxStack + stacks * maxStack + items;
}
if (count != 0 && count != calcCount) {
console.log("Inconsistent numbers found: " + calcCount + " x " + count);
console.log(" line = " + line);
newMaterials.push('?');
fail = true;
continue;
}
newMaterials.push(material + ' ' + calcCount);
let paletteCount = palette.get(material) || 0;
palette.set(material, paletteCount + calcCount);
totalCount += calcCount;
}
if (!fail) {
schematic = createSchematic(palette, totalCount);
}
insertResult(newMaterials, schematic);
}
materials.addEventListener("change", refreshOutput);
function refreshOutput(e) {
results.innerHTML = '';
downloadBtn.setAttribute("disabled", "disabled");
run();
}
function download(filename, uint8array) {
const element = document.createElement("a");
const blob = new Blob([uint8array], {type: "application/octet-stream"});
element.setAttribute('href', window.URL.createObjectURL(blob));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
const STATE_UNDEF = -1;
const STATE_NONE = 0;
const STATE_NUMBER = 1;
const STATE_NUMBER_END = 2;
const STATE_WORD = 3;
function parseLine(line) {
let boxes = 0;
let stacks = 0;
let items = 0;
let word = "";
let number = "";
let statePrev = STATE_UNDEF;
let stateNext = STATE_UNDEF;
let count = 0;
//remove " x " for ease of parsing
line = line.replace(/ x /i, "");
for (let i = 0; i < line.length; i++) {
const c = line[i];
if (c >= '0' && c <= '9') {
number += c;
stateNext = STATE_NUMBER;
} else if (c == 'B' || c == 'b') {
if (statePrev == STATE_NUMBER || statePrev == STATE_NUMBER_END) {
if (boxes != 0) {
console.log ("Already found Shulker Boxes: " + boxes);
return false;
}
boxes = +number; //end of shulker box amount
number = "";
stateNext = STATE_NONE;
} else {
word += 'b';
stateNext = STATE_WORD;
}
} else if (c == 'S' || c == 's') {
if (statePrev == STATE_NUMBER || statePrev == STATE_NUMBER_END) {
if (stacks != 0) {
console.log ("Already found Stacks: " + stacks);
return false;
}
stacks = +number; //end of stacks amount
number = "";
stateNext = STATE_NONE;
} else {
word += 's';
stateNext = STATE_WORD;
}
} else if (c == 'I' || c == 'i') {
if (statePrev == STATE_NUMBER || statePrev == STATE_NUMBER_END) {
if (items != 0) {
console.log ("Already found Items: " + items);
return false;
}
items = +number; //end of items amount
number = "";
stateNext = STATE_NONE;
} else {
word += 'i';
stateNext = STATE_WORD;
}
} else if ((c >= 'a' && c <= 'z') || c == '_') {
word += c;
stateNext = STATE_WORD;
} else if (c >= 'A' && c <= 'Z') {
word += c.toLowerCase();
stateNext = STATE_WORD;
} else if (c == ' ') {
//Look ahead
if (statePrev == STATE_WORD) {
const cNext = line[i + 1];
if ((cNext >= 'a' && cNext <= 'z') || (cNext >= 'A' && cNext <= 'Z')) {
word += '_';
stateNext = STATE_WORD;
} else {
stateNext = STATE_NONE;
}
} else if (statePrev == STATE_NUMBER) {
stateNext = STATE_NUMBER_END;
}
} else {
if (statePrev == STATE_NUMBER || statePrev == STATE_NUMBER_END) {
if (count == 0) {
count = +number;
} else {
console.log ("Already found raw count: " + number);
return false;
}
number = '';
stateNext = STATE_NUMBER_END;
}
}
//next iteration
statePrev = stateNext;
stateNext = STATE_UNDEF;
}
if (word === "") {
console.log("No material found");
return false;
}
//there may be a repeated amount (like "Material 2 B = 3456")
if (number !== '') {
if (count == 0) {
count = +number;
} else {
console.log ("Already found raw count: " + number);
return false;
}
}
if (boxes == 0 && stacks == 0 && items == 0 && count == 0) {
console.log("No count found");
return false;
}
return [word, boxes, stacks, items, count];
}
function createSchematic(palette, totalCount) {
let hexahedronSide = Math.ceil(Math.cbrt(totalCount));
const schematic = {
blocks: new Map(),
xsize: hexahedronSide,
ysize: hexahedronSide,
zsize: hexahedronSide
}
let key = 0;
for (let [material, count] of palette.entries()) {
if (!material.startsWith('minecraft:'))
material = 'minecraft:' + material;
while (count--) {
schematic.blocks.set(key++, [material]);
}
}
return schematic;
}
</script>
</body>
</html>