Skip to content

Commit 50f8830

Browse files
committed
🛠️ Fix #359
1 parent 538b1c3 commit 50f8830

File tree

4 files changed

+86
-97
lines changed

4 files changed

+86
-97
lines changed

src/systems/datapackCompiler/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ function getNodeTags(node: AnyRenderedNode, rig: IRenderedRig): NbtList {
5454

5555
function recurseParents(node: AnyRenderedNode) {
5656
if (node.parent) {
57-
console.log('Parent:', node.parent)
5857
parentNames.push({
5958
name: rig.nodes[node.parent].safe_name,
6059
type: rig.nodes[node.parent].type,

src/systems/resourcepackCompiler/1.20.4.ts

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -149,26 +149,6 @@ export default async function compileResourcePack(options: {
149149
)
150150
if (aj.resource_pack_export_mode === 'raw') {
151151
ajmeta.read()
152-
153-
PROGRESS_DESCRIPTION.set('Removing Old Resource Pack Files...')
154-
PROGRESS.set(0)
155-
MAX_PROGRESS.set(ajmeta.oldFiles.size)
156-
157-
const removedFolders = new Set<string>()
158-
for (const file of ajmeta.oldFiles) {
159-
if (fs.existsSync(file)) await fs.promises.unlink(file)
160-
let folder = PathModule.dirname(file)
161-
while (
162-
!removedFolders.has(folder) &&
163-
fs.existsSync(folder) &&
164-
(await fs.promises.readdir(folder)).length === 0
165-
) {
166-
await fs.promises.rm(folder, { recursive: true })
167-
removedFolders.add(folder)
168-
folder = PathModule.dirname(folder)
169-
}
170-
PROGRESS.set(PROGRESS.get() + 1)
171-
}
172152
}
173153

174154
const exportedFiles = new Map<string, string | Buffer>()
@@ -231,27 +211,20 @@ export default async function compileResourcePack(options: {
231211
}
232212

233213
// Texture atlas
234-
const blockAtlasFile = PathModule.join(
214+
const blockAtlasPath = PathModule.join(
235215
resourcePackFolder,
236216
'assets/minecraft/atlases/blocks.json'
237217
)
238-
let blockAtlas: ITextureAtlas = { sources: [] }
239-
if (fs.existsSync(blockAtlasFile)) {
240-
const content = await fs.promises.readFile(blockAtlasFile, 'utf-8').catch(() => {
241-
throw new IntentionalExportError(
242-
'Failed to read block atlas file after it was confirmed to exist!'
243-
)
218+
const blockAtlas: ITextureAtlas = await fs.promises
219+
.readFile(blockAtlasPath, 'utf-8')
220+
.catch(() => {
221+
console.log('Creating new block atlas...')
222+
return '{ sources: [] }'
244223
})
245-
try {
246-
blockAtlas = JSON.parse(content)
247-
} catch (e: any) {
248-
throw new IntentionalExportError(
249-
`Failed to parse block atlas file: ${e.message as string}`
250-
)
251-
}
252-
}
224+
.then(content => JSON.parse(content) as ITextureAtlas)
225+
253226
if (
254-
blockAtlas.sources.some(
227+
blockAtlas.sources?.some(
255228
source =>
256229
source.type === 'directory' &&
257230
source.source === 'blueprint' &&
@@ -260,13 +233,14 @@ export default async function compileResourcePack(options: {
260233
) {
261234
// Do nothing. The blueprint directory is already there.
262235
} else {
236+
blockAtlas.sources ??= []
263237
blockAtlas.sources.push({
264238
type: 'directory',
265239
source: 'blueprint',
266240
prefix: 'blueprint/',
267241
})
268242
}
269-
exportedFiles.set(blockAtlasFile, autoStringify(blockAtlas))
243+
exportedFiles.set(blockAtlasPath, autoStringify(blockAtlas))
270244

271245
// Transparent texture
272246
const transparentTexturePath = PathModule.join(
@@ -297,7 +271,30 @@ export default async function compileResourcePack(options: {
297271
// Do nothing
298272
console.log('Plugin mode enabled. Skipping resource pack export.')
299273
} else if (aj.resource_pack_export_mode === 'raw') {
274+
// Cleanup old files
275+
PROGRESS_DESCRIPTION.set('Removing Old Resource Pack Files...')
276+
PROGRESS.set(0)
277+
MAX_PROGRESS.set(ajmeta.oldFiles.size)
278+
279+
const removedFolders = new Set<string>()
280+
for (const file of ajmeta.oldFiles) {
281+
if (fs.existsSync(file)) await fs.promises.unlink(file)
282+
let folder = PathModule.dirname(file)
283+
while (
284+
!removedFolders.has(folder) &&
285+
fs.existsSync(folder) &&
286+
(await fs.promises.readdir(folder)).length === 0
287+
) {
288+
await fs.promises.rm(folder, { recursive: true })
289+
removedFolders.add(folder)
290+
folder = PathModule.dirname(folder)
291+
}
292+
PROGRESS.set(PROGRESS.get() + 1)
293+
}
294+
295+
// Write new files
300296
ajmeta.files = new Set(exportedFiles.keys())
297+
ajmeta.files.delete(blockAtlasPath)
301298
ajmeta.write()
302299

303300
// Since we don't want to erase the display item every export, we add it's model file after the files have been added to the ajmeta.

src/systems/resourcepackCompiler/1.21.2.ts

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,52 +31,25 @@ export default async function compileResourcePack(options: {
3131
)
3232
if (aj.resource_pack_export_mode === 'raw') {
3333
ajmeta.read()
34-
35-
PROGRESS_DESCRIPTION.set('Removing Old Resource Pack Files...')
36-
PROGRESS.set(0)
37-
MAX_PROGRESS.set(ajmeta.oldFiles.size)
38-
39-
const removedFolders = new Set<string>()
40-
for (const file of ajmeta.oldFiles) {
41-
if (fs.existsSync(file)) await fs.promises.unlink(file)
42-
let folder = PathModule.dirname(file)
43-
while (
44-
!removedFolders.has(folder) &&
45-
fs.existsSync(folder) &&
46-
(await fs.promises.readdir(folder)).length === 0
47-
) {
48-
await fs.promises.rm(folder, { recursive: true })
49-
removedFolders.add(folder)
50-
folder = PathModule.dirname(folder)
51-
}
52-
PROGRESS.set(PROGRESS.get() + 1)
53-
}
5434
}
5535

5636
const exportedFiles = new Map<string, string | Buffer>()
5737

5838
// Texture atlas
59-
const blockAtlasFile = PathModule.join(
39+
const blockAtlasPath = PathModule.join(
6040
resourcePackFolder,
6141
'assets/minecraft/atlases/blocks.json'
6242
)
63-
let blockAtlas: ITextureAtlas = { sources: [] }
64-
if (fs.existsSync(blockAtlasFile)) {
65-
const content = await fs.promises.readFile(blockAtlasFile, 'utf-8').catch(() => {
66-
throw new IntentionalExportError(
67-
'Failed to read block atlas file after it was confirmed to exist!'
68-
)
43+
const blockAtlas: ITextureAtlas = await fs.promises
44+
.readFile(blockAtlasPath, 'utf-8')
45+
.catch(() => {
46+
console.log('Creating new block atlas...')
47+
return '{ sources: [] }'
6948
})
70-
try {
71-
blockAtlas = JSON.parse(content)
72-
} catch (e: any) {
73-
throw new IntentionalExportError(
74-
`Failed to parse block atlas file: ${e.message as string}`
75-
)
76-
}
77-
}
49+
.then(content => JSON.parse(content) as ITextureAtlas)
50+
7851
if (
79-
blockAtlas.sources.some(
52+
blockAtlas.sources?.some(
8053
source =>
8154
source.type === 'directory' &&
8255
source.source === 'blueprint' &&
@@ -85,13 +58,14 @@ export default async function compileResourcePack(options: {
8558
) {
8659
// Do nothing. The blueprint directory is already there.
8760
} else {
61+
blockAtlas.sources ??= []
8862
blockAtlas.sources.push({
8963
type: 'directory',
9064
source: 'blueprint',
9165
prefix: 'blueprint/',
9266
})
9367
}
94-
exportedFiles.set(blockAtlasFile, autoStringify(blockAtlas))
68+
exportedFiles.set(blockAtlasPath, autoStringify(blockAtlas))
9569

9670
// Internal Models
9771
exportedFiles.set(
@@ -154,6 +128,12 @@ export default async function compileResourcePack(options: {
154128
? PathModule.join(correctedModelExportFolder, bone.name + '.json')
155129
: PathModule.join(correctedModelExportFolder, variant.name, bone.name + '.json')
156130
// Hacky workaround for this version enforcing the `item` namespace.
131+
if (variantModel.model?.parent) {
132+
variantModel.model.parent = variantModel.model.parent.replace(
133+
'animated_java:blueprint/',
134+
'animated_java:item/'
135+
)
136+
}
157137
variantModel.item_model = variantModel.item_model.replace(
158138
'animated_java:blueprint/',
159139
'animated_java:'
@@ -167,7 +147,30 @@ export default async function compileResourcePack(options: {
167147
// Do nothing
168148
console.log('Plugin mode enabled. Skipping resource pack export.')
169149
} else if (aj.resource_pack_export_mode === 'raw') {
150+
// Clean up old files
151+
PROGRESS_DESCRIPTION.set('Removing Old Resource Pack Files...')
152+
PROGRESS.set(0)
153+
MAX_PROGRESS.set(ajmeta.oldFiles.size)
154+
155+
const removedFolders = new Set<string>()
156+
for (const file of ajmeta.oldFiles) {
157+
if (fs.existsSync(file)) await fs.promises.unlink(file)
158+
let folder = PathModule.dirname(file)
159+
while (
160+
!removedFolders.has(folder) &&
161+
fs.existsSync(folder) &&
162+
(await fs.promises.readdir(folder)).length === 0
163+
) {
164+
await fs.promises.rm(folder, { recursive: true })
165+
removedFolders.add(folder)
166+
folder = PathModule.dirname(folder)
167+
}
168+
PROGRESS.set(PROGRESS.get() + 1)
169+
}
170+
171+
// Write new files
170172
ajmeta.files = new Set(exportedFiles.keys())
173+
ajmeta.files.delete(blockAtlasPath)
171174
ajmeta.write()
172175

173176
PROGRESS_DESCRIPTION.set('Writing Resource Pack...')

src/systems/resourcepackCompiler/1.21.4.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,20 @@ export default async function compileResourcePack(options: {
3838
)
3939

4040
// Texture atlas
41-
const blockAtlasFile = PathModule.join(
41+
const blockAtlasPath = PathModule.join(
4242
resourcePackFolder,
4343
'assets/minecraft/atlases/blocks.json'
4444
)
45-
let blockAtlas: ITextureAtlas = { sources: [] }
46-
console.log('Block atlas file:', blockAtlasFile, fs.existsSync(blockAtlasFile))
47-
if (fs.existsSync(blockAtlasFile)) {
48-
const content = await fs.promises.readFile(blockAtlasFile, 'utf-8').catch(() => {
49-
throw new IntentionalExportError(
50-
'Failed to read block atlas file after it was confirmed to exist!'
51-
)
45+
const blockAtlas: ITextureAtlas = await fs.promises
46+
.readFile(blockAtlasPath, 'utf-8')
47+
.catch(() => {
48+
console.log('Creating new block atlas...')
49+
return '{ sources: [] }'
5250
})
53-
try {
54-
blockAtlas = JSON.parse(content)
55-
} catch (e: any) {
56-
throw new IntentionalExportError(
57-
`Failed to parse block atlas file: ${e.message as string}`
58-
)
59-
}
60-
console.log('Pre-existing Block atlas:', blockAtlas)
61-
} else {
62-
console.log('Block atlas file does not exist. Creating a new one.')
63-
}
51+
.then(content => JSON.parse(content) as ITextureAtlas)
52+
6453
if (
65-
blockAtlas.sources.some(
54+
blockAtlas.sources?.some(
6655
source =>
6756
source.type === 'directory' &&
6857
source.source === 'blueprint' &&
@@ -71,14 +60,14 @@ export default async function compileResourcePack(options: {
7160
) {
7261
// Do nothing. The blueprint directory is already there.
7362
} else {
63+
blockAtlas.sources ??= []
7464
blockAtlas.sources.push({
7565
type: 'directory',
7666
source: 'blueprint',
7767
prefix: 'blueprint/',
7868
})
7969
}
80-
console.log('Block atlas:', blockAtlas)
81-
exportedFiles.set(blockAtlasFile, autoStringify(blockAtlas))
70+
exportedFiles.set(blockAtlasPath, autoStringify(blockAtlas))
8271

8372
// Internal Models
8473
exportedFiles.set(PathModule.join(globalModelsFolder, 'empty.json'), '{}')
@@ -186,6 +175,7 @@ export default async function compileResourcePack(options: {
186175

187176
// Write new files
188177
ajmeta.files = new Set(exportedFiles.keys())
178+
ajmeta.files.delete(blockAtlasPath)
189179
ajmeta.write()
190180

191181
PROGRESS_DESCRIPTION.set('Writing Resource Pack...')

0 commit comments

Comments
 (0)