All Projects → BRA1L0R → go-mcproto

BRA1L0R / go-mcproto

Licence: MIT license
Minecraft Protocol implementation in Go

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to go-mcproto

Geyser
A bridge/proxy allowing you to connect to Minecraft: Java Edition servers with Minecraft: Bedrock Edition.
Stars: ✭ 2,851 (+8539.39%)
Mutual labels:  spigot, protocol
ProtocolLib
Provides read and write access to the Minecraft protocol with Bukkit.
Stars: ✭ 625 (+1793.94%)
Mutual labels:  spigot, protocol
can-prog
Command-line tool to flashing devices by CAN-BUS
Stars: ✭ 66 (+100%)
Mutual labels:  protocol
ignite
A Mixin and Access Widener mod loader for Spigot/Paper
Stars: ✭ 115 (+248.48%)
Mutual labels:  spigot
grakkit
A modern JavaScript development environment for Minecraft.
Stars: ✭ 184 (+457.58%)
Mutual labels:  spigot
HamsterAPI
Simple and easy to use API to read and write Packets.
Stars: ✭ 25 (-24.24%)
Mutual labels:  spigot
network
Monorepo containing all the main components of Streamr Network.
Stars: ✭ 522 (+1481.82%)
Mutual labels:  protocol
goridge-php
PHP Goridge Protocol implementation
Stars: ✭ 53 (+60.61%)
Mutual labels:  protocol
LevelledMobs
Level-up mobs on your Spigot/Paper server, RPG-style!
Stars: ✭ 143 (+333.33%)
Mutual labels:  spigot
republic-go
An official reference implementation of Republic Protocol, written in Go
Stars: ✭ 58 (+75.76%)
Mutual labels:  protocol
gonano
An implementation of the Nano cryptocurrency in Go
Stars: ✭ 34 (+3.03%)
Mutual labels:  protocol
Assemble
⚡ Highly performant Scoreboard library for Spigot 1.7.x to 1.17.x
Stars: ✭ 99 (+200%)
Mutual labels:  spigot
AdvancedCore
Core API for my plugins on SpigotMC
Stars: ✭ 54 (+63.64%)
Mutual labels:  spigot
osdp-python
A Python control panel implementation of the Open Supervised Device Protocol (OSDP)
Stars: ✭ 28 (-15.15%)
Mutual labels:  protocol
TileCulling
Hides tiles that players are not able to see due to blocks in the way, preventing cheaters from seeing chests behind walls.
Stars: ✭ 31 (-6.06%)
Mutual labels:  spigot
433MHz Tx Rx
Arduino based 433MHz Tx and Rx combo using Manchester protocol
Stars: ✭ 27 (-18.18%)
Mutual labels:  protocol
Skript-1.8
The Skript plugin made for Minecraft 1.8.x only. Releases will follow the original repository, except for some bug fixes. Please read the README before updating to Skript-1.8 !
Stars: ✭ 38 (+15.15%)
Mutual labels:  spigot
simple-commands
An (even more) simplified and intuitive command framework for Spigot.
Stars: ✭ 14 (-57.58%)
Mutual labels:  spigot
HeadsPlus
A heads plugin that has grown for over two years into something more ambitious than other plugins.
Stars: ✭ 35 (+6.06%)
Mutual labels:  spigot
procbridge
A super-lightweight IPC (Inter-Process Communication) protocol over TCP socket.
Stars: ✭ 118 (+257.58%)
Mutual labels:  protocol

go-mcproto

Go Reference

Install

Go 1.16.x is required to use this library

go get github.com/BRA1L0R/go-mcproto

Opening a connection

// Define the client
client := mcproto.Client{}

// Use the Initialize method which connects to the server and sends Login information
client.Initialize("127.0.0.1", 25565, 755, "GolangBot")

Before opening a connection to a server, you'll have to specify some vital information such as the host, the port, the name of your bot (which will login in offline mode) and the protocol version, which will have to match the server one, unless the server uses some kind of backward compatibility plugin such as ViaVersion.

In this case, 755 is the protocol version for Minecraft 1.17 but you can find all the versions here

Once you define client, you can either use the already implemented handshake method (client.Initialize) or you can manually open the connection using client.Connect

client.Initialize only supports offline mode (unauthenticated SP) at the moment but I will soon implement online mode as well.

Defining a packet

Not all packets are implemented in the library there are only the ones that will get you past the login state.

Fortunately, defining a packet with go-mcproto is as easy as declaring a struct. Here's a quick example

type ChatMessage struct {
  packets.MinecraftPacket

  JsonData string `mc:"string"`
  Position byte   `mc:"inherit"`
  Sender   []byte `mc:"bytes" len:"16"` // UUID is 128bit, hence the 16 byte len
}

packets.MinecraftPackets will add the rest of the fields which are needed for a packet to be conformant to the standard format, compressed or not

ChatMessage will also inherit from packets.MinecraftPackets the methods for serialization of the fields into Data and the final serialization which returns the byte slice that can be sent over a connection using client.WritePacket

Arrays

