mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-09 18:08:37 +01:00
Cleanup AdventurePacketConvertor
This commit is contained in:
parent
cfba291522
commit
ef7329351f
@ -5,7 +5,14 @@ import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
||||
import net.kyori.adventure.audience.MessageType;
|
||||
import net.kyori.adventure.bossbar.BossBar;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import net.kyori.adventure.sound.SoundStop;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.minestom.server.network.packet.server.ServerPacket;
|
||||
import net.minestom.server.network.packet.server.play.NamedSoundEffectPacket;
|
||||
import net.minestom.server.network.packet.server.play.SoundEffectPacket;
|
||||
import net.minestom.server.network.packet.server.play.StopSoundPacket;
|
||||
import net.minestom.server.registry.Registries;
|
||||
import net.minestom.server.sound.SoundEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -34,10 +41,20 @@ public class AdventurePacketConvertor {
|
||||
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.WHITE, 15);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the int value of a boss bar overlay.
|
||||
* @param overlay the overlay
|
||||
* @return the value
|
||||
*/
|
||||
public static int getBossBarOverlayValue(@NotNull BossBar.Overlay overlay) {
|
||||
return overlay.ordinal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the byte value of a collection of boss bar flags.
|
||||
* @param flags the flags
|
||||
* @return the value
|
||||
*/
|
||||
public static byte getBossBarFlagValue(@NotNull Collection<BossBar.Flag> flags) {
|
||||
byte val = 0x0;
|
||||
for (BossBar.Flag flag : flags) {
|
||||
@ -46,24 +63,86 @@ public class AdventurePacketConvertor {
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the int value of a boss bar color.
|
||||
* @param color the color
|
||||
* @return the value
|
||||
*/
|
||||
public static int getBossBarColorValue(@NotNull BossBar.Color color) {
|
||||
return color.ordinal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the int value of a sound source.
|
||||
* @param source the source
|
||||
* @return the value
|
||||
*/
|
||||
public static int getSoundSourceValue(@NotNull Sound.Source source) {
|
||||
return source.ordinal();
|
||||
}
|
||||
|
||||
public static byte getMessageTypeValue(@NotNull MessageType messageType) {
|
||||
switch (messageType) {
|
||||
case CHAT: return 0x00;
|
||||
case SYSTEM: return 0x01;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Cannot get message type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the int value from a named text color.
|
||||
* @param color the color
|
||||
* @return the int value
|
||||
*/
|
||||
public static int getNamedTextColorValue(@NotNull NamedTextColor color) {
|
||||
return NAMED_TEXT_COLOR_ID_MAP.getInt(color);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a sound packet from a sound and a location.
|
||||
* @param sound the sound
|
||||
* @param x the x coordinate
|
||||
* @param y the y coordinate
|
||||
* @param z the z coordinate
|
||||
* @return the sound packet
|
||||
*/
|
||||
public static ServerPacket createSoundPacket(@NotNull Sound sound, double x, double y, double z) {
|
||||
SoundEvent minestomSound = Registries.getSoundEvent(sound.name());
|
||||
|
||||
if (minestomSound == null) {
|
||||
NamedSoundEffectPacket packet = new NamedSoundEffectPacket();
|
||||
packet.soundName = sound.name().asString();
|
||||
packet.soundSource = sound.source();
|
||||
packet.x = (int) x;
|
||||
packet.y = (int) y;
|
||||
packet.z = (int) z;
|
||||
packet.volume = sound.volume();
|
||||
packet.pitch = sound.pitch();
|
||||
return packet;
|
||||
} else {
|
||||
SoundEffectPacket packet = new SoundEffectPacket();
|
||||
packet.soundId = minestomSound.getId();
|
||||
packet.soundSource = sound.source();
|
||||
packet.x = (int) x;
|
||||
packet.y = (int) y;
|
||||
packet.z = (int) z;
|
||||
packet.volume = sound.volume();
|
||||
packet.pitch = sound.pitch();
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a sound stop packet from a sound stop.
|
||||
* @param stop the sound stop
|
||||
* @return the sound stop packet
|
||||
*/
|
||||
public static ServerPacket createSoundStopPacket(@NotNull SoundStop stop) {
|
||||
StopSoundPacket packet = new StopSoundPacket();
|
||||
packet.flags = 0x0;
|
||||
|
||||
if (stop.source() != null) {
|
||||
packet.flags |= 0x1;
|
||||
packet.source = AdventurePacketConvertor.getSoundSourceValue(stop.source());
|
||||
}
|
||||
|
||||
if (stop.sound() != null) {
|
||||
packet.flags |= 0x2;
|
||||
packet.sound = stop.sound().asString();
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user