-
-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add primitive support for sound api
- Loading branch information
1 parent
7a9227d
commit 87b0e57
Showing
8 changed files
with
262 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...src/main/java/com/velocitypowered/proxy/protocol/packet/ClientboundSoundEntityPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (C) 2024 Velocity Contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.velocitypowered.proxy.protocol.packet; | ||
|
||
import com.velocitypowered.api.network.ProtocolVersion; | ||
import com.velocitypowered.proxy.connection.MinecraftSessionHandler; | ||
import com.velocitypowered.proxy.protocol.MinecraftPacket; | ||
import com.velocitypowered.proxy.protocol.ProtocolUtils; | ||
import io.netty.buffer.ByteBuf; | ||
import net.kyori.adventure.sound.Sound; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Random; | ||
|
||
public class ClientboundSoundEntityPacket implements MinecraftPacket { | ||
|
||
private static final Random SEEDS_RANDOM = new Random(); | ||
|
||
private Sound sound; | ||
private @Nullable Float fixedRange; | ||
private int entityId; | ||
|
||
public ClientboundSoundEntityPacket() {} | ||
|
||
public ClientboundSoundEntityPacket(Sound sound, @Nullable Float fixedRange, int entityId) { | ||
this.sound = sound; | ||
this.fixedRange = fixedRange; | ||
this.entityId = entityId; | ||
} | ||
|
||
@Override | ||
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { | ||
throw new UnsupportedOperationException("Decode is not implemented"); | ||
} | ||
|
||
@Override | ||
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { | ||
ProtocolUtils.writeVarInt(buf, 0); // version-dependent hardcoded sound id | ||
|
||
ProtocolUtils.writeString(buf, sound.name().asMinimalString()); // not using writeKey, as the client already defaults to the vanilla namespace | ||
|
||
buf.writeBoolean(fixedRange != null); | ||
if (fixedRange != null) | ||
buf.writeFloat(fixedRange); | ||
|
||
ProtocolUtils.writeVarInt(buf, sound.source().ordinal()); | ||
|
||
ProtocolUtils.writeVarInt(buf, entityId); | ||
|
||
buf.writeFloat(sound.volume()); | ||
|
||
buf.writeFloat(sound.pitch()); | ||
|
||
buf.writeLong(sound.seed().orElse(SEEDS_RANDOM.nextLong())); | ||
} | ||
|
||
@Override | ||
public boolean handle(MinecraftSessionHandler handler) { | ||
return handler.handle(this); | ||
} | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
...y/src/main/java/com/velocitypowered/proxy/protocol/packet/ClientboundStopSoundPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (C) 2024 Velocity Contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.velocitypowered.proxy.protocol.packet; | ||
|
||
import com.velocitypowered.api.network.ProtocolVersion; | ||
import com.velocitypowered.proxy.connection.MinecraftSessionHandler; | ||
import com.velocitypowered.proxy.protocol.MinecraftPacket; | ||
import com.velocitypowered.proxy.protocol.ProtocolUtils; | ||
import io.netty.buffer.ByteBuf; | ||
import net.kyori.adventure.key.Key; | ||
import net.kyori.adventure.sound.Sound; | ||
import net.kyori.adventure.sound.SoundStop; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public class ClientboundStopSoundPacket implements MinecraftPacket { | ||
|
||
private @Nullable Sound.Source source; | ||
private @Nullable Key soundName; | ||
|
||
public ClientboundStopSoundPacket() {} | ||
|
||
public ClientboundStopSoundPacket(SoundStop soundStop) { | ||
this(soundStop.source(), soundStop.sound()); | ||
} | ||
|
||
public ClientboundStopSoundPacket(@Nullable Sound.Source source, @Nullable Key soundName) { | ||
this.source = source; | ||
this.soundName = soundName; | ||
} | ||
|
||
@Override | ||
public void decode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { | ||
int flagsBitmask = buf.readByte(); | ||
|
||
if ((flagsBitmask & 1) != 0) { | ||
source = Sound.Source.values()[ProtocolUtils.readVarInt(buf)]; | ||
} else { | ||
source = null; | ||
} | ||
|
||
if ((flagsBitmask & 2) != 0) { | ||
soundName = ProtocolUtils.readKey(buf); | ||
} else { | ||
soundName = null; | ||
} | ||
} | ||
|
||
@Override | ||
public void encode(ByteBuf buf, ProtocolUtils.Direction direction, ProtocolVersion protocolVersion) { | ||
int flagsBitmask = 0; | ||
if (source != null && soundName == null) { | ||
flagsBitmask |= 1; | ||
} else if (soundName != null && source == null) { | ||
flagsBitmask |= 2; | ||
} else if (source != null /*&& sound != null*/) { | ||
flagsBitmask |= 3; | ||
} | ||
|
||
buf.writeByte(flagsBitmask); | ||
|
||
if (source != null) { | ||
ProtocolUtils.writeVarInt(buf, source.ordinal()); | ||
} | ||
|
||
if (soundName != null) { | ||
ProtocolUtils.writeString(buf, soundName.asMinimalString()); // not using writeKey, as the client already defaults to the vanilla namespace | ||
} | ||
} | ||
|
||
@Override | ||
public boolean handle(MinecraftSessionHandler handler) { | ||
return handler.handle(this); | ||
} | ||
|
||
@Nullable | ||
public Sound.Source getSource() { | ||
return source; | ||
} | ||
|
||
@Nullable | ||
public Key getSoundName() { | ||
return soundName; | ||
} | ||
|
||
} |