Fallback version for dynamic versions (#983)

* added fallbackVersion to dynamic version support

* mark fallbackVersion as optional in docs

* added fallbackVersion to ping responses

* don't enable dynamic cross version support if version is undefined
This commit is contained in:
Matthias Neid 2022-05-15 00:39:20 +02:00 committed by GitHub
parent a9cc6cee85
commit 7a1d857602
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 11 deletions

View file

@ -22,6 +22,7 @@ automatically logged in and validated against mojang's auth.
* maxPlayers : default to 20
* keepAlive : send keep alive packets : default to true
* version : the version of the server, defaults to the latest version. Set version to `false` to enable dynamic cross version support.
* fallbackVersion (optional) : the version that should be used as a fallback, if the client version isn't supported, only works with dynamic cross version support.
* favicon (optional) : the favicon to set, base64 encoded
* customPackets (optional) : an object index by version/state/direction/name, see client_custom_packet for an example
* errorHandler : A way to override the default error handler for client errors. A function that takes a Client and an error.

1
src/index.d.ts vendored
View file

@ -166,6 +166,7 @@ declare module 'minecraft-protocol' {
maxPlayers?: number
keepAlive?: boolean
version?: string | false
fallbackVersion?: string
favicon?: string
customPackets?: any
errorHandler?: (client: Client, error: Error) => void

View file

@ -1,6 +1,6 @@
const states = require('../states')
module.exports = function (client, server, { version }) {
module.exports = function (client, server, { version, fallbackVersion }) {
client.once('set_protocol', onHandshake)
function onHandshake (packet) {
@ -8,12 +8,20 @@ module.exports = function (client, server, { version }) {
client.serverPort = packet.serverPort
client.protocolVersion = packet.protocolVersion
if (version === false || version === undefined) {
if (version === false) {
if (require('minecraft-data')(client.protocolVersion)) {
client.version = client.protocolVersion
} else {
let fallback
if (fallbackVersion !== undefined) {
fallback = require('minecraft-data')(fallbackVersion)
}
if (fallback) {
client.version = fallback.version.version
} else {
client.end('Protocol version ' + client.protocolVersion + ' is not supported')
}
}
} else if (client.protocolVersion !== server.mcversion.version && packet.nextState !== 1) {
client.end('Wrong protocol version, expected: ' + server.mcversion.version + ' and you are using: ' + client.protocolVersion)
}

View file

@ -1,19 +1,31 @@
const endianToggle = require('endian-toggle')
module.exports = function (client, server, { beforePing = null, version }) {
module.exports = function (client, server, { beforePing = null, version, fallbackVersion }) {
client.once('ping_start', onPing)
client.once('legacy_server_list_ping', onLegacyPing)
function onPing () {
// Use client version if dynamic cross version support is enabled.
const responseVersion = (version === false)
? {
let responseVersion = {
name: server.mcversion.minecraftVersion,
protocol: server.mcversion.version
}
if (version === false) {
let minecraftData = require('minecraft-data')(client.protocolVersion)
if (!minecraftData && fallbackVersion !== undefined) {
minecraftData = require('minecraft-data')(fallbackVersion)
}
if (minecraftData) {
responseVersion = {
name: minecraftData.version.minecraftVersion,
protocol: minecraftData.version.version
}
} else {
responseVersion = {
name: client.version,
protocol: client.protocolVersion
}
: {
name: server.mcversion.minecraftVersion,
protocol: server.mcversion.version
}
}
const response = {