@@ -6,8 +6,8 @@ import { dirname } from 'node:path'
6
6
import supportedVersions from '../src/supportedVersions.mjs'
7
7
import { gzipSizeFromFileSync } from 'gzip-size'
8
8
import 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'
11
11
import MinecraftData from 'minecraft-data'
12
12
import MCProtocol from 'minecraft-protocol'
13
13
@@ -21,12 +21,12 @@ const require = Module.createRequire(import.meta.url)
21
21
22
22
const dataPaths = require ( 'minecraft-data/minecraft-data/data/dataPaths.json' )
23
23
24
- function toMajor ( version ) {
24
+ function toMajor ( version ) {
25
25
const [ a , b ] = ( version + '' ) . split ( '.' )
26
26
return `${ a } .${ b } `
27
27
}
28
28
29
- const versions = { }
29
+ let versions = { }
30
30
const dataTypes = new Set ( )
31
31
32
32
for ( const [ version , dataSet ] of Object . entries ( dataPaths . pc ) ) {
@@ -42,6 +42,31 @@ const versionToNumber = (ver) => {
42
42
return + `${ x . padStart ( 2 , '0' ) } ${ y . padStart ( 2 , '0' ) } ${ z . padStart ( 2 , '0' ) } `
43
43
}
44
44
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
+
45
70
// if not included here (even as {}) will not be bundled & accessible!
46
71
// const compressedOutput = !!process.env.SINGLE_FILE_BUILD
47
72
const compressedOutput = true
@@ -57,18 +82,20 @@ const dataTypeBundling2 = {
57
82
}
58
83
}
59
84
const dataTypeBundling = {
60
- language : {
85
+ language : process . env . SKIP_MC_DATA_LANGUAGE === 'true' ? {
86
+ raw : { }
87
+ } : {
61
88
ignoreRemoved : true ,
62
89
ignoreChanges : true
63
90
} ,
64
91
blocks : {
65
92
arrKey : 'name' ,
66
- processData ( current , prev ) {
93
+ processData ( current , prev ) {
67
94
for ( const block of current ) {
68
95
if ( block . transparent ) {
69
96
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 )
70
97
71
- const prevBlock = prev ?. find ( x => x . name === block . name ) ;
98
+ const prevBlock = prev ?. find ( x => x . name === block . name )
72
99
if ( forceOpaque || ( prevBlock && ! prevBlock . transparent ) ) {
73
100
block . transparent = false
74
101
}
@@ -136,7 +163,9 @@ const dataTypeBundling = {
136
163
blockLoot : {
137
164
arrKey : 'block'
138
165
} ,
139
- recipes : {
166
+ recipes : process . env . SKIP_MC_DATA_RECIPES === 'true' ? {
167
+ raw : { }
168
+ } : {
140
169
raw : true
141
170
// processData: processRecipes
142
171
} ,
@@ -150,7 +179,7 @@ const dataTypeBundling = {
150
179
// }
151
180
}
152
181
153
- function processRecipes ( current , prev , getData , version ) {
182
+ function processRecipes ( current , prev , getData , version ) {
154
183
// can require the same multiple times per different versions
155
184
if ( current . _proccessed ) return
156
185
const items = getData ( 'items' )
@@ -242,30 +271,39 @@ for (const [i, [version, dataSet]] of versionsArr.reverse().entries()) {
242
271
for ( const [ dataType , dataPath ] of Object . entries ( dataSet ) ) {
243
272
const config = dataTypeBundling [ dataType ]
244
273
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
+
249
276
let injectCode = ''
250
- const getData = ( type ) => {
277
+ const getRealData = ( type ) => {
251
278
const loc = `minecraft-data/data/${ dataSet [ type ] } /`
252
279
const dataPathAbsolute = require . resolve ( `minecraft-data/${ loc } ${ type } ` )
253
280
// const data = fs.readFileSync(dataPathAbsolute, 'utf8')
254
281
const dataRaw = require ( dataPathAbsolute )
255
282
return dataRaw
256
283
}
257
- const dataRaw = getData ( dataType )
284
+ const dataRaw = getRealData ( dataType )
258
285
let rawData = dataRaw
259
286
if ( config . raw ) {
260
287
rawDataVersions [ dataType ] ??= { }
261
288
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
+ }
263
301
} else {
264
302
if ( ! diffSources [ dataType ] ) {
265
303
diffSources [ dataType ] = new JsonOptimizer ( config . arrKey , config . ignoreChanges , config . ignoreRemoved )
266
304
}
267
305
try {
268
- config . processData ?. ( dataRaw , previousData [ dataType ] , getData , version )
306
+ config . processData ?. ( dataRaw , previousData [ dataType ] , getRealData , version )
269
307
diffSources [ dataType ] . recordDiff ( version , dataRaw )
270
308
injectCode = `restoreDiff(sources, ${ JSON . stringify ( dataType ) } , ${ JSON . stringify ( version ) } )`
271
309
} catch ( err ) {
@@ -297,16 +335,16 @@ console.log('total size (mb)', totalSize / 1024 / 1024)
297
335
console . log (
298
336
'size per data type (mb, %)' ,
299
337
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 ) ] ]
301
339
} ) . sort ( ( a , b ) => {
302
340
//@ts -ignore
303
- return b [ 1 ] [ 1 ] - a [ 1 ] [ 1 ] ;
341
+ return b [ 1 ] [ 1 ] - a [ 1 ] [ 1 ]
304
342
} ) )
305
343
)
306
344
307
345
function compressToBase64 ( input ) {
308
- const buffer = gzipSync ( input ) ;
309
- return buffer . toString ( 'base64' ) ;
346
+ const buffer = gzipSync ( input )
347
+ return buffer . toString ( 'base64' )
310
348
}
311
349
312
350
const filePath = './generated/minecraft-data-optimized.json'
0 commit comments