Handle screen open/send content
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
)
|
||||
|
||||
@ -47,8 +48,8 @@ func (p *Player) handleJoinGamePacket(packet pk.Packet) error {
|
||||
(*pk.Byte)(&p.PrevGamemode),
|
||||
&WorldCount,
|
||||
pk.Ary{Len: &WorldCount, Ary: &WorldNames},
|
||||
pk.NBT(new(interface{})),
|
||||
pk.NBT(new(interface{})),
|
||||
pk.NBT(new(nbt.RawMessage)),
|
||||
pk.NBT(new(nbt.RawMessage)),
|
||||
(*pk.Identifier)(&p.WorldName),
|
||||
(*pk.Long)(&p.HashedSeed),
|
||||
(*pk.VarInt)(&p.MaxPlayers),
|
||||
|
@ -1,8 +1,16 @@
|
||||
package screen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Tnze/go-mc/data/item"
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
)
|
||||
|
||||
type Inventory struct {
|
||||
}
|
||||
|
||||
func (inv Inventory) SetSlot(i int, id int32, count byte, NBT interface{}) {
|
||||
panic("implement me")
|
||||
func (inv Inventory) SetSlot(i int, id int32, count byte, NBT nbt.RawMessage) {
|
||||
// TODO: accept inv data
|
||||
fmt.Printf("Inventory[%d] = minecraft:%v * %d\n", i, item.ByID[item.ID(id)].Name, count)
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"github.com/Tnze/go-mc/bot"
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
)
|
||||
|
||||
@ -18,6 +19,7 @@ func NewManager(c *bot.Client) *Manager {
|
||||
m.Screens[0] = &Inventory{}
|
||||
c.Events.AddListener(
|
||||
bot.PacketHandler{Priority: 64, ID: packetid.OpenWindow, F: m.onOpenScreen},
|
||||
bot.PacketHandler{Priority: 64, ID: packetid.WindowItems, F: m.onSetContentPacket},
|
||||
)
|
||||
return m
|
||||
}
|
||||
@ -60,7 +62,7 @@ type slot struct {
|
||||
present pk.Boolean
|
||||
id pk.VarInt
|
||||
count pk.Byte
|
||||
nbt interface{}
|
||||
nbt nbt.RawMessage
|
||||
}
|
||||
|
||||
func (s *slot) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
@ -74,7 +76,7 @@ func (s *slot) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
}
|
||||
|
||||
type Container interface {
|
||||
SetSlot(i int, id int32, count byte, NBT interface{})
|
||||
SetSlot(i int, id int32, count byte, NBT nbt.RawMessage)
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
|
@ -1,5 +1,4 @@
|
||||
// Package inv maps window types to inventory slot information.
|
||||
package inv
|
||||
package screen
|
||||
|
||||
type Info struct {
|
||||
Name string
|
@ -1,7 +1,5 @@
|
||||
// Daze could join an offline-mode server as client.
|
||||
// Just standing there and do nothing. Automatically reborn after five seconds of death.
|
||||
//
|
||||
// BUG(Tnze): Kick by Disconnect: Time Out
|
||||
package main
|
||||
|
||||
import (
|
||||
@ -14,6 +12,7 @@ import (
|
||||
|
||||
"github.com/Tnze/go-mc/bot"
|
||||
"github.com/Tnze/go-mc/bot/basic"
|
||||
"github.com/Tnze/go-mc/bot/screen"
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
_ "github.com/Tnze/go-mc/data/lang/zh-cn"
|
||||
)
|
||||
@ -33,6 +32,7 @@ func main() {
|
||||
Disconnect: onDisconnect,
|
||||
Death: onDeath,
|
||||
}.Attach(client)
|
||||
_ = screen.NewManager(client)
|
||||
|
||||
//Login
|
||||
err := client.JoinServer(*address)
|
||||
|
@ -441,20 +441,23 @@ type nbtField struct {
|
||||
FieldName string
|
||||
}
|
||||
|
||||
// Encode a nbtField
|
||||
func (n nbtField) WriteTo(w io.Writer) (int64, error) {
|
||||
var buf bytes.Buffer
|
||||
if err := nbt.NewEncoder(&buf).Encode(n.V, n.FieldName); err != nil {
|
||||
if n.V == nil {
|
||||
buf.WriteByte(nbt.TagEnd)
|
||||
} else if err := nbt.NewEncoder(&buf).Encode(n.V, n.FieldName); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return buf.WriteTo(w)
|
||||
}
|
||||
|
||||
// Decode a nbtField
|
||||
func (n nbtField) ReadFrom(r io.Reader) (int64, error) {
|
||||
// LimitReader is used to count reader length
|
||||
lr := &io.LimitedReader{R: r, N: math.MaxInt64}
|
||||
err := nbt.NewDecoder(lr).Decode(n.V)
|
||||
if err != nil && errors.Is(err, nbt.ErrEND) {
|
||||
err = nil
|
||||
}
|
||||
return math.MaxInt64 - lr.N, err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user