@@ -6,8 +6,8 @@ import { dirname } from 'node:path'
66import supportedVersions from '../src/supportedVersions.mjs'
77import { gzipSizeFromFileSync } from 'gzip-size'
88import fs from 'fs'
9- import { default as _JsonOptimizer } from '../src/optimizeJson'
10- import { gzipSync } from 'zlib' ;
9+ import { default as _JsonOptimizer } from '../src/optimizeJson'
10+ import { gzipSync } from 'zlib'
1111import MinecraftData from 'minecraft-data'
1212import MCProtocol from 'minecraft-protocol'
1313
@@ -21,12 +21,12 @@ const require = Module.createRequire(import.meta.url)
2121
2222const dataPaths = require ( 'minecraft-data/minecraft-data/data/dataPaths.json' )
2323
24- function toMajor ( version ) {
24+ function toMajor ( version ) {
2525 const [ a , b ] = ( version + '' ) . split ( '.' )
2626 return `${ a } .${ b } `
2727}
2828
29- const versions = { }
29+ let versions = { }
3030const dataTypes = new Set ( )
3131
3232for ( const [ version , dataSet ] of Object . entries ( dataPaths . pc ) ) {
@@ -42,6 +42,31 @@ const versionToNumber = (ver) => {
4242 return + `${ x . padStart ( 2 , '0' ) } ${ y . padStart ( 2 , '0' ) } ${ z . padStart ( 2 , '0' ) } `
4343}
4444
45+ // Version clipping support
46+ const minVersion = process . env . MIN_MC_VERSION
47+ const maxVersion = process . env . MAX_MC_VERSION
48+
49+ // Filter versions based on MIN_VERSION and MAX_VERSION if provided
50+ if ( minVersion || maxVersion ) {
51+ const filteredVersions = { }
52+ const minVersionNum = minVersion ? versionToNumber ( minVersion ) : 0
53+ const maxVersionNum = maxVersion ? versionToNumber ( maxVersion ) : Infinity
54+
55+ for ( const [ version , dataSet ] of Object . entries ( versions ) ) {
56+ const versionNum = versionToNumber ( version )
57+ if ( versionNum >= minVersionNum && versionNum <= maxVersionNum ) {
58+ filteredVersions [ version ] = dataSet
59+ }
60+ }
61+
62+ versions = filteredVersions
63+
64+ console . log ( `Version clipping applied: ${ minVersion || 'none' } to ${ maxVersion || 'none' } ` )
65+ console . log ( `Processing ${ Object . keys ( versions ) . length } versions:` , Object . keys ( versions ) . sort ( ( a , b ) => versionToNumber ( a ) - versionToNumber ( b ) ) )
66+ }
67+
68+ console . log ( 'Bundling version range:' , Object . keys ( versions ) [ 0 ] , 'to' , Object . keys ( versions ) . at ( - 1 ) )
69+
4570// if not included here (even as {}) will not be bundled & accessible!
4671// const compressedOutput = !!process.env.SINGLE_FILE_BUILD
4772const compressedOutput = true
@@ -57,18 +82,20 @@ const dataTypeBundling2 = {
5782 }
5883}
5984const dataTypeBundling = {
60- language : {
85+ language : process . env . SKIP_MC_DATA_LANGUAGE === 'true' ? {
86+ raw : { }
87+ } : {
6188 ignoreRemoved : true ,
6289 ignoreChanges : true
6390 } ,
6491 blocks : {
6592 arrKey : 'name' ,
66- processData ( current , prev ) {
93+ processData ( current , prev ) {
6794 for ( const block of current ) {
6895 if ( block . transparent ) {
6996 const forceOpaque = block . name . includes ( 'shulker_box' ) || block . name . match ( / ^ d o u b l e _ .+ _ s l a b \d ? $ / ) || [ 'melon_block' , 'lit_pumpkin' , 'lit_redstone_ore' , 'lit_furnace' ] . includes ( block . name )
7097
71- const prevBlock = prev ?. find ( x => x . name === block . name ) ;
98+ const prevBlock = prev ?. find ( x => x . name === block . name )
7299 if ( forceOpaque || ( prevBlock && ! prevBlock . transparent ) ) {
73100 block . transparent = false
74101 }
@@ -136,7 +163,9 @@ const dataTypeBundling = {
136163 blockLoot : {
137164 arrKey : 'block'
138165 } ,
139- recipes : {
166+ recipes : process . env . SKIP_MC_DATA_RECIPES === 'true' ? {
167+ raw : { }
168+ } : {
140169 raw : true
141170 // processData: processRecipes
142171 } ,
@@ -150,7 +179,7 @@ const dataTypeBundling = {
150179 // }
151180}
152181
153- function processRecipes ( current , prev , getData , version ) {
182+ function processRecipes ( current , prev , getData , version ) {
154183 // can require the same multiple times per different versions
155184 if ( current . _proccessed ) return
156185 const items = getData ( 'items' )
@@ -242,30 +271,39 @@ for (const [i, [version, dataSet]] of versionsArr.reverse().entries()) {
242271 for ( const [ dataType , dataPath ] of Object . entries ( dataSet ) ) {
243272 const config = dataTypeBundling [ dataType ]
244273 if ( ! config ) continue
245- if ( dataType === 'blockCollisionShapes' && versionToNumber ( version ) >= versionToNumber ( '1.13' ) ) {
246- // contents += ` get ${dataType} () { return window.globalGetCollisionShapes?.("${version}") },\n`
247- continue
248- }
274+ const ignoreCollisionShapes = dataType === 'blockCollisionShapes' && versionToNumber ( version ) >= versionToNumber ( '1.13' )
275+
249276 let injectCode = ''
250- const getData = ( type ) => {
277+ const getRealData = ( type ) => {
251278 const loc = `minecraft-data/data/${ dataSet [ type ] } /`
252279 const dataPathAbsolute = require . resolve ( `minecraft-data/${ loc } ${ type } ` )
253280 // const data = fs.readFileSync(dataPathAbsolute, 'utf8')
254281 const dataRaw = require ( dataPathAbsolute )
255282 return dataRaw
256283 }
257- const dataRaw = getData ( dataType )
284+ const dataRaw = getRealData ( dataType )
258285 let rawData = dataRaw
259286 if ( config . raw ) {
260287 rawDataVersions [ dataType ] ??= { }
261288 rawDataVersions [ dataType ] [ version ] = rawData
262- rawData = dataRaw
289+ if ( config . raw === true ) {
290+ rawData = dataRaw
291+ } else {
292+ rawData = config . raw
293+ }
294+
295+ if ( ignoreCollisionShapes && dataType === 'blockCollisionShapes' ) {
296+ rawData = {
297+ blocks : { } ,
298+ shapes : { }
299+ }
300+ }
263301 } else {
264302 if ( ! diffSources [ dataType ] ) {
265303 diffSources [ dataType ] = new JsonOptimizer ( config . arrKey , config . ignoreChanges , config . ignoreRemoved )
266304 }
267305 try {
268- config . processData ?. ( dataRaw , previousData [ dataType ] , getData , version )
306+ config . processData ?. ( dataRaw , previousData [ dataType ] , getRealData , version )
269307 diffSources [ dataType ] . recordDiff ( version , dataRaw )
270308 injectCode = `restoreDiff(sources, ${ JSON . stringify ( dataType ) } , ${ JSON . stringify ( version ) } )`
271309 } catch ( err ) {
@@ -297,16 +335,16 @@ console.log('total size (mb)', totalSize / 1024 / 1024)
297335console . log (
298336 'size per data type (mb, %)' ,
299337 Object . fromEntries ( Object . entries ( sizePerDataType ) . map ( ( [ dataType , size ] ) => {
300- return [ dataType , [ size / 1024 / 1024 , Math . round ( size / totalSize * 100 ) ] ] ;
338+ return [ dataType , [ size / 1024 / 1024 , Math . round ( size / totalSize * 100 ) ] ]
301339 } ) . sort ( ( a , b ) => {
302340 //@ts -ignore
303- return b [ 1 ] [ 1 ] - a [ 1 ] [ 1 ] ;
341+ return b [ 1 ] [ 1 ] - a [ 1 ] [ 1 ]
304342 } ) )
305343)
306344
307345function compressToBase64 ( input ) {
308- const buffer = gzipSync ( input ) ;
309- return buffer . toString ( 'base64' ) ;
346+ const buffer = gzipSync ( input )
347+ return buffer . toString ( 'base64' )
310348}
311349
312350const filePath = './generated/minecraft-data-optimized.json'
0 commit comments