diff --git a/api/src/main/java/lol/pyr/znpcsplus/util/NamedColor.java b/api/src/main/java/lol/pyr/znpcsplus/util/NamedColor.java new file mode 100644 index 0000000..42147d6 --- /dev/null +++ b/api/src/main/java/lol/pyr/znpcsplus/util/NamedColor.java @@ -0,0 +1,20 @@ +package lol.pyr.znpcsplus.util; + +public enum NamedColor { + BLACK, + DARK_BLUE, + DARK_GREEN, + DARK_AQUA, + DARK_RED, + DARK_PURPLE, + GOLD, + GRAY, + DARK_GRAY, + BLUE, + GREEN, + AQUA, + RED, + LIGHT_PURPLE, + YELLOW, + WHITE +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 5aaa60a..46addce 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -246,7 +246,7 @@ public class ZNpcsPlus { manager.registerParser(Double.class, new DoubleParser(incorrectUsageMessage)); manager.registerParser(Float.class, new FloatParser(incorrectUsageMessage)); manager.registerParser(Boolean.class, new BooleanParser(incorrectUsageMessage)); - manager.registerParser(NamedTextColor.class, new NamedTextColorParser(incorrectUsageMessage)); + manager.registerParser(NamedColor.class, new NamedColorParser(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/commands/property/PropertySetCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertySetCommand.java index d5ff9dd..334d7f6 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertySetCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertySetCommand.java @@ -57,7 +57,7 @@ public class PropertySetCommand implements CommandHandler { valueName = bukkitStack.toString(); } } - else if (type == NamedTextColor.class && context.argSize() < 1 && npc.getProperty(property) != null) { + else if (type == NamedColor.class && context.argSize() < 1 && npc.getProperty(property) != null) { value = null; valueName = "NONE"; } @@ -151,7 +151,7 @@ public class PropertySetCommand implements CommandHandler { if (type == Vector3f.class && context.argSize() <= 5) return context.suggestLiteral("0", "0.0"); if (context.argSize() == 3) { if (type == Boolean.class) return context.suggestLiteral("true", "false"); - if (type == NamedTextColor.class) return context.suggestCollection(NamedTextColor.NAMES.keys()); + if (type == NamedColor.class) return context.suggestEnum(NamedColor.values()); if (type == Color.class) return context.suggestLiteral("0x0F00FF", "#FFFFFF"); if (type == BlockState.class) return context.suggestLiteral("hand", "looking_at", "block"); if (type == SpellType.class) return PacketEvents.getAPI().getServerManager().getVersion().isOlderThan(ServerVersion.V_1_13) ? 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..b011137 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 NamedColorPropertySerializer()); registerSerializer(new SkinDescriptorSerializer(skinCache)); registerSerializer(new ItemStackPropertySerializer()); registerSerializer(new ColorPropertySerializer()); @@ -273,7 +273,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { else if (ver.isNewerThanOrEquals(ServerVersion.V_1_9)) guardianIndex = 11; else guardianIndex = 16; register(new BitsetProperty("is_elder", guardianIndex, 0x04, false, legacyBooleans)); - register(new BitsetProperty("is_retracting_spikes", guardianIndex++, 0x02, false, legacyBooleans)); + register(new BitsetProperty("is_retracting_spikes", guardianIndex, 0x02, false, legacyBooleans)); linkProperties("is_elder", "is_retracting_spikes"); // TODO: add guardian beam target } else { 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..e32f61d 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.NamedColor; 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, NamedColor.class); this.packetFactory = packetFactory; } @Override public void apply(Player player, PacketEntity entity, boolean isSpawned, Map properties) { - NamedTextColor value = entity.getProperty(this); + NamedColor 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/NamedColorPropertySerializer.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/NamedColorPropertySerializer.java new file mode 100644 index 0000000..a227ae7 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/NamedColorPropertySerializer.java @@ -0,0 +1,25 @@ +package lol.pyr.znpcsplus.entity.serializers; + +import lol.pyr.znpcsplus.entity.PropertySerializer; +import lol.pyr.znpcsplus.util.NamedColor; + +public class NamedColorPropertySerializer implements PropertySerializer { + @Override + public String serialize(NamedColor property) { + return property.name(); + } + + @Override + public NamedColor deserialize(String property) { + try { + return NamedColor.valueOf(property.toUpperCase()); + } catch (IllegalArgumentException exception) { + return NamedColor.WHITE; + } + } + + @Override + public Class getTypeClass() { + return NamedColor.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 deleted file mode 100644 index 49eea80..0000000 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/NamedTextColorPropertySerializer.java +++ /dev/null @@ -1,21 +0,0 @@ -package lol.pyr.znpcsplus.entity.serializers; - -import lol.pyr.znpcsplus.entity.PropertySerializer; -import net.kyori.adventure.text.format.NamedTextColor; - -public class NamedTextColorPropertySerializer implements PropertySerializer { - @Override - public String serialize(NamedTextColor property) { - return String.valueOf(property.value()); - } - - @Override - public NamedTextColor deserialize(String property) { - return NamedTextColor.namedColor(Integer.parseInt(property)); - } - - @Override - public Class getTypeClass() { - return NamedTextColor.class; - } -} 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..6fe3fde 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/packets/PacketFactory.java @@ -4,7 +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 net.kyori.adventure.text.format.NamedTextColor; +import lol.pyr.znpcsplus.util.NamedColor; import org.bukkit.entity.Player; import java.util.List; @@ -17,7 +17,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, NamedColor namedColor); 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..45b6e41 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,8 +8,8 @@ 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.NamedColor; import lol.pyr.znpcsplus.util.NpcLocation; -import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -27,6 +27,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", NamedColor.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..761476f 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,8 +9,8 @@ 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.NamedColor; import lol.pyr.znpcsplus.util.NpcLocation; -import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -28,7 +28,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", NamedColor.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..4eb4df2 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.NamedColor; 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", NamedColor.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", NamedColor.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, NamedColor namedColor) { 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, + namedColor == null ? NamedTextColor.WHITE : NamedTextColor.NAMES.value(namedColor.name().toLowerCase()), 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/NamedColorParser.java b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/NamedColorParser.java new file mode 100644 index 0000000..33451a0 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/NamedColorParser.java @@ -0,0 +1,23 @@ +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.NamedColor; +import java.util.Deque; + +public class NamedColorParser extends ParserType { + public NamedColorParser(Message message) { + super(message); + } + + @Override + public NamedColor parse(Deque deque) throws CommandExecutionException { + try { + return NamedColor.valueOf(deque.pop()); + } catch (IllegalArgumentException exception) { + throw new CommandExecutionException(); + } + } +} \ 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 deleted file mode 100644 index f399d47..0000000 --- a/plugin/src/main/java/lol/pyr/znpcsplus/parsers/NamedTextColorParser.java +++ /dev/null @@ -1,22 +0,0 @@ -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 net.kyori.adventure.text.format.NamedTextColor; - -import java.util.Deque; - -public class NamedTextColorParser extends ParserType { - public NamedTextColorParser(Message message) { - super(message); - } - - @Override - public NamedTextColor parse(Deque deque) throws CommandExecutionException { - NamedTextColor color = NamedTextColor.NAMES.value(deque.pop()); - if (color == null) throw new CommandExecutionException(); - return color; - } -}