Skip to content

Commit a7ecfb0

Browse files
committed
Fix predicate merger
1 parent 179e8ec commit a7ecfb0

File tree

1 file changed

+57
-53
lines changed

1 file changed

+57
-53
lines changed

src/exporter/resourcePackExporter.ts

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ export async function exportResources(
2929
textureExportFolder: string,
3030
rigItemModelExportPath: string
3131
) {
32+
const advancedResourcePackSettingsEnabled =
33+
projectSettings.enable_advanced_resource_pack_settings.value
3234
const projectNamespace = projectSettings.project_namespace.value
3335
const resourcePackPath = PathModule.parse(projectSettings.resource_pack_mcmeta.value).dir
34-
const resourcePackFolder = new VirtualFolder('internal_resource_pack_folder')
36+
const resourcePackFolder = new VirtualFolder(
37+
advancedResourcePackSettingsEnabled
38+
? 'internal_resource_pack_folder'
39+
: PathModule.basename(resourcePackPath)
40+
)
3541
const assetsPackFolder = resourcePackFolder.newFolder('assets')
36-
const advancedResourcePackSettingsEnabled =
37-
projectSettings.enable_advanced_resource_pack_settings.value
3842

3943
//------------------------------------
4044
// Minecraft namespace
@@ -61,8 +65,16 @@ export async function exportResources(
6165

6266
const predicateItemFilePath = advancedResourcePackSettingsEnabled
6367
? rigItemModelExportPath
64-
: PathModule.join(resourcePackPath, minecraftFolder.path, `${rigItemName}.json`)
65-
const content: IPredicateItemModel = {
68+
: PathModule.join(
69+
PathModule.dirname(resourcePackPath),
70+
minecraftFolder.path,
71+
`${rigItemName}.json`
72+
)
73+
74+
console.log('Predicate item file path:', predicateItemFilePath)
75+
76+
// Default predicate item file content
77+
let content: IPredicateItemModel = {
6678
parent: 'item/generated',
6779
textures: {
6880
layer0: `${rigItemNamespace}:item/${rigItemName}`,
@@ -72,64 +84,56 @@ export async function exportResources(
7284
rigs: {},
7385
},
7486
}
75-
const predicateItemFile = minecraftFolder.newFile(
76-
`${rigItemName}.json`,
77-
content,
78-
// TODO
79-
(oldContent, newContent) => {
80-
console.log('Merging predicate file...', oldContent, newContent)
81-
if (!oldContent.animated_java) {
82-
showPredicateFileOverwriteConfirmation(predicateItemFilePath)
83-
oldContent.animated_java = {
84-
rigs: {
85-
[projectNamespace]: { used_ids: [] },
86-
},
87-
}
88-
}
89-
return newContent as unknown
90-
}
91-
)
92-
let successfullyReadPredicateItemFile = false
87+
const usedIds: number[] = [] // IDs that are used by other projects
88+
const consumedIds: number[] = [] // IDs that are used by this project
89+
// Read predicate item file if it exists
9390
if (fs.existsSync(predicateItemFilePath)) {
94-
const stringContent = await fs.promises.readFile(predicateItemFilePath, 'utf8')
91+
console.log('Reading predicate item file')
9592
try {
96-
const localContent = JSON.parse(stringContent)
97-
Object.assign(content, localContent)
98-
successfullyReadPredicateItemFile = true
93+
const stringContent = await fs.promises.readFile(predicateItemFilePath, 'utf8')
94+
content = JSON.parse(stringContent)
9995
} catch (e) {
100-
console.warn('Failed to read predicate item file as JSON')
96+
console.warn('Failed to read predicate item file JSON')
10197
console.warn(e)
10298
}
103-
} else successfullyReadPredicateItemFile = true
104-
if (!successfullyReadPredicateItemFile || !content.animated_java) {
105-
showPredicateFileOverwriteConfirmation(predicateItemFilePath)
106-
}
107-
if (!content.overrides) content.overrides = []
108-
if (!content.animated_java.rigs) content.animated_java.rigs = {}
109-
110-
// const content = predicateItemFile.content as IPredicateItemModel
111-
const usedIds: number[] = [] // IDs that are used by other projects
112-
const consumedIds: number[] = [] // IDs that are used by this project
113-
for (const [name, rig] of Object.entries(content.animated_java.rigs)) {
114-
if (!rig.used_ids) {
115-
console.warn('Found existing rig in predicate file, but it is missing used_ids.')
116-
continue
99+
// Show overwrite confirmation if predicate file wasn't created by animated_java.
100+
if (!content.animated_java) {
101+
showPredicateFileOverwriteConfirmation(predicateItemFilePath)
102+
content.animated_java = {
103+
rigs: {
104+
ORIGINAL_PREDICATE_FILE: {
105+
used_ids: content.overrides
106+
.filter(o => o.predicate.custom_model_data !== undefined)
107+
.map(o => o.predicate.custom_model_data),
108+
},
109+
},
110+
}
117111
}
118-
const localUsedIds = rig.used_ids
119-
if (name === projectNamespace) {
120-
// Clean out old overrides
121-
content.overrides = content.overrides.filter(o => {
122-
return !localUsedIds.includes(o.predicate.custom_model_data)
123-
})
124-
continue
112+
113+
// Clean up content
114+
content.animated_java ??= { rigs: {} }
115+
content.animated_java.rigs ??= {}
116+
// Merge with existing predicate file
117+
console.log('Merging with existing predicate file')
118+
console.log(content)
119+
for (const [name, rig] of Object.entries(content.animated_java.rigs)) {
120+
const localUsedIds = rig.used_ids
121+
if (name === projectNamespace) {
122+
// Clean out old overrides
123+
content.overrides = content.overrides.filter(o => {
124+
return !localUsedIds.includes(o.predicate.custom_model_data)
125+
})
126+
continue
127+
}
128+
usedIds.push(...localUsedIds)
125129
}
126-
usedIds.push(...rig.used_ids)
127130
}
128131

129132
CustomModelData.usedIds = usedIds
130-
content.animated_java.rigs[projectNamespace] = {
131-
used_ids: consumedIds,
132-
}
133+
content.animated_java.rigs[projectNamespace] = { used_ids: consumedIds }
134+
135+
// Create virtual predicate item file with content
136+
const predicateItemFile = minecraftFolder.newFile(`${rigItemName}.json`, content)
133137

134138
//------------------------------------
135139
// Project namespace

0 commit comments

Comments
 (0)