ClientboundSoundPacket 0x77

ClientboundSoundPacket: Minecraft 26.3 packet (0x77 Play clientbound). Wire name 'sound', fields: sound, source, x, y, z, volume, handled

Minecraft 26.3PlayClientboundnet.minecraft.network.protocol.game

Protocol slots (1)

IDdecStateDirectionRegistry
0x77119PLAYClientboundGamePacketTypes.CLIENTBOUND_SOUND: "sound"

Packet ids are positional: the id is the index of the addPacket call in the protocol builder (see ProtocolInfoBuilder.listPackets). Ids change between game versions.

Wire fields (8)

FieldType
soundHolder<SoundEvent>
sourceSoundSource
xdouble
ydouble
zdouble
volumefloat
pitchfloat
seedlong

Stream codec

static final StreamCodec<RegistryFriendlyByteBuf, ClientboundSoundPacket> STREAM_CODEC = Packet.codec( ClientboundSoundPacket::write, ClientboundSoundPacket::new );

Modern packets serialize through StreamCodecs instead of read/write methods.

Handling

Resolved on the ClientGamePacketListener packet-listener interface by void handleSoundEvent(ClientboundSoundPacket clientboundsoundpacket). The implementation in the game is ClientPacketListener.

Full Source

ClientboundSoundPacket.java108 lines · Minecraft 26.3 · verbatimSource page ↗
1
package net.minecraft.network.protocol.game;
2
3
import net.minecraft.core.Holder;
4
import net.minecraft.network.RegistryFriendlyByteBuf;
5
import net.minecraft.network.codec.StreamCodec;
6
import net.minecraft.network.protocol.Packet;
7
import net.minecraft.network.protocol.PacketType;
8
import net.minecraft.sounds.SoundEvent;
9
import net.minecraft.sounds.SoundSource;
10
11
public class ClientboundSoundPacket implements Packet<ClientGamePacketListener> {
12
	public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundSoundPacket> STREAM_CODEC = Packet.codec(
13
		ClientboundSoundPacket::write, ClientboundSoundPacket::new
14
	);
15
	public static final float LOCATION_ACCURACY = 8.0F;
16
	private final Holder<SoundEvent> sound;
17
	private final SoundSource source;
18
	private final int x;
19
	private final int y;
20
	private final int z;
21
	private final float volume;
22
	private final float pitch;
23
	private final long seed;
24
25
	public ClientboundSoundPacket(
26
		final Holder<SoundEvent> sound,
27
		final SoundSource source,
28
		final double x,
29
		final double y,
30
		final double z,
31
		final float volume,
32
		final float pitch,
33
		final long seed
34
	) {
35
		this.sound = sound;
36
		this.source = source;
37
		this.x = (int)(x * 8.0);
38
		this.y = (int)(y * 8.0);
39
		this.z = (int)(z * 8.0);
40
		this.volume = volume;
41
		this.pitch = pitch;
42
		this.seed = seed;
43
	}
44
45
	private ClientboundSoundPacket(final RegistryFriendlyByteBuf input) {
46
		this.sound = SoundEvent.STREAM_CODEC.decode(input);
47
		this.source = input.readEnum(SoundSource.class);
48
		this.x = input.readInt();
49
		this.y = input.readInt();
50
		this.z = input.readInt();
51
		this.volume = input.readFloat();
52
		this.pitch = input.readFloat();
53
		this.seed = input.readLong();
54
	}
55
56
	private void write(final RegistryFriendlyByteBuf output) {
57
		SoundEvent.STREAM_CODEC.encode(output, this.sound);
58
		output.writeEnum(this.source);
59
		output.writeInt(this.x);
60
		output.writeInt(this.y);
61
		output.writeInt(this.z);
62
		output.writeFloat(this.volume);
63
		output.writeFloat(this.pitch);
64
		output.writeLong(this.seed);
65
	}
66
67
	@Override
68
	public PacketType<ClientboundSoundPacket> type() {
69
		return GamePacketTypes.CLIENTBOUND_SOUND;
70
	}
71
72
	public void handle(final ClientGamePacketListener listener) {
73
		listener.handleSoundEvent(this);
74
	}
75
76
	public Holder<SoundEvent> getSound() {
77
		return this.sound;
78
	}
79
80
	public SoundSource getSource() {
81
		return this.source;
82
	}
83
84
	public double getX() {
85
		return this.x / 8.0F;
86
	}
87
88
	public double getY() {
89
		return this.y / 8.0F;
90
	}
91
92
	public double getZ() {
93
		return this.z / 8.0F;
94
	}
95
96
	public float getVolume() {
97
		return this.volume;
98
	}
99
100
	public float getPitch() {
101
		return this.pitch;
102
	}
103
104
	public long getSeed() {
105
		return this.seed;
106
	}
107
}
108