Skip to content

Commit 00d4dc6

Browse files
committed
feat: Add primitive support for sound api
1 parent 4eb02c8 commit 00d4dc6

File tree

8 files changed

+260
-4
lines changed

8 files changed

+260
-4
lines changed

api/src/main/java/com/velocitypowered/api/proxy/Player.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,14 @@ default void clearHeaderAndFooter() {
383383
/**
384384
* {@inheritDoc}
385385
*
386-
* <b>This method is not currently implemented in Velocity
387-
* and will not perform any actions.</b>
386+
* <p>Note: This method is currently only implemented for players from version 1.19.3 and above.
387+
* <br>A {@link ServerConnection} is required for this to function, so a {@link #getCurrentServer()}.isPresent() check should be made beforehand.
388+
*
389+
* @param sound the sound to play
390+
* @throws IllegalArgumentException if the player is from a version lower than 1.19.3
391+
* @throws IllegalStateException if no server is connected
392+
* @since 3.3.0
393+
* @sinceMinecraft 1.19.3
388394
*/
389395
@Override
390396
default void playSound(@NotNull Sound sound) {
@@ -413,8 +419,12 @@ default void playSound(@NotNull Sound sound, Sound.Emitter emitter) {
413419
/**
414420
* {@inheritDoc}
415421
*
416-
* <b>This method is not currently implemented in Velocity
417-
* and will not perform any actions.</b>
422+
* <p>Note: This method is currently only implemented for players from version 1.19.3 and above.
423+
*
424+
* @param stop the sound and/or a sound source, to stop
425+
* @throws IllegalArgumentException if the player is from a version lower than 1.19.3
426+
* @since 3.3.0
427+
* @sinceMinecraft 1.19.3
418428
*/
419429
@Override
420430
default void stopSound(@NotNull SoundStop stop) {

proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftSessionHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.velocitypowered.proxy.protocol.packet.BundleDelimiterPacket;
2424
import com.velocitypowered.proxy.protocol.packet.ClientSettingsPacket;
2525
import com.velocitypowered.proxy.protocol.packet.ClientboundCookieRequestPacket;
26+
import com.velocitypowered.proxy.protocol.packet.ClientboundSoundEntityPacket;
27+
import com.velocitypowered.proxy.protocol.packet.ClientboundStopSoundPacket;
2628
import com.velocitypowered.proxy.protocol.packet.ClientboundStoreCookiePacket;
2729
import com.velocitypowered.proxy.protocol.packet.DisconnectPacket;
2830
import com.velocitypowered.proxy.protocol.packet.EncryptionRequestPacket;
@@ -364,4 +366,12 @@ default boolean handle(ClientboundCustomReportDetailsPacket packet) {
364366
default boolean handle(ClientboundServerLinksPacket packet) {
365367
return false;
366368
}
369+
370+
default boolean handle(ClientboundSoundEntityPacket packet) {
371+
return false;
372+
}
373+
374+
default boolean handle(ClientboundStopSoundPacket packet) {
375+
return false;
376+
}
367377
}

proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.util.Map;
5454
import java.util.Optional;
5555
import java.util.concurrent.CompletableFuture;
56+
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
5657
import org.checkerframework.checker.nullness.qual.Nullable;
5758
import org.jetbrains.annotations.NotNull;
5859

@@ -70,6 +71,7 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation,
7071
private boolean gracefulDisconnect = false;
7172
private BackendConnectionPhase connectionPhase = BackendConnectionPhases.UNKNOWN;
7273
private final Map<Long, Long> pendingPings = new HashMap<>();
74+
private @MonotonicNonNull Integer entityId;
7375

7476
/**
7577
* Initializes a new server connection.
@@ -324,6 +326,14 @@ public Map<Long, Long> getPendingPings() {
324326
return pendingPings;
325327
}
326328

329+
public Integer getEntityId() {
330+
return entityId;
331+
}
332+
333+
public void setEntityId(Integer entityId) {
334+
this.entityId = entityId;
335+
}
336+
327337
/**
328338
* Ensures that this server connection remains "active": the connection is established and not
329339
* closed, the player is still connected to the server, and the player still remains online.

proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@ public void handleBackendJoinGame(JoinGamePacket joinGame, VelocityServerConnect
565565
}
566566
}
567567

568+
destination.setEntityId(joinGame.getEntityId()); // used for sound api
569+
568570
// Remove previous boss bars. These don't get cleared when sending JoinGame, thus the need to
569571
// track them.
570572
for (UUID serverBossBar : serverBossBars) {

proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
import com.velocitypowered.proxy.protocol.packet.BundleDelimiterPacket;
7373
import com.velocitypowered.proxy.protocol.packet.ClientSettingsPacket;
7474
import com.velocitypowered.proxy.protocol.packet.ClientboundCookieRequestPacket;
75+
import com.velocitypowered.proxy.protocol.packet.ClientboundSoundEntityPacket;
76+
import com.velocitypowered.proxy.protocol.packet.ClientboundStopSoundPacket;
7577
import com.velocitypowered.proxy.protocol.packet.ClientboundStoreCookiePacket;
7678
import com.velocitypowered.proxy.protocol.packet.DisconnectPacket;
7779
import com.velocitypowered.proxy.protocol.packet.HeaderAndFooterPacket;
@@ -124,6 +126,8 @@
124126
import net.kyori.adventure.resource.ResourcePackInfoLike;
125127
import net.kyori.adventure.resource.ResourcePackRequest;
126128
import net.kyori.adventure.resource.ResourcePackRequestLike;
129+
import net.kyori.adventure.sound.Sound;
130+
import net.kyori.adventure.sound.SoundStop;
127131
import net.kyori.adventure.text.Component;
128132
import net.kyori.adventure.text.format.NamedTextColor;
129133
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
@@ -1005,6 +1009,32 @@ void setClientBrand(final @Nullable String clientBrand) {
10051009
this.clientBrand = clientBrand;
10061010
}
10071011

1012+
@Override
1013+
public void playSound(@NotNull Sound sound) {
1014+
Preconditions.checkNotNull(sound, "sound");
1015+
Preconditions.checkArgument(
1016+
getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19_3),
1017+
"Player version must be 1.19.3 to be able to interact with sounds");
1018+
if (connection.getState() != StateRegistry.PLAY) {
1019+
throw new IllegalStateException("Can only interact with sounds in PLAY protocol");
1020+
}
1021+
1022+
connection.write(new ClientboundSoundEntityPacket(sound, null, ensureAndGetCurrentServer().getEntityId()));
1023+
}
1024+
1025+
@Override
1026+
public void stopSound(@NotNull SoundStop stop) {
1027+
Preconditions.checkNotNull(stop, "stop");
1028+
Preconditions.checkArgument(
1029+
getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_19_3),
1030+
"Player version must be 1.19.3 to be able to interact with sounds");
1031+
if (connection.getState() != StateRegistry.PLAY) {
1032+
throw new IllegalStateException("Can only interact with sounds in PLAY protocol");
1033+
}
1034+
1035+
connection.write(new ClientboundStopSoundPacket(stop));
1036+
}
1037+
10081038
@Override
10091039
public void transferToHost(final InetSocketAddress address) {
10101040
Preconditions.checkNotNull(address);

proxy/src/main/java/com/velocitypowered/proxy/protocol/StateRegistry.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
import com.velocitypowered.proxy.protocol.packet.BundleDelimiterPacket;
5555
import com.velocitypowered.proxy.protocol.packet.ClientSettingsPacket;
5656
import com.velocitypowered.proxy.protocol.packet.ClientboundCookieRequestPacket;
57+
import com.velocitypowered.proxy.protocol.packet.ClientboundSoundEntityPacket;
58+
import com.velocitypowered.proxy.protocol.packet.ClientboundStopSoundPacket;
5759
import com.velocitypowered.proxy.protocol.packet.ClientboundStoreCookiePacket;
5860
import com.velocitypowered.proxy.protocol.packet.DisconnectPacket;
5961
import com.velocitypowered.proxy.protocol.packet.EncryptionRequestPacket;
@@ -399,6 +401,20 @@ public enum StateRegistry {
399401
clientbound.register(
400402
ClientboundCookieRequestPacket.class, ClientboundCookieRequestPacket::new,
401403
map(0x16, MINECRAFT_1_20_5, false));
404+
clientbound.register(
405+
ClientboundSoundEntityPacket.class, ClientboundSoundEntityPacket::new,
406+
map(0x5D, MINECRAFT_1_19_3, false),
407+
map(0x61, MINECRAFT_1_19_4, false),
408+
map(0x63, MINECRAFT_1_20_2, false),
409+
map(0x65, MINECRAFT_1_20_3, false),
410+
map(0x67, MINECRAFT_1_20_5, false));
411+
clientbound.register(
412+
ClientboundStopSoundPacket.class, ClientboundStopSoundPacket::new,
413+
map(0x5F, MINECRAFT_1_19_3, false),
414+
map(0x63, MINECRAFT_1_19_4, false),
415+
map(0x66, MINECRAFT_1_20_2, false),
416+
map(0x68, MINECRAFT_1_20_3, false),
417+
map(0x6A, MINECRAFT_1_20_5, false));
402418
clientbound.register(
403419
PluginMessagePacket.class,
404420
PluginMessagePacket::new,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 2024 Velocity Contributors
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.velocitypowered.proxy.protocol.packet;
19+
20+
import com.velocitypowered.api.network.ProtocolVersion;
21+
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
22+
import com.velocitypowered.proxy.protocol.MinecraftPacket;
23+
import com.velocitypowered.proxy.protocol.ProtocolUtils;
24+
import io.netty.buffer.ByteBuf;
25+
import net.kyori.adventure.sound.Sound;
26+
import org.jetbrains.annotations.Nullable;
27+
28+
import java.util.Random;
29+
30+
public class ClientboundSoundEntityPacket implements MinecraftPacket {
31+
32+
private static final Random SEEDS_RANDOM = new Random();
33+
34+
private Sound sound;
35+
private @Nullable Float fixedRange;
36+
private int entityId;
37+
38+
public ClientboundSoundEntityPacket() {}
39+
40+
public ClientboundSoundEntityPacket(Sound sound, @Nullable Float fixedRange, int entityId) {
41+
this.sound = sound;
42+
this.fixedRange = fixedRange;
43+
this.entityId = entityId;
44+
}
45+
46+
@Override
47+
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) {
48+
throw new UnsupportedOperationException("Decode is not implemented");
49+
}
50+
51+
@Override
52+
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) {
53+
ProtocolUtils.writeVarInt(buf, 0); // version-dependent hardcoded sound id
54+
55+
ProtocolUtils.writeString(buf, sound.name().asMinimalString()); // not using writeKey, as the client already defaults to the vanilla namespace
56+
57+
buf.writeBoolean(fixedRange != null);
58+
if (fixedRange != null)
59+
buf.writeFloat(fixedRange);
60+
61+
ProtocolUtils.writeVarInt(buf, sound.source().ordinal());
62+
63+
ProtocolUtils.writeVarInt(buf, entityId);
64+
65+
buf.writeFloat(sound.volume());
66+
67+
buf.writeFloat(sound.pitch());
68+
69+
buf.writeLong(sound.seed().orElse(SEEDS_RANDOM.nextLong()));
70+
}
71+
72+
@Override
73+
public boolean handle(MinecraftSessionHandler handler) {
74+
return handler.handle(this);
75+
}
76+
77+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (C) 2024 Velocity Contributors
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.velocitypowered.proxy.protocol.packet;
19+
20+
import com.velocitypowered.api.network.ProtocolVersion;
21+
import com.velocitypowered.proxy.connection.MinecraftSessionHandler;
22+
import com.velocitypowered.proxy.protocol.MinecraftPacket;
23+
import com.velocitypowered.proxy.protocol.ProtocolUtils;
24+
import io.netty.buffer.ByteBuf;
25+
import net.kyori.adventure.key.Key;
26+
import net.kyori.adventure.sound.Sound;
27+
import net.kyori.adventure.sound.SoundStop;
28+
29+
import javax.annotation.Nullable;
30+
31+
public class ClientboundStopSoundPacket implements MinecraftPacket {
32+
33+
private @Nullable Sound.Source source;
34+
private @Nullable Key soundName;
35+
36+
public ClientboundStopSoundPacket() {}
37+
38+
public ClientboundStopSoundPacket(SoundStop soundStop) {
39+
this(soundStop.source(), soundStop.sound());
40+
}
41+
42+
public ClientboundStopSoundPacket(@Nullable Sound.Source source, @Nullable Key soundName) {
43+
this.source = source;
44+
this.soundName = soundName;
45+
}
46+
47+
@Override
48+
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) {
49+
int flagsBitmask = buf.readByte();
50+
51+
if ((flagsBitmask & 1) != 0) {
52+
source = Sound.Source.values()[ProtocolUtils.readVarInt(buf)];
53+
} else {
54+
source = null;
55+
}
56+
57+
if ((flagsBitmask & 2) != 0) {
58+
soundName = ProtocolUtils.readKey(buf);
59+
} else {
60+
soundName = null;
61+
}
62+
}
63+
64+
@Override
65+
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) {
66+
int flagsBitmask = 0;
67+
if (source != null && soundName == null) {
68+
flagsBitmask |= 1;
69+
} else if (soundName != null && source == null) {
70+
flagsBitmask |= 2;
71+
} else if (source != null /*&& sound != null*/) {
72+
flagsBitmask |= 3;
73+
}
74+
75+
buf.writeByte(flagsBitmask);
76+
77+
if (source != null) {
78+
ProtocolUtils.writeVarInt(buf, source.ordinal());
79+
}
80+
81+
if (soundName != null) {
82+
ProtocolUtils.writeString(buf, soundName.asMinimalString()); // not using writeKey, as the client already defaults to the vanilla namespace
83+
}
84+
}
85+
86+
@Override
87+
public boolean handle(MinecraftSessionHandler handler) {
88+
return handler.handle(this);
89+
}
90+
91+
@Nullable
92+
public Sound.Source getSource() {
93+
return source;
94+
}
95+
96+
@Nullable
97+
public Key getSoundName() {
98+
return soundName;
99+
}
100+
101+
}

0 commit comments

Comments
 (0)