-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathextract_block_lootTables.js
72 lines (56 loc) · 1.78 KB
/
extract_block_lootTables.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
'use strict'
const fs = require('fs')
const path = require('path')
function getItemId (data, name) {
for (const prop in data) {
const item = data[prop]
if (`minecraft:${item.name}` === name) return item.id
}
}
function extractDropIds (itemData, lootTable) {
const dropIds = []
function recursiveDropSearch (object) {
for (const prop in object) {
const info = object[prop]
if (typeof info === 'string') {
const block = getItemId(itemData, info)
if (block !== undefined && dropIds.indexOf(block) === -1) {
dropIds.push(block)
}
continue
}
recursiveDropSearch(object[prop])
}
}
recursiveDropSearch(lootTable)
return dropIds
}
/**
* adds drops to a blocks.json
* @param {string} outPath path to folder with blocks.json & items.json
* @param {string} inPath extracted data
*/
function handle (outPath, inPath) {
const outPathResolved = path.resolve(outPath)
const dataFolder = path.join(inPath, 'data', 'loot_tables', 'blocks')
const blocksFilePath = path.join(outPathResolved, 'blocks.json')
const itemDataPath = path.join(outPathResolved, 'items.json')
const blockData = require(blocksFilePath)
const itemData = require(itemDataPath)
for (const prop in blockData) {
const block = blockData[prop]
const inputPath = path.join(dataFolder, block.name + '.json')
if (!fs.existsSync(inputPath)) {
block.drops = []
continue
}
const lootTable = require(inputPath)
block.drops = extractDropIds(itemData, lootTable)
}
fs.writeFileSync(blocksFilePath, JSON.stringify(blockData, null, 2))
}
if (!process.argv[2] || !process.argv[3]) {
console.log('Usage: extract_block_lootTables.js <jsonPath> <dataPath>')
process.exit(1)
}
handle(process.argv[2], process.argv[3])