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-10-31 19:29:41 +01:00
|
|
|
import net.kyori.adventure.text.Component;
|
2021-03-24 17:50:02 +01:00
|
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
2021-10-31 19:29:41 +01:00
|
|
|
import net.kyori.adventure.title.Title;
|
|
|
|
import net.kyori.adventure.title.TitlePart;
|
2021-11-30 17:49:41 +01:00
|
|
|
import net.minestom.server.coordinate.Pos;
|
2023-05-21 16:51:13 +02:00
|
|
|
import net.minestom.server.coordinate.Vec;
|
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-10-31 19:29:41 +01:00
|
|
|
import net.minestom.server.network.packet.server.play.*;
|
2021-03-25 13:58:18 +01:00
|
|
|
import net.minestom.server.sound.SoundEvent;
|
2021-10-31 19:29:41 +01:00
|
|
|
import net.minestom.server.utils.TickUtils;
|
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) {
|
2023-05-21 16:51:13 +02:00
|
|
|
return new SoundEffectPacket(sound.name().asString(), null, sound.source(),
|
|
|
|
new Vec(x, y, z), sound.volume(), sound.pitch(), 0);
|
2021-03-25 13:58:18 +01:00
|
|
|
} else {
|
2023-05-21 16:51:13 +02:00
|
|
|
return new SoundEffectPacket(minestomSound, null, sound.source(),
|
|
|
|
new Vec(x, y, z), sound.volume(), sound.pitch(), 0);
|
2021-03-25 13:58:18 +01:00
|
|
|
}
|
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) {
|
2023-05-21 16:51:13 +02:00
|
|
|
return new EntitySoundEffectPacket(minestomSound, null, sound.source(), entity.getEntityId(), sound.volume(), sound.pitch(), 0);
|
2021-06-11 16:51:45 +02:00
|
|
|
} else {
|
2023-05-21 16:51:13 +02:00
|
|
|
return new EntitySoundEffectPacket(sound.name().asString(), null, sound.source(), entity.getEntityId(), sound.volume(), sound.pitch(), 0);
|
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) {
|
2021-11-30 17:49:41 +01:00
|
|
|
byte flags = 0x0;
|
|
|
|
Sound.Source source = stop.source();
|
|
|
|
String sound = null;
|
2021-03-25 13:58:18 +01:00
|
|
|
|
2021-11-30 17:49:41 +01:00
|
|
|
if (source != null) flags |= 0x1;
|
2021-03-25 13:58:18 +01:00
|
|
|
|
2021-11-30 17:49:41 +01:00
|
|
|
final Key soundKey = stop.sound();
|
|
|
|
if (soundKey != null) {
|
|
|
|
flags |= 0x2;
|
|
|
|
sound = soundKey.asString();
|
2021-03-25 13:58:18 +01:00
|
|
|
}
|
|
|
|
|
2021-11-30 17:49:41 +01:00
|
|
|
return new StopSoundPacket(flags, source, sound);
|
2021-03-24 17:50:02 +01:00
|
|
|
}
|
2021-10-31 19:29:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates one of the three title packets from a title part and a value.
|
|
|
|
*
|
2021-11-30 17:49:41 +01:00
|
|
|
* @param part the part
|
2021-10-31 19:29:41 +01:00
|
|
|
* @param value the value
|
2021-11-30 17:49:41 +01:00
|
|
|
* @param <T> the type of the part
|
2021-10-31 19:29:41 +01:00
|
|
|
* @return the title packet
|
|
|
|
*/
|
|
|
|
public static <T> @NotNull ServerPacket createTitlePartPacket(@NotNull TitlePart<T> part, @NotNull T value) {
|
|
|
|
if (part == TitlePart.TITLE) {
|
|
|
|
return new SetTitleTextPacket((Component) value);
|
|
|
|
} else if (part == TitlePart.SUBTITLE) {
|
|
|
|
return new SetTitleSubTitlePacket((Component) value);
|
|
|
|
} else if (part == TitlePart.TIMES) {
|
|
|
|
Title.Times times = (Title.Times) value;
|
|
|
|
return new SetTitleTimePacket(
|
|
|
|
TickUtils.fromDuration(times.fadeIn(), TickUtils.CLIENT_TICK_MS),
|
|
|
|
TickUtils.fromDuration(times.stay(), TickUtils.CLIENT_TICK_MS),
|
|
|
|
TickUtils.fromDuration(times.fadeOut(), TickUtils.CLIENT_TICK_MS));
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Unknown TitlePart " + part);
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 20:07:33 +01:00
|
|
|
}
|