From 6d4cd5199a44e3b2af2cb19455cc79a17f5d52f3 Mon Sep 17 00:00:00 2001 From: D3v1s0m <49519439+D3v1s0m@users.noreply.github.com> Date: Sun, 25 Jun 2023 13:05:28 +0530 Subject: [PATCH] Implemented potion color and potion ambient properties --- .../lol/pyr/znpcsplus/util/PotionColor.java | 31 +++++++++++++++++++ .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 1 + .../znpcsplus/commands/PropertyCommand.java | 6 ++++ .../entity/EntityPropertyRegistryImpl.java | 4 ++- .../PotionColorPropertySerializer.java | 21 +++++++++++++ .../znpcsplus/metadata/MetadataFactory.java | 2 ++ .../metadata/V1_10MetadataFactory.java | 10 ++++++ .../metadata/V1_14MetadataFactory.java | 10 ++++++ .../metadata/V1_17MetadataFactory.java | 10 ++++++ .../metadata/V1_8MetadataFactory.java | 10 ++++++ .../metadata/V1_9MetadataFactory.java | 5 +++ .../lol/pyr/znpcsplus/npc/NpcTypeImpl.java | 2 ++ .../znpcsplus/packets/V1_8PacketFactory.java | 3 ++ .../znpcsplus/parsers/PotionColorParser.java | 20 ++++++++++++ 14 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/lol/pyr/znpcsplus/util/PotionColor.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/PotionColorPropertySerializer.java create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/parsers/PotionColorParser.java diff --git a/api/src/main/java/lol/pyr/znpcsplus/util/PotionColor.java b/api/src/main/java/lol/pyr/znpcsplus/util/PotionColor.java new file mode 100644 index 0000000..fbc7997 --- /dev/null +++ b/api/src/main/java/lol/pyr/znpcsplus/util/PotionColor.java @@ -0,0 +1,31 @@ +package lol.pyr.znpcsplus.util; + +public class PotionColor { + + private final String color; + + public static PotionColor DEFAULT = new PotionColor("0"); + + public PotionColor(String color) { + this.color = color; + } + + public int getColor() { + if (color.startsWith("#")) { + if (color.length() != 7) + throw new IllegalArgumentException("Hex color must be 6 characters long"); + return Integer.parseInt(color.substring(1), 16); + } else if (color.startsWith("0x")) { + if (color.length() != 8) + throw new IllegalArgumentException("Hex color must be 6 characters long"); + return Integer.parseInt(color.substring(2), 16); + } else { + return Integer.parseInt(color); + } + } + + public String toString() { + return String.valueOf(color); + } + +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 68ad3de..31bc641 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -259,6 +259,7 @@ public class ZNpcsPlus extends JavaPlugin { manager.registerParser(NamedTextColor.class, new NamedTextColorParser(incorrectUsageMessage)); manager.registerParser(InteractionType.class, new InteractionTypeParser(incorrectUsageMessage)); manager.registerParser(NpcPose.class, new NpcPoseParser(incorrectUsageMessage)); + manager.registerParser(PotionColor.class, new PotionColorParser(incorrectUsageMessage)); manager.registerCommand("npc", new MultiCommand(loadHelpMessage("root")) .addSubcommand("create", new CreateCommand(npcRegistry, typeRegistry)) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertyCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertyCommand.java index e181f35..08121c2 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertyCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertyCommand.java @@ -11,6 +11,7 @@ import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import lol.pyr.znpcsplus.util.NpcPose; +import lol.pyr.znpcsplus.util.PotionColor; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -49,6 +50,10 @@ public class PropertyCommand implements CommandHandler { value = null; valueName = "NONE"; } + else if (type == PotionColor.class && context.argSize() < 1 && npc.getProperty(property) != null) { + value = PotionColor.DEFAULT; + valueName = "NONE"; + } else { value = context.parse(type); valueName = String.valueOf(value); @@ -69,6 +74,7 @@ public class PropertyCommand implements CommandHandler { if (type == Boolean.class) return context.suggestLiteral("true", "false"); if (type == NamedTextColor.class) return context.suggestCollection(NamedTextColor.NAMES.keys()); if (type == NpcPose.class) return context.suggestEnum(NpcPose.values()); + if (property.getName().equals("potion_color")) return context.suggestLiteral("0x0F00FF", "#FFFFFF", "16711935"); } return Collections.emptyList(); } 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 a27a42b..e792100 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyRegistryImpl.java @@ -7,6 +7,7 @@ import lol.pyr.znpcsplus.api.skin.SkinDescriptor; import lol.pyr.znpcsplus.entity.serializers.*; import lol.pyr.znpcsplus.skin.cache.SkinCache; import lol.pyr.znpcsplus.util.NpcPose; +import lol.pyr.znpcsplus.util.PotionColor; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -28,6 +29,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { registerSerializer(new SkinDescriptorSerializer(skinCache)); registerSerializer(new ItemStackPropertySerializer()); registerSerializer(new NpcPosePropertySerializer()); + registerSerializer(new PotionColorPropertySerializer()); registerType("glow", NamedTextColor.class); registerType("fire", false); @@ -46,7 +48,7 @@ public class EntityPropertyRegistryImpl implements EntityPropertyRegistry { registerType("offhand", ItemStack.class); registerType("using_item", false); // TODO: Eating/Drinking/Blocking with sword/etc - registerType("potion_color", 0xFFFFFF); // TODO + registerType("potion_color", PotionColor.class); // TODO registerType("potion_ambient", false); // TODO registerType("shaking", false); registerType("baby", false); // TODO diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/PotionColorPropertySerializer.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/PotionColorPropertySerializer.java new file mode 100644 index 0000000..8cd504d --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/serializers/PotionColorPropertySerializer.java @@ -0,0 +1,21 @@ +package lol.pyr.znpcsplus.entity.serializers; + +import lol.pyr.znpcsplus.util.PotionColor; +import lol.pyr.znpcsplus.entity.PropertySerializer; + +public class PotionColorPropertySerializer implements PropertySerializer { + @Override + public String serialize(PotionColor property) { + return property.toString(); + } + + @Override + public PotionColor deserialize(String property) { + return new PotionColor(property); + } + + @Override + public Class getTypeClass() { + return PotionColor.class; + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java index 1843e23..be1f708 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java @@ -27,4 +27,6 @@ public interface MetadataFactory { EntityData noGravity(); EntityData pose(EntityPose pose); EntityData shaking(boolean enabled); + EntityData potionColor(int color); + EntityData potionAmbient(boolean ambient); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_10MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_10MetadataFactory.java index 1004728..dfd6f96 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_10MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_10MetadataFactory.java @@ -8,4 +8,14 @@ public class V1_10MetadataFactory extends V1_9MetadataFactory { public EntityData noGravity() { return newEntityData(5, EntityDataTypes.BOOLEAN, true); } + + @Override + public EntityData potionColor(int color) { + return newEntityData(8, EntityDataTypes.INT, color); + } + + @Override + public EntityData potionAmbient(boolean ambient) { + return newEntityData(9, EntityDataTypes.BOOLEAN, ambient); + } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_14MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_14MetadataFactory.java index 6671efc..57a25a8 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_14MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_14MetadataFactory.java @@ -14,4 +14,14 @@ public class V1_14MetadataFactory extends V1_13MetadataFactory { public EntityData pose(EntityPose pose) { return newEntityData(6, EntityDataTypes.ENTITY_POSE, pose); } + + @Override + public EntityData potionColor(int color) { + return newEntityData(9, EntityDataTypes.INT, color); + } + + @Override + public EntityData potionAmbient(boolean ambient) { + return newEntityData(10, EntityDataTypes.BOOLEAN, ambient); + } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_17MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_17MetadataFactory.java index 137447b..f76687d 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_17MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_17MetadataFactory.java @@ -18,4 +18,14 @@ public class V1_17MetadataFactory extends V1_16MetadataFactory { public EntityData shaking(boolean enabled) { return newEntityData(7, EntityDataTypes.INT, enabled ? 140 : 0); } + + @Override + public EntityData potionColor(int color) { + return newEntityData(10, EntityDataTypes.INT, color); + } + + @Override + public EntityData potionAmbient(boolean ambient) { + return newEntityData(11, EntityDataTypes.BOOLEAN, ambient); + } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java index 8b79756..bd77ef8 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_8MetadataFactory.java @@ -45,6 +45,16 @@ public class V1_8MetadataFactory implements MetadataFactory { throw new UnsupportedOperationException("The shaking entity data isn't supported on this version"); } + @Override + public EntityData potionColor(int color) { + return newEntityData(7, EntityDataTypes.INT, color); + } + + @Override + public EntityData potionAmbient(boolean ambient) { + return newEntityData(8, EntityDataTypes.BYTE, (byte) (ambient ? 1 : 0)); + } + @Override public EntityData silent(boolean enabled) { return newEntityData(4, EntityDataTypes.BYTE, (byte) (enabled ? 1 : 0)); diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_9MetadataFactory.java b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_9MetadataFactory.java index 54cdb16..ad4d3c7 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_9MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/V1_9MetadataFactory.java @@ -18,6 +18,11 @@ public class V1_9MetadataFactory extends V1_8MetadataFactory { return newEntityData(0, EntityDataTypes.BYTE, (byte) ((onFire ? 0x01 : 0) | (invisible ? 0x20 : 0) | (glowing ? 0x40 : 0) | (usingElytra ? 0x80 : 0))); } + @Override + public EntityData potionAmbient(boolean ambient) { + return newEntityData(8, EntityDataTypes.BOOLEAN, ambient); + } + @Override public Collection name(Component name) { return list( diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java index 2aefc8a..8162aba 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcTypeImpl.java @@ -77,6 +77,8 @@ public class NpcTypeImpl implements NpcType { allowedProperties.add(propertyRegistry.getByName("silent")); allowedProperties.add(propertyRegistry.getByName("look")); allowedProperties.add(propertyRegistry.getByName("skin_cape")); + allowedProperties.add(propertyRegistry.getByName("potion_color")); + allowedProperties.add(propertyRegistry.getByName("potion_ambient")); if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_9)) allowedProperties.add(propertyRegistry.getByName("glow")); if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_14)) 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 3c6ca5f..3cf3b08 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 @@ -15,6 +15,7 @@ import lol.pyr.znpcsplus.api.skin.SkinDescriptor; import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl; import lol.pyr.znpcsplus.entity.PacketEntity; +import lol.pyr.znpcsplus.util.PotionColor; import lol.pyr.znpcsplus.metadata.MetadataFactory; import lol.pyr.znpcsplus.scheduling.TaskScheduler; import lol.pyr.znpcsplus.skin.BaseSkinDescriptor; @@ -142,6 +143,8 @@ public class V1_8PacketFactory implements PacketFactory { false) ); add(data, metadataFactory.silent(properties.getProperty(propertyRegistry.getByName("silent", Boolean.class)))); + add(data, metadataFactory.potionColor(properties.getProperty(propertyRegistry.getByName("potion_color", PotionColor.class)).getColor())); + add(data, metadataFactory.potionAmbient(properties.getProperty(propertyRegistry.getByName("potion_ambient", Boolean.class)))); if (properties.hasProperty(propertyRegistry.getByName("name"))) addAll(data, metadataFactory.name(properties.getProperty(propertyRegistry.getByName("name", Component.class)))); return data; } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/parsers/PotionColorParser.java b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/PotionColorParser.java new file mode 100644 index 0000000..fbd2f0a --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/parsers/PotionColorParser.java @@ -0,0 +1,20 @@ +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.PotionColor; + +import java.util.Deque; + +public class PotionColorParser extends ParserType { + public PotionColorParser(Message message) { + super(message); + } + + @Override + public PotionColor parse(Deque deque) throws CommandExecutionException { + return new PotionColor(deque.pop()); + } +}