Skip to content

Commit

Permalink
Support 1.20.3 disconnect, enable 1.20.3/4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Apr 14, 2024
1 parent 174c968 commit 809ff69
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Disclaimer: This app is NOT an official Minecraft product. It is not associated
## Features (still being worked on)

- Fully open-source with no ads! Easily report issues through GitHub and get a direct response.
- Supports connecting to Minecraft 1.16.4 through Minecraft 1.20.2 servers. (Older versions planned.)
- Supports connecting to Minecraft 1.16.4 through Minecraft 1.20.4 servers. (Older versions planned.)
- Supports all Minecraft chat features, which sometimes trip up other chat apps.
- Send join messages and commands on connecting to a server.
- Health change indicator and respawn on death support.
Expand Down
3 changes: 1 addition & 2 deletions src/components/servers/EditServerDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ const EditServerDialog = ({
dropdownIconColor={darkMode ? '#ffffff' : '#000000'}
>
<Picker.Item label='Auto' value='auto' />
{/* FIXME: Update README after enablement. */}
<Picker.Item label='1.20.3/1.20.4 (DOES NOT WORK!)' value='1.20.3' />
<Picker.Item label='1.20.3/1.20.4 (WIP)' value='1.20.3' />
<Picker.Item label='1.20.2 (WIP)' value='1.20.2' />
<Picker.Item label='1.20/1.20.1 (WIP)' value='1.20' />
<Picker.Item label='1.19.4 (WIP)' value='1.19.4' />
Expand Down
3 changes: 2 additions & 1 deletion src/minecraft/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type Packet } from '../packet'
import { type Certificate } from '../api/mojang'
import initiateJavaScriptConnection from './javascript'
import initiateNativeConnection, { isNativeConnectionAvailable } from './native'
import { type MinecraftChat } from '../chatToJsx'

export interface ConnectionOptions {
serverName: string
Expand All @@ -26,7 +27,7 @@ export interface ServerConnection extends events.EventEmitter {
state: ConnectionState
closed: boolean
msgSalt?: Buffer
disconnectReason?: string // FIXME: This is no longer just a string, it may be NBT too...
disconnectReason?: MinecraftChat

writePacket: (packetId: number, data: Buffer) => Promise<boolean>

Expand Down
19 changes: 13 additions & 6 deletions src/minecraft/connection/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ import {
type ConnectionOptions,
ConnectionState
} from '.'
import { type MinecraftChat } from '../chatToJsx'
import { getLoginPacket, handleEncryptionRequest } from './shared'
import { readVarInt, writeVarInt, resolveHostname, protocolMap } from '../utils'
import {
readVarInt,
writeVarInt,
resolveHostname,
protocolMap,
parseChat
} from '../utils'
import packetIds from '../packets/ids'

export declare interface JavaScriptServerConnection {
Expand All @@ -45,7 +52,7 @@ export class JavaScriptServerConnection
socket: net.Socket
options: ConnectionOptions
disconnectTimer?: NodeJS.Timeout
disconnectReason?: string
disconnectReason?: MinecraftChat
aesDecipher?: Decipher
aesCipher?: Cipher

Expand Down Expand Up @@ -199,10 +206,10 @@ const initiateJavaScriptConnection = async (
(packet.id === packetIds.CLIENTBOUND_DISCONNECT_PLAY(version) &&
conn.state === ConnectionState.PLAY)
) {
const [chatLength, chatVarIntLength] = readVarInt(packet.data)
conn.disconnectReason = packet.data
.slice(chatVarIntLength, chatVarIntLength + chatLength)
.toString('utf8')
conn.disconnectReason = parseChat(
packet.data, // The Disconnect (login) packet always returns JSON.
conn.state === ConnectionState.LOGIN ? undefined : version
)[0]
} else if (
packet.id === 0x04 &&
conn.state === ConnectionState.LOGIN
Expand Down
19 changes: 13 additions & 6 deletions src/minecraft/connection/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ import {
type ConnectionOptions,
ConnectionState
} from '.'
import { type MinecraftChat } from '../chatToJsx'
import { concatPacketData, type Packet } from '../packet'
import { getLoginPacket, handleEncryptionRequest } from './shared'
import { readVarInt, writeVarInt, resolveHostname, protocolMap } from '../utils'
import {
readVarInt,
writeVarInt,
resolveHostname,
protocolMap,
parseChat
} from '../utils'
import packetIds from '../packets/ids'

const { ConnectionModule } = NativeModules
Expand Down Expand Up @@ -50,7 +57,7 @@ export class NativeServerConnection
id: string
options: ConnectionOptions
disconnectTimer?: NodeJS.Timeout
disconnectReason?: string
disconnectReason?: MinecraftChat
msgSalt?: Buffer

constructor(id: string, options: ConnectionOptions) {
Expand Down Expand Up @@ -118,10 +125,10 @@ export class NativeServerConnection
(packet.id === packetIds.CLIENTBOUND_DISCONNECT_PLAY(version) &&
this.state === ConnectionState.PLAY)
) {
const [chatLength, chatVarIntLength] = readVarInt(packet.data)
this.disconnectReason = packet.data
.slice(chatVarIntLength, chatVarIntLength + chatLength)
.toString('utf8')
this.disconnectReason = parseChat(
packet.data, // The Disconnect (login) packet always returns JSON.
this.state === ConnectionState.LOGIN ? undefined : version
)[0]
} else if (packet.id === 0x04 && this.state === ConnectionState.LOGIN) {
/* Login Plugin Request */
const [msgId] = readVarInt(packet.data)
Expand Down
2 changes: 1 addition & 1 deletion src/minecraft/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const protocolMap = {
'1.20.2': 764,
'1.20.3': 765,
'1.20.4': 765,
latest: 764, // FIXME: Set to 765 when 1.20.4 is supported.
latest: 765,
auto: -1
}

Expand Down
14 changes: 3 additions & 11 deletions src/utilities/connection/connectionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ import { refresh } from '../../minecraft/api/yggdrasil'
import initiateConnection, {
type ServerConnection
} from '../../minecraft/connection'
import {
parseIp,
parseJsonChat,
protocolMap,
resolveHostname
} from '../../minecraft/utils'
import { parseIp, protocolMap, resolveHostname } from '../../minecraft/utils'
import config from '../../../config.json'

export const getSession = async (
Expand Down Expand Up @@ -133,11 +128,8 @@ export const createConnection = async (
certificate: settings.enableChatSigning ? session?.certificate : undefined
})
const onCloseOrError = (): void => {
closeChatScreen(
newConn.disconnectReason
? { server, reason: parseJsonChat(newConn.disconnectReason) }
: undefined
)
const reason = newConn.disconnectReason
closeChatScreen(reason ? { server, reason } : undefined)
setConnection(undefined)
}
newConn.on('close', onCloseOrError)
Expand Down

0 comments on commit 809ff69

Please sign in to comment.