Skip to content

Commit 19a014c

Browse files
committed
fix(network): fix AvailableCommands packet for mods
Fixes: #322 Co-authored-by: @uberswe
1 parent 6c5b676 commit 19a014c

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

pkg/edition/java/proto/packet/brigadier/registry.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (r *argPropReg) Encode(wr io.Writer, argType brigodier.ArgumentType, protoc
6161
if err != nil {
6262
return err
6363
}
64-
return util.WriteBytes(wr, property.Data)
64+
return util.WriteRawBytes(wr, property.Data)
6565
default:
6666
codec, ok := r.byType[argType.String()]
6767
id, ok2 := r.typeToID[argType.String()]

pkg/edition/java/proto/util/reader.go

+33-24
Original file line numberDiff line numberDiff line change
@@ -97,47 +97,56 @@ func ReadStringWithoutLen(rd io.Reader) (string, error) {
9797
return string(b), err
9898
}
9999

100-
func ReadVarInt(r io.Reader) (result int, err error) {
100+
func ReadVarInt(r io.Reader) (int, error) {
101101
if br, ok := r.(io.ByteReader); ok {
102-
var n uint32
103-
for i := 0; ; i++ {
104-
sec, err := br.ReadByte()
102+
var (
103+
result int32
104+
numRead int
105+
)
106+
for {
107+
b, err := br.ReadByte()
105108
if err != nil {
106109
return 0, err
107110
}
108111

109-
n |= uint32(sec&0x7F) << uint32(7*i)
112+
result |= int32(b&0x7F) << (7 * numRead)
110113

111-
if i >= 5 {
114+
numRead++
115+
if numRead > 5 {
112116
return 0, errors.New("decode: VarInt is too big")
113-
} else if sec&0x80 == 0 {
117+
}
118+
119+
// MSB clear → last byte
120+
if (b & 0x80) == 0 {
114121
break
115122
}
116123
}
117-
return int(n), nil
124+
return int(result), nil
118125
}
119126

120-
var bytes byte = 0
121-
var b byte
122-
var uresult uint32 = 0
127+
const maxBytes = 5
128+
var (
129+
result int32
130+
numRead int
131+
buf [1]byte
132+
)
123133
for {
124-
b, err = ReadUint8(r)
125-
if err != nil {
126-
return
134+
if _, err := io.ReadFull(r, buf[:]); err != nil {
135+
return 0, err
127136
}
128-
uresult |= uint32(b&0x7F) << uint32(bytes*7)
129-
bytes++
130-
if bytes > 5 {
131-
err = errors.New("decode: VarInt is too big")
132-
return
137+
b := buf[0]
138+
139+
result |= int32(b&0x7F) << (7 * numRead)
140+
141+
numRead++
142+
if numRead > maxBytes {
143+
return 0, errors.New("decode: VarInt is too big")
133144
}
134-
if (b & 0x80) == 0x80 {
135-
continue
145+
if (b & 0x80) == 0 {
146+
break
136147
}
137-
break
138148
}
139-
result = int(int32(uresult))
140-
return
149+
return int(result), nil
141150
}
142151

143152
// ReadVarIntArray reads a VarInt array from the reader.

0 commit comments

Comments
 (0)