2021-03-05 20:07:33 +01:00
|
|
|
package net.minestom.server.adventure;
|
|
|
|
|
2021-03-24 17:50:02 +01:00
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
2021-03-05 20:07:33 +01:00
|
|
|
import net.kyori.adventure.bossbar.BossBar;
|
2021-06-11 16:51:45 +02:00
|
|
|
import net.kyori.adventure.key.Key;
|
2021-03-05 20:07:33 +01:00
|
|
|
import net.kyori.adventure.sound.Sound;
|
2021-03-25 13:58:18 +01:00
|
|
|
import net.kyori.adventure.sound.SoundStop;
|
2021-03-24 17:50:02 +01:00
|
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
2021-03-27 14:59:08 +01:00
|
|
|
import net.minestom.server.entity.Entity;
|
2021-03-25 13:58:18 +01:00
|
|
|
import net.minestom.server.network.packet.server.ServerPacket;
|
2021-03-27 14:59:08 +01:00
|
|
|
import net.minestom.server.network.packet.server.play.EntitySoundEffectPacket;
|
2021-03-25 13:58:18 +01:00
|
|
|
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.sound.SoundEvent;
|
2021-03-05 20:07:33 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility methods to convert adventure enums to their packet values.
|
|
|
|
*/
|
|
|
|
public class AdventurePacketConvertor {
|
2021-03-24 17:50:02 +01:00
|
|
|
private static final Object2IntMap<NamedTextColor> NAMED_TEXT_COLOR_ID_MAP = new Object2IntArrayMap<>(16);
|
2021-07-06 20:44:24 +02:00
|
|
|
|
2021-03-24 17:50:02 +01:00
|
|
|
static {
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.BLACK, 0);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_BLUE, 1);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_GREEN, 2);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_AQUA, 3);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_RED, 4);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_PURPLE, 5);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.GOLD, 6);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.GRAY, 7);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.DARK_GRAY, 8);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.BLUE, 9);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.GREEN, 10);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.AQUA, 11);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.RED, 12);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.LIGHT_PURPLE, 13);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.YELLOW, 14);
|
|
|
|
NAMED_TEXT_COLOR_ID_MAP.put(NamedTextColor.WHITE, 15);
|
|
|
|
}
|
2021-03-05 20:07:33 +01:00
|
|
|
|
2021-03-25 13:58:18 +01:00
|
|
|
/**
|
|
|
|
* Gets the int value of a boss bar overlay.
|
2021-06-11 16:51:45 +02:00
|
|
|
*
|
2021-03-25 13:58:18 +01:00
|
|
|
* @param overlay the overlay
|
|
|
|
* @return the value
|
|
|
|
*/
|
2021-03-05 20:07:33 +01:00
|
|
|
public static int getBossBarOverlayValue(@NotNull BossBar.Overlay overlay) {
|
|
|
|
return overlay.ordinal();
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:58:18 +01:00
|
|
|
/**
|
|
|
|
* Gets the byte value of a collection of boss bar flags.
|
2021-06-11 16:51:45 +02:00
|
|
|
*
|
2021-03-25 13:58:18 +01:00
|
|
|
* @param flags the flags
|
|
|
|
* @return the value
|
|
|
|
*/
|
2021-03-05 20:07:33 +01:00
|
|
|
public static byte getBossBarFlagValue(@NotNull Collection<BossBar.Flag> flags) {
|
|
|
|
byte val = 0x0;
|
|
|
|
for (BossBar.Flag flag : flags) {
|
|
|
|
val |= flag.ordinal();
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:58:18 +01:00
|
|
|
/**
|
|
|
|
* Gets the int value of a boss bar color.
|
2021-06-11 16:51:45 +02:00
|
|
|
*
|
2021-03-25 13:58:18 +01:00
|
|
|
* @param color the color
|
|
|
|
* @return the value
|
|
|
|
*/
|
2021-03-05 20:07:33 +01:00
|
|
|
public static int getBossBarColorValue(@NotNull BossBar.Color color) {
|
|
|
|
return color.ordinal();
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:58:18 +01:00
|
|
|
/**
|
|
|
|
* Gets the int value of a sound source.
|
2021-06-11 16:51:45 +02:00
|
|
|
*
|
2021-03-25 13:58:18 +01:00
|
|
|
* @param source the source
|
|
|
|
* @return the value
|
|
|
|
*/
|
2021-03-05 20:59:28 +01:00
|
|
|
public static int getSoundSourceValue(@NotNull Sound.Source source) {
|
2021-03-05 20:07:33 +01:00
|
|
|
return source.ordinal();
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:58:18 +01:00
|
|
|
/**
|
|
|
|
* Gets the int value from a named text color.
|
2021-06-11 16:51:45 +02:00
|
|
|
*
|
2021-03-25 13:58:18 +01:00
|
|
|
* @param color the color
|
|
|
|
* @return the int value
|
|
|
|
*/
|
|
|
|
public static int getNamedTextColorValue(@NotNull NamedTextColor color) {
|
|
|
|
return NAMED_TEXT_COLOR_ID_MAP.getInt(color);
|
|
|
|
}
|
2021-03-05 20:07:33 +01:00
|
|
|
|
2021-03-25 13:58:18 +01:00
|
|
|
/**
|
|
|
|
* Creates a sound packet from a sound and a location.
|
2021-06-11 16:51:45 +02:00
|
|
|
*
|
2021-03-25 13:58:18 +01:00
|
|
|
* @param sound the sound
|
2021-07-06 20:44:24 +02:00
|
|
|
* @param x the x coordinate
|
|
|
|
* @param y the y coordinate
|
|
|
|
* @param z the z coordinate
|
2021-03-25 13:58:18 +01:00
|
|
|
* @return the sound packet
|
|
|
|
*/
|
2021-06-11 16:51:45 +02:00
|
|
|
public static @NotNull ServerPacket createSoundPacket(@NotNull Sound sound, double x, double y, double z) {
|
2021-07-28 14:29:28 +02:00
|
|
|
final SoundEvent minestomSound = SoundEvent.fromNamespaceId(sound.name().asString());
|
2021-03-25 13:58:18 +01:00
|
|
|
if (minestomSound == null) {
|
2021-06-11 16:51:45 +02:00
|
|
|
final NamedSoundEffectPacket packet = new NamedSoundEffectPacket();
|
2021-03-25 13:58:18 +01:00
|
|
|
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 {
|
2021-06-11 16:51:45 +02:00
|
|
|
final SoundEffectPacket packet = new SoundEffectPacket();
|
2021-07-28 14:29:28 +02:00
|
|
|
packet.soundId = minestomSound.id();
|
2021-03-25 13:58:18 +01:00
|
|
|
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;
|
|
|
|
}
|
2021-03-05 20:07:33 +01:00
|
|
|
}
|
2021-03-24 17:50:02 +01:00
|
|
|
|
2021-03-27 14:59:08 +01:00
|
|
|
/**
|
2021-06-11 16:51:45 +02:00
|
|
|
* Creates a sound effect packet from a sound and an emitter.
|
|
|
|
*
|
2021-07-06 20:44:24 +02:00
|
|
|
* @param sound the sound
|
2021-06-11 16:51:45 +02:00
|
|
|
* @param emitter the emitter, must be an {@link Entity}
|
|
|
|
* @return the sound packet
|
2021-03-27 14:59:08 +01:00
|
|
|
*/
|
2021-06-11 16:51:45 +02:00
|
|
|
public static @NotNull ServerPacket createSoundPacket(@NotNull Sound sound, Sound.@NotNull Emitter emitter) {
|
|
|
|
if (emitter == Sound.Emitter.self())
|
|
|
|
throw new IllegalArgumentException("you must replace instances of Emitter.self() before calling this method");
|
2021-10-22 02:19:38 +02:00
|
|
|
if (!(emitter instanceof Entity entity))
|
2021-06-11 16:51:45 +02:00
|
|
|
throw new IllegalArgumentException("you can only call this method with entities");
|
2021-03-27 14:59:08 +01:00
|
|
|
|
2021-07-28 14:29:28 +02:00
|
|
|
final SoundEvent minestomSound = SoundEvent.fromNamespaceId(sound.name().asString());
|
2021-06-11 16:51:45 +02:00
|
|
|
|
|
|
|
if (minestomSound != null) {
|
|
|
|
final EntitySoundEffectPacket packet = new EntitySoundEffectPacket();
|
2021-07-28 14:29:28 +02:00
|
|
|
packet.soundId = minestomSound.id();
|
2021-03-27 14:59:08 +01:00
|
|
|
packet.soundSource = sound.source();
|
|
|
|
packet.entityId = entity.getEntityId();
|
|
|
|
packet.volume = sound.volume();
|
|
|
|
packet.pitch = sound.pitch();
|
|
|
|
return packet;
|
2021-06-11 16:51:45 +02:00
|
|
|
} else {
|
2021-07-06 20:44:24 +02:00
|
|
|
final var pos = entity.getPosition();
|
2021-06-11 16:51:45 +02:00
|
|
|
final NamedSoundEffectPacket packet = new NamedSoundEffectPacket();
|
|
|
|
packet.soundName = sound.name().asString();
|
|
|
|
packet.soundSource = sound.source();
|
2021-07-06 20:44:24 +02:00
|
|
|
packet.x = (int) pos.x();
|
|
|
|
packet.y = (int) pos.y();
|
|
|
|
packet.z = (int) pos.z();
|
2021-06-11 16:51:45 +02:00
|
|
|
packet.volume = sound.volume();
|
|
|
|
packet.pitch = sound.pitch();
|
|
|
|
return packet;
|
2021-03-27 14:59:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 16:51:45 +02:00
|
|
|
/**
|
|
|
|
* Creates an entity sound packet from an Adventure sound.
|
|
|
|
*
|
2021-07-06 20:44:24 +02:00
|
|
|
* @param sound the sound
|
2021-06-11 16:51:45 +02:00
|
|
|
* @param entity the entity the sound is coming from
|
|
|
|
* @return the packet
|
|
|
|
* @deprecated Use {@link #createSoundPacket(Sound, Sound.Emitter)}
|
|
|
|
*/
|
|
|
|
@Deprecated(forRemoval = true)
|
|
|
|
public static ServerPacket createEntitySoundPacket(@NotNull Sound sound, @NotNull Entity entity) {
|
|
|
|
return createSoundPacket(sound, entity);
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:58:18 +01:00
|
|
|
/**
|
|
|
|
* Creates a sound stop packet from a sound stop.
|
2021-06-11 16:51:45 +02:00
|
|
|
*
|
2021-03-25 13:58:18 +01:00
|
|
|
* @param stop the sound stop
|
|
|
|
* @return the sound stop packet
|
|
|
|
*/
|
|
|
|
public static ServerPacket createSoundStopPacket(@NotNull SoundStop stop) {
|
|
|
|
StopSoundPacket packet = new StopSoundPacket();
|
|
|
|
packet.flags = 0x0;
|
|
|
|
|
2021-06-11 16:51:45 +02:00
|
|
|
final Sound.Source source = stop.source();
|
|
|
|
if (source != null) {
|
2021-03-25 13:58:18 +01:00
|
|
|
packet.flags |= 0x1;
|
2021-06-11 16:51:45 +02:00
|
|
|
packet.source = AdventurePacketConvertor.getSoundSourceValue(source);
|
2021-03-25 13:58:18 +01:00
|
|
|
}
|
|
|
|
|
2021-06-11 16:51:45 +02:00
|
|
|
final Key sound = stop.sound();
|
|
|
|
if (sound != null) {
|
2021-03-25 13:58:18 +01:00
|
|
|
packet.flags |= 0x2;
|
2021-06-11 16:51:45 +02:00
|
|
|
packet.sound = sound.asString();
|
2021-03-25 13:58:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return packet;
|
2021-03-24 17:50:02 +01:00
|
|
|
}
|
2021-03-05 20:07:33 +01:00
|
|
|
}
|