-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (127 loc) · 4.08 KB
/
index.js
File metadata and controls
139 lines (127 loc) · 4.08 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
// Import requirements
var fs = require('fs'),
nbt = require('prismarine-nbt'),
// Prompting
readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
require('./blocks.js');
// console.log(blocks);
// Convert JSON-structured structure data to a simplified version with only an array of blocks
function simplifyJSON(data) {
// Get size information from decoded NBT data
var size = data.size.value.value;
// Create three-dimensional array with dimensions matching structure
var box = new Array(size[1]).fill(0).map(
(i) => new Array(size[0]).fill(0).map(
(j) => new Array(size[2]).fill('minecraft:air')
)
);
// Loop through all non-air blocks in structure
data.blocks.value.value.forEach((block) => {
var pos = block.pos.value.value;
// Set name of block in correct position
box[pos[1]][pos[0]][pos[2]] = data.palette.value.value[block.state.value].Name.value;
});
// Return generated array
return box;
}
// Convert data into an object containing an array where each number corresponds to a block
function valueEncodeSimple(data) {
var output = {
'blocks': [],
'palette': []
};
output.blocks = simplifyJSON(data);
for (var i = 0; i < output.blocks.length; i ++) {
for (var j = 0; j < output.blocks[i].length; j ++) {
for (var k = 0; k < output.blocks[i][j].length; k ++) {
var block = output.blocks[i][j][k];
if (!output.palette.includes(block)) {
output.palette.push(block);
}
output.blocks[i][j][k] = output.palette.indexOf(block);
}
}
}
return output;
}
// It is essentially a simplification of the default JSON data generated when the .nbt file is parsed
function valueEncodeComplex(data) {
var output = {
'blocks': [],
'palette': []
};
var size = data.size.value.value;
var box = new Array(size[1]).fill(0).map(
(i) => new Array(size[0]).fill(0).map(
(j) => new Array(size[2]).fill(0)
)
);
output.blocks = box;
data.blocks.value.value.forEach((block) => {
var pos = block.pos.value.value;
output.blocks[pos[1]][pos[0]][pos[2]] = block.state.value;
});
data.palette.value.value.forEach((state) => {
output.palette.push({
'name': state.Name.value,
'properties': state.Properties ? state.Properties.value : {}
});
});
return output;
}
function allBlockEncoding(data) {
var output = {
'blocks': [],
'palette': blocks
};
output.blocks = simplifyJSON(data);
for (var i = 0; i < output.blocks.length; i ++) {
for (var j = 0; j < output.blocks[i].length; j ++) {
for (var k = 0; k < output.blocks[i][j].length; k ++) {
var block = output.blocks[i][j][k];
output.blocks[i][j][k] = output.palette.indexOf(block);
}
}
}
return output;
}
readline.question('Name of .nbt file to open: ', (name) => {
fs.readFile('./' + name + '.nbt', function(error, data) {
// Error handling
if (error) throw error;
nbt.parse(data, function(error, data) {
// If directory does not exist, create it
var dir = './' + name;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
// Create parsed JSON file or original NBT data
fs.writeFile(name + '/' + name + '-original.json', JSON.stringify(data.value, null, "\t"), (err) => {
if (err) throw err;
});
// Create simplified array of blocks
fs.writeFile(name + '/' + name + '-simplified.json', JSON.stringify(simplifyJSON(data.value), null, "\t"), (err) => {
if (err) throw err;
});
// Create array of blocks by ID
fs.writeFile(name + '/' + name + '-values_simple.json', JSON.stringify(valueEncodeSimple(data.value), null, "\t"), (err) => {
if (err) throw err;
});
// Encode all block states and types as separate numbers
fs.writeFile(name + '/' + name + '-values_complex.json', JSON.stringify(valueEncodeComplex(data.value), null, "\t"), (err) => {
if (err) throw err;
});
// Global block ID encoding
fs.writeFile(name + '/' + name + '-global.json', JSON.stringify(allBlockEncoding(data.value), null, "\t"), (err) => {
if (err) throw err;
});
// Log success message
console.log('Conversion successful!');
});
});
// Close prompts
readline.close();
});