/
slot.go
92 lines (71 loc) · 1.54 KB
/
slot.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
90
91
92
package minecraft
import (
"bytes"
"compress/gzip"
"io"
"github.com/minero/minero/proto/nbt"
"github.com/minero/minero/util/must"
)
// Slot
// http://wiki.vg/Slot_Data
type Slot struct {
BlockId int16
Amount byte
Damage int16
Enchantments *nbt.Compound
}
func NewSlot() *Slot {
return &Slot{
BlockId: -1,
Amount: 1,
Damage: 0,
Enchantments: nil,
}
}
func (s *Slot) ReadFrom(r io.Reader) (n int64, err error) {
var rw must.ReadWriter
s.BlockId = rw.ReadInt16(r)
if s.BlockId == -1 {
return rw.Result()
}
s.Amount = byte(rw.ReadInt8(r))
s.Damage = rw.ReadInt16(r)
Length := rw.ReadInt16(r)
if Length == -1 {
return rw.Result()
}
var br bytes.Buffer
// Copy gzip'd NBT Compound
gs := rw.ReadByteArray(r, int(Length))
bn := bytes.NewBuffer(gs)
// Ungzip byte array
gr, err := gzip.NewReader(bn)
rw.Check(err)
rw.Must(io.Copy(&br, gr))
rw.Check(gr.Close())
// Read NBT Compound
s.Enchantments, err = nbt.Read(&br)
rw.Check(err)
return rw.Result()
}
func (s *Slot) WriteTo(w io.Writer) (n int64, err error) {
var rw must.ReadWriter
rw.WriteInt16(w, s.BlockId)
if s.BlockId == -1 {
return rw.Result()
}
rw.WriteInt8(w, int8(s.Amount))
rw.WriteInt16(w, s.Damage)
if s.Enchantments == nil {
rw.WriteInt16(w, -1)
return rw.Result()
}
var bn, bw bytes.Buffer
rw.Check(nbt.Write(&bn, s.Enchantments))
gw := gzip.NewWriter(&bw)
rw.Must(io.Copy(gw, &bn))
rw.Check(gw.Close())
rw.WriteInt16(w, int16(bw.Len()))
rw.WriteByteArray(w, bw.Bytes())
return rw.Result()
}