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