From bbb3c13a272e2ebbab32fda3fcc9180fc5ff59e7 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Mon, 4 Sep 2023 11:24:34 +0300 Subject: [PATCH] fix autoversion (download data on demand) --- scripts/esbuildPlugins.mjs | 21 ++++++++++++++++++--- src/index.js | 13 ++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/scripts/esbuildPlugins.mjs b/scripts/esbuildPlugins.mjs index eccfd89f..cbd0edec 100644 --- a/scripts/esbuildPlugins.mjs +++ b/scripts/esbuildPlugins.mjs @@ -26,15 +26,29 @@ const plugins = [ if (!resolveDir.endsWith('minecraft-data')) return return { namespace: 'load-global-minecraft-data', - path + path, + pluginData: { + resolveDir + }, } }) build.onLoad({ filter: /.+/, namespace: 'load-global-minecraft-data', - }, () => { + }, ({ pluginData: { resolveDir } }) => { + const defaultVersionsObj = { + // default protocol data, needed for auto-select + "1.20.1": { + version: { + "minecraftVersion": "1.20.1", + "version": 763, + "majorVersion": "1.20" + }, + protocol: JSON.parse(fs.readFileSync(join(resolveDir, 'minecraft-data/data/pc/1.20/protocol.json'), 'utf8')), + } + } return { - contents: 'window.mcData ??= {};module.exports = { pc: window.mcData }', + contents: `window.mcData ??= ${JSON.stringify(defaultVersionsObj)};module.exports = { pc: window.mcData }`, loader: 'js', } }) @@ -230,6 +244,7 @@ const plugins = [ polyfillNode({ polyfills: { fs: false, + dns: false, crypto: false, events: false, http: false, diff --git a/src/index.js b/src/index.js index 550b60d7..dc7ebab6 100644 --- a/src/index.js +++ b/src/index.js @@ -364,12 +364,16 @@ async function connect (connectOptions) { let localServer try { Object.assign(serverOptions, _.defaultsDeep({}, connectOptions.serverOverrides ?? {}, options.localServerOptions, serverOptions)) - let version = connectOptions.botVersion ?? serverOptions.version - if (version) { + const downloadMcData = async (version) => { setLoadingScreenStatus(`Downloading data for ${version}`) await loadScript(`./mc-data/${toMajorVersion(version)}.js`) } + const version = connectOptions.botVersion ?? serverOptions.version + if (version) { + downloadMcData(version) + } + if (singeplayer) { // SINGLEPLAYER EXPLAINER: // Note 1: here we have custom sync communication between server Client (flying-squid) and game client (mineflayer) @@ -411,7 +415,10 @@ async function connect (connectOptions) { viewDistance: 'tiny', checkTimeoutInterval: 240 * 1000, noPongTimeout: 240 * 1000, - closeTimeout: 240 * 1000 + closeTimeout: 240 * 1000, + async versionSelectedHook (client) { + await downloadMcData(client.version) + } }) if (singeplayer) { const _supportFeature = bot.supportFeature