Skip to content

Commit c6cb5b2

Browse files
committed
Smartened up model path resolution. Should work in 99.9% of all cases now
1 parent 85dc0a1 commit c6cb5b2

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

src/exporting.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { settings } from './settings'
44
import { mkdir } from './util/ezfs'
55
import { CustomError } from './util/customError'
66
import { tl } from './util/intl'
7+
import { getModelPath } from './util/minecraft/resourcepack'
78
// import { safeFunctionName } from './util'
89
import transparent from './assets/transparent.png'
910

@@ -131,13 +132,6 @@ async function exportRigModels(models, variantModels) {
131132

132133
console.groupEnd('Export Rig Models')
133134
}
134-
function getMCPath(raw) {
135-
let list = raw.split(path.sep)
136-
console.log(list)
137-
const index = list.indexOf('assets')
138-
list = list.slice(index + 1, list.length)
139-
return `${list[0]}:${list.slice(2).join('/')}`
140-
}
141135

142136
async function exportPredicate(models, variantModels, ajSettings) {
143137
console.groupCollapsed('Export Predicate Model')
@@ -150,20 +144,18 @@ async function exportPredicate(models, variantModels, ajSettings) {
150144
overrides: [],
151145
}
152146

153-
const modelPath = getMCPath(ajSettings.rigModelsExportFolder)
154-
console.log(modelPath)
155147
for (const [modelName, model] of Object.entries(models)) {
156148
predicateJSON.overrides.push({
157149
predicate: { custom_model_data: model.aj.customModelData },
158-
model: modelPath + '/' + modelName,
150+
model: getModelPath(path.join(ajSettings.rigModelsExportFolder, modelName)),
159151
})
160152
}
161153

162154
for (const [variantName, variant] of Object.entries(variantModels))
163155
for (const [modelName, model] of Object.entries(variant)) {
164156
predicateJSON.overrides.push({
165157
predicate: { custom_model_data: model.aj.customModelData },
166-
model: [modelPath, variantName, `${modelName}`].join('/'),
158+
model: getModelPath(path.join(modelPath, variantName, modelName), modelName),
167159
})
168160
}
169161

src/lang/en.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ animatedJava.popup.error.unableToGenerateTexturePath.body: |-
7474
Please make sure it is properly saved inside of a valid Resource Pack in a sub folder of 'assets/namespace/textures/'
7575
eg. 'resources/assets/minecraft/textures/item/diamond.png'
7676
77+
animatedJava.popup.error.unableToGenerateModelPath.title: Invalid texture path
78+
animatedJava.popup.error.unableToGenerateModelPath.body: |-
79+
The model '%modelName' has an invalid location inside a Resource Pack
80+
Please make sure it is properly saved inside of a valid Resource Pack in a sub folder of 'assets/namespace/models/'
81+
eg. 'resources/assets/minecraft/models/item/diamond.json'
82+
7783
animatedJava.popup.error.invalidCubeRotations.title: Invalid Element Rotations
7884
animatedJava.popup.error.invalidCubeRotations.body: |-
7985
The rotations of some cubes are invalid for the java model format.

src/util/minecraft/resourcepack.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export function getTexturePath(texture: any) {
3232
const textureIndex = relative.indexOf('textures') // Locate 'texture' in the path
3333
if (textureIndex > -1) {
3434
relative.splice(textureIndex, 1) // Remove 'texture' from the path
35-
return `${namespace}:${relative.join('/')}` // Generate texture path
35+
console.log('Texture Path', `${namespace}:${path.join(...relative)}`)
36+
return `${namespace}:${path.join(...relative)}` // Generate texture path
3637
}
3738
}
3839
}
@@ -54,3 +55,40 @@ export function getTexturePath(texture: any) {
5455
},
5556
})
5657
}
58+
59+
export function getModelPath(modelPath: string, modelName: string) {
60+
console.log(modelPath)
61+
const parts = modelPath.split(path.sep)
62+
const assetsIndex = parts.indexOf('assets')
63+
if (assetsIndex) {
64+
const relative = parts.slice(assetsIndex + 1) // Remove 'assets' and everything before it from the path
65+
const namespace = relative.shift() // Remove the namespace from the path and store it
66+
if (namespace && relative.length) {
67+
relative.push(relative.pop().replace(/.png$/, '')) // Remove file type (.png)
68+
if (relative) {
69+
const modelsIndex = relative.indexOf('models') // Locate 'texture' in the path
70+
if (modelsIndex > -1) {
71+
relative.splice(modelsIndex, 1) // Remove 'texture' from the path
72+
console.log('Model Path', `${namespace}:${path.join(...relative)}`)
73+
return `${namespace}:${path.join(...relative)}` // Generate texture path
74+
}
75+
}
76+
}
77+
}
78+
throw new CustomError('Unable to generate model path', {
79+
dialog: {
80+
title: tl(
81+
'animatedJava.popup.error.unableToGenerateModelPath.title'
82+
),
83+
lines: format(
84+
tl('animatedJava.popup.error.unableToGenerateModelPath.body'),
85+
{
86+
modelName
87+
}
88+
)
89+
.split('\n')
90+
.map((line: string) => `<p>${line}</p>`),
91+
width: 512,
92+
},
93+
})
94+
}

0 commit comments

Comments
 (0)