From 91bb1d75ffdf56f8602d94b61ec2879bc53d7275 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Fri, 15 Sep 2023 04:49:20 +0300 Subject: [PATCH] add blocks generate script for resourcepack --- .gitignore | 1 + package.json | 3 +- scripts/gen-texturepack-files.mjs | 51 ++++++++++++++++++++++++++++++ scripts/test-texturepack-files.mjs | 16 ++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 scripts/gen-texturepack-files.mjs create mode 100644 scripts/test-texturepack-files.mjs diff --git a/.gitignore b/.gitignore index 950306c9..131dc688 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ world out *.iml .vercel +generated diff --git a/package.json b/package.json index a947d7a1..95bde096 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -104,7 +105,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" } diff --git a/scripts/gen-texturepack-files.mjs b/scripts/gen-texturepack-files.mjs new file mode 100644 index 00000000..ccac9569 --- /dev/null +++ b/scripts/gen-texturepack-files.mjs @@ -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} */ +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 })) diff --git a/scripts/test-texturepack-files.mjs b/scripts/test-texturepack-files.mjs new file mode 100644 index 00000000..0446a2fe --- /dev/null +++ b/scripts/test-texturepack-files.mjs @@ -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) + } +}