Skip to content

Commit 02b1860

Browse files
committed
🚧 Version warning
Added a warning when loading AJ data packs in versions they weren't exported for.
1 parent cc88b83 commit 02b1860

File tree

5 files changed

+94
-50
lines changed

5 files changed

+94
-50
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@typescript-eslint/consistent-indexed-object-style": ["warn", "record"],
4848
"@typescript-eslint/consistent-generic-constructors": "warn",
4949
"@typescript-eslint/no-namespace": "off",
50+
"@typescript-eslint/restrict-template-expressions": "off",
5051
"@typescript-eslint/naming-convention": [
5152
"warn",
5253
{

src/components/blueprintSettingsDialog.svelte

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,6 @@
575575
tooltip={translate('dialog.blueprint_settings.resource_pack_export_mode.description')}
576576
options={{
577577
raw: translate('dialog.blueprint_settings.resource_pack_export_mode.options.raw'),
578-
// zip: translate('dialog.blueprint_settings.resource_pack_export_mode.options.zip'),
579578
none: translate('dialog.blueprint_settings.resource_pack_export_mode.options.none'),
580579
}}
581580
defaultOption={'raw'}
@@ -587,7 +586,6 @@
587586
tooltip={translate('dialog.blueprint_settings.data_pack_export_mode.description')}
588587
options={{
589588
raw: translate('dialog.blueprint_settings.data_pack_export_mode.options.raw'),
590-
// zip: translate('dialog.blueprint_settings.data_pack_export_mode.options.zip'),
591589
none: translate('dialog.blueprint_settings.data_pack_export_mode.options.none'),
592590
}}
593591
defaultOption={'raw'}

src/systems/datapackCompiler/index.ts

Lines changed: 84 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { Variant } from '../../variants'
88
import { NbtByte, NbtCompound, NbtFloat, NbtInt, NbtList, NbtString } from 'deepslate/lib/nbt'
99
import {
1010
arrayToNbtFloatArray,
11+
getFunctionNamespace,
1112
matrixToNbtFloatArray,
1213
replacePathPart,
1314
sortObjectKeys,
1415
transformationToNbt,
15-
zip,
1616
} from '../util'
1717
import { BoneConfig, TextDisplayConfig } from '../../nodeConfigs'
1818
import {
@@ -26,7 +26,6 @@ import {
2626
import { JsonText } from '../minecraft/jsonText'
2727
import { MAX_PROGRESS, PROGRESS, PROGRESS_DESCRIPTION } from '../../interface/exportProgressDialog'
2828
import { eulerFromQuaternion, floatToHex, roundTo, tinycolorToDecimal } from '../../util/misc'
29-
import { setTimeout } from 'timers'
3029
import { MSLimiter } from '../../util/msLimiter'
3130

3231
const 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-
806849
async 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,

src/systems/util.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,8 @@ export function isCubeValid(cube: Cube) {
108108
}
109109
return false
110110
}
111+
112+
export function getFunctionNamespace(version: string): 'function' | 'functions' {
113+
// If the target version is 1.21.0 or higher, use the 'function' namespace instead of 'functions'
114+
return compareVersions(version, '1.21.0') ? 'function' : 'functions'
115+
}

test_blueprints/armor_stand.ajblueprint

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
"enable_plugin_mode": false,
1515
"resource_pack_export_mode": "raw",
1616
"data_pack_export_mode": "raw",
17-
"target_minecraft_version": "1.21.4",
17+
"target_minecraft_version": "1.20.4",
1818
"display_item": "minecraft:white_dye",
1919
"custom_model_data_offset": 0,
2020
"enable_advanced_resource_pack_settings": false,
2121
"enable_advanced_resource_pack_folders": false,
22-
"resource_pack": "C:\\Users\\SnaveSutit\\AppData\\Roaming\\.modrinth\\profiles\\Animated Java Dev\\resourcepacks\\resourcepack",
22+
"resource_pack": "C:\\Users\\SnaveSutit\\AppData\\Roaming\\.modrinth\\profiles\\AJ Tests 1.20.4\\resourcepacks\\resourcepack",
2323
"display_item_path": "",
2424
"model_folder": "",
2525
"texture_folder": "",
2626
"enable_advanced_data_pack_settings": false,
27-
"data_pack": "C:\\Users\\SnaveSutit\\AppData\\Roaming\\.modrinth\\profiles\\Animated Java Dev\\saves\\Animated Java Dev\\datapacks\\datapack",
27+
"data_pack": "C:\\Users\\SnaveSutit\\AppData\\Roaming\\.modrinth\\profiles\\AJ Tests 1.20.4\\saves\\AJ Tests 1_21_4\\datapacks\\datapack",
2828
"summon_commands": "",
2929
"ticking_commands": "",
3030
"interpolation_duration": 1,
@@ -1502,7 +1502,7 @@
15021502
"default": {
15031503
"name": "default",
15041504
"display_name": "Default",
1505-
"uuid": "76aede3a-8293-ba9b-f2ca-247a75dc1ea6",
1505+
"uuid": "b3c20335-1d42-762c-f9a7-2d7abaec2ece",
15061506
"texture_map": {},
15071507
"excluded_nodes": [],
15081508
"is_default": true

0 commit comments

Comments
 (0)