// DisplayMessage responds with a disconnect message to the player // when they attempt to connect to the server. func DisplayMessage(s protocol.Stream, message string) error { responsePacket := protocol.NewPacketWithID(0x00) chatMessage := message data, err := json.Marshal(chatMessage) if err != nil { return err } responsePacket.WriteString(string(data)) err = s.WritePacket(responsePacket) return err }
// WriteHandshakeResponse writes a response with a status that will be // displayed on the requesting player's server list menu. func WriteHandshakeResponse(s protocol.Stream, status Status) error { statusResponse := statusResponse{ Version: version{ Name: "1lann/beacon " + getReleaseName(status.ProtocolNumber), Protocol: status.ProtocolNumber, }, Players: players{ Max: status.MaxPlayers, Online: status.OnlinePlayers, }, Description: status.Message, } data, err := json.Marshal(statusResponse) if err != nil { return err } responsePacket := protocol.NewPacketWithID(0x00) responsePacket.WriteString(string(data)) err = s.WritePacket(responsePacket) return err }
// ReadHandshakePacket reads a handshake packet (packet ID 0) and decodes it. func ReadHandshakePacket(s protocol.Stream) (HandshakePacket, error) { handshake := HandshakePacket{} var err error if handshake.ProtocolNumber, err = s.ReadVarInt(); err != nil { return HandshakePacket{}, err } if handshake.ServerAddress, err = s.ReadString(); err != nil { return HandshakePacket{}, err } if handshake.ServerPort, err = s.ReadUInt16(); err != nil { return HandshakePacket{}, err } handshake.NextState, err = s.ReadVarInt() return handshake, err }
// HandlePingPacket handles a ping packet used by the Minecraft client // used to measure the round trip time of the connection. func HandlePingPacket(s protocol.Stream, status Status) error { if !status.ShowConnection { s.ReadInt64() return nil } time, err := s.ReadInt64() if err != nil { return err } responsePacket := protocol.NewPacketWithID(0x01) responsePacket.WriteInt64(time) err = s.WritePacket(responsePacket) return err }