Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for non-full packets #1278

Closed
wants to merge 12 commits into from
6 changes: 4 additions & 2 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const createDecipher = require('./transforms/encryption').createDecipher
const closeTimeout = 30 * 1000

class Client extends EventEmitter {
constructor (isServer, version, customPackets, hideErrors = false) {
constructor (isServer, version, customPackets, hideErrors = false, fullParser = true) {
super()
this.customPackets = customPackets
this.version = version
Expand All @@ -28,6 +28,7 @@ class Client extends EventEmitter {
this.ended = true
this.latency = 0
this.hideErrors = hideErrors
this.fullParser = fullParser
this.closeTimer = null
const mcData = require('minecraft-data')(version)
this.state = states.HANDSHAKING
Expand All @@ -47,7 +48,8 @@ class Client extends EventEmitter {
packetsToParse:
this.packetsToParse,
customPackets: this.customPackets,
noErrorLogging: this.hideErrors
noErrorLogging: this.hideErrors,
fullParser: this.fullParser
})

this.splitter.recognizeLegacyPing = state === states.HANDSHAKING
Expand Down
2 changes: 1 addition & 1 deletion src/createClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function createClient (options) {
const hideErrors = options.hideErrors || false
const Client = options.Client || DefaultClientImpl

const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors)
const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors, options.fullParser ?? true)

tcpDns(client, options)
if (options.auth instanceof Function) {
Expand Down
5 changes: 3 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function createServer (options = {}) {
favicon,
customPackets,
motdMsg, // This is when you want to send formated motd's from ChatMessage instances
socketType = 'tcp'
socketType = 'tcp',
fullParser = true
} = options

const maxPlayers = options['max-players'] !== undefined ? maxPlayersOld : maxPlayersNew
Expand All @@ -37,7 +38,7 @@ function createServer (options = {}) {
const mcversion = mcData.version
const hideErrors = options.hideErrors || false

const server = new Server(mcversion.minecraftVersion, customPackets, hideErrors)
const server = new Server(mcversion.minecraftVersion, customPackets, hideErrors, fullParser)
server.mcversion = mcversion
server.motd = motd
server.motdMsg = motdMsg
Expand Down
11 changes: 8 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type PromiseLike = Promise<void> | void

declare module 'minecraft-protocol' {
export class Client extends EventEmitter {
constructor(isServer: boolean, version: string, customPackets?: any)
constructor(isServer: boolean, version: string, customPackets?: any, hideErrors?: boolean | undefined, fullParser?: boolean | undefined)
state: States
isServer: boolean
socket: Socket
Expand Down Expand Up @@ -141,10 +141,12 @@ declare module 'minecraft-protocol' {
disableChatSigning?: boolean
/** Pass custom client implementation if needed. */
Client?: Client
// Enable bounds checking
fullParser?: boolean
}

export class Server extends EventEmitter {
constructor(version: string, customPackets?: any)
constructor(version: string, customPackets?: any, hideErrors?: boolean | undefined, fullParser?: boolean | undefined)
writeToClients(clients: Client[], name: string, params: any): void
onlineModeExceptions: object
clients: { [key: number]: ServerClient }
Expand Down Expand Up @@ -200,13 +202,16 @@ declare module 'minecraft-protocol' {
enableChatPreview?: boolean
socketType?: 'tcp' | 'ipc'
Server?: Server
// Enable bounds checking
fullParser?: boolean
}

export interface SerializerOptions {
customPackets: any
isServer?: boolean
state?: States
version: string
fullParser?: boolean
}

export interface MicrosoftDeviceAuthorizationResponse {
Expand Down Expand Up @@ -284,7 +289,7 @@ declare module 'minecraft-protocol' {

// TODO: Create typings on protodef to define here
export function createSerializer({ state, isServer, version, customPackets }: SerializerOptions): any
export function createDeserializer({ state, isServer, version, customPackets }: SerializerOptions): any
export function createDeserializer({ state, isServer, version, customPackets, fullParser }: SerializerOptions): any

export function ping(options: PingOptions, callback?: (error: Error, result: OldPingResult | NewPingResult) => void): Promise<OldPingResult | NewPingResult>
}
5 changes: 3 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const states = require('./states')
const { createSerializer } = require('./transforms/serializer')

class Server extends EventEmitter {
constructor (version, customPackets, hideErrors = false) {
constructor (version, customPackets, hideErrors = false, fullParser = true) {
super()
this.version = version
this.socketServer = null
Expand All @@ -16,6 +16,7 @@ class Server extends EventEmitter {
this.clients = {}
this.customPackets = customPackets
this.hideErrors = hideErrors
this.fullParser = fullParser
this.serializer = createSerializer({ state: 'play', isServer: true, version, customPackets })
}

Expand All @@ -24,7 +25,7 @@ class Server extends EventEmitter {
let nextId = 0
self.socketServer = net.createServer()
self.socketServer.on('connection', socket => {
const client = new Client(true, this.version, this.customPackets, this.hideErrors)
const client = new Client(true, this.version, this.customPackets, this.hideErrors, this.fullParser)
client._end = client.end
client.end = function end (endReason, fullReason = JSON.stringify({ text: endReason })) {
if (client.state === states.PLAY) {
Expand Down
11 changes: 5 additions & 6 deletions src/transforms/serializer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict'

const ProtoDef = require('protodef').ProtoDef
const Serializer = require('protodef').Serializer
const Parser = require('protodef').FullPacketParser
const { ProtoDefCompiler } = require('protodef').Compiler
const { ProtoDef, Serializer, Parser, FullPacketParser, Compiler: { ProtoDefCompiler } } = require('protodef')

const nbt = require('prismarine-nbt')
const minecraft = require('../datatypes/minecraft')
Expand Down Expand Up @@ -49,8 +46,10 @@ function createSerializer ({ state = states.HANDSHAKING, isServer = false, versi
return new Serializer(createProtocol(state, !isServer ? 'toServer' : 'toClient', version, customPackets, compiled), 'packet')
}

function createDeserializer ({ state = states.HANDSHAKING, isServer = false, version, customPackets, compiled = true, noErrorLogging = false } = {}) {
return new Parser(createProtocol(state, isServer ? 'toServer' : 'toClient', version, customPackets, compiled), 'packet', noErrorLogging)
function createDeserializer ({ state = states.HANDSHAKING, isServer = false, version, customPackets, compiled = true, noErrorLogging = false, fullParser = true } = {}) {
return fullParser
? new FullPacketParser(createProtocol(state, isServer ? 'toServer' : 'toClient', version, customPackets, compiled), 'packet', noErrorLogging)
: new Parser(createProtocol(state, isServer ? 'toServer' : 'toClient', version, customPackets, compiled), 'packet', noErrorLogging)
}

module.exports = {
Expand Down
Loading