Skip to content

Commit b030bce

Browse files
committed
🚧 Fix Left-Over Refs in Merged Function Tags
1 parent 7cbcaa0 commit b030bce

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/systems/datapackCompiler.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ import {
1313
sortObjectKeys,
1414
} from './util'
1515
import { BoneConfig, TextDisplayConfig } from '../nodeConfigs'
16-
import { IFunctionTag, mergeTag, parseBlock, parseDataPackPath } from '../util/minecraftUtil'
16+
import {
17+
IFunctionTag,
18+
mergeTag,
19+
parseBlock,
20+
parseDataPackPath,
21+
parseResourceLocation,
22+
} from '../util/minecraftUtil'
1723
import { JsonText } from './minecraft/jsonText'
1824
import { MAX_PROGRESS, PROGRESS, PROGRESS_DESCRIPTION } from '../interface/exportProgressDialog'
1925
import { roundTo } from '../util/misc'
@@ -531,7 +537,6 @@ export async function compileDataPack(options: {
531537
for (const file of ajmeta.oldDatapack.files) {
532538
if (!isFunctionTagPath(file)) {
533539
if (fs.existsSync(file)) await fs.promises.unlink(file)
534-
PROGRESS.set(PROGRESS.get() + 1)
535540
} else if (aj.export_namespace !== Project!.last_used_export_namespace) {
536541
const resourceLocation = parseDataPackPath(file)!.resourceLocation
537542
if (
@@ -550,7 +555,6 @@ export async function compileDataPack(options: {
550555
await fs.promises.copyFile(file, newPath)
551556
await fs.promises.unlink(file)
552557
}
553-
PROGRESS.set(PROGRESS.get() + 1)
554558
}
555559
let folder = PathModule.dirname(file)
556560
while (
@@ -562,6 +566,7 @@ export async function compileDataPack(options: {
562566
removedFolders.add(folder)
563567
folder = PathModule.dirname(folder)
564568
}
569+
PROGRESS.set(PROGRESS.get() + 1)
565570
}
566571

567572
const exportedFiles = new Map<string, string>()
@@ -618,14 +623,14 @@ export async function compileDataPack(options: {
618623

619624
PROGRESS_DESCRIPTION.set('Writing Data Pack...')
620625
console.time('Writing Files took')
621-
await writeFiles(exportedFiles)
626+
await writeFiles(exportedFiles, options.dataPackFolder)
622627
console.timeEnd('Writing Files took')
623628

624629
ajmeta.write()
625630
console.timeEnd('Data Pack Compilation took')
626631
}
627632

628-
async function writeFiles(map: Map<string, string>) {
633+
async function writeFiles(map: Map<string, string>, dataPackFolder: string) {
629634
PROGRESS.set(0)
630635
MAX_PROGRESS.set(map.size)
631636
const aj = Project!.animated_java
@@ -636,6 +641,7 @@ async function writeFiles(map: Map<string, string>) {
636641
const oldFile: IFunctionTag = JSON.parse(fs.readFileSync(path, 'utf-8'))
637642
const newFile: IFunctionTag = JSON.parse(content)
638643
const merged = mergeTag(oldFile, newFile)
644+
// console.log('Merged Function Tag:', path, merged)
639645
if (aj.export_namespace !== Project!.last_used_export_namespace) {
640646
merged.values = merged.values.filter(v => {
641647
const value = typeof v === 'string' ? v : v.id
@@ -647,6 +653,28 @@ async function writeFiles(map: Map<string, string>) {
647653
)
648654
})
649655
}
656+
merged.values = merged.values.filter(v => {
657+
const value = typeof v === 'string' ? v : v.id
658+
const isTag = value.startsWith('#')
659+
const location = parseResourceLocation(isTag ? value.substring(1) : value)
660+
const vPath = PathModule.join(
661+
dataPackFolder,
662+
'data',
663+
location.namespace,
664+
isTag ? 'tags/function' : 'function',
665+
location.path + (isTag ? '.json' : '.mcfunction')
666+
)
667+
const exists = map.has(vPath) || fs.existsSync(vPath)
668+
if (!exists) {
669+
const parentLocation = parseDataPackPath(path)
670+
console.warn(
671+
`The referenced ${isTag ? 'tag' : 'function'} '${value}' in '${
672+
parentLocation?.resourceLocation || path
673+
}' does not exist! Removing reference...`
674+
)
675+
}
676+
return exists
677+
})
650678
content = JSON.stringify(merged)
651679
}
652680

test_blueprints/armor_stand.ajblueprint

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"show_bounding_box": false,
1212
"auto_bounding_box": true,
1313
"bounding_box": [95, 32],
14-
"enable_plugin_mode": true,
14+
"enable_plugin_mode": false,
1515
"enable_resource_pack": true,
1616
"enable_data_pack": true,
1717
"display_item": "minecraft:white_dye",
@@ -27,7 +27,7 @@
2727
"interpolation_duration": 1,
2828
"teleportation_duration": 1,
2929
"use_storage_for_animation": false,
30-
"baked_animations": false,
30+
"baked_animations": true,
3131
"json_file": "C:\\Users\\SnaveSutit\\Downloads\\test.json",
3232
"custom_summon_commands": ""
3333
},

0 commit comments

Comments
 (0)