forked from Tnze/go-mc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.go
89 lines (76 loc) · 1.99 KB
/
inventory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Package ptypes implements encoding and decoding for high-level packets.
package ptypes
import (
"bytes"
"errors"
"github.com/Tnze/go-mc/bot/world/entity"
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data"
"github.com/Tnze/go-mc/nbt"
pk "github.com/Tnze/go-mc/net/packet"
)
// SetSlot is a clientbound packet which configures an inventory slot.
// A window ID of -1 represents the cursor, and a window ID of 0 represents
// the players inventory.
type SetSlot struct {
WindowID pk.Byte
Slot pk.Short
SlotData entity.Slot
}
func (p *SetSlot) Decode(pkt pk.Packet) error {
if err := pkt.Scan(&p.WindowID, &p.Slot, &p.SlotData); err != nil && !errors.Is(err, nbt.ErrEND) {
return err
}
return nil
}
// WindowItems is a clientbound packet describing the contents of multiple
// inventory slots in a window/inventory.
type WindowItems struct {
WindowID pk.Byte
Slots []entity.Slot
}
func (p *WindowItems) Decode(pkt pk.Packet) error {
r := bytes.NewReader(pkt.Data)
if err := p.WindowID.Decode(r); err != nil {
return err
}
var count pk.Short
if err := count.Decode(r); err != nil {
return err
}
p.Slots = make([]entity.Slot, int(count))
for i := 0; i < int(count); i++ {
if err := p.Slots[i].Decode(r); err != nil && !errors.Is(err, nbt.ErrEND) {
return err
}
}
return nil
}
// OpenWindow is a clientbound packet which opens an inventory.
type OpenWindow struct {
WindowID pk.VarInt
WindowType pk.VarInt
Title chat.Message
}
func (p *OpenWindow) Decode(pkt pk.Packet) error {
if err := pkt.Scan(&p.WindowID, &p.WindowType, &p.Title); err != nil && !errors.Is(err, nbt.ErrEND) {
return err
}
return nil
}
type ConfirmTransaction struct {
WindowID pk.Byte
ActionID pk.Short
Accepted pk.Boolean
}
func (p *ConfirmTransaction) Decode(pkt pk.Packet) error {
return pkt.Scan(&p.WindowID, &p.ActionID, &p.Accepted)
}
func (p ConfirmTransaction) Encode() pk.Packet {
return pk.Marshal(
data.TransactionServerbound,
p.WindowID,
p.ActionID,
p.Accepted,
)
}