Skip to content

Commit c5bea3a

Browse files
committed
1.2.6
- Rewrite release version checking to map min version to pack format instead of pack format to applicable versions - Rewrite `getVersions()` entirely - Fix 1.18 resource pack format being 8
1 parent e23dace commit c5bea3a

File tree

6 files changed

+67
-62
lines changed

6 files changed

+67
-62
lines changed

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.2.6
4+
*2021-09-23*
5+
- Fixed `getVersions()` returning invalid snapshots.
6+
- Fixed 1.18 resource pack version being `8` instead of `7`.
7+
38
## 1.2.5
49
*2021-09-16*
510
- Fixed CLI usage not working.

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pack-format",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"description": "Returns the pack_format of any Minecraft version, including snapshots",
55
"scripts": {
66
"prepublish": "tsc",

src/cli.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ if (ver) {
3030
console.log(`pack-format v${VERSION}`)
3131
}
3232
else if (args.list) {
33-
console.debug(ver, args)
3433
if (Number.isNaN(ver)) throw new Error(`'${ver}' is not a valid pack format`)
3534
if (Math.round(+ver) !== +ver) throw new Error(`'${ver}' is a version number, not a pack format`)
3635
const type = args.data ? 'data' : 'resource'

src/index.ts

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,60 @@ class Snapshot {
88
get id(): number { return (this.year - 10) * 52 + this.week }
99
}
1010

11-
const LATEST = { resource: 8, data: 8 }
12-
13-
const RELEASES: Record<number, VersionName[]> = {
14-
1: ['1.6.x', '1.7.x', '1.8.x'],
15-
2: ['1.9.x', '1.10.x'],
16-
3: ['1.11.x', '1.12.x'],
17-
4: ['1.13.x', '1.14.x'],
18-
5: ['1.15.x', '1.16.0', '1.16.1'],
19-
6: ['1.16.x'],
20-
7: ['1.17.x'],
21-
8: ['1.18.x'],
22-
}
23-
24-
const SPECIAL: Record<number, string[]> = {
25-
4: ['combat1', 'combat2', 'combat3'],
26-
5: ['combat4', 'combat5'],
27-
6: ['combat6', 'combat7a', 'combat7b', 'combat8a', 'combat8b', 'combat8c'],
11+
// Data sets //
12+
13+
const LATEST = { resource: 7, data: 8 }
14+
15+
const START_RELEASES: Record<VersionName, Record<PackType, FormatResult>> = {
16+
'1.6.x': { resource: 1, data: undefined },
17+
'1.9.x': { resource: 2, data: undefined },
18+
'1.11.x': { resource: 3, data: undefined },
19+
'1.13.x': { resource: 4, data: 4 },
20+
'1.15.x': { resource: 5, data: 5 },
21+
'1.16.2': { resource: 6, data: 6 },
22+
'1.17.x': { resource: 7, data: 7 },
23+
'1.18.x': { resource: 7, data: 8 },
24+
'1.19.x': { resource: undefined, data: undefined },
2825
}
2926

3027
const d = new Date(), year = d.getFullYear() - 2000, maxWeek = (d.getMonth() + 1) * 5
3128
const fauxCurrentSnapshot: SnapshotName = `${year}w${maxWeek.toString().padStart(2, '0')}a`
32-
const START_SNAPSHOTS: Record<SnapshotName, Record<PackType, FormatResult>> = {
29+
const START_SNAPSHOTS: Record<string, Record<PackType, FormatResult>> = {
3330
'13w24a': { resource: 1, data: undefined },
3431
'15w31a': { resource: 2, data: undefined },
3532
'16w32a': { resource: 3, data: undefined },
3633
'17w48a': { resource: 4, data: 4 },
3734
'20w06a': { resource: 5, data: 5 },
3835
'20w45a': { resource: 7, data: 6 },
3936
'20w46a': { resource: 7, data: 7 },
40-
'21w37a': { resource: 8, data: 8 },
37+
'21w37a': { resource: 7, data: 8 },
4138
[fauxCurrentSnapshot]: { resource: undefined, data: undefined },
4239
}
4340

41+
const SPECIAL: Record<number, string[]> = {
42+
4: ['combat1', 'combat2', 'combat3'],
43+
5: ['combat4', 'combat5'],
44+
6: ['combat6', 'combat7a', 'combat7b', 'combat8a', 'combat8b', 'combat8c'],
45+
}
46+
4447
function getPackFormat(version: string, type: PackType = 'resource'): FormatResult {
4548
if (!version) return undefined
4649
version = version.toString().toLowerCase().trim()
4750

4851
// Special //
4952
for (const format in SPECIAL) {
50-
if (SPECIAL[format].includes(version)) return +format;
53+
if (SPECIAL[format].includes(version)) return +format
5154
}
5255

53-
// Snapshot //
56+
if (!version.includes('.') && !/\d{2}w\d{2}\w/.test(version)) return undefined
5457

58+
// Snapshot //
5559
if (/^\d\d[w]\d\d[a-z]$/.test(version)) {
5660
const snapshot = new Snapshot(version)
5761
let ver: FormatResult
5862
for (const testSnap in START_SNAPSHOTS) {
5963
if (snapshot.id >= (new Snapshot(testSnap)).id) {
60-
// @ts-ignore: #45159
61-
ver = START_SNAPSHOTS[testSnap][type]
64+
ver = START_SNAPSHOTS[testSnap as SnapshotName][type]
6265
}
6366
}
6467
return ver
@@ -78,15 +81,14 @@ function getPackFormat(version: string, type: PackType = 'resource'): FormatResu
7881
// Default to the parent version
7982
version = version.replace(/-.+$/, '')
8083
}
81-
if (/^\d+\.\d+$/.test(version)) version += '.0'
82-
83-
for (const format in RELEASES) {
84-
if (+format < 4 && type === 'data') continue
85-
for (let testVer of RELEASES[format]) {
86-
const matchExact = testVer === version
87-
const matchMinor = testVer.includes('x') && version.includes(testVer.replace('.x', ''))
88-
if (matchExact || matchMinor) return +format
84+
85+
for (const testVer of Object.keys(START_RELEASES).reverse()) {
86+
const getId = (ver: string): number => {
87+
const [, major, minor] = ver.split('.')
88+
return +major.padStart(3, '0') + +(minor ?? 0) / 100
8989
}
90+
if (getId(testVer.replace('.x', '')) > getId(version)) continue
91+
return START_RELEASES[testVer as VersionName][type]
9092
}
9193

9294
return undefined
@@ -99,26 +101,24 @@ function getVersions(format: number, type: PackType = 'resource'): VersionsResul
99101
}
100102
if (!format || format > LATEST[type] || (type === 'data' && format < 4)) return output
101103

102-
output.releases.min = RELEASES[format][0]
103-
output.releases.max = RELEASES[format].slice(-1)[0]
104-
105-
const startSnaps: string[] = Object.keys(START_SNAPSHOTS)
106-
for (const snap in START_SNAPSHOTS) {
107-
// @ts-ignore: #45159
108-
if (START_SNAPSHOTS[snap][type] === format) {
109-
let maxSnap = snap as SnapshotName
110-
let i = 1
111-
do {
112-
const nextSnap = startSnaps[startSnaps.indexOf(snap) + i++] as SnapshotName
113-
if (nextSnap) maxSnap = nextSnap
114-
else break
115-
}
116-
while (getPackFormat(maxSnap, type) === getPackFormat(snap, type))
104+
// Min and max releases
105+
const startReleases = Object.entries(START_RELEASES)
106+
const relIndex = startReleases.findIndex(([, data]) => data[type] === format)
107+
if (relIndex >= 0) {
108+
const minRelease = startReleases[relIndex][0]
109+
const maxRelease = startReleases[relIndex + 1][0].replace(/\.(\d+)\./, (_, major) => `.${major - 1}.`)
110+
output.releases.min = minRelease as VersionName
111+
output.releases.max = maxRelease as VersionName
112+
}
117113

118-
output.snapshots.min = snap as SnapshotName
119-
output.snapshots.max = maxSnap.replace(/(\d\d)[a-z]$/, (_, n) => (+n - 1).toString() + 'a') as SnapshotName
120-
break
121-
}
114+
// Min and max snapshots
115+
const startSnaps = Object.entries(START_SNAPSHOTS)
116+
const snapIndex = startSnaps.findIndex(([, data]) => data[type] === format)
117+
if (snapIndex >= 0) {
118+
const maxSnap = startSnaps[snapIndex][0]
119+
const minSnap = startSnaps[snapIndex + 1][0].replace(/(\d)\w$/, (_, n) => `${n - 1}a`)
120+
output.snapshots.min = maxSnap as SnapshotName
121+
output.snapshots.max = minSnap as SnapshotName
122122
}
123123

124124
return output

test/test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ testPackFormat(['16w31a'], 2)
5050
testPackFormat(['16w32a'], 3)
5151
testPackFormat(['20w30a'], 5)
5252
testPackFormat(['20w45a'], 7)
53-
testPackFormat(['21w37a'], 8)
5453
testPackFormat(['20w45a', 'resource'], 7)
5554
testPackFormat(['20w45a', 'data'], 6)
5655
testPackFormat(['20w46a', 'data'], 7)
56+
testPackFormat(['21w37a'], 7)
57+
testPackFormat(['21w37a', 'resource'], 7)
58+
testPackFormat(['21w37a', 'data'], 8)
5759
testPackFormat(['combat3'], 4)
5860
testPackFormat(['1.18-exp1'], 7)
5961
testPackFormat(['1.18-es2'], 7)
6062
testPackFormat(['1.18 experimental snapshot 3'], 7)
61-
testPackFormat(['1.18'], 8)
6263

6364
testVersions([3, 'data'], { releases: { min: '', max: '' }, snapshots: { min: '', max: '' } })
6465
testVersions([6, 'resource'], { releases: { min: '1.16.x', max: '1.16.x' }, snapshots: { min: '', max: '' } })

0 commit comments

Comments
 (0)