diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 4cd04f2..9cf735f 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -22,7 +22,7 @@ jobs: - run: mkdir -p ./bin/tools - name: Build tools - run: go build -o ./bin/tools ./cmd/... + run: go build -o ./bin/tools ./examples/... - name: Upload tools uses: actions/upload-artifact@v1 diff --git a/.gitignore b/.gitignore index 8b0ec8b..07b3b11 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,6 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out -cmd/test/test.go +examples/test/test.go .idea/ .vscode/ \ No newline at end of file diff --git a/README.md b/README.md index e1ba300..3e37171 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,6 @@ bot: > 由于仍在开发中,部分API在未来版本中可能会变动 -Some examples are at `/cmd` folder. -有一些例子在cmd目录下 - > `1.13.2` version is at [gomcbot](https://github.com/Tnze/gomcbot). ## Getting start @@ -105,4 +102,4 @@ Originally it's all right to write a bot with only `go-mc/net` package, but cons 理论上讲,只用`go-mc/net`包实现一个bot是完全可行的,但是为了节省大家从头去理解MC握手、登录、加密等协议的过程,在`go-mc/bot`中我已经把这些都实现了,只不过它不是跨版本的。你可以直接使用,或者作为自己实现的参考。 -Now, go and have a look at the example! +Now, go and have a look at the examples! diff --git a/bot/mcbot.go b/bot/mcbot.go index cce5132..654cc54 100644 --- a/bot/mcbot.go +++ b/bot/mcbot.go @@ -1,7 +1,7 @@ // Package bot implements a simple Minecraft client that can join a server // or just ping it for getting information. // -// Runnable example could be found at cmd/ . +// Runnable example could be found at examples/ . package bot import ( diff --git a/cmd/autofish/autofish.go b/examples/autofish/autofish.go similarity index 76% rename from cmd/autofish/autofish.go rename to examples/autofish/autofish.go index 5908663..ffbc7b9 100644 --- a/cmd/autofish/autofish.go +++ b/examples/autofish/autofish.go @@ -1,6 +1,9 @@ package main import ( + "github.com/Tnze/go-mc/bot/basic" + "github.com/Tnze/go-mc/data/packetid" + pk "github.com/Tnze/go-mc/net/packet" "log" "time" @@ -15,13 +18,31 @@ import ( const timeout = 45 var ( - c *bot.Client + c *bot.Client + p *basic.Player + watch chan time.Time ) func main() { log.SetOutput(colorable.NewColorableStdout()) c = bot.NewClient() + p = basic.NewPlayer(c, basic.DefaultSettings) + + //Register event handlers + basic.EventsListener{ + GameStart: onGameStart, + ChatMsg: onChatMsg, + Disconnect: onDisconnect, + Death: onDeath, + }.Attach(c) + c.Events.AddListener(bot.PacketHandler{ + ID: packetid.NamedSoundEffect, + Priority: 0, + F: func(p pk.Packet) error { + return onSound() + }, + }) //Login err := c.JoinServer("127.0.0.1") @@ -30,13 +51,6 @@ func main() { } log.Println("Login success") - //Register event handlers - c.Events.GameStart = onGameStart - c.Events.ChatMsg = onChatMsg - c.Events.Disconnect = onDisconnect - c.Events.SoundPlay = onSound - c.Events.Die = onDeath - //JoinGame err = c.HandleGame() if err != nil { @@ -47,7 +61,7 @@ func main() { func onDeath() error { log.Println("Died and Respawned") // If we exclude Respawn(...) then the player won't press the "Respawn" button upon death - return c.Respawn() + return p.Respawn() } func onGameStart() error { diff --git a/cmd/daze/daze.go b/examples/daze/daze.go similarity index 100% rename from cmd/daze/daze.go rename to examples/daze/daze.go diff --git a/cmd/luncher/luncher.go b/examples/luncher/luncher.go similarity index 100% rename from cmd/luncher/luncher.go rename to examples/luncher/luncher.go diff --git a/cmd/mcadump/mcadump.go b/examples/mcadump/mcadump.go similarity index 100% rename from cmd/mcadump/mcadump.go rename to examples/mcadump/mcadump.go diff --git a/cmd/mcping/README.md b/examples/mcping/README.md similarity index 100% rename from cmd/mcping/README.md rename to examples/mcping/README.md diff --git a/cmd/mcping/mcping.go b/examples/mcping/mcping.go similarity index 96% rename from cmd/mcping/mcping.go rename to examples/mcping/mcping.go index 5ff98ad..e520d80 100644 --- a/cmd/mcping/mcping.go +++ b/examples/mcping/mcping.go @@ -1,4 +1,4 @@ -// Usage: go run cmd/ping/ping.go localhost +// Usage: go run examples/ping/ping.go localhost package main import ( diff --git a/cmd/simpleServer/main.go b/examples/simpleServer/main.go similarity index 100% rename from cmd/simpleServer/main.go rename to examples/simpleServer/main.go diff --git a/cmd/simpleServer/status.go b/examples/simpleServer/status.go similarity index 100% rename from cmd/simpleServer/status.go rename to examples/simpleServer/status.go