add configuration support for /server packet
This commit is contained in:
@ -1,175 +0,0 @@
|
||||
// This example is a minimal minecraft 1.15.2 server allowing vanilla clients or go-mc/bot to connect.
|
||||
//
|
||||
// This example handles "ping and list", so you can see its motd and player count in server list.
|
||||
// Players can join the server and seeing an empty world. No authentication profile is checked.
|
||||
// The KeepAlive packet is not handled, so client might exit 20 seconds later.
|
||||
//
|
||||
// It doesn't use go-mc/server but the go-mc/net package to handle the connection and implement basic 1.15.2 protocol,
|
||||
// proving that even latest go-mc also support old minecraft versions.
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/Tnze/go-mc/net"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
"github.com/Tnze/go-mc/offline"
|
||||
)
|
||||
|
||||
const (
|
||||
ProtocolVersion = 578
|
||||
MaxPlayer = 200
|
||||
)
|
||||
|
||||
// Packet IDs
|
||||
const (
|
||||
PlayerPositionAndLookClientbound = 0x36
|
||||
JoinGame = 0x26
|
||||
)
|
||||
|
||||
func main() {
|
||||
l, err := net.ListenMC(":25565")
|
||||
if err != nil {
|
||||
log.Fatalf("Listen error: %v", err)
|
||||
}
|
||||
|
||||
for {
|
||||
conn, err := l.Accept()
|
||||
if err != nil {
|
||||
log.Fatalf("Accept error: %v", err)
|
||||
}
|
||||
go acceptConn(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func acceptConn(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
// handshake
|
||||
protocol, intention, err := handshake(conn)
|
||||
if err != nil {
|
||||
log.Printf("Handshake error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch intention {
|
||||
default: // unknown error
|
||||
log.Printf("Unknown handshake intention: %v", intention)
|
||||
case 1: // for status
|
||||
acceptListPing(conn)
|
||||
case 2: // for login
|
||||
handlePlaying(conn, protocol)
|
||||
}
|
||||
}
|
||||
|
||||
func handlePlaying(conn net.Conn, protocol int32) {
|
||||
// login, get player info
|
||||
info, err := acceptLogin(conn)
|
||||
if err != nil {
|
||||
log.Print("Login failed")
|
||||
return
|
||||
}
|
||||
|
||||
// Write LoginSuccess packet
|
||||
|
||||
if err = loginSuccess(conn, info.Name, info.UUID); err != nil {
|
||||
log.Print("Login failed on success")
|
||||
return
|
||||
}
|
||||
|
||||
if err := joinGame(conn); err != nil {
|
||||
log.Print("Login failed on joinGame")
|
||||
return
|
||||
}
|
||||
if err := conn.WritePacket(pk.Marshal(PlayerPositionAndLookClientbound,
|
||||
// https://wiki.vg/index.php?title=Protocol&oldid=16067#Player_Position_And_Look_.28clientbound.29
|
||||
pk.Double(0), pk.Double(0), pk.Double(0), // XYZ
|
||||
pk.Float(0), pk.Float(0), // Yaw Pitch
|
||||
pk.Byte(0), // flag
|
||||
pk.VarInt(0), // TP ID
|
||||
)); err != nil {
|
||||
log.Print("Login failed on sending PlayerPositionAndLookClientbound")
|
||||
return
|
||||
}
|
||||
// Just for block this goroutine. Keep the connection
|
||||
for {
|
||||
var p pk.Packet
|
||||
if err := conn.ReadPacket(&p); err != nil {
|
||||
log.Printf("ReadPacket error: %v", err)
|
||||
break
|
||||
}
|
||||
// KeepAlive packet is not handled, so client might
|
||||
// exit because of "time out".
|
||||
}
|
||||
}
|
||||
|
||||
type PlayerInfo struct {
|
||||
Name string
|
||||
UUID uuid.UUID
|
||||
OPLevel int
|
||||
}
|
||||
|
||||
// acceptLogin check player's account
|
||||
func acceptLogin(conn net.Conn) (info PlayerInfo, err error) {
|
||||
// login start
|
||||
var p pk.Packet
|
||||
err = conn.ReadPacket(&p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = p.Scan((*pk.String)(&info.Name)) // decode username as pk.String
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// auth
|
||||
const OnlineMode = false
|
||||
if OnlineMode {
|
||||
log.Panic("Not Implement")
|
||||
} else {
|
||||
// offline-mode UUID
|
||||
info.UUID = offline.NameToUUID(info.Name)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// handshake receive and parse Handshake packet
|
||||
func handshake(conn net.Conn) (protocol, intention int32, err error) {
|
||||
var (
|
||||
p pk.Packet
|
||||
Protocol, Intention pk.VarInt
|
||||
ServerAddress pk.String // ignored
|
||||
ServerPort pk.UnsignedShort // ignored
|
||||
)
|
||||
// receive handshake packet
|
||||
if err = conn.ReadPacket(&p); err != nil {
|
||||
return
|
||||
}
|
||||
err = p.Scan(&Protocol, &ServerAddress, &ServerPort, &Intention)
|
||||
return int32(Protocol), int32(Intention), err
|
||||
}
|
||||
|
||||
// loginSuccess send LoginSuccess packet to client
|
||||
func loginSuccess(conn net.Conn, name string, uuid uuid.UUID) error {
|
||||
return conn.WritePacket(pk.Marshal(0x02,
|
||||
pk.String(uuid.String()), // uuid as string with hyphens
|
||||
pk.String(name),
|
||||
))
|
||||
}
|
||||
|
||||
func joinGame(conn net.Conn) error {
|
||||
return conn.WritePacket(pk.Marshal(JoinGame,
|
||||
pk.Int(0), // EntityID
|
||||
pk.UnsignedByte(1), // Gamemode
|
||||
pk.Int(0), // Dimension
|
||||
pk.Long(0), // HashedSeed
|
||||
pk.UnsignedByte(MaxPlayer), // MaxPlayer
|
||||
pk.String("default"), // LevelType
|
||||
pk.VarInt(15), // View Distance
|
||||
pk.Boolean(false), // Reduced Debug Info
|
||||
pk.Boolean(true), // Enable respawn screen
|
||||
))
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/net"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
)
|
||||
|
||||
func acceptListPing(conn net.Conn) {
|
||||
var p pk.Packet
|
||||
for i := 0; i < 2; i++ { // ping or list. Only accept twice
|
||||
err := conn.ReadPacket(&p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch p.ID {
|
||||
case 0x00: // List
|
||||
err = conn.WritePacket(pk.Marshal(0x00, pk.String(listResp())))
|
||||
case 0x01: // Ping
|
||||
err = conn.WritePacket(p)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type player struct {
|
||||
Name string `json:"name"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
}
|
||||
|
||||
// listResp return server status as JSON string
|
||||
func listResp() string {
|
||||
var list struct {
|
||||
Version struct {
|
||||
Name string `json:"name"`
|
||||
Protocol int `json:"protocol"`
|
||||
} `json:"version"`
|
||||
Players struct {
|
||||
Max int `json:"max"`
|
||||
Online int `json:"online"`
|
||||
Sample []player `json:"sample"`
|
||||
} `json:"players"`
|
||||
Description chat.Message `json:"description"`
|
||||
FavIcon string `json:"favicon,omitempty"`
|
||||
}
|
||||
|
||||
list.Version.Name = "Chat Server"
|
||||
list.Version.Protocol = ProtocolVersion
|
||||
list.Players.Max = MaxPlayer
|
||||
list.Players.Online = 123
|
||||
list.Players.Sample = []player{} // must init. can't be nil
|
||||
list.Description = chat.Message{Text: "Powered by go-mc", Color: "blue"}
|
||||
|
||||
data, err := json.Marshal(list)
|
||||
if err != nil {
|
||||
log.Panic("Marshal JSON for status checking fail")
|
||||
}
|
||||
return string(data)
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
{
|
||||
piglin_safe: 0b,
|
||||
natural: 1b,
|
||||
ambient_light: 0.0f,
|
||||
infiniburn: "minecraft:infiniburn_overworld",
|
||||
respawn_anchor_works: 0b,
|
||||
has_skylight: 1b,
|
||||
bed_works: 1b,
|
||||
effects: "minecraft:overworld",
|
||||
has_raids: 1b,
|
||||
min_y: 0,
|
||||
height: 256,
|
||||
logical_height: 256,
|
||||
coordinate_scale: 1.0d,
|
||||
ultrawarm: 0b,
|
||||
has_ceiling: 0b
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,186 +0,0 @@
|
||||
// This example is a minimal minecraft 1.17.1 server allowing vanilla clients or go-mc/bot to connect.
|
||||
// Has the same functionality as "simpleServer1.15.2", but with 1.17.1 protocol.
|
||||
//
|
||||
// It is used to test DimensionCodec stuffs during go-mc development.
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"log"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
"github.com/Tnze/go-mc/net"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
"github.com/Tnze/go-mc/offline"
|
||||
)
|
||||
|
||||
const (
|
||||
ProtocolVersion = 756
|
||||
MaxPlayer = 200
|
||||
)
|
||||
|
||||
// Packet IDs
|
||||
const (
|
||||
PlayerPositionAndLookClientbound = 0x38
|
||||
JoinGame = 0x26
|
||||
)
|
||||
|
||||
func main() {
|
||||
l, err := net.ListenMC(":25565")
|
||||
if err != nil {
|
||||
log.Fatalf("Listen error: %v", err)
|
||||
}
|
||||
|
||||
for {
|
||||
conn, err := l.Accept()
|
||||
if err != nil {
|
||||
log.Fatalf("Accept error: %v", err)
|
||||
}
|
||||
go acceptConn(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func acceptConn(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
// handshake
|
||||
protocol, intention, err := handshake(conn)
|
||||
if err != nil {
|
||||
log.Printf("Handshake error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch intention {
|
||||
default: // unknown error
|
||||
log.Printf("Unknown handshake intention: %v", intention)
|
||||
case 1: // for status
|
||||
acceptListPing(conn)
|
||||
case 2: // for login
|
||||
handlePlaying(conn, protocol)
|
||||
}
|
||||
}
|
||||
|
||||
func handlePlaying(conn net.Conn, protocol int32) {
|
||||
// login, get player info
|
||||
info, err := acceptLogin(conn)
|
||||
if err != nil {
|
||||
log.Print("Login failed")
|
||||
return
|
||||
}
|
||||
|
||||
// Write LoginSuccess packet
|
||||
|
||||
if err = loginSuccess(conn, info.Name, info.UUID); err != nil {
|
||||
log.Print("Login failed on success")
|
||||
return
|
||||
}
|
||||
|
||||
if err := joinGame(conn); err != nil {
|
||||
log.Print("Login failed on joinGame")
|
||||
return
|
||||
}
|
||||
if err := conn.WritePacket(pk.Marshal(PlayerPositionAndLookClientbound,
|
||||
// https://wiki.vg/index.php?title=Protocol&oldid=16067#Player_Position_And_Look_.28clientbound.29
|
||||
pk.Double(0), pk.Double(0), pk.Double(0), // XYZ
|
||||
pk.Float(0), pk.Float(0), // Yaw Pitch
|
||||
pk.Byte(0), // flag
|
||||
pk.VarInt(0), // TP ID
|
||||
pk.Boolean(false), // Dismount vehicle
|
||||
)); err != nil {
|
||||
log.Printf("Login failed on sending PlayerPositionAndLookClientbound: %v", err)
|
||||
return
|
||||
}
|
||||
// Just for block this goroutine. Keep the connection
|
||||
for {
|
||||
var p pk.Packet
|
||||
if err := conn.ReadPacket(&p); err != nil {
|
||||
log.Printf("ReadPacket error: %v", err)
|
||||
break
|
||||
}
|
||||
// KeepAlive packet is not handled, so client might
|
||||
// exit because of "time out".
|
||||
}
|
||||
}
|
||||
|
||||
type PlayerInfo struct {
|
||||
Name string
|
||||
UUID uuid.UUID
|
||||
OPLevel int
|
||||
}
|
||||
|
||||
// acceptLogin check player's account
|
||||
func acceptLogin(conn net.Conn) (info PlayerInfo, err error) {
|
||||
// login start
|
||||
var p pk.Packet
|
||||
err = conn.ReadPacket(&p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = p.Scan((*pk.String)(&info.Name)) // decode username as pk.String
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// auth
|
||||
const OnlineMode = false
|
||||
if OnlineMode {
|
||||
log.Panic("Not Implement")
|
||||
} else {
|
||||
// offline-mode UUID
|
||||
info.UUID = offline.NameToUUID(info.Name)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// handshake receive and parse Handshake packet
|
||||
func handshake(conn net.Conn) (protocol, intention int32, err error) {
|
||||
var (
|
||||
p pk.Packet
|
||||
Protocol, Intention pk.VarInt
|
||||
ServerAddress pk.String // ignored
|
||||
ServerPort pk.UnsignedShort // ignored
|
||||
)
|
||||
// receive handshake packet
|
||||
if err = conn.ReadPacket(&p); err != nil {
|
||||
return
|
||||
}
|
||||
err = p.Scan(&Protocol, &ServerAddress, &ServerPort, &Intention)
|
||||
return int32(Protocol), int32(Intention), err
|
||||
}
|
||||
|
||||
// loginSuccess send LoginSuccess packet to client
|
||||
func loginSuccess(conn net.Conn, name string, uuid uuid.UUID) error {
|
||||
return conn.WritePacket(pk.Marshal(0x02,
|
||||
pk.UUID(uuid),
|
||||
pk.String(name),
|
||||
))
|
||||
}
|
||||
|
||||
//go:embed DimensionCodec.snbt
|
||||
var dimensionCodecSNBT string
|
||||
|
||||
//go:embed Dimension.snbt
|
||||
var dimensionSNBT string
|
||||
|
||||
func joinGame(conn net.Conn) error {
|
||||
return conn.WritePacket(pk.Marshal(JoinGame,
|
||||
pk.Int(0), // EntityID
|
||||
pk.Boolean(false), // Is hardcore
|
||||
pk.UnsignedByte(1), // Gamemode
|
||||
pk.Byte(1), // Previous Gamemode
|
||||
pk.Array([]pk.Identifier{"world"}), // World Names
|
||||
pk.NBT(nbt.StringifiedMessage(dimensionCodecSNBT)), // Dimension codec
|
||||
pk.NBT(nbt.StringifiedMessage(dimensionSNBT)), // Dimension
|
||||
pk.Identifier("world"), // World Name
|
||||
pk.Long(0), // Hashed Seed
|
||||
pk.VarInt(MaxPlayer), // Max Players
|
||||
pk.VarInt(15), // View Distance
|
||||
pk.Boolean(false), // Reduced Debug Info
|
||||
pk.Boolean(true), // Enable respawn screen
|
||||
pk.Boolean(false), // Is Debug
|
||||
pk.Boolean(true), // Is Flat
|
||||
))
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/net"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func acceptListPing(conn net.Conn) {
|
||||
var p pk.Packet
|
||||
for i := 0; i < 2; i++ { // ping or list. Only accept twice
|
||||
err := conn.ReadPacket(&p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch p.ID {
|
||||
case 0x00: // List
|
||||
err = conn.WritePacket(pk.Marshal(0x00, pk.String(listResp())))
|
||||
case 0x01: // Ping
|
||||
err = conn.WritePacket(p)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type player struct {
|
||||
Name string `json:"name"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
}
|
||||
|
||||
// listResp return server status as JSON string
|
||||
func listResp() string {
|
||||
var list struct {
|
||||
Version struct {
|
||||
Name string `json:"name"`
|
||||
Protocol int `json:"protocol"`
|
||||
} `json:"version"`
|
||||
Players struct {
|
||||
Max int `json:"max"`
|
||||
Online int `json:"online"`
|
||||
Sample []player `json:"sample"`
|
||||
} `json:"players"`
|
||||
Description chat.Message `json:"description"`
|
||||
FavIcon string `json:"favicon,omitempty"`
|
||||
}
|
||||
|
||||
list.Version.Name = "Chat Server"
|
||||
list.Version.Protocol = ProtocolVersion
|
||||
list.Players.Max = MaxPlayer
|
||||
list.Players.Online = 123
|
||||
list.Players.Sample = []player{} // must init. can't be nil
|
||||
list.Description = chat.Message{Text: "Powered by go-mc", Color: "blue"}
|
||||
|
||||
data, err := json.Marshal(list)
|
||||
if err != nil {
|
||||
log.Panic("Marshal JSON for status checking fail")
|
||||
}
|
||||
return string(data)
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
// sniffRegistryCodec is an example that acts as a client,
|
||||
// connects to the server and saves its RegistryCodec to a .nbt file.
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
//"github.com/mattn/go-colorable"
|
||||
|
||||
"github.com/Tnze/go-mc/bot"
|
||||
"github.com/Tnze/go-mc/bot/basic"
|
||||
_ "github.com/Tnze/go-mc/data/lang/zh-cn"
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
)
|
||||
|
||||
var (
|
||||
address = flag.String("address", "127.0.0.1", "The server address")
|
||||
client *bot.Client
|
||||
player *basic.Player
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
// log.SetOutput(colorable.NewColorableStdout())
|
||||
client = bot.NewClient()
|
||||
client.Auth.Name = "Daze"
|
||||
player = basic.NewPlayer(client, basic.DefaultSettings, basic.EventsListener{})
|
||||
|
||||
// To receive the raw NBT data, create a new packet handler
|
||||
// instead of just reading player.RegistryCodec in GameStart event.
|
||||
client.Events.AddListener(bot.PacketHandler{
|
||||
ID: packetid.ClientboundLogin,
|
||||
Priority: 50,
|
||||
F: onLogin,
|
||||
})
|
||||
|
||||
// Login
|
||||
err := client.JoinServer(*address)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Login success")
|
||||
|
||||
// JoinGame
|
||||
for {
|
||||
if err = client.HandleGame(); err == nil {
|
||||
panic("HandleGame never return nil")
|
||||
}
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func onLogin(p pk.Packet) error {
|
||||
var DimensionNames []pk.Identifier
|
||||
var RegistryCodec nbt.RawMessage
|
||||
err := p.Scan(
|
||||
new(pk.Int), // Entity ID
|
||||
new(pk.Boolean), // Is hardcore
|
||||
new(pk.Byte), // Gamemode
|
||||
new(pk.Byte), // Previous Gamemode
|
||||
pk.Array(&DimensionNames), // Dimension Names
|
||||
pk.NBT(&RegistryCodec), // Registry Codec (Only care about this)
|
||||
// ...Ignored
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = saveToFile("RegistryCodec.nbt", RegistryCodec)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Print("Successfully written RegistryCodec.nbt")
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveToFile(filename string, value any) (err error) {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer func(f *os.File) {
|
||||
if err2 := f.Close(); err == nil {
|
||||
err = err2
|
||||
}
|
||||
}(f)
|
||||
return nbt.NewEncoder(f).Encode(value, "")
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
// This example used to act as a launcher, log in and obtain the access token.
|
||||
// The Yggdrasil Authentication is no longer available. This example doesn't work now.
|
||||
//
|
||||
// For now, you should use Microsoft Authentication. The description and example code can be found here:
|
||||
// https://wiki.vg/Microsoft_Authentication_Scheme
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/Tnze/go-mc/yggdrasil"
|
||||
)
|
||||
|
||||
var (
|
||||
user = flag.String("user", "", "Can be an email address or player name for unmigrated accounts")
|
||||
pswd = flag.String("password", "", "Your password")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
resp, err := yggdrasil.Authenticate(*user, *pswd)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
id, name := resp.SelectedProfile()
|
||||
fmt.Println("user:", name)
|
||||
fmt.Println("uuid:", id)
|
||||
fmt.Println("astk:", resp.AccessToken())
|
||||
}
|
Reference in New Issue
Block a user