Part of refactor

This commit is contained in:
Tnze
2021-02-27 20:20:17 +08:00
parent 918fffed1f
commit fb1d3a3506
6 changed files with 57 additions and 50 deletions

View File

@ -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在未来版本中可能会变动

View File

@ -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

View File

@ -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",
}

View File

@ -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
}

View File

@ -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)
}
}

10
offline/uuid_test.go Normal file
View File

@ -0,0 +1,10 @@
package offline
import "fmt"
func ExampleNameToUUID() {
fmt.Println(NameToUUID("Tnze"))
// output:
// c7b9eece-2f2e-325c-8da8-6fc8f3d0edb0
}