-
Notifications
You must be signed in to change notification settings - Fork 7
/
convertWB.js
88 lines (80 loc) · 2.85 KB
/
convertWB.js
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
'use strict'
const fs = require('fs');
let rawdata = fs.readFileSync('./WesterosBlocks.json');
let content = JSON.parse(rawdata);
let newcontent = { blocks: [] };
function makeName(label) {
label = label.toLowerCase();
label = label.replace(/\s+/g,"_");
return label.replace(/[^0-9a-z_]/gi, '')
}
const skippedFields = [ 'blockName', 'blockID', 'blockIDs', 'subBlocks', 'modelBlockMeta' ];
const skippedSubBlockFields = [ 'meta' ];
let ids = new Set();
content.blocks.forEach(block => {
// If stack, build stack data and drop extra subblocks
if (block.blockType.includes("-stack")) {
let first = block.subBlocks[0];
first.stack = [];
block.subBlocks.forEach(subblock => {
let rec = { };
if (subblock.textures) rec.textures = subblock.textures;
if (subblock.boundingBox) rec.boundingBox = subblock.boundingBox;
if (subblock.cuboids) rec.cuboids = subblock.cuboids;
if (subblock.collisionBoxes) rec.collisionBoxes = subblock.collisionBoxes;
delete subblock.textures;
delete subblock.boundingBox;
delete subblock.cuboids;
delete subblock.collisionBoxes;
first.stack.push(rec);
});
block.subBlocks = [ first ];
}
block.subBlocks.forEach(subblock => {
let newblock = { blockName: makeName(subblock.label) };
let cnt = 1;
while (ids.has(newblock.blockName)) {
cnt++;
newblock.blockName = makeName(subblock.label) + cnt;
}
ids.add(newblock.blockName);
Object.keys(block).forEach(k => {
if (skippedFields.includes(k)) return;
newblock[k] = block[k];
});
Object.keys(subblock).forEach(k => {
if (skippedSubBlockFields.includes(k)) return;
newblock[k] = subblock[k];
});
newblock.legacyBlockID = block.blockName + ":" + subblock.meta;
subblock.newName = newblock.blockName; // Remember new name
newcontent.blocks.push(newblock);
});
});
// Second pass for sake of modelBlockNames
content.blocks.forEach(block => {
if (block.modelBlockName) {
let modelLegacyID = block.modelBlockName + ":" + block.modelBlockMeta;
let model = newcontent.blocks.find(r => r.legacyBlockID == modelLegacyID);
let modelID;
if (model) {
modelID = model.blockName;
}
else if (block.modelBlockName.startsWith("minecraft:")) {
if (block.modelBlockName == 'minecraft:hardened_clay')
modelID = 'minecraft:terracotta';
else if (block.modelBlockName == 'minecraft:double_stone_slab')
modelID = 'minecraft:stone_slab';
else
modelID = block.modelBlockName;
}
else {
console.log(`Bad modelBlockName ${block.modelBlockName} for ${block.blockName}`);
}
if (modelID) {
let newrec = newcontent.blocks.find(r => r.blockName == block.subBlocks[0].newName);
newrec.modelBlockName = modelID;
}
}
});
console.log(JSON.stringify(newcontent, null, ' '));