Update /bot for 1.18

This commit is contained in:
Tnze
2021-12-11 02:40:41 +08:00
parent a70aaa81d6
commit 97b706f6eb
10 changed files with 57 additions and 30 deletions

View File

@ -18,9 +18,9 @@ type Player struct {
func NewPlayer(c *bot.Client, settings Settings) *Player { func NewPlayer(c *bot.Client, settings Settings) *Player {
b := &Player{c: c, Settings: settings} b := &Player{c: c, Settings: settings}
c.Events.AddListener( c.Events.AddListener(
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundLogin, F: b.handleJoinGamePacket}, bot.PacketHandler{Priority: 0, ID: packetid.ClientboundLogin, F: b.handleLoginPacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundKeepAlive, F: b.handleKeepAlivePacket}, bot.PacketHandler{Priority: 0, ID: packetid.ClientboundKeepAlive, F: b.handleKeepAlivePacket},
bot.PacketHandler{Priority: 0, ID: packetid.ClientboundPlayerPosition, F: b.handlePlayerPositionAndLook}, bot.PacketHandler{Priority: 0, ID: packetid.ClientboundPlayerPosition, F: b.handlePlayerPosition},
) )
return b return b
} }

View File

@ -39,7 +39,7 @@ type ServInfo struct {
Brand string Brand string
} }
func (p *Player) handleJoinGamePacket(packet pk.Packet) error { func (p *Player) handleLoginPacket(packet pk.Packet) error {
var WorldCount pk.VarInt var WorldCount pk.VarInt
var WorldNames = make([]pk.Identifier, 0) var WorldNames = make([]pk.Identifier, 0)
err := packet.Scan( err := packet.Scan(
@ -89,7 +89,8 @@ func (p *Player) handleJoinGamePacket(packet pk.Packet) error {
pk.Boolean(p.Settings.ChatColors), pk.Boolean(p.Settings.ChatColors),
pk.UnsignedByte(p.Settings.DisplayedSkinParts), pk.UnsignedByte(p.Settings.DisplayedSkinParts),
pk.VarInt(p.Settings.MainHand), pk.VarInt(p.Settings.MainHand),
pk.Boolean(p.Settings.DisableTextFiltering), pk.Boolean(p.Settings.EnableTextFiltering),
pk.Boolean(p.Settings.AllowListing),
)) ))
if err != nil { if err != nil {
return Error{err} return Error{err}

View File

@ -21,7 +21,7 @@ func (p Player) handleKeepAlivePacket(packet pk.Packet) error {
return nil return nil
} }
func (p *Player) handlePlayerPositionAndLook(packet pk.Packet) error { func (p *Player) handlePlayerPosition(packet pk.Packet) error {
var ( var (
X, Y, Z pk.Double X, Y, Z pk.Double
Yaw, Pitch pk.Float Yaw, Pitch pk.Float
@ -48,7 +48,6 @@ func (p *Player) handlePlayerPositionAndLook(packet pk.Packet) error {
packetid.ServerboundMoveVehicle, packetid.ServerboundMoveVehicle,
X, Y-1.62, Z, X, Y-1.62, Z,
Yaw, Pitch, Yaw, Pitch,
pk.Boolean(true),
)) ))
if err != nil { if err != nil {
return Error{err} return Error{err}

View File

@ -9,17 +9,17 @@ type Settings struct {
DisplayedSkinParts uint8 //皮肤显示 DisplayedSkinParts uint8 //皮肤显示
MainHand int //主手 MainHand int //主手
// Disables filtering of text on signs and written book titles. // Enables filtering of text on signs and written book titles.
// Currently always true in vanilla client (i.e. the filtering is disabled) // Currently, always false (i.e. the filtering is disabled)
DisableTextFiltering bool EnableTextFiltering bool
AllowListing bool
Brand string // The brand string presented to the server. // The brand string presented to the server.
Brand string
} }
/* // Used by Settings.DisplayedSkinParts.
Used by Settings.DisplayedSkinParts. // For each bit set if shows match part.
For each bits set if shows match part.
*/
const ( const (
_ = 1 << iota _ = 1 << iota
Jacket Jacket

View File

@ -28,7 +28,7 @@ func PingAndList(addr string) ([]byte, time.Duration, error) {
return pingAndList(addr, conn) return pingAndList(addr, conn)
} }
// PingAndListTimeout PingAndLIstTimeout is the version of PingAndList with max request time. // PingAndListTimeout is the version of PingAndList with max request time.
func PingAndListTimeout(addr string, timeout time.Duration) ([]byte, time.Duration, error) { func PingAndListTimeout(addr string, timeout time.Duration) ([]byte, time.Duration, error) {
deadLine := time.Now().Add(timeout) deadLine := time.Now().Add(timeout)

View File

@ -12,14 +12,19 @@ import (
) )
type Manager struct { type Manager struct {
c *bot.Client
Screens map[int]Container Screens map[int]Container
Inventory Inventory Inventory Inventory
Cursor Slot Cursor Slot
events EventsListener events EventsListener
// The last received State ID from server
stateID int32
} }
func NewManager(c *bot.Client, e EventsListener) *Manager { func NewManager(c *bot.Client, e EventsListener) *Manager {
m := &Manager{ m := &Manager{
c: c,
Screens: make(map[int]Container), Screens: make(map[int]Container),
events: e, events: e,
} }
@ -33,6 +38,18 @@ func NewManager(c *bot.Client, e EventsListener) *Manager {
return m return m
} }
//func (m *Manager) ContainerClick(id int, slot int16, button byte, mode int32) error {
// return m.c.Conn.WritePacket(pk.Marshal(
// packetid.ServerboundContainerClick,
// pk.UnsignedByte(id),
// pk.VarInt(m.stateID),
// pk.Short(slot),
// pk.Byte(button),
// pk.VarInt(mode),
// pk.VarInt()
// ))
//}
func (m *Manager) onOpenScreen(p pk.Packet) error { func (m *Manager) onOpenScreen(p pk.Packet) error {
var ( var (
ContainerID pk.VarInt ContainerID pk.VarInt
@ -72,6 +89,7 @@ func (m *Manager) onSetContentPacket(p pk.Packet) error {
); err != nil { ); err != nil {
return Error{err} return Error{err}
} }
m.stateID = int32(StateID)
// copy the slot data to container // copy the slot data to container
container, ok := m.Screens[int(ContainerID)] container, ok := m.Screens[int(ContainerID)]
if !ok { if !ok {
@ -113,19 +131,21 @@ func (m *Manager) onCloseScreen(p pk.Packet) error {
func (m *Manager) onSetSlot(p pk.Packet) (err error) { func (m *Manager) onSetSlot(p pk.Packet) (err error) {
var ( var (
ContainerID pk.Byte ContainerID pk.Byte
StateID pk.VarInt
SlotID pk.Short SlotID pk.Short
ItemStack Slot SlotData Slot
) )
if err := p.Scan(&ContainerID, &SlotID, &ItemStack); err != nil { if err := p.Scan(&ContainerID, &StateID, &SlotID, &SlotData); err != nil {
return Error{err} return Error{err}
} }
m.stateID = int32(StateID)
if ContainerID == -1 && SlotID == -1 { if ContainerID == -1 && SlotID == -1 {
m.Cursor = ItemStack m.Cursor = SlotData
} else if ContainerID == -2 { } else if ContainerID == -2 {
err = m.Inventory.onSetSlot(int(SlotID), ItemStack) err = m.Inventory.onSetSlot(int(SlotID), SlotData)
} else if c, ok := m.Screens[int(ContainerID)]; ok { } else if c, ok := m.Screens[int(ContainerID)]; ok {
err = c.onSetSlot(int(SlotID), ItemStack) err = c.onSetSlot(int(SlotID), SlotData)
} }
if m.events.SetSlot != nil { if m.events.SetSlot != nil {

View File

@ -13,6 +13,10 @@ type Item struct {
StackSize uint StackSize uint
} }
func (i Item) String() string {
return i.DisplayName
}
var ( var (
Stone = Item{ Stone = Item{
ID: 1, ID: 1,

View File

@ -1,17 +1,17 @@
package main package main
import ( import (
"github.com/Tnze/go-mc/bot/basic"
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
"log" "log"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/Tnze/go-mc/bot" "github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/bot/basic"
"github.com/Tnze/go-mc/chat" "github.com/Tnze/go-mc/chat"
_ "github.com/Tnze/go-mc/data/lang/en-us" _ "github.com/Tnze/go-mc/data/lang/en-us"
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/mattn/go-colorable" "github.com/mattn/go-colorable"
) )

View File

@ -9,6 +9,7 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/mattn/go-colorable"
"github.com/Tnze/go-mc/bot" "github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/bot/basic" "github.com/Tnze/go-mc/bot/basic"
@ -25,6 +26,7 @@ var screenManager *screen.Manager
func main() { func main() {
flag.Parse() flag.Parse()
log.SetOutput(colorable.NewColorableStdout())
client = bot.NewClient() client = bot.NewClient()
client.Auth.Name = "Daze" client.Auth.Name = "Daze"
player = basic.NewPlayer(client, basic.DefaultSettings) player = basic.NewPlayer(client, basic.DefaultSettings)
@ -86,7 +88,7 @@ func onGameStart() error {
} }
func onChatMsg(c chat.Message, _ byte, _ uuid.UUID) error { func onChatMsg(c chat.Message, _ byte, _ uuid.UUID) error {
log.Println("Chat:", c.ClearString()) // output chat message without any format code (like color or bold) log.Println("Chat:", c) // output chat message without any format code (like color or bold)
return nil return nil
} }
@ -98,14 +100,12 @@ func onScreenSlotChange(id, index int) error {
} else { } else {
container, ok := screenManager.Screens[id] container, ok := screenManager.Screens[id]
if ok { if ok {
// Currently only inventory container is supported // Currently, only inventory container is supported
switch container.(type) { switch container.(type) {
case *screen.Inventory: case *screen.Inventory:
slot := container.(*screen.Inventory).Slots[index] slot := container.(*screen.Inventory).Slots[index]
itemInfo := item.ByID[item.ID(slot.ID)] itemInfo := item.ByID[item.ID(slot.ID)]
if slot.ID != 0 { log.Printf("Slot: Screen[%d].Slot[%d]: [%v] * %d | NBT: %v", id, index, itemInfo, slot.Count, slot.NBT)
log.Printf("Slot: Screen[%d].Slot[%d]: [%v] * %d | NBT: %v", id, index, itemInfo.DisplayName, slot.Count, slot.NBT)
}
} }
} }
} }

View File

@ -43,9 +43,12 @@ func (m *RawMessage) Decode(tagType byte, r DecoderReader) error {
// String convert the data into the SNBT(Stringified NBT) format. // String convert the data into the SNBT(Stringified NBT) format.
// The output is valid for using in in-game command. // The output is valid for using in in-game command.
// Expect two exceptions:
// - Empty string "" if there is only an TagEnd in the NBT (aka: []byte{0}).
// - "<Invalid: $Err>" if the content is not valid NBT data.
func (m RawMessage) String() string { func (m RawMessage) String() string {
if m.Type == TagEnd { if m.Type == TagEnd {
return "TagEnd" return ""
} }
var snbt StringifiedMessage var snbt StringifiedMessage
var sb strings.Builder var sb strings.Builder
@ -53,7 +56,7 @@ func (m RawMessage) String() string {
d := NewDecoder(r) d := NewDecoder(r)
err := snbt.encode(d, &sb, m.Type) err := snbt.encode(d, &sb, m.Type)
if err != nil { if err != nil {
return "Invalid" return "<Invalid: " + err.Error() + ">"
} }
return sb.String() return sb.String()
} }