Skip to content

Commit

Permalink
feat: initial support for websocket (direct connection) servers. mcra…
Browse files Browse the repository at this point in the history
…ft-fun-mineflayer plugin
  • Loading branch information
zardoy committed Dec 18, 2024
1 parent 961cf01 commit 10ee4c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type ConnectOptions = {
/** If true, will show a UI to authenticate with a new account */
authenticatedAccount?: AuthenticatedAccount | true
peerOptions?: any
viewerWsConnect?: string
}

export const downloadNeededDataOnConnect = async (version: string) => {
Expand Down
27 changes: 24 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import { mainMenuState } from './react/MainMenuRenderApp'
import { ItemsRenderer } from 'mc-assets/dist/itemsRenderer'
import './mobileShim'
import { parseFormattedMessagePacket } from './botUtils'
import { getWsProtocolStream } from './viewerConnector'

Check failure on line 106 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Cannot find module './viewerConnector' or its corresponding type declarations.

window.debug = debug
window.THREE = THREE
Expand Down Expand Up @@ -471,12 +472,20 @@ async function connect (connectOptions: ConnectOptions) {
connectingServer: server.host
}) : undefined

let clientDataStream
if (p2pMultiplayer) {
clientDataStream = await connectToPeer(connectOptions.peerId!, connectOptions.peerOptions)
}
if (connectOptions.viewerWsConnect) {
clientDataStream = await getWsProtocolStream(connectOptions.viewerWsConnect)
}

bot = mineflayer.createBot({
host: server.host,
port: server.port ? +server.port : undefined,
version: connectOptions.botVersion || false,
...p2pMultiplayer ? {
stream: await connectToPeer(connectOptions.peerId!, connectOptions.peerOptions),
...clientDataStream ? {
stream: clientDataStream,
} : {},
...singleplayer || p2pMultiplayer ? {
keepAlive: false,
Expand Down Expand Up @@ -559,12 +568,15 @@ async function connect (connectOptions: ConnectOptions) {
return _supportFeature(feature)
}) as typeof bot.supportFeature

bot.emit('inject_allowed')
bot._client.emit('connect')
} else if (connectOptions.viewerWsConnect) {
bot.emit('inject_allowed')
bot._client.emit('connect')
} else {
const setupConnectHandlers = () => {
bot._client.socket.on('connect', () => {
console.log('WebSocket connection established')
console.log('Proxy WebSocket connection established')
//@ts-expect-error
bot._client.socket._ws.addEventListener('close', () => {
console.log('WebSocket connection closed')
Expand Down Expand Up @@ -1039,6 +1051,15 @@ downloadAndOpenFile().then((downloadAction) => {
if (qs.get('serversList')) {
showModal({ reactType: 'serversList' })
}

const viewerWsConnect = qs.get('viewerConnect')
if (viewerWsConnect) {
void connect({
username: `viewer-${Math.random().toString(36).slice(2, 10)}`,
botVersion: '1.21.1',
viewerWsConnect,
})
}
}, (err) => {
console.error(err)
alert(`Failed to download file: ${err}`)
Expand Down

0 comments on commit 10ee4c0

Please sign in to comment.