Skip to content

Commit

Permalink
add blocks generate script for resourcepack
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Sep 19, 2023
1 parent 9947c13 commit 1f14154
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ world
out
*.iml
.vercel
generated
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test:cypress": "cypress run",
"test:e2e": "start-test http-get://localhost:8080 test:cypress",
"prod-start": "node server.js",
"postinstall": "node scripts/gen-texturepack-files.mjs",
"prepublishOnly": "npm run build"
},
"keywords": [
Expand Down Expand Up @@ -108,7 +109,7 @@
"overrides": {
"prismarine-block": "github:zardoy/prismarine-block#next-era",
"prismarine-world": "github:zardoy/prismarine-world#next-era",
"minecraft-data": "latest",
"minecraft-data": "3.45.0",
"prismarine-provider-anvil": "github:zardoy/prismarine-provider-anvil#everything",
"minecraft-protocol": "github:zardoy/minecraft-protocol#custom-client-extra"
}
Expand Down
51 changes: 51 additions & 0 deletions scripts/gen-texturepack-files.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//@ts-check
import fs from 'fs'
import minecraftAssets from 'minecraft-assets'

// why store another data?
// 1. want to make it compatible (at least for now)
// 2. don't want to read generated blockStates as it might change in future, and the current way was faster to implement

const blockNames = []
const indexesPerVersion = {}
/** @type {Map<string, number>} */
const allBlocksMap = new Map()
const getBlockIndex = (block) => {
if (allBlocksMap.has(block)) {
return allBlocksMap.get(block)
}

const index = blockNames.length
allBlocksMap.set(block, index)
blockNames.push(block)
return index
}

// const blocksFull = []
// const allBlocks = []
// // we can even optimize it even futher by doing prev-step resolving
// const blocksDiff = {}

for (const [i, version] of minecraftAssets.versions.reverse().entries()) {
const assets = minecraftAssets(version)
const blocksDir = assets.directory + '/blocks'
const blocks = fs.readdirSync(blocksDir)
indexesPerVersion[version] = blocks.map(block => {
if (!block.endsWith('.png')) return undefined
return getBlockIndex(block)
}).filter(i => i !== undefined)

// if (!blocksFull.length) {
// // first iter
// blocksFull.push(...blocks)
// } else {
// const missing = blocksFull.map((b, i) => !blocks.includes(b) ? i : -1).filter(i => i !== -1)
// const added = blocks.filter(b => !blocksFull.includes(b))
// blocksDiff[version] = {
// missing,
// added
// }
// }
}

fs.writeFileSync('./generated/blocks.json', JSON.stringify({ blockNames: blockNames, indexes: indexesPerVersion }))
16 changes: 16 additions & 0 deletions scripts/test-texturepack-files.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fs from 'fs'
import minecraftAssets from 'minecraft-assets'

const gen = JSON.parse(fs.readFileSync('./blocks.json', 'utf8'))

const version = '1.8.8'
const { blockNames, indexes } = gen

const blocksActual = indexes[version].map((i) => blockNames[i])

const blocksExpected = fs.readdirSync(minecraftAssets(version).directory + '/blocks')
for (const [i, item] of blocksActual.entries()) {
if (item !== blocksExpected[i]) {
console.log('not equal at', i)
}
}

0 comments on commit 1f14154

Please sign in to comment.