Skip to content

Commit 4592ec4

Browse files
fix: SoundEntityPacket panic and protocol desync (minekube#605)
* fix panic and protocol desync in SoundEntityPacket * Refactor SoundEntityPacket decoding logic to simplify fixed range handling --------- Co-authored-by: robinbraemer <robin.braemer@web.de>
1 parent 11882b0 commit 4592ec4

1 file changed

Lines changed: 37 additions & 36 deletions

File tree

pkg/edition/java/proto/packet/sound.go

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,21 @@ func (s *SoundEntityPacket) Encode(c *proto.PacketContext, wr io.Writer) error {
9494

9595
// Write sound ID (hardcoded to 0 for named sounds)
9696
w.VarInt(s.SoundID)
97-
w.MinimalKey(s.SoundName)
9897

99-
if w.Bool(s.FixedRange != nil) {
100-
w.Float32(*s.FixedRange)
98+
if s.SoundID == 0 {
99+
w.MinimalKey(s.SoundName)
100+
101+
hasFixedRange := s.FixedRange != nil
102+
w.Bool(hasFixedRange)
103+
if hasFixedRange {
104+
w.Float32(*s.FixedRange)
105+
}
101106
}
102107

103-
if err := writeSoundSource(wr, c.Protocol, s.SoundSource); err != nil {
104-
return err
108+
if c.Protocol.Lower(version.Minecraft_1_21_5) && s.SoundSource == SoundSourceUI {
109+
return fmt.Errorf("UI sound-source is only supported in 1.21.5+")
105110
}
111+
w.VarInt(int(s.SoundSource))
106112

107113
w.VarInt(s.EntityID)
108114
w.Float32(s.Volume)
@@ -121,17 +127,28 @@ func (s *SoundEntityPacket) Decode(c *proto.PacketContext, rd io.Reader) (err er
121127
pr := util.PanicReader(rd)
122128

123129
pr.VarInt(&s.SoundID)
124-
pr.MinimalKey(&s.SoundName)
125130

126-
if pr.Ok() {
127-
var fixedRange float32
128-
pr.Float32(&fixedRange)
129-
s.FixedRange = &fixedRange
131+
if s.SoundID == 0 {
132+
pr.MinimalKey(&s.SoundName)
133+
134+
if pr.Ok() {
135+
var fixedRange float32
136+
pr.Float32(&fixedRange)
137+
s.FixedRange = &fixedRange
138+
} else {
139+
s.FixedRange = nil
140+
}
141+
} else {
142+
s.SoundName = nil
143+
s.FixedRange = nil
130144
}
131145

132-
s.SoundSource, err = readSoundSource(rd, c.Protocol)
133-
if err != nil {
134-
return err
146+
var sourceOrdinal int
147+
pr.VarInt(&sourceOrdinal)
148+
s.SoundSource = SoundSource(sourceOrdinal)
149+
150+
if c.Protocol.Lower(version.Minecraft_1_21_5) && s.SoundSource == SoundSourceUI {
151+
return fmt.Errorf("UI sound-source is only supported in 1.21.5+")
135152
}
136153

137154
pr.VarInt(&s.EntityID)
@@ -186,11 +203,14 @@ func (s *StopSoundPacket) Decode(c *proto.PacketContext, rd io.Reader) error {
186203
pr.Byte(&flags)
187204

188205
if flags&0x01 != 0 {
189-
source, err := readSoundSource(rd, c.Protocol)
190-
if err != nil {
191-
return err
206+
var sourceOrdinal int
207+
pr.VarInt(&sourceOrdinal)
208+
209+
if c.Protocol.Lower(version.Minecraft_1_21_5) && sourceOrdinal == int(SoundSourceUI) {
210+
return fmt.Errorf("UI sound-source is only supported in 1.21.5+")
192211
}
193-
s.Source = &source
212+
src := SoundSource(sourceOrdinal)
213+
s.Source = &src
194214
}
195215

196216
if flags&0x02 != 0 {
@@ -201,22 +221,3 @@ func (s *StopSoundPacket) Decode(c *proto.PacketContext, rd io.Reader) error {
201221

202222
return nil
203223
}
204-
205-
func writeSoundSource(wr io.Writer, protocol proto.Protocol, s SoundSource) error {
206-
// UI sound source is only supported in 1.21.5+
207-
if protocol.Lower(version.Minecraft_1_21_5) && s == SoundSourceUI {
208-
return fmt.Errorf("UI sound-source is only supported in 1.21.5+")
209-
}
210-
return util.WriteVarInt(wr, int(s))
211-
}
212-
213-
func readSoundSource(rd io.Reader, protocol proto.Protocol) (SoundSource, error) {
214-
ordinal, err := util.ReadVarInt(rd)
215-
if err != nil {
216-
return 0, err
217-
}
218-
if protocol.Lower(version.Minecraft_1_21_5) && ordinal == int(SoundSourceUI) {
219-
return 0, fmt.Errorf("UI sound-source is only supported in 1.21.5+")
220-
}
221-
return SoundSource(ordinal), nil
222-
}

0 commit comments

Comments
 (0)