Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bedrock support, add blockVariantsByStateId map #23

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ blocks textures by name

array of blocks textures

### blocksModels

map of block variant => model

### blockStates

array of block states

## Items

### items
Expand All @@ -30,6 +38,10 @@ texture content by name

array of texture content

### blockStateVariantsByStateId

Returns an array of block model variants mapped by block state ID, lookup with .blocksModels field

## version

## directory
Expand Down
292 changes: 148 additions & 144 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,152 +1,156 @@
const mcDataToNode = require('./lib/loader')
const cache = {} // prevent reindexing when requiring multiple time the same version

function getVersion (mcVersion) {
if (cache[mcVersion]) { return cache[mcVersion] }
const mcData = data[mcVersion]
if (mcData == null) { return null }
const nmcData = mcDataToNode(mcData, mcVersion)
cache[mcVersion] = nmcData
return nmcData
}

function toMajor (version) {
const [a, b] = (version + '').split('.')
return a + '.' + b
const path = require('path')
const data = {
pc: {
'1.8.8': {
directory: path.join(__dirname, './minecraft-assets/data/1.8.8/'),
blocksTextures: require('./minecraft-assets/data/1.8.8/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.8.8/items_textures'),
textureContent: require('./minecraft-assets/data/1.8.8/texture_content'),
blocksStates: require('./minecraft-assets/data/1.8.8/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.8.8/blocks_models')
},
1.9: {
directory: path.join(__dirname, './minecraft-assets/data/1.9/'),
blocksTextures: require('./minecraft-assets/data/1.9/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.9/items_textures'),
textureContent: require('./minecraft-assets/data/1.9/texture_content'),
blocksStates: require('./minecraft-assets/data/1.9/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.9/blocks_models')
},
'1.10': {
directory: path.join(__dirname, './minecraft-assets/data/1.10/'),
blocksTextures: require('./minecraft-assets/data/1.10/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.10/items_textures'),
textureContent: require('./minecraft-assets/data/1.10/texture_content'),
blocksStates: require('./minecraft-assets/data/1.10/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.10/blocks_models')
},
'1.11.2': {
directory: path.join(__dirname, './minecraft-assets/data/1.11.2/'),
blocksTextures: require('./minecraft-assets/data/1.11.2/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.11.2/items_textures'),
textureContent: require('./minecraft-assets/data/1.11.2/texture_content'),
blocksStates: require('./minecraft-assets/data/1.11.2/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.11.2/blocks_models')
},
1.12: {
directory: path.join(__dirname, './minecraft-assets/data/1.12/'),
blocksTextures: require('./minecraft-assets/data/1.12/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.12/items_textures'),
textureContent: require('./minecraft-assets/data/1.12/texture_content'),
blocksStates: require('./minecraft-assets/data/1.12/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.12/blocks_models')
},
1.13: {
directory: path.join(__dirname, './minecraft-assets/data/1.13/'),
blocksTextures: require('./minecraft-assets/data/1.13/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.13/items_textures'),
textureContent: require('./minecraft-assets/data/1.13/texture_content'),
blocksStates: require('./minecraft-assets/data/1.13/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.13/blocks_models')
},
'1.13.2': {
directory: path.join(__dirname, './minecraft-assets/data/1.13.2/'),
blocksTextures: require('./minecraft-assets/data/1.13.2/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.13.2/items_textures'),
textureContent: require('./minecraft-assets/data/1.13.2/texture_content'),
blocksStates: require('./minecraft-assets/data/1.13.2/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.13.2/blocks_models')
},
'1.14.4': {
directory: path.join(__dirname, './minecraft-assets/data/1.14.4/'),
blocksTextures: require('./minecraft-assets/data/1.14.4/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.14.4/items_textures'),
textureContent: require('./minecraft-assets/data/1.14.4/texture_content'),
blocksStates: require('./minecraft-assets/data/1.14.4/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.14.4/blocks_models')
},
'1.15.2': {
directory: path.join(__dirname, './minecraft-assets/data/1.15.2/'),
blocksTextures: require('./minecraft-assets/data/1.15.2/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.15.2/items_textures'),
textureContent: require('./minecraft-assets/data/1.15.2/texture_content'),
blocksStates: require('./minecraft-assets/data/1.15.2/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.15.2/blocks_models')
},
'1.16.1': {
directory: path.join(__dirname, './minecraft-assets/data/1.16.1/'),
blocksTextures: require('./minecraft-assets/data/1.16.1/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.16.1/items_textures'),
textureContent: require('./minecraft-assets/data/1.16.1/texture_content'),
blocksStates: require('./minecraft-assets/data/1.16.1/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.16.1/blocks_models')
},
'1.16.4': {
directory: path.join(__dirname, './minecraft-assets/data/1.16.4/'),
blocksTextures: require('./minecraft-assets/data/1.16.4/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.16.4/items_textures'),
textureContent: require('./minecraft-assets/data/1.16.4/texture_content'),
blocksStates: require('./minecraft-assets/data/1.16.4/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.16.4/blocks_models')
},
'1.17.1': {
directory: path.join(__dirname, './minecraft-assets/data/1.17.1/'),
blocksTextures: require('./minecraft-assets/data/1.17.1/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.17.1/items_textures'),
textureContent: require('./minecraft-assets/data/1.17.1/texture_content'),
blocksStates: require('./minecraft-assets/data/1.17.1/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.17.1/blocks_models')
},
'1.18.1': {
directory: path.join(__dirname, './minecraft-assets/data/1.18.1/'),
blocksTextures: require('./minecraft-assets/data/1.18.1/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.18.1/items_textures'),
textureContent: require('./minecraft-assets/data/1.18.1/texture_content'),
blocksStates: require('./minecraft-assets/data/1.18.1/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.18.1/blocks_models')
},
'1.19.1': {
directory: path.join(__dirname, './minecraft-assets/data/1.19.1/'),
blocksTextures: require('./minecraft-assets/data/1.19.1/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.19.1/items_textures'),
textureContent: require('./minecraft-assets/data/1.19.1/texture_content'),
blocksStates: require('./minecraft-assets/data/1.19.1/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.19.1/blocks_models')
},
'1.20.2': {
blocksTextures: require('./minecraft-assets/data/1.20.2/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.20.2/items_textures'),
textureContent: require('./minecraft-assets/data/1.20.2/texture_content'),
blocksStates: require('./minecraft-assets/data/1.20.2/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.20.2/blocks_models')
}
}
}

function minor (version) {
const [, , c] = (version + '.0').split('.')
return parseInt(c, 10)
}
Object.assign(data, {
bedrock: {
'1.19.1': data.pc['1.19.1'],
'1.20.0': data.pc['1.20.2']
}
})

module.exports = function (mcVersion) {
// Check exact version first
let assets = getVersion(mcVersion)
if (assets) { return assets }
// If not found, resort to the last of major
assets = getVersion(lastOfMajor[toMajor(mcVersion)])
return assets
}
// Get {'1.18': ['1.18', '1.18.1', ...]} from ['1.18', '1.18.1', ...]
const reduce = (what) => what.reduce((acc, cur) => {
const major = cur.split('.').slice(0, 2).join('.')
const arr = acc[major] = acc[major] || []
arr.push(cur)
return acc
}, {})

const data = {
'1.8.8': {
blocksTextures: require('./minecraft-assets/data/1.8.8/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.8.8/items_textures'),
textureContent: require('./minecraft-assets/data/1.8.8/texture_content'),
blocksStates: require('./minecraft-assets/data/1.8.8/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.8.8/blocks_models')
},
1.9: {
blocksTextures: require('./minecraft-assets/data/1.9/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.9/items_textures'),
textureContent: require('./minecraft-assets/data/1.9/texture_content'),
blocksStates: require('./minecraft-assets/data/1.9/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.9/blocks_models')
},
'1.10': {
blocksTextures: require('./minecraft-assets/data/1.10/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.10/items_textures'),
textureContent: require('./minecraft-assets/data/1.10/texture_content'),
blocksStates: require('./minecraft-assets/data/1.10/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.10/blocks_models')
},
'1.11.2': {
blocksTextures: require('./minecraft-assets/data/1.11.2/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.11.2/items_textures'),
textureContent: require('./minecraft-assets/data/1.11.2/texture_content'),
blocksStates: require('./minecraft-assets/data/1.11.2/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.11.2/blocks_models')
},
1.12: {
blocksTextures: require('./minecraft-assets/data/1.12/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.12/items_textures'),
textureContent: require('./minecraft-assets/data/1.12/texture_content'),
blocksStates: require('./minecraft-assets/data/1.12/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.12/blocks_models')
},
1.13: {
blocksTextures: require('./minecraft-assets/data/1.13/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.13/items_textures'),
textureContent: require('./minecraft-assets/data/1.13/texture_content'),
blocksStates: require('./minecraft-assets/data/1.13/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.13/blocks_models')
},
'1.13.2': {
blocksTextures: require('./minecraft-assets/data/1.13.2/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.13.2/items_textures'),
textureContent: require('./minecraft-assets/data/1.13.2/texture_content'),
blocksStates: require('./minecraft-assets/data/1.13.2/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.13.2/blocks_models')
},
'1.14.4': {
blocksTextures: require('./minecraft-assets/data/1.14.4/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.14.4/items_textures'),
textureContent: require('./minecraft-assets/data/1.14.4/texture_content'),
blocksStates: require('./minecraft-assets/data/1.14.4/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.14.4/blocks_models')
},
'1.15.2': {
blocksTextures: require('./minecraft-assets/data/1.15.2/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.15.2/items_textures'),
textureContent: require('./minecraft-assets/data/1.15.2/texture_content'),
blocksStates: require('./minecraft-assets/data/1.15.2/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.15.2/blocks_models')
},
'1.16.1': {
blocksTextures: require('./minecraft-assets/data/1.16.1/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.16.1/items_textures'),
textureContent: require('./minecraft-assets/data/1.16.1/texture_content'),
blocksStates: require('./minecraft-assets/data/1.16.1/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.16.1/blocks_models')
},
'1.16.4': {
blocksTextures: require('./minecraft-assets/data/1.16.4/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.16.4/items_textures'),
textureContent: require('./minecraft-assets/data/1.16.4/texture_content'),
blocksStates: require('./minecraft-assets/data/1.16.4/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.16.4/blocks_models')
},
'1.17.1': {
blocksTextures: require('./minecraft-assets/data/1.17.1/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.17.1/items_textures'),
textureContent: require('./minecraft-assets/data/1.17.1/texture_content'),
blocksStates: require('./minecraft-assets/data/1.17.1/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.17.1/blocks_models')
},
'1.18.1': {
blocksTextures: require('./minecraft-assets/data/1.18.1/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.18.1/items_textures'),
textureContent: require('./minecraft-assets/data/1.18.1/texture_content'),
blocksStates: require('./minecraft-assets/data/1.18.1/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.18.1/blocks_models')
},
'1.19.1': {
blocksTextures: require('./minecraft-assets/data/1.19.1/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.19.1/items_textures'),
textureContent: require('./minecraft-assets/data/1.19.1/texture_content'),
blocksStates: require('./minecraft-assets/data/1.19.1/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.19.1/blocks_models')
},
'1.20.2': {
blocksTextures: require('./minecraft-assets/data/1.20.2/blocks_textures'),
itemsTextures: require('./minecraft-assets/data/1.20.2/items_textures'),
textureContent: require('./minecraft-assets/data/1.20.2/texture_content'),
blocksStates: require('./minecraft-assets/data/1.20.2/blocks_states'),
blocksModels: require('./minecraft-assets/data/1.20.2/blocks_models')
}
}
const cache = { pc: {}, bedrock: {} } // prevent reindexing when requiring multiple time the same version
const byMajor = { pc: reduce(Object.keys(data.pc)), bedrock: reduce(Object.keys(data.bedrock)) }
const lastOfMajor = (type, major) => byMajor[type][major][byMajor[type][major].length - 1]

module.exports.versions = Object.keys(data)
module.exports = function (registry) {
if (typeof registry === 'string') registry = require('prismarine-registry')(registry)
const v = registry.version
if (cache[v.type][v.minecraftVersion]) return cache[v.type][v.minecraftVersion]

const lastOfMajor = {}
for (const version in data) {
const major = toMajor(version)
if (lastOfMajor[major]) {
if (minor(lastOfMajor[major]) < minor(version)) {
lastOfMajor[major] = version
}
} else {
lastOfMajor[major] = version
}
// Check exact version first, else check the last minor version of the major
const assets = data[v.type][v.minecraftVersion] || data[v.type][lastOfMajor(v.type, v.majorVersion)]
const updated = cache[v.type][v.minecraftVersion] = mcDataToNode(assets, registry)
return updated
}
module.exports.versions = { pc: Object.keys(data.pc), bedrock: Object.keys(data.bedrock) }
Loading
Loading