Sometimes you might encounter a packet that sends before an array the length of it. Fortunately, you can still deserialize the packet with no extra steps thanks to the len tag:

type Biome struct {
  BiomeID int32 `mc:"varint"`
}

type ChunkData struct {
  packets.MinecraftPacket

  BiomesLength int32   `mc:"varint"`
  Biomes       []Biome `mc:"array" len:"BiomesLength"`
}

The Biome struct can contain as many fields as you like, as the minecraft protocol has a lot of cases where there are multi-fielded arrays

Field Dependency

In some cases, a field is present only if the previous one is true (in the case of a boolean). You can still manage to do this with only struct tags by using the depends_on tag:

type PlayerInfo struct {
  packets.MinecraftPacket

  HasDisplayName bool   `mc:"inherit"`
  DisplayName    string `mc:"string" depends_on:"HasDisplayName"`
}

All the available tags

This library already integrates all the possible types to define all possible data types, including NBTs.

Here's a list of all the available tags:

  • mc:"inherit": this tag covers all integers, complexes, floats, and the byte type. It inherits the length from the type defined in the struct. See the examples above. Please note: the Minecraft protocol is effectively big-endian, except varints, which are defined using another tag.
  • mc:"varint" and mc:"varlong": Used to encode varints and varlongs respectively. Requires a int32 and int64 (respectively) as the field types
  • mc:"string": requires a string field type in the struct, it encodes its length using a varint end then the string is encoded using the UTF-8.
  • mc:"bytes" len:"X": Reads X bytes from the buffer and puts it in a byte slice, which must be the type of the struct field this tag is assigned to. X is only required for deserialization in this case
  • mc:"ignore" len:"X": Ignores X bytes from the buffer. This means that it discards X bytes from the data buffer in case of deserialization or writes X null bytes in the data buffer in case of serialization
  • mc:"nbt": Encodes or decodes an nbt struct. For more information check https://github.com/Tnze/go-mc/tree/master/nbt
  • mc:"array" len:"X": Can be used to array every previous tags. Check this section

Sending a packet

Once you have defined a packet you want to send, you will have to call client.WritePacket() to serialize the data and send it over a connection.

packet := new(MyPacket)
packet.PacketID = 0x00
// you can look up the packet ids on wiki.vg
// all the other fields

client.WritePacket(packet)

Sending a raw packet

If you already put data into the Data buffer by yourself, without using the included serialized, then you can send the packet using the WriteRawPacket method.

Receiving a packet

Receiving a packet is done by calling client.ReceivePacket. The server will receive the packet length and wait for the server until all bytes are fulfilled. If it encounters an error it will return it, but it will keep receiving packets as all the packet length is already consumed from the connection.

client.ReceivePacket will return a MinecraftPacket, which can be deserialized using the DeserializeData method. It'a called by passing a pointer to a struct containing mc struct tags, as explained here

packet, err := client.ReceivePacket()
if err != nil {
  // an error has been encountered during the reception of the packet
  panic(err)
}

keepalive := new(models.KeepAlivePacket)
err := packet.DeserializeData(keepalive)
if err != nil {
  panic(err)
}

fmt.Println(keepalive.KeepAliveID) // 123456

Variable packet content

There are multiple cases in the Minecraft protocol where the packet content is variable depending on certain values inside the struct. This is no problem for the library, as you can easily receive a part of the packet (by defining only the fixed fields in the struct) and then, later on, continue the deserialization by calling DeserializeData on a different struct.

Here's a practical demonstration:

type FixedContent struct {
  packets.MinecraftPacket

  PacketType int32 `mc:"varint"`
}

type SomeOtherContent struct {
  packets.MinecraftPacket

  Data string `mc:"string"`
}

packet, _ := client.ReceivePacket()
// from now on I'm gonna avoid doing error handling in the examples for practical reasons
// but you MUST do it.

fixedPacket := new(FixedContent)
packet.DeserializeData(fixedPacket)

if fixedPacket.PacketType == 0x05 {
  someOtherContent := new(SomeOtherContent)
  packet.DeserializeData(someOtherContent)
} else {
  panic("Unknown packet type!")
}

Example

This example initializes the connection between the client and a server, thus switching to the Play state, and listens for keepalive packets to which it responds

package main

import (
	"github.com/BRA1L0R/go-mcproto"
	"github.com/BRA1L0R/go-mcproto/packets/models"
)

func main() {
  client := mcproto.Client{}
	client.Initialize("127.0.0.1", 25565, 755, "GolangBot")

  for {
    packet, err := client.ReceivePacket()
    if err != nil {
      panic(err)
    }

    if packet.PacketID == 0x1F { // clientbound keepalive packetid
      receivedKeepalive := new(models.KeepAlivePacket)
      err := packet.DeserializeData(receivedKeepalive)
      if err != nil {
        panic(err)
      }

      serverBoundKeepalive := new(models.KeepAlivePacket)
      serverBoundKeepalive.KeepAliveID = receivedKeepalive.KeepAliveID
      serverBoundKeepalive.PacketID = 0x10 // serverbound keepaliveid

      err = client.WritePacket(serverBoundKeepalive)
    }
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].