Skip to content

Commit 88d361f

Browse files
authored
Fix version check (#3259)
* Fix version check * improve variable naming * fix common test server close to handle already closed server * handle too old protocol versions * fix syntax * Update version.js lint * remove `supportedVersions` * expose latestSupportedVersion/oldestSupportedVersion * Update loader.js
1 parent 79cd7bd commit 88d361f

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,8 @@ export class Particle {
867867
);
868868
}
869869

870-
export let supportedVersions: string[]
871870
export let testedVersions: string[]
871+
export let latestSupportedVersion: string
872+
export let oldestSupportedVersion: string
872873

873874
export function supportFeature (feature: string, version: string): boolean

lib/loader.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ const plugins = {
4545
particle: require('./plugins/particle')
4646
}
4747

48-
const supportedVersions = require('./version').supportedVersions
49-
const testedVersions = require('./version').testedVersions
48+
const minecraftData = require('minecraft-data')
49+
const { testedVersions, latestSupportedVersion, oldestSupportedVersion } = require('./version')
5050

5151
module.exports = {
5252
createBot,
@@ -55,9 +55,10 @@ module.exports = {
5555
ScoreBoard: require('./scoreboard'),
5656
BossBar: require('./bossbar'),
5757
Particle: require('./particle'),
58-
supportedVersions,
58+
latestSupportedVersion,
59+
oldestSupportedVersion,
5960
testedVersions,
60-
supportFeature: (feature, version) => require('prismarine-registry')(version).supportFeature(feature)
61+
supportFeature: (feature, version) => minecraftData(version).supportFeature(feature)
6162
}
6263

6364
function createBot (options = {}) {
@@ -107,22 +108,21 @@ function createBot (options = {}) {
107108
if (!bot._client.wait_connect) next()
108109
else bot._client.once('connect_allowed', next)
109110
function next () {
110-
bot.registry = require('prismarine-registry')(bot._client.version)
111-
const version = bot.registry.version
112-
if (supportedVersions.indexOf(version.majorVersion) === -1) {
113-
throw new Error(`Version ${version.minecraftVersion} is not supported.`)
114-
}
111+
const serverPingVersion = bot._client.version
112+
bot.registry = require('prismarine-registry')(serverPingVersion)
113+
if (!bot.registry?.version) throw new Error(`Server version '${serverPingVersion}' is not supported, no data for version`)
115114

116-
const latestTestedVersion = testedVersions[testedVersions.length - 1]
117-
const latestProtocolVersion = require('prismarine-registry')(latestTestedVersion).protocolVersion
118-
if (version.protocolVersion > latestProtocolVersion) {
119-
throw new Error(`Version ${version.minecraftVersion} is not supported. Latest supported version is ${latestTestedVersion}.`)
115+
const versionData = bot.registry.version
116+
if (versionData['>'](latestSupportedVersion)) {
117+
throw new Error(`Server version '${serverPingVersion}' is not supported. Latest supported version is '${latestSupportedVersion}'.`)
118+
} else if (versionData['<'](oldestSupportedVersion)) {
119+
throw new Error(`Server version '${serverPingVersion}' is not supported. Oldest supported version is '${oldestSupportedVersion}'.`)
120120
}
121121

122-
bot.protocolVersion = version.version
123-
bot.majorVersion = version.majorVersion
124-
bot.version = version.minecraftVersion
125-
options.version = version.minecraftVersion
122+
bot.protocolVersion = versionData.version
123+
bot.majorVersion = versionData.majorVersion
124+
bot.version = versionData.minecraftVersion
125+
options.version = versionData.minecraftVersion
126126
bot.supportFeature = bot.registry.supportFeature
127127
setTimeout(() => bot.emit('inject_allowed'), 0)
128128
}

lib/version.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
const testedVersions = ['1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1']
12
module.exports = {
2-
supportedVersions: ['1.8', '1.9', '1.10', '1.11', '1.12', '1.13', '1.14', '1.15', '1.16', '1.17', '1.18', '1.19', '1.20'],
3-
testedVersions: ['1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20.1']
3+
testedVersions,
4+
latestSupportedVersion: testedVersions[testedVersions.length - 1],
5+
oldestSupportedVersion: testedVersions[0]
46
}

test/externalTests/plugins/testCommon.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,14 @@ function inject (bot) {
193193
const closeExample = async (err) => {
194194
console.log('kill process ' + child.pid)
195195

196-
process.kill(child.pid, 'SIGTERM')
197-
198-
const [code] = await once(child, 'close')
199-
console.log('close requested', code)
196+
try {
197+
process.kill(child.pid, 'SIGTERM')
198+
const [code] = await onceWithCleanup(child, 'close', { timeout: 1000 })
199+
console.log('close requested', code)
200+
} catch (e) {
201+
console.log(e)
202+
console.log('process termination failed, process may already be closed')
203+
}
200204

201205
if (err) {
202206
throw err

0 commit comments

Comments
 (0)