Implement Realm joining (#193)
* Pass client options to ping * Implement RealmAPI to auth * Add Realm join example * Update package.json * Update README.md * Update index.d.ts * Show one option, remove listener * Fix wording * Explain options * Optional fields * Fix typo * Moved retry ad host/port extraction to prealms * Add docs * Fix lint * Depend on prealms release Co-authored-by: LucienHH <Lucien.holloway@aprox.co.uk>
This commit is contained in:
parent
dfff13867d
commit
cde600d51e
7 changed files with 88 additions and 3 deletions
|
|
@ -3,6 +3,7 @@ const { Authflow: PrismarineAuth, Titles } = require('prismarine-auth')
|
|||
const minecraftFolderPath = require('minecraft-folder-path')
|
||||
const debug = require('debug')('minecraft-protocol')
|
||||
const { uuidFrom } = require('../datatypes/util')
|
||||
const { RealmAPI } = require('prismarine-realms')
|
||||
|
||||
function validateOptions (options) {
|
||||
if (!options.profilesFolder) {
|
||||
|
|
@ -16,7 +17,35 @@ function validateOptions (options) {
|
|||
|
||||
async function realmAuthenticate (options) {
|
||||
validateOptions(options)
|
||||
throw new Error('Not implemented')
|
||||
|
||||
options.authflow = new PrismarineAuth(options.username, options.profilesFolder, options, options.onMsaCode)
|
||||
|
||||
const api = RealmAPI.from(options.authflow, 'bedrock')
|
||||
const realms = await api.getRealms()
|
||||
|
||||
debug('realms', realms)
|
||||
|
||||
if (!realms || !realms.length) throw Error('Couldn\'t find any Realms for the authenticated account')
|
||||
|
||||
let realm
|
||||
|
||||
if (options.realms.realmId) {
|
||||
realm = realms.find(e => e.id === Number(options.realms.realmId))
|
||||
} else if (options.realms.realmInvite) {
|
||||
realm = await api.getRealmFromInvite(options.realms.realmInvite)
|
||||
} else if (options.realms.pickRealm) {
|
||||
if (typeof options.realms.pickRealm !== 'function') throw Error('realms.pickRealm must be a function')
|
||||
realm = await options.realms.pickRealm(realms)
|
||||
}
|
||||
|
||||
if (!realm) throw Error('Couldn\'t find a Realm to connect to. Authenticated account must be the owner or has been invited to the Realm.')
|
||||
|
||||
const { host, port } = await realm.getAddress()
|
||||
|
||||
debug('realms connection', { host, port })
|
||||
|
||||
options.host = host
|
||||
options.port = port
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ function createClient (options) {
|
|||
if (options.skipPing) {
|
||||
client.init()
|
||||
} else {
|
||||
ping(options).then(ad => {
|
||||
ping(client.options).then(ad => {
|
||||
const adVersion = ad.version?.split('.').slice(0, 3).join('.') // Only 3 version units
|
||||
client.options.version = options.version ?? (Options.Versions[adVersion] ? adVersion : Options.CURRENT_VERSION)
|
||||
client.conLog?.(`Connecting to server ${ad.motd} (${ad.name}), version ${ad.version}`, client.options.version !== ad.version ? ` (as ${client.options.version})` : '')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue