From fb1d3a35064de3d5688b92b1b10eacb64f91c9f1 Mon Sep 17 00:00:00 2001 From: Tnze Date: Sat, 27 Feb 2021 20:20:17 +0800 Subject: [PATCH] Part of refactor --- README.md | 30 ++++++++------------------ bot/basic/keepalive.go | 2 -- bot/basic/settings.go | 8 +++---- bot/example_test.go | 17 +++++---------- examples/autofish/autofish.go | 40 +++++++++++++++++++++++++---------- offline/uuid_test.go | 10 +++++++++ 6 files changed, 57 insertions(+), 50 deletions(-) create mode 100644 offline/uuid_test.go diff --git a/README.md b/README.md index 3e37171..9f8f8e2 100644 --- a/README.md +++ b/README.md @@ -5,31 +5,19 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/Tnze/go-mc)](https://goreportcard.com/report/github.com/Tnze/go-mc) [![Build Status](https://travis-ci.org/Tnze/go-mc.svg?branch=master)](https://travis-ci.org/Tnze/go-mc) +Require Go version: 1.16 + There's some library in Go support you to create your Minecraft client or server. -这是一些Golang库,用于帮助你编写自己的Minecraft客户端或服务器, -- [x] Chat -- [x] NBT +这是一些Golang库,用于帮助你编写自己的Minecraft客户端或服务器。 + +- [x] Chat Message (Support Json or old `§`) +- [x] NBT (Based on reflection) - [x] Yggdrasil - [x] Realms Server -- [x] RCON protocol -- [x] Saves decoding /encoding +- [x] RCON protocol (Server & Client) +- [x] Saves decoding & encoding - [x] Minecraft network protocol -- [x] Simple MC robot lib - -bot: -- [x] Swing arm -- [x] Get inventory -- [x] Pick item -- [x] Drop item -- [x] Swap item in hands -- [x] Use item -- [x] Use entity -- [x] Attack entity -- [x] Use/Place block -- [x] Mine block -- [x] Custom packets -- [ ] Record entities - +- [x] Robot player framework > 由于仍在开发中,部分API在未来版本中可能会变动 diff --git a/bot/basic/keepalive.go b/bot/basic/keepalive.go index 1e033fa..8a398b7 100644 --- a/bot/basic/keepalive.go +++ b/bot/basic/keepalive.go @@ -3,7 +3,6 @@ package basic import ( "github.com/Tnze/go-mc/data/packetid" pk "github.com/Tnze/go-mc/net/packet" - "log" ) func (p Player) handleKeepAlivePacket(packet pk.Packet) error { @@ -54,7 +53,6 @@ func (p *Player) handlePlayerPositionAndLook(packet pk.Packet) error { return Error{err} } p.isSpawn = true - log.Print("Position confirmed") } return nil diff --git a/bot/basic/settings.go b/bot/basic/settings.go index c14d261..8302c40 100644 --- a/bot/basic/settings.go +++ b/bot/basic/settings.go @@ -8,8 +8,8 @@ type Settings struct { ChatColors bool //聊天颜色 DisplayedSkinParts uint8 //皮肤显示 MainHand int //主手 - ReceiveMap bool //接收地图数据 - Brand string // The brand string presented to the server. + + Brand string // The brand string presented to the server. } /* @@ -33,6 +33,6 @@ var DefaultSettings = Settings{ ChatMode: 0, DisplayedSkinParts: Jacket | LeftSleeve | RightSleeve | LeftPantsLeg | RightPantsLeg | Hat, MainHand: 1, - ReceiveMap: true, - Brand: "vanilla", + + Brand: "vanilla", } diff --git a/bot/example_test.go b/bot/example_test.go index 3b28839..195854f 100644 --- a/bot/example_test.go +++ b/bot/example_test.go @@ -1,15 +1,15 @@ package bot import ( + "log" + "encoding/hex" - "fmt" "github.com/Tnze/go-mc/offline" "github.com/Tnze/go-mc/yggdrasil" - "log" ) func ExamplePingAndList() { - resp, delay, err := PingAndList("localhost", 25565) + resp, delay, err := PingAndList("localhost:25565") if err != nil { log.Fatalf("ping and list server fail: %v", err) } @@ -54,8 +54,8 @@ func ExampleClient_JoinServer_online() { panic(err) } - c.Auth.UUID, c.Name = auth.SelectedProfile() - c.AsTk = auth.AccessToken() + c.Auth.UUID, c.Auth.Name = auth.SelectedProfile() + c.Auth.AsTk = auth.AccessToken() //Connect server err = c.JoinServer("127.0.0.1") @@ -76,10 +76,3 @@ func ExampleClient_JoinServer_online() { log.Fatal(err) } } - -func ExampleOfflineUUID() { - fmt.Println(offline.NameToUUID("Tnze")) - - // output: - // c7b9eece-2f2e-325c-8da8-6fc8f3d0edb0 -} diff --git a/examples/autofish/autofish.go b/examples/autofish/autofish.go index ffbc7b9..fef43f9 100644 --- a/examples/autofish/autofish.go +++ b/examples/autofish/autofish.go @@ -36,13 +36,7 @@ func main() { Disconnect: onDisconnect, Death: onDeath, }.Attach(c) - c.Events.AddListener(bot.PacketHandler{ - ID: packetid.NamedSoundEffect, - Priority: 0, - F: func(p pk.Packet) error { - return onSound() - }, - }) + c.Events.AddListener(soundListener) //Login err := c.JoinServer("127.0.0.1") @@ -70,18 +64,42 @@ func onGameStart() error { watch = make(chan time.Time) go watchDog() - return c.UseItem(0) + return UseItem(0) +} + +var soundListener = bot.PacketHandler{ + ID: packetid.NamedSoundEffect, + Priority: 0, + F: func(p pk.Packet) error { + var ( + SoundName pk.Identifier + SoundCategory pk.VarInt + X, Y, Z pk.Int + Volume, Pitch pk.Float + ) + if err := p.Scan(&SoundName, &SoundCategory, &X, &Y, &Z, &Volume, &Pitch); err != nil { + return err + } + return onSound(string(SoundName), int(SoundCategory), float64(X)/8, float64(Y)/8, float64(Z)/8, float32(Volume), float32(Pitch)) + }, +} + +func UseItem(hand int32) error { + return c.Conn.WritePacket(pk.Marshal( + packetid.UseItem, + pk.VarInt(hand), + )) } //goland:noinspection SpellCheckingInspection func onSound(name string, category int, x, y, z float64, volume, pitch float32) error { if name == "entity.fishing_bobber.splash" { - if err := c.UseItem(0); err != nil { //retrieve + if err := UseItem(0); err != nil { //retrieve return err } log.Println("gra~") time.Sleep(time.Millisecond * 300) - if err := c.UseItem(0); err != nil { //throw + if err := UseItem(0); err != nil { //throw return err } watch <- time.Now() @@ -106,7 +124,7 @@ func watchDog() { case <-watch: case <-to.C: log.Println("rethrow") - if err := c.UseItem(0); err != nil { + if err := UseItem(0); err != nil { panic(err) } } diff --git a/offline/uuid_test.go b/offline/uuid_test.go new file mode 100644 index 0000000..0b0f2c5 --- /dev/null +++ b/offline/uuid_test.go @@ -0,0 +1,10 @@ +package offline + +import "fmt" + +func ExampleNameToUUID() { + fmt.Println(NameToUUID("Tnze")) + + // output: + // c7b9eece-2f2e-325c-8da8-6fc8f3d0edb0 +}