mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-10 02:17:41 +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.audience.MessageType;
|
||||||
import net.kyori.adventure.bossbar.BossBar;
|
import net.kyori.adventure.bossbar.BossBar;
|
||||||
import net.kyori.adventure.sound.Sound;
|
import net.kyori.adventure.sound.Sound;
|
||||||
|
import net.kyori.adventure.sound.SoundStop;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -34,10 +41,20 @@ public class AdventurePacketConvertor {
|
|||||||
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.WHITE, 15);
|
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) {
|
public static int getBossBarOverlayValue(@NotNull BossBar.Overlay overlay) {
|
||||||
return overlay.ordinal();
|
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) {
|
public static byte getBossBarFlagValue(@NotNull Collection<BossBar.Flag> flags) {
|
||||||
byte val = 0x0;
|
byte val = 0x0;
|
||||||
for (BossBar.Flag flag : flags) {
|
for (BossBar.Flag flag : flags) {
|
||||||
@ -46,24 +63,86 @@ public class AdventurePacketConvertor {
|
|||||||
return val;
|
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) {
|
public static int getBossBarColorValue(@NotNull BossBar.Color color) {
|
||||||
return color.ordinal();
|
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) {
|
public static int getSoundSourceValue(@NotNull Sound.Source source) {
|
||||||
return source.ordinal();
|
return source.ordinal();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte getMessageTypeValue(@NotNull MessageType messageType) {
|
/**
|
||||||
switch (messageType) {
|
* Gets the int value from a named text color.
|
||||||
case CHAT: return 0x00;
|
* @param color the color
|
||||||
case SYSTEM: return 0x01;
|
* @return the int value
|
||||||
}
|
*/
|
||||||
|
|
||||||
throw new IllegalArgumentException("Cannot get message type");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getNamedTextColorValue(@NotNull NamedTextColor color) {
|
public static int getNamedTextColorValue(@NotNull NamedTextColor color) {
|
||||||
return NAMED_TEXT_COLOR_ID_MAP.getInt(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