From 585b19d8dcc42d4cf853ae467bb01fb930cc5c23 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Tue, 22 Apr 2025 19:19:59 +0300 Subject: [PATCH] up mcraft-fun-mineflayer support --- README.MD | 1 + package.json | 2 +- pnpm-lock.yaml | 12 ++++----- src/index.ts | 5 ++-- src/react/MineflayerPluginConsole.tsx | 1 + src/viewerConnector.ts | 36 ++++++++++++++++++++++++++- 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/README.MD b/README.MD index ef65ca01..aa36f7e8 100644 --- a/README.MD +++ b/README.MD @@ -12,6 +12,7 @@ For building the project yourself / contributing, see [Development, Debugging & ### Big Features +- Official Mineflayer [plugin integration](https://github.com/zardoy/mcraft-fun-mineflayer-plugin)! View / Control your bot remotely. - Open any zip world file or even folder in read-write mode! - Connect to Java servers running in both offline (cracked) and online mode* (it's possible because of proxy servers, see below) - Integrated JS server clone capable of opening Java world saves in any way (folders, zip, web chunks streaming, etc) diff --git a/package.json b/package.json index 2a92c0a1..9d776082 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "google-drive-browserfs": "github:zardoy/browserfs#google-drive", "jszip": "^3.10.1", "lodash-es": "^4.17.21", - "mcraft-fun-mineflayer": "^0.1.14", + "mcraft-fun-mineflayer": "^0.1.21", "minecraft-data": "3.83.1", "minecraft-protocol": "github:PrismarineJS/node-minecraft-protocol#master", "mineflayer-item-map-downloader": "github:zardoy/mineflayer-item-map-downloader", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 120e5646..faf6991a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -132,8 +132,8 @@ importers: specifier: ^4.17.21 version: 4.17.21 mcraft-fun-mineflayer: - specifier: ^0.1.14 - version: 0.1.14(encoding@0.1.13)(mineflayer@https://codeload.github.com/GenerelSchwerz/mineflayer/tar.gz/d459d2ed76a997af1a7c94718ed7d5dee4478b8a(encoding@0.1.13)) + specifier: ^0.1.21 + version: 0.1.21(encoding@0.1.13)(mineflayer@https://codeload.github.com/GenerelSchwerz/mineflayer/tar.gz/d459d2ed76a997af1a7c94718ed7d5dee4478b8a(encoding@0.1.13)) minecraft-data: specifier: 3.83.1 version: 3.83.1 @@ -6459,9 +6459,9 @@ packages: resolution: {integrity: sha512-Ucsu2pDLr/cs8bxbxU9KTszdf/vPTLphYgEHUEWxuYlMkPQUCpsQwkn3YgyykJ7RXaca7zZGlZXaTPXBAqJT6A==} engines: {node: '>=18.0.0'} - mcraft-fun-mineflayer@0.1.14: - resolution: {integrity: sha512-q/qXQaNbkGJIvXjRvudUT7/k0EsJgphFcvYjrSRWYyGDJeb61MKRVqq1hhMjqx7UK7FMfBKvjfPSxq/QlAP7WQ==} - version: 0.1.14 + mcraft-fun-mineflayer@0.1.21: + resolution: {integrity: sha512-FtzebYMvLvunApQy9ilF1RGqiX01DJn8y7q4xAONiIhBrIT7BrHK3O63IA50YgklldvdgVxn7s3m4QANvsH2JA==} + version: 0.1.21 engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} peerDependencies: '@roamhq/wrtc': '*' @@ -16993,7 +16993,7 @@ snapshots: maxrects-packer: '@zardoy/maxrects-packer@2.7.4' zod: 3.24.2 - mcraft-fun-mineflayer@0.1.14(encoding@0.1.13)(mineflayer@https://codeload.github.com/GenerelSchwerz/mineflayer/tar.gz/d459d2ed76a997af1a7c94718ed7d5dee4478b8a(encoding@0.1.13)): + mcraft-fun-mineflayer@0.1.21(encoding@0.1.13)(mineflayer@https://codeload.github.com/GenerelSchwerz/mineflayer/tar.gz/d459d2ed76a997af1a7c94718ed7d5dee4478b8a(encoding@0.1.13)): dependencies: '@zardoy/flying-squid': 0.0.49(encoding@0.1.13) exit-hook: 2.2.1 diff --git a/src/index.ts b/src/index.ts index a94aa35d..e370e8be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -82,7 +82,7 @@ import packetsPatcher from './mineflayer/plugins/packetsPatcher' import { mainMenuState } from './react/MainMenuRenderApp' import './mobileShim' import { parseFormattedMessagePacket } from './botUtils' -import { getViewerVersionData, getWsProtocolStream, handleCustomChannel } from './viewerConnector' +import { getViewerVersionData, getWsProtocolStream, onBotCreatedViewerHandler } from './viewerConnector' import { getWebsocketStream } from './mineflayer/websocket-core' import { appQueryParams, appQueryParamsArray } from './appParams' import { playerState } from './mineflayer/playerState' @@ -552,8 +552,7 @@ export async function connect (connectOptions: ConnectOptions) { }) as unknown as typeof __type_bot window.bot = bot if (connectOptions.viewerWsConnect) { - void handleCustomChannel() - bot.physicsEnabled = false + void onBotCreatedViewerHandler() } customEvents.emit('mineflayerBotCreated') if (singleplayer || p2pMultiplayer || localReplaySession) { diff --git a/src/react/MineflayerPluginConsole.tsx b/src/react/MineflayerPluginConsole.tsx index e681f882..2e1b6c1a 100644 --- a/src/react/MineflayerPluginConsole.tsx +++ b/src/react/MineflayerPluginConsole.tsx @@ -17,6 +17,7 @@ export const mineflayerConsoleState = proxy({ messages: [] as ConsoleMessage[], replEnabled: false, consoleEnabled: false, + takeoverMode: false }) const MessageLine = ({ message }: { message: ConsoleMessage }) => { diff --git a/src/viewerConnector.ts b/src/viewerConnector.ts index 69e2115a..6a8a7d73 100644 --- a/src/viewerConnector.ts +++ b/src/viewerConnector.ts @@ -8,6 +8,7 @@ import { CustomChannelPacketFromClient, CustomChannelPacketFromServer, UIDefinit import { activeModalStack } from './globalState' import { mineflayerPluginHudState } from './react/MineflayerPluginHud' import { mineflayerConsoleState } from './react/MineflayerPluginConsole' +import { showNotification } from './react/NotificationProvider' export const viewerVersionState = proxy({ forwardChat: true, @@ -43,6 +44,7 @@ export const getViewerVersionData = async (url: string) => { requiresPass: boolean, forwardChat: boolean, clientIgnoredPackets?: string[] + takeoverMode?: boolean }>((resolve, reject) => { ws.addEventListener('message', async (message) => { const { data } = message @@ -67,6 +69,7 @@ export const getViewerVersionData = async (url: string) => { }) mineflayerConsoleState.consoleEnabled = result.consoleEnabled mineflayerConsoleState.replEnabled = result.replEnabled + mineflayerConsoleState.takeoverMode = result.takeoverMode ?? false return result } @@ -125,7 +128,7 @@ export const getWsProtocolStream = async (url: string) => { const CHANNEL_NAME = 'minecraft-web-client:data' -export const handleCustomChannel = async () => { +const handleCustomChannel = () => { bot._client.registerChannel(CHANNEL_NAME, ['string', []]) const toCleanup = [] as Array<() => void> subscribe(activeModalStack, () => { @@ -288,4 +291,35 @@ export const handleCustomChannel = async () => { // No default } }) + + return { + send + } +} + +export const onBotCreatedViewerHandler = async () => { + const { send } = handleCustomChannel() + bot.physicsEnabled = false + + await new Promise(resolve => { + bot.once('inject_allowed', resolve) + }) + + const originalSetControlState = bot.setControlState.bind(bot) + bot.setControlState = (control, state) => { + if (bot.controlState[control] === state) { + return + } + if (!mineflayerConsoleState.takeoverMode) { + showNotification('Remote control is not enabled', 'Enable takeoverMode in bot plugin settings first') + return + } + // send command to viewer + send({ + type: 'setControlState', + control, + value: state + }) + originalSetControlState(control, state) + } }