Minestom/src/main/java/net/minestom/server/adventure/AdventurePacketConvertor.java

204 lines
7.4 KiB
Java
Raw Normal View History

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;
import net.kyori.adventure.bossbar.BossBar;
2021-06-11 16:51:45 +02:00
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
2021-03-25 13:58:18 +01:00
import net.kyori.adventure.sound.SoundStop;
import net.kyori.adventure.text.Component;
2021-03-24 17:50:02 +01:00
import net.kyori.adventure.text.format.NamedTextColor;
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;
1.19.3 (#1851) * hollow-cube/1.19.3 Signed-off-by: mworzala <mattheworzala@gmail.com> fix sounds v2 (cherry picked from commit e6d4a2cc919d6ab5aa9402cc871a70535069d803) fix command packet (cherry picked from commit f3efd64f7bd1d0473e0899d202268f77bab35abf) fix sound effect packets (cherry picked from commit 8530b37b354129a2149aafe2820183b28766be00) remove named sound effect & fix sound effect, entity sound effect packet is still wrong (cherry picked from commit 612f6065a12c465c07816c8551b1c44f17c21159) update NamedSoundEffectPacket to 1.19.3 (cherry picked from commit 8c78d9beac96f94770f6fd0e9216452c3421bcfd) update datagen, add read method to player info update (though it seems kinda broken) (cherry picked from commit 6464a72dabc5edaf9b09ef1b8100815965bbad74) Add ChatSession Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 0488915fdaff4b51c4375da27be2dcf79215435d) Unnecessary line change Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 915836f490fb3c28b3326edcc6da1de57fec118c) Make tests compile Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 220217fcc1030441f1d2c3f6e96d3d4ca68331f3) Fix info update Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 1a606285c0ceb1252ed505acd963430a3d8cb0a7) Fix unsigned message Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 7ba55fdfef9f753c3ba6a1ca3d569b3c6fe4418e) 1.19.3 support (cherry picked from commit f09fdd862bd185f4f8bdff8fdfcd45945b38f4ab) * Cleanup * Fix tests * Fix tests round 2 * Cleanup round 2 * remove logback * remove chatbound * don't use magic numbers * Fix formatting * Unused imports * Fix and add update display name packet test back * Cleanup tests * Bump minestom data * Add ArgumentResource and ArgumentResourceOrTag * Fix spacing --------- Co-authored-by: themode <themode@outlook.fr> Co-authored-by: KrystilizeNevaDies <tyreece@rozycki.family> Co-authored-by: iam <iam4722202468@users.noreply.github.com>
2023-05-21 16:51:13 +02:00
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Entity;
2021-03-25 13:58:18 +01:00
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.play.*;
2021-03-25 13:58:18 +01:00
import net.minestom.server.sound.SoundEvent;
import net.minestom.server.utils.TickUtils;
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-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
*/
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
*/
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
*/
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
*/
public static int getSoundSourceValue(@NotNull Sound.Source source) {
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-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) {
1.19.3 (#1851) * hollow-cube/1.19.3 Signed-off-by: mworzala <mattheworzala@gmail.com> fix sounds v2 (cherry picked from commit e6d4a2cc919d6ab5aa9402cc871a70535069d803) fix command packet (cherry picked from commit f3efd64f7bd1d0473e0899d202268f77bab35abf) fix sound effect packets (cherry picked from commit 8530b37b354129a2149aafe2820183b28766be00) remove named sound effect & fix sound effect, entity sound effect packet is still wrong (cherry picked from commit 612f6065a12c465c07816c8551b1c44f17c21159) update NamedSoundEffectPacket to 1.19.3 (cherry picked from commit 8c78d9beac96f94770f6fd0e9216452c3421bcfd) update datagen, add read method to player info update (though it seems kinda broken) (cherry picked from commit 6464a72dabc5edaf9b09ef1b8100815965bbad74) Add ChatSession Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 0488915fdaff4b51c4375da27be2dcf79215435d) Unnecessary line change Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 915836f490fb3c28b3326edcc6da1de57fec118c) Make tests compile Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 220217fcc1030441f1d2c3f6e96d3d4ca68331f3) Fix info update Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 1a606285c0ceb1252ed505acd963430a3d8cb0a7) Fix unsigned message Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 7ba55fdfef9f753c3ba6a1ca3d569b3c6fe4418e) 1.19.3 support (cherry picked from commit f09fdd862bd185f4f8bdff8fdfcd45945b38f4ab) * Cleanup * Fix tests * Fix tests round 2 * Cleanup round 2 * remove logback * remove chatbound * don't use magic numbers * Fix formatting * Unused imports * Fix and add update display name packet test back * Cleanup tests * Bump minestom data * Add ArgumentResource and ArgumentResourceOrTag * Fix spacing --------- Co-authored-by: themode <themode@outlook.fr> Co-authored-by: KrystilizeNevaDies <tyreece@rozycki.family> Co-authored-by: iam <iam4722202468@users.noreply.github.com>
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 {
1.19.3 (#1851) * hollow-cube/1.19.3 Signed-off-by: mworzala <mattheworzala@gmail.com> fix sounds v2 (cherry picked from commit e6d4a2cc919d6ab5aa9402cc871a70535069d803) fix command packet (cherry picked from commit f3efd64f7bd1d0473e0899d202268f77bab35abf) fix sound effect packets (cherry picked from commit 8530b37b354129a2149aafe2820183b28766be00) remove named sound effect & fix sound effect, entity sound effect packet is still wrong (cherry picked from commit 612f6065a12c465c07816c8551b1c44f17c21159) update NamedSoundEffectPacket to 1.19.3 (cherry picked from commit 8c78d9beac96f94770f6fd0e9216452c3421bcfd) update datagen, add read method to player info update (though it seems kinda broken) (cherry picked from commit 6464a72dabc5edaf9b09ef1b8100815965bbad74) Add ChatSession Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 0488915fdaff4b51c4375da27be2dcf79215435d) Unnecessary line change Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 915836f490fb3c28b3326edcc6da1de57fec118c) Make tests compile Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 220217fcc1030441f1d2c3f6e96d3d4ca68331f3) Fix info update Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 1a606285c0ceb1252ed505acd963430a3d8cb0a7) Fix unsigned message Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 7ba55fdfef9f753c3ba6a1ca3d569b3c6fe4418e) 1.19.3 support (cherry picked from commit f09fdd862bd185f4f8bdff8fdfcd45945b38f4ab) * Cleanup * Fix tests * Fix tests round 2 * Cleanup round 2 * remove logback * remove chatbound * don't use magic numbers * Fix formatting * Unused imports * Fix and add update display name packet test back * Cleanup tests * Bump minestom data * Add ArgumentResource and ArgumentResourceOrTag * Fix spacing --------- Co-authored-by: themode <themode@outlook.fr> Co-authored-by: KrystilizeNevaDies <tyreece@rozycki.family> Co-authored-by: iam <iam4722202468@users.noreply.github.com>
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-24 17:50:02 +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-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");
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-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) {
1.19.3 (#1851) * hollow-cube/1.19.3 Signed-off-by: mworzala <mattheworzala@gmail.com> fix sounds v2 (cherry picked from commit e6d4a2cc919d6ab5aa9402cc871a70535069d803) fix command packet (cherry picked from commit f3efd64f7bd1d0473e0899d202268f77bab35abf) fix sound effect packets (cherry picked from commit 8530b37b354129a2149aafe2820183b28766be00) remove named sound effect & fix sound effect, entity sound effect packet is still wrong (cherry picked from commit 612f6065a12c465c07816c8551b1c44f17c21159) update NamedSoundEffectPacket to 1.19.3 (cherry picked from commit 8c78d9beac96f94770f6fd0e9216452c3421bcfd) update datagen, add read method to player info update (though it seems kinda broken) (cherry picked from commit 6464a72dabc5edaf9b09ef1b8100815965bbad74) Add ChatSession Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 0488915fdaff4b51c4375da27be2dcf79215435d) Unnecessary line change Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 915836f490fb3c28b3326edcc6da1de57fec118c) Make tests compile Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 220217fcc1030441f1d2c3f6e96d3d4ca68331f3) Fix info update Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 1a606285c0ceb1252ed505acd963430a3d8cb0a7) Fix unsigned message Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 7ba55fdfef9f753c3ba6a1ca3d569b3c6fe4418e) 1.19.3 support (cherry picked from commit f09fdd862bd185f4f8bdff8fdfcd45945b38f4ab) * Cleanup * Fix tests * Fix tests round 2 * Cleanup round 2 * remove logback * remove chatbound * don't use magic numbers * Fix formatting * Unused imports * Fix and add update display name packet test back * Cleanup tests * Bump minestom data * Add ArgumentResource and ArgumentResourceOrTag * Fix spacing --------- Co-authored-by: themode <themode@outlook.fr> Co-authored-by: KrystilizeNevaDies <tyreece@rozycki.family> Co-authored-by: iam <iam4722202468@users.noreply.github.com>
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 {
1.19.3 (#1851) * hollow-cube/1.19.3 Signed-off-by: mworzala <mattheworzala@gmail.com> fix sounds v2 (cherry picked from commit e6d4a2cc919d6ab5aa9402cc871a70535069d803) fix command packet (cherry picked from commit f3efd64f7bd1d0473e0899d202268f77bab35abf) fix sound effect packets (cherry picked from commit 8530b37b354129a2149aafe2820183b28766be00) remove named sound effect & fix sound effect, entity sound effect packet is still wrong (cherry picked from commit 612f6065a12c465c07816c8551b1c44f17c21159) update NamedSoundEffectPacket to 1.19.3 (cherry picked from commit 8c78d9beac96f94770f6fd0e9216452c3421bcfd) update datagen, add read method to player info update (though it seems kinda broken) (cherry picked from commit 6464a72dabc5edaf9b09ef1b8100815965bbad74) Add ChatSession Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 0488915fdaff4b51c4375da27be2dcf79215435d) Unnecessary line change Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 915836f490fb3c28b3326edcc6da1de57fec118c) Make tests compile Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 220217fcc1030441f1d2c3f6e96d3d4ca68331f3) Fix info update Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 1a606285c0ceb1252ed505acd963430a3d8cb0a7) Fix unsigned message Signed-off-by: TheMode <themode@outlook.fr> (cherry picked from commit 7ba55fdfef9f753c3ba6a1ca3d569b3c6fe4418e) 1.19.3 support (cherry picked from commit f09fdd862bd185f4f8bdff8fdfcd45945b38f4ab) * Cleanup * Fix tests * Fix tests round 2 * Cleanup round 2 * remove logback * remove chatbound * don't use magic numbers * Fix formatting * Unused imports * Fix and add update display name packet test back * Cleanup tests * Bump minestom data * Add ArgumentResource and ArgumentResourceOrTag * Fix spacing --------- Co-authored-by: themode <themode@outlook.fr> Co-authored-by: KrystilizeNevaDies <tyreece@rozycki.family> Co-authored-by: iam <iam4722202468@users.noreply.github.com>
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-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
}
/**
* 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
* @param value the value
2021-11-30 17:49:41 +01:00
* @param <T> the type of the part
* @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);
}
}
}