@@ -8,11 +8,11 @@ import { Variant } from '../../variants'
88import { NbtByte , NbtCompound , NbtFloat , NbtInt , NbtList , NbtString } from 'deepslate/lib/nbt'
99import {
1010 arrayToNbtFloatArray ,
11+ getFunctionNamespace ,
1112 matrixToNbtFloatArray ,
1213 replacePathPart ,
1314 sortObjectKeys ,
1415 transformationToNbt ,
15- zip ,
1616} from '../util'
1717import { BoneConfig , TextDisplayConfig } from '../../nodeConfigs'
1818import {
@@ -26,7 +26,6 @@ import {
2626import { JsonText } from '../minecraft/jsonText'
2727import { MAX_PROGRESS , PROGRESS , PROGRESS_DESCRIPTION } from '../../interface/exportProgressDialog'
2828import { eulerFromQuaternion , floatToHex , roundTo , tinycolorToDecimal } from '../../util/misc'
29- import { setTimeout } from 'timers'
3029import { MSLimiter } from '../../util/msLimiter'
3130
3231const BONE_TYPES = [ 'bone' , 'text_display' , 'item_display' , 'block_display' ]
@@ -616,7 +615,9 @@ export default async function compileDataPack(options: {
616615 animationHash : string
617616} ) {
618617 console . time ( 'Data Pack Compilation took' )
619- const { rig, animations, rigHash, animationHash } = options
618+ const { rig, animations, rigHash, animationHash, dataPackFolder } = options
619+ const overrideFolder = PathModule . join ( dataPackFolder , 'animated_java' )
620+
620621 const aj = Project ! . animated_java
621622 console . log ( 'Compiling Data Pack...' , options )
622623 const compiler = new Compiler ( 'src/' , {
@@ -635,10 +636,10 @@ export default async function compileDataPack(options: {
635636 let ajmeta : DataPackAJMeta | null = null
636637 if ( aj . data_pack_export_mode === 'raw' ) {
637638 ajmeta = new DataPackAJMeta (
638- PathModule . join ( options . dataPackFolder , 'data.ajmeta' ) ,
639+ PathModule . join ( dataPackFolder , 'data.ajmeta' ) ,
639640 aj . export_namespace ,
640641 Project ! . last_used_export_namespace ,
641- options . dataPackFolder
642+ dataPackFolder
642643 )
643644 ajmeta . read ( )
644645
@@ -698,7 +699,7 @@ export default async function compileDataPack(options: {
698699 function createSyncIO ( ) : SyncIo {
699700 const io = new SyncIo ( )
700701 io . write = ( localPath , content ) => {
701- const writePath = PathModule . join ( options . dataPackFolder , localPath )
702+ const writePath = PathModule . join ( overrideFolder , localPath )
702703 exportedFiles . set ( writePath , content )
703704 if ( ajmeta ) ajmeta . files . add ( writePath )
704705 }
@@ -753,56 +754,98 @@ export default async function compileDataPack(options: {
753754
754755 PROGRESS_DESCRIPTION . set ( 'Compiling Data Pack...' )
755756 PROGRESS . set ( 0 )
756- await new Promise ( resolve => setTimeout ( resolve , 2000 / framespersecond ) )
757+ await new Promise ( resolve => requestAnimationFrame ( resolve ) )
757758 console . time ( 'MC-Build Compiler took' )
758759 const tokens = Tokenizer . tokenize ( mcbFile , 'src/animated_java.mcb' )
759760 compiler . addFile ( 'src/animated_java.mcb' , Parser . parseMcbFile ( tokens ) )
760761 compiler . compile ( VariableMap . fromObject ( variables ) )
761762 console . timeEnd ( 'MC-Build Compiler took' )
762763
764+ // Incorrect version warning
765+ const functionNamespace = getFunctionNamespace ( aj . target_minecraft_version )
766+ const invalidVersionMessage = new JsonText ( [
767+ TELLRAW_ERROR_PREFIX ( ) ,
768+ [
769+ {
770+ text : 'Attempting to load an Animated Java Data Pack that was exported for ' ,
771+ color : 'red' ,
772+ } ,
773+ { text : `Minecraft ${ aj . target_minecraft_version } ` , color : 'aqua' } ,
774+ { text : ' in an different version!' , color : 'red' } ,
775+ {
776+ text : '\n Please ensure that the data pack is loaded in the correct version, or that your blueprint settings are configured to target the correct version(s) of Minecraft.' ,
777+ color : 'yellow' ,
778+ } ,
779+ ] ,
780+ TELLRAW_SUFFIX ( ) ,
781+ ] )
782+ exportedFiles . set (
783+ PathModule . join (
784+ dataPackFolder ,
785+ `data/animated_java/${ functionNamespace } /global/on_load.mcfunction`
786+ ) ,
787+ `tellraw @a ${ invalidVersionMessage } `
788+ )
789+ exportedFiles . set (
790+ PathModule . join ( dataPackFolder , `data/minecraft/tags/${ functionNamespace } /load.json` ) ,
791+ autoStringify ( {
792+ replace : false ,
793+ values : [ 'animated_java:global/on_load' ] ,
794+ } )
795+ )
796+
797+ interface IPackMeta {
798+ pack ?: {
799+ pack_format ?: number
800+ description ?: string
801+ }
802+ overlays ?: {
803+ entries ?: Array < {
804+ directory ?: string
805+ formats ?: number | number [ ] | { min_inclusive : number ; max_inclusive : number }
806+ } >
807+ }
808+ }
809+
810+ // pack.mcmeta
811+ const packMetaPath = PathModule . join ( dataPackFolder , 'pack.mcmeta' )
812+ let packMeta = { } as IPackMeta
813+ if ( fs . existsSync ( packMetaPath ) ) {
814+ try {
815+ const content = fs . readFileSync ( packMetaPath , 'utf-8' )
816+ packMeta = JSON . parse ( content )
817+ } catch ( e ) {
818+ console . error ( 'Failed to parse pack.mcmeta:' , e )
819+ }
820+ }
821+ packMeta . pack ??= { }
822+ packMeta . pack . pack_format = getDataPackFormat ( aj . target_minecraft_version )
823+ packMeta . pack . description ??= `Animated Java Data Pack for ${ aj . target_minecraft_version } `
824+ packMeta . overlays ??= { }
825+ packMeta . overlays . entries ??= [ ]
826+ const overlay = packMeta . overlays . entries . find ( e => e . directory === 'animated_java' )
827+ if ( ! overlay ) {
828+ packMeta . overlays . entries . push ( {
829+ directory : 'animated_java' ,
830+ formats : getDataPackFormat ( aj . target_minecraft_version ) ,
831+ } )
832+ } else {
833+ overlay . formats = getDataPackFormat ( aj . target_minecraft_version )
834+ }
835+
836+ exportedFiles . set ( PathModule . join ( dataPackFolder , 'pack.mcmeta' ) , autoStringify ( packMeta ) )
837+
763838 PROGRESS_DESCRIPTION . set ( 'Writing Data Pack...' )
764839 if ( aj . data_pack_export_mode === 'raw' ) {
765840 console . time ( 'Writing Files took' )
766- await writeFiles ( exportedFiles , options . dataPackFolder )
841+ await writeFiles ( exportedFiles , overrideFolder )
767842 console . timeEnd ( 'Writing Files took' )
768843 ajmeta ! . write ( )
769- } else if ( aj . data_pack_export_mode === 'zip' ) {
770- exportedFiles . set (
771- PathModule . join ( options . dataPackFolder , 'pack.mcmeta' ) ,
772- autoStringify ( {
773- pack : {
774- pack_format : 48 ,
775- description : `${ Project ! . name } . Generated with Animated Java` ,
776- } ,
777- } )
778- )
779-
780- const exportPath =
781- options . dataPackFolder + ( options . dataPackFolder . endsWith ( '.zip' ) ? '' : '.zip' )
782- console . time ( 'Writing Zip took' )
783- await writeZip ( exportedFiles , exportPath )
784- console . timeEnd ( 'Writing Zip took' )
785844 }
786845
787846 console . timeEnd ( 'Data Pack Compilation took' )
788847}
789848
790- async function writeZip ( map : Map < string , string > , dataPackPath : string ) {
791- const data : Record < string , Uint8Array > = { }
792-
793- for ( const [ path , content ] of map ) {
794- const relativePath = PathModule . relative ( dataPackPath , path )
795- if ( typeof content === 'string' ) {
796- data [ relativePath ] = Buffer . from ( content )
797- } else {
798- data [ relativePath ] = content
799- }
800- }
801-
802- const zipped = await zip ( data , { } )
803- await fs . promises . writeFile ( dataPackPath , zipped )
804- }
805-
806849async function writeFiles ( map : Map < string , string > , dataPackFolder : string ) {
807850 PROGRESS . set ( 0 )
808851 MAX_PROGRESS . set ( map . size )
@@ -829,10 +872,7 @@ async function writeFiles(map: Map<string, string>, dataPackFolder: string) {
829872 const value = typeof v === 'string' ? v : v . id
830873 const isTag = value . startsWith ( '#' )
831874 const location = parseResourceLocation ( isTag ? value . substring ( 1 ) : value )
832- // If the target version is 1.21.0 or higher, use the 'function' namespace instead of 'functions'
833- const functionNamespace = compareVersions ( aj . target_minecraft_version , '1.21.0' )
834- ? 'function'
835- : 'functions'
875+ const functionNamespace = getFunctionNamespace ( aj . target_minecraft_version )
836876 console . log ( 'Checking:' , value , location , functionNamespace )
837877 const vPath = PathModule . join (
838878 dataPackFolder ,
0 commit comments