node-gamedig/protocols/minecraftvanilla.js
CosminPerRam da7a4a6334
Remove Players::setNum and stabilize field numplayers (#389)
* Remove Players Set Num

* Stabilize numplayers on armagetron

* Stabilize numplayers on ase

* Stabilize numplayers on assettocorsa

* Optimize away a variable declaration

* Stabilize numplayers on buildandshoot

* Stabilize numplayers on cs2d

* Fix wrong raw field parsed on Doom3

* Updated CHANGELOG and README regarding doom3 fix and numplayers

* Stabilize numplayers on doom3

* Stabilize numplayers on eco

* Stabilize numplayers on ffow

* Stabilize numplayers on quake2

* Stabilize numplayers on gamespy1

* Stabilize numplayers on gamespy2

* Stabilize numplayers on gamespy3

* Remove reductant numplayers setter in jc2mp

* Stabilize numplayers on kspdmp

* Stabilize numplayers on mafia2mp

* Stabilize numplayers on minecraftvanilla and remove players empty placeholders

* Stabilize numplayers on nadeo

* Stabilize numplayers on samp and reduce unused setters

* Stabilize numplayers on terraria

* Stabilize numplayers on tribes1

* Stabilize numplayers on unreal2

* Stabilize numplayers on valve

* Stabilize numplayers on ventrilo

* Battlefield: Set numplayers from info, not players

* Stabilize numplayers on minecraft

* Stabilize numplayers on teamspeak2

* Stabilize numplayers on teamspeak3

* Update CHANGELOG.md to add removal of players placeholders

* Replaced minecraft gamespy numplayers
2023-10-27 19:48:56 +03:00

75 lines
1.9 KiB
JavaScript

import Core from './core.js'
import Varint from 'varint'
export default class minecraftvanilla extends Core {
async run (state) {
const portBuf = Buffer.alloc(2)
portBuf.writeUInt16BE(this.options.port, 0)
const addressBuf = Buffer.from(this.options.host, 'utf8')
const bufs = [
this.varIntBuffer(47),
this.varIntBuffer(addressBuf.length),
addressBuf,
portBuf,
this.varIntBuffer(1)
]
const outBuffer = Buffer.concat([
this.buildPacket(0, Buffer.concat(bufs)),
this.buildPacket(0)
])
const data = await this.withTcp(async socket => {
return await this.tcpSend(socket, outBuffer, data => {
if (data.length < 10) return
const reader = this.reader(data)
const length = reader.varint()
if (data.length < length) return
return reader.rest()
})
})
const reader = this.reader(data)
const packetId = reader.varint()
this.logger.debug('Packet ID: ' + packetId)
const strLen = reader.varint()
this.logger.debug('String Length: ' + strLen)
const str = reader.rest().toString('utf8')
this.logger.debug(str)
const json = JSON.parse(str)
delete json.favicon
state.raw = json
state.maxplayers = json.players.max
state.numplayers = json.players.online
if (json.players.sample) {
for (const player of json.players.sample) {
state.players.push({
id: player.id,
name: player.name
})
}
}
}
varIntBuffer (num) {
return Buffer.from(Varint.encode(num))
}
buildPacket (id, data) {
if (!data) data = Buffer.from([])
const idBuffer = this.varIntBuffer(id)
return Buffer.concat([
this.varIntBuffer(data.length + idBuffer.length),
idBuffer,
data
])
}
}