0.9.0 • Published 4 years ago

mcproto v0.9.0

Weekly downloads
158
License
MIT
Repository
github
Last release
4 years ago

Minecraft Protocol

npm downloads license

mcproto is a small and lightweight implementation of the Minecraft protocol. It aims to be a low-level library that provides the foundations for building clients, servers, proxies and higher level abstractions. This implementation only decodes packets that are related to the connection state or login procedure. That makes it mostly version-independent since those packets usually don't change from version to version.

Features

  • Compression
  • Encryption for client and server
  • Utility classes for writing / reading packets.
  • Asynchronous nextPacket method for reading the next packet (with id).
  • VarLong and 64 bit data types using BigInts

Examples

Server list ping

const { Client, PacketWriter, State } = require("mcproto")

const host = "play.hivemc.com", port = 25565

const client = await Client.connect(host, port)

client.send(new PacketWriter(0x0).writeVarInt(404)
    .writeString(host).writeUInt16(port)
    .writeVarInt(State.Status))

client.send(new PacketWriter(0x0))

const response = await client.nextPacket(0x0)
console.log(response.readJSON())

client.end()

Client

const { Client, PacketWriter, State } = require("mcproto")

const host = "localhost", port = 25565, username = "Notch"

const client = await Client.connect(host, port)

client.send(new PacketWriter(0x0).writeVarInt(340)
    .writeString(host).writeUInt16(port).writeVarInt(State.Login))

// Send login start
client.send(new PacketWriter(0x0).writeString(username))

const listener = client.onPacket(0x0, packet => {
    console.log(packet.readJSON())
})

// The server can request encryption and compression which will be handled
// in the background, so just wait until login success.
await client.nextPacket(0x2, false)
listener.dispose()

client.on("packet", packet => {
    if (packet.id == 0xf) console.log(packet.readJSON())
})

// Send chat message
client.send(new PacketWriter(0x2).writeString("Hello"))

For online servers, you must specify an accessToken and profile ID:

Client.connect("localhost", 25565, {
    profile: "<id>", accessToken: "<token>"
})

More examples can be found in the repository's examples folder.

Events and errors

mcproto uses it's own tiny event emitter class and provides different methods to handle packet, socket and error events.

Since a lot of the API is promise based, errors that happen during the lifetime a promise will result in the promise being rejected.

Errors that happen outside of async method calls should be handled with a error event handler on the connection instance.

const listener = client.on("error", console.error)

// listeners can be removed with:
listener.dispose()
// or
client.off("error", console.error)

The server class does allow to return a Promise in the client handler and it will forward errors to the server's event emitter.

const server = new Server(async client => {
    // errors thrown inside here won't cause a crash but might
    // show warnings if not handled.
    throw "error"
})
server.on("error", console.error)
server.listen()
client.on("packet", packet => {
    // make sure to catch errors inside event handlers
})

For details about packets and general information about the protocol, https://wiki.vg/Protocol is a great reference.

Related projects

0.9.0

4 years ago

0.8.5

4 years ago

0.8.4

4 years ago

0.8.3

4 years ago

0.8.2

4 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.14

5 years ago

0.7.13

5 years ago

0.7.12

5 years ago

0.7.11

5 years ago

0.7.10

5 years ago

0.7.9

5 years ago

0.7.8

5 years ago

0.7.7

5 years ago

0.7.6

5 years ago

0.7.5

5 years ago

0.7.4

5 years ago

0.7.3

5 years ago

0.7.2

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.4

5 years ago

0.6.3

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.4

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago