From 55c82cfddb75e01bf76da377ffba5cf7e7b3adb6 Mon Sep 17 00:00:00 2001 From: EternalHuman Date: Tue, 31 Oct 2023 11:49:10 +0300 Subject: [PATCH] NamedTextColor -> GlowColor --- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 1 + .../conversion/znpcs/ZNpcImporter.java | 1 + .../entity/EntityPropertyRegistryImpl.java | 2 +- .../entity/properties/GlowProperty.java | 8 +- .../GlowColorPropertySerializer.java | 22 ++ .../NamedTextColorPropertySerializer.java | 1 + .../pyr/znpcsplus/hologram/HologramImpl.java | 2 +- .../pyr/znpcsplus/packets/PacketFactory.java | 3 +- .../znpcsplus/packets/V1_17PacketFactory.java | 3 +- .../packets/V1_20_2PacketFactory.java | 3 +- .../znpcsplus/packets/V1_8PacketFactory.java | 9 +- .../znpcsplus/parsers/GlowColorParser.java | 21 ++ .../parsers/NamedTextColorParser.java | 1 + .../lol/pyr/znpcsplus/util/GlowColor.java | 252 ++++++++++++++++++ 14 files changed, 316 insertions(+), 13 deletions(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/GlowColorPropertySerializer.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/parsers/GlowColorParser.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/util/GlowColor.java diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 5aaa60a..b71004c 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -247,6 +247,7 @@ public class ZNpcsPlus { manager.registerParser(Float.class, new FloatParser(incorrectUsageMessage)); manager.registerParser(Boolean.class, new BooleanParser(incorrectUsageMessage)); manager.registerParser(NamedTextColor.class, new NamedTextColorParser(incorrectUsageMessage)); + manager.registerParser(GlowColor.class, new GlowColorParser(incorrectUsageMessage)); manager.registerParser(InteractionType.class, new InteractionTypeParser(incorrectUsageMessage)); manager.registerParser(Color.class, new ColorParser(incorrectUsageMessage)); manager.registerParser(Vector3f.class, new Vector3fParser(incorrectUsageMessage)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/conversion/znpcs/ZNpcImporter.java b/plugin/src/main/java/lol/pyr/znpcsplus/conversion/znpcs/ZNpcImporter.java index 09233b6..3ecdad1 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/conversion/znpcs/ZNpcImporter.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/conversion/znpcs/ZNpcImporter.java @@ -36,6 +36,7 @@ import lol.pyr.znpcsplus.util.LookType; import lol.pyr.znpcsplus.util.NpcLocation; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.DyeColor; import org.bukkit.inventory.ItemStack; diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java index 86662d7..ee1a5be 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -49,7 +49,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { public EntityPropertyRegistryImpl(MojangSkinCache skinCache, ConfigManager configManager) { registerSerializer(new ComponentPropertySerializer()); - registerSerializer(new NamedTextColorPropertySerializer()); + registerSerializer(new GlowColorPropertySerializer()); registerSerializer(new SkinDescriptorSerializer(skinCache)); registerSerializer(new ItemStackPropertySerializer()); registerSerializer(new ColorPropertySerializer()); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/GlowProperty.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/GlowProperty.java index 13931d8..6c45325 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/GlowProperty.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/GlowProperty.java @@ -5,22 +5,22 @@ import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.packets.PacketFactory; -import net.kyori.adventure.text.format.NamedTextColor; +import lol.pyr.znpcsplus.util.GlowColor; import org.bukkit.entity.Player; import java.util.Map; -public class GlowProperty extends EntityPropertyImpl { +public class GlowProperty extends EntityPropertyImpl { private final PacketFactory packetFactory; public GlowProperty(PacketFactory packetFactory) { - super("glow", null, NamedTextColor.class); + super("glow", null, GlowColor.class); this.packetFactory = packetFactory; } @Override public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { - NamedTextColor value = entity.getProperty(this); + GlowColor value = entity.getProperty(this); EntityData oldData = properties.get(0); byte oldValue = oldData == null ? 0 : (byte) oldData.getValue(); properties.put(0, newEntityData(0, EntityDataTypes.BYTE, (byte) (oldValue | (value == null ? 0 : 0x40)))); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/GlowColorPropertySerializer.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/GlowColorPropertySerializer.java new file mode 100644 index 0000000..c7f2d4f --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/GlowColorPropertySerializer.java @@ -0,0 +1,22 @@ +package lol.pyr.znpcsplus.entity.serializers; + +import lol.pyr.znpcsplus.entity.PropertySerializer; +import lol.pyr.znpcsplus.util.GlowColor; +import net.kyori.adventure.text.format.NamedTextColor; + +public class GlowColorPropertySerializer implements PropertySerializer { + @Override + public String serialize(GlowColor property) { + return String.valueOf(property.value()); + } + + @Override + public GlowColor deserialize(String property) { + return GlowColor.namedColor(Integer.parseInt(property)); + } + + @Override + public Class getTypeClass() { + return GlowColor.class; + } +} \ No newline at end of file diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/NamedTextColorPropertySerializer.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/NamedTextColorPropertySerializer.java index 49eea80..8916ae4 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/NamedTextColorPropertySerializer.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/NamedTextColorPropertySerializer.java @@ -3,6 +3,7 @@ package lol.pyr.znpcsplus.entity.serializers; import lol.pyr.znpcsplus.entity.PropertySerializer; import net.kyori.adventure.text.format.NamedTextColor; +@Deprecated public class NamedTextColorPropertySerializer implements PropertySerializer { @Override public String serialize(NamedTextColor property) { diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java index e1816e0..2380fdd 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/hologram/HologramImpl.java @@ -45,7 +45,7 @@ public class HologramImpl extends Viewable implements Hologram { } public void addTextLine(String line) { - addTextLineComponent(textSerializer.deserialize(textSerializer.serialize(MiniMessage.miniMessage().deserialize(line)))); + addTextLineComponent(Component.text(line)); } public void addItemLineStack(org.bukkit.inventory.ItemStack item) { diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java index 462a99d..32dd8a5 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java @@ -4,6 +4,7 @@ import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.player.Equipment; import lol.pyr.znpcsplus.api.entity.PropertyHolder; import lol.pyr.znpcsplus.entity.PacketEntity; +import lol.pyr.znpcsplus.util.GlowColor; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.entity.Player; @@ -17,7 +18,7 @@ public interface PacketFactory { void teleportEntity(Player player, PacketEntity entity); CompletableFuture addTabPlayer(Player player, PacketEntity entity, PropertyHolder properties); void removeTabPlayer(Player player, PacketEntity entity); - void createTeam(Player player, PacketEntity entity, NamedTextColor glowColor); + void createTeam(Player player, PacketEntity entity, GlowColor glowColor); void removeTeam(Player player, PacketEntity entity); void sendAllMetadata(Player player, PacketEntity entity, PropertyHolder properties); void sendEquipment(Player player, PacketEntity entity, Equipment equipment); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_17PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_17PacketFactory.java index dcb4c27..5a26b85 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_17PacketFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_17PacketFactory.java @@ -8,6 +8,7 @@ import lol.pyr.znpcsplus.config.ConfigManager; import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl; import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.scheduling.TaskScheduler; +import lol.pyr.znpcsplus.util.GlowColor; import lol.pyr.znpcsplus.util.NpcLocation; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; @@ -27,6 +28,6 @@ public class V1_17PacketFactory extends V1_8PacketFactory { sendPacket(player, new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.of(entity.getUuid()), entity.getType(), npcLocationToVector(location), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.of(new Vector3d()))); sendAllMetadata(player, entity, properties); - createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedTextColor.class))); + createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", GlowColor.class))); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_20_2PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_20_2PacketFactory.java index 6b44216..bfdf5bd 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_20_2PacketFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_20_2PacketFactory.java @@ -9,6 +9,7 @@ import lol.pyr.znpcsplus.config.ConfigManager; import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl; import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.scheduling.TaskScheduler; +import lol.pyr.znpcsplus.util.GlowColor; import lol.pyr.znpcsplus.util.NpcLocation; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; @@ -28,7 +29,7 @@ public class V1_20_2PacketFactory extends V1_19_2PacketFactory { @Override public void spawnPlayer(Player player, PacketEntity entity, PropertyHolder properties) { addTabPlayer(player, entity, properties).thenAccept(ignored -> { - createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedTextColor.class))); + createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", GlowColor.class))); NpcLocation location = entity.getLocation(); sendPacket(player, new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.of(entity.getUuid()), entity.getType(), npcLocationToVector(location), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.of(new Vector3d()))); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java index 1bee6a7..8aa3a08 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/V1_8PacketFactory.java @@ -20,6 +20,7 @@ import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl; import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.scheduling.TaskScheduler; import lol.pyr.znpcsplus.skin.BaseSkinDescriptor; +import lol.pyr.znpcsplus.util.GlowColor; import lol.pyr.znpcsplus.util.NpcLocation; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -48,7 +49,7 @@ public class V1_8PacketFactory implements PacketFactory { @Override public void spawnPlayer(Player player, PacketEntity entity, PropertyHolder properties) { addTabPlayer(player, entity, properties).thenAccept(ignored -> { - createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedTextColor.class))); + createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", GlowColor.class))); NpcLocation location = entity.getLocation(); sendPacket(player, new WrapperPlayServerSpawnPlayer(entity.getEntityId(), entity.getUuid(), npcLocationToVector(location), location.getYaw(), location.getPitch(), Collections.emptyList())); @@ -69,7 +70,7 @@ public class V1_8PacketFactory implements PacketFactory { new WrapperPlayServerSpawnEntity(entity.getEntityId(), Optional.of(entity.getUuid()), entity.getType(), npcLocationToVector(location), location.getPitch(), location.getYaw(), location.getYaw(), 0, Optional.empty())); sendAllMetadata(player, entity, properties); - createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", NamedTextColor.class))); + createTeam(player, entity, properties.getProperty(propertyRegistry.getByName("glow", GlowColor.class))); } protected Vector3d npcLocationToVector(NpcLocation location) { @@ -111,12 +112,12 @@ public class V1_8PacketFactory implements PacketFactory { } @Override - public void createTeam(Player player, PacketEntity entity, NamedTextColor glowColor) { + public void createTeam(Player player, PacketEntity entity, GlowColor glowColor) { sendPacket(player, new WrapperPlayServerTeams("npc_team_" + entity.getEntityId(), WrapperPlayServerTeams.TeamMode.CREATE, new WrapperPlayServerTeams.ScoreBoardTeamInfo( Component.empty(), Component.empty(), Component.empty(), WrapperPlayServerTeams.NameTagVisibility.NEVER, WrapperPlayServerTeams.CollisionRule.NEVER, - glowColor == null ? NamedTextColor.WHITE : glowColor, + glowColor == null ? NamedTextColor.WHITE : glowColor.toNamedTextColor(), WrapperPlayServerTeams.OptionData.NONE ))); sendPacket(player, new WrapperPlayServerTeams("npc_team_" + entity.getEntityId(), WrapperPlayServerTeams.TeamMode.ADD_ENTITIES, (WrapperPlayServerTeams.ScoreBoardTeamInfo) null, diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/parsers/GlowColorParser.java b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/GlowColorParser.java new file mode 100644 index 0000000..1210bb5 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/GlowColorParser.java @@ -0,0 +1,21 @@ +package lol.pyr.znpcsplus.parsers; + +import lol.pyr.director.adventure.command.CommandContext; +import lol.pyr.director.adventure.parse.ParserType; +import lol.pyr.director.common.command.CommandExecutionException; +import lol.pyr.director.common.message.Message; +import lol.pyr.znpcsplus.util.GlowColor; +import java.util.Deque; + +public class GlowColorParser extends ParserType { + public GlowColorParser(Message message) { + super(message); + } + + @Override + public GlowColor parse(Deque deque) throws CommandExecutionException { + GlowColor color = GlowColor.NAMES.value(deque.pop()); + if (color == null) throw new CommandExecutionException(); + return color; + } +} \ No newline at end of file diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/parsers/NamedTextColorParser.java b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/NamedTextColorParser.java index f399d47..604f74d 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/parsers/NamedTextColorParser.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/NamedTextColorParser.java @@ -8,6 +8,7 @@ import net.kyori.adventure.text.format.NamedTextColor; import java.util.Deque; +@Deprecated public class NamedTextColorParser extends ParserType { public NamedTextColorParser(Message message) { super(message); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/util/GlowColor.java b/plugin/src/main/java/lol/pyr/znpcsplus/util/GlowColor.java new file mode 100644 index 0000000..fd0fb68 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/util/GlowColor.java @@ -0,0 +1,252 @@ +package lol.pyr.znpcsplus.util; + +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.util.HSVLike; +import net.kyori.adventure.util.Index; +import net.kyori.examination.ExaminableProperty; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Stream; + +public class GlowColor implements TextColor { + private static final int BLACK_VALUE = 0x000000; + private static final int DARK_BLUE_VALUE = 0x0000aa; + private static final int DARK_GREEN_VALUE = 0x00aa00; + private static final int DARK_AQUA_VALUE = 0x00aaaa; + private static final int DARK_RED_VALUE = 0xaa0000; + private static final int DARK_PURPLE_VALUE = 0xaa00aa; + private static final int GOLD_VALUE = 0xffaa00; + private static final int GRAY_VALUE = 0xaaaaaa; + private static final int DARK_GRAY_VALUE = 0x555555; + private static final int BLUE_VALUE = 0x5555ff; + private static final int GREEN_VALUE = 0x55ff55; + private static final int AQUA_VALUE = 0x55ffff; + private static final int RED_VALUE = 0xff5555; + private static final int LIGHT_PURPLE_VALUE = 0xff55ff; + private static final int YELLOW_VALUE = 0xffff55; + private static final int WHITE_VALUE = 0xffffff; + + /** + * The standard {@code black} colour. + * + * @since 4.0.0 + */ + public static final GlowColor BLACK = new GlowColor("black", BLACK_VALUE); + /** + * The standard {@code dark_blue} colour. + * + * @since 4.0.0 + */ + public static final GlowColor DARK_BLUE = new GlowColor("dark_blue", DARK_BLUE_VALUE); + /** + * The standard {@code dark_green} colour. + * + * @since 4.0.0 + */ + public static final GlowColor DARK_GREEN = new GlowColor("dark_green", DARK_GREEN_VALUE); + /** + * The standard {@code dark_aqua} colour. + * + * @since 4.0.0 + */ + public static final GlowColor DARK_AQUA = new GlowColor("dark_aqua", DARK_AQUA_VALUE); + /** + * The standard {@code dark_red} colour. + * + * @since 4.0.0 + */ + public static final GlowColor DARK_RED = new GlowColor("dark_red", DARK_RED_VALUE); + /** + * The standard {@code dark_purple} colour. + * + * @since 4.0.0 + */ + public static final GlowColor DARK_PURPLE = new GlowColor("dark_purple", DARK_PURPLE_VALUE); + /** + * The standard {@code gold} colour. + * + * @since 4.0.0 + */ + public static final GlowColor GOLD = new GlowColor("gold", GOLD_VALUE); + /** + * The standard {@code gray} colour. + * + * @since 4.0.0 + */ + public static final GlowColor GRAY = new GlowColor("gray", GRAY_VALUE); + /** + * The standard {@code dark_gray} colour. + * + * @since 4.0.0 + */ + public static final GlowColor DARK_GRAY = new GlowColor("dark_gray", DARK_GRAY_VALUE); + /** + * The standard {@code blue} colour. + * + * @since 4.0.0 + */ + public static final GlowColor BLUE = new GlowColor("blue", BLUE_VALUE); + /** + * The standard {@code green} colour. + * + * @since 4.0.0 + */ + public static final GlowColor GREEN = new GlowColor("green", GREEN_VALUE); + /** + * The standard {@code aqua} colour. + * + * @since 4.0.0 + */ + public static final GlowColor AQUA = new GlowColor("aqua", AQUA_VALUE); + /** + * The standard {@code red} colour. + * + * @since 4.0.0 + */ + public static final GlowColor RED = new GlowColor("red", RED_VALUE); + /** + * The standard {@code light_purple} colour. + * + * @since 4.0.0 + */ + public static final GlowColor LIGHT_PURPLE = new GlowColor("light_purple", LIGHT_PURPLE_VALUE); + /** + * The standard {@code yellow} colour. + * + * @since 4.0.0 + */ + public static final GlowColor YELLOW = new GlowColor("yellow", YELLOW_VALUE); + /** + * The standard {@code white} colour. + * + * @since 4.0.0 + */ + public static final GlowColor WHITE = new GlowColor("white", WHITE_VALUE); + + private static final List VALUES = Collections.unmodifiableList(Arrays.asList(BLACK, DARK_BLUE, DARK_GREEN, DARK_AQUA, DARK_RED, DARK_PURPLE, GOLD, GRAY, DARK_GRAY, BLUE, GREEN, AQUA, RED, LIGHT_PURPLE, YELLOW, WHITE)); + /** + * An index of name to color. + * + * @since 4.0.0 + */ + public static final Index NAMES = Index.create(constant -> constant.name, VALUES); + + /** + * Gets the named color exactly matching the provided color. + * + * @param value the color to match + * @return the matched color, or null + * @since 4.10.0 + */ + public static @Nullable GlowColor namedColor(final int value) { + switch (value) { + case BLACK_VALUE: return BLACK; + case DARK_BLUE_VALUE: return DARK_BLUE; + case DARK_GREEN_VALUE: return DARK_GREEN; + case DARK_AQUA_VALUE: return DARK_AQUA; + case DARK_RED_VALUE: return DARK_RED; + case DARK_PURPLE_VALUE: return DARK_PURPLE; + case GOLD_VALUE: return GOLD; + case GRAY_VALUE: return GRAY; + case DARK_GRAY_VALUE: return DARK_GRAY; + case BLUE_VALUE: return BLUE; + case GREEN_VALUE: return GREEN; + case AQUA_VALUE: return AQUA; + case RED_VALUE: return RED; + case LIGHT_PURPLE_VALUE: return LIGHT_PURPLE; + case YELLOW_VALUE: return YELLOW; + case WHITE_VALUE: return WHITE; + default: return null; + } + } + + /** + * Gets the named color exactly matching the provided color. + * + * @param value the color to match + * @return the matched color, or null + * @since 4.0.0 + * @deprecated for removal since 4.10.0, use {@link #namedColor(int)} instead + */ + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "5.0.0") + public static @Nullable GlowColor ofExact(final int value) { + switch (value) { + case BLACK_VALUE: return BLACK; + case DARK_BLUE_VALUE: return DARK_BLUE; + case DARK_GREEN_VALUE: return DARK_GREEN; + case DARK_AQUA_VALUE: return DARK_AQUA; + case DARK_RED_VALUE: return DARK_RED; + case DARK_PURPLE_VALUE: return DARK_PURPLE; + case GOLD_VALUE: return GOLD; + case GRAY_VALUE: return GRAY; + case DARK_GRAY_VALUE: return DARK_GRAY; + case BLUE_VALUE: return BLUE; + case GREEN_VALUE: return GREEN; + case AQUA_VALUE: return AQUA; + case RED_VALUE: return RED; + case LIGHT_PURPLE_VALUE: return LIGHT_PURPLE; + case YELLOW_VALUE: return YELLOW; + case WHITE_VALUE: return WHITE; + default: return null; + } + } + + /** + * Find the named colour nearest to the provided colour. + * + * @param any colour to match + * @return nearest named colour. will always return a value + * @since 4.0.0 + */ + public static @NotNull GlowColor nearestTo(final @NotNull TextColor any) { + if (any instanceof NamedTextColor) { + return (GlowColor) any; + } + + return TextColor.nearestColorTo(VALUES, any); + } + + private final String name; + private final int value; + private final HSVLike hsv; + + private GlowColor(final String name, final int value) { + this.name = name; + this.value = value; + this.hsv = HSVLike.fromRGB(this.red(), this.green(), this.blue()); + } + + @Override + public int value() { + return this.value; + } + + public NamedTextColor toNamedTextColor() { + return NamedTextColor.namedColor(this.value); + } + + @Override + public @NotNull HSVLike asHSV() { + return this.hsv; + } + + @Override + public @NotNull String toString() { + return this.name; + } + + @Override + public @NotNull Stream examinableProperties() { + return Stream.concat( + Stream.of(ExaminableProperty.of("name", this.name)), + TextColor.super.examinableProperties() + ); + } +}