From 5a659956995297057060ab6d1458c700541fcd8d Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Thu, 11 May 2023 05:41:15 +0100 Subject: [PATCH] properties --- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 4 ++++ .../znpcsplus/commands/PropertiesCommand.java | 19 ++++++++++++++- .../parsers/EntityPropertyParser.java | 23 +++++++++++++++++++ .../znpcsplus/entity/EntityPropertyImpl.java | 21 +++++++++++++---- .../znpcsplus/metadata/MetadataFactory.java | 4 ++-- 5 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/EntityPropertyParser.java diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index ffa632f..e1feea1 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -6,10 +6,12 @@ import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder; import lol.pyr.director.adventure.command.CommandManager; import lol.pyr.director.adventure.command.MultiCommand; +import lol.pyr.director.adventure.parse.primitive.BooleanParser; import lol.pyr.director.adventure.parse.primitive.IntegerParser; import lol.pyr.znpcsplus.api.ZApiProvider; import lol.pyr.znpcsplus.commands.*; import lol.pyr.znpcsplus.commands.hologram.*; +import lol.pyr.znpcsplus.commands.parsers.EntityPropertyParser; import lol.pyr.znpcsplus.commands.parsers.NpcEntryParser; import lol.pyr.znpcsplus.commands.parsers.NpcTypeParser; import lol.pyr.znpcsplus.config.Configs; @@ -192,7 +194,9 @@ public class ZNpcsPlus extends JavaPlugin { CommandManager manager = new CommandManager(this, ADVENTURE, context -> {}); manager.registerParser(NpcTypeImpl.class, new NpcTypeParser(context -> {})); manager.registerParser(NpcEntryImpl.class, new NpcEntryParser(context -> {})); + manager.registerParser(EntityPropertyImpl.class, new EntityPropertyParser(context -> {})); manager.registerParser(Integer.class, new IntegerParser(context -> {})); + manager.registerParser(Boolean.class, new BooleanParser(context -> {})); manager.registerCommand("npc", new MultiCommand() .addSubcommand("action", new ActionCommand()) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertiesCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertiesCommand.java index b9d27b1..3dbc200 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertiesCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertiesCommand.java @@ -3,6 +3,11 @@ package lol.pyr.znpcsplus.commands; import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.adventure.command.CommandHandler; import lol.pyr.director.common.command.CommandExecutionException; +import lol.pyr.znpcsplus.entity.EntityPropertyImpl; +import lol.pyr.znpcsplus.npc.NpcEntryImpl; +import lol.pyr.znpcsplus.npc.NpcImpl; +import lol.pyr.znpcsplus.npc.NpcRegistryImpl; +import net.kyori.adventure.text.Component; import java.util.Collections; import java.util.List; @@ -10,11 +15,23 @@ import java.util.List; public class PropertiesCommand implements CommandHandler { @Override public void run(CommandContext context) throws CommandExecutionException { - context.send("Not implemented yet!"); + NpcEntryImpl entry = context.parse(NpcEntryImpl.class); + NpcImpl npc = entry.getNpc(); + EntityPropertyImpl property = context.parse(EntityPropertyImpl.class); + + if (!npc.getType().getAllowedProperties().contains(property)) context.halt(Component.text("Property " + property.getName() + " not allowed for npc type " + npc.getType().getName())); + + // TODO: implement all the parsers for the types used in EntityPropertyImpl + Object value = context.parse(property.getType()); + npc.UNSAFE_setProperty(property, value); + context.send(Component.text("Set property " + property.getName() + " for NPC " + entry.getId() + " to " + value.toString())); } @Override public List suggest(CommandContext context) throws CommandExecutionException { + if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds()); + if (context.argSize() == 2) return context.suggestStream(context.suggestionParse(1, NpcEntryImpl.class) + .getNpc().getType().getAllowedProperties().stream().map(EntityPropertyImpl::getName)); return Collections.emptyList(); } } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/EntityPropertyParser.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/EntityPropertyParser.java new file mode 100644 index 0000000..da54447 --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/parsers/EntityPropertyParser.java @@ -0,0 +1,23 @@ +package lol.pyr.znpcsplus.commands.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.entity.EntityPropertyImpl; + +import java.util.Deque; + +@SuppressWarnings("rawtypes") +public class EntityPropertyParser extends ParserType*/> { + public EntityPropertyParser(Message message) { + super(message); + } + + @Override + public EntityPropertyImpl parse(Deque deque) throws CommandExecutionException { + EntityPropertyImpl property = EntityPropertyImpl.getByName(deque.pop()); + if (property == null) throw new CommandExecutionException(); + return property; + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyImpl.java index 1cd654c..98b6274 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/entity/EntityPropertyImpl.java @@ -14,17 +14,24 @@ import java.util.Map; public class EntityPropertyImpl implements EntityProperty { private final String name; private final T defaultValue; + private final Class clazz; private final PropertySerializer serializer; private final PropertyDeserializer deserializer; - public EntityPropertyImpl(String name, PropertySerializer serializer, PropertyDeserializer deserializer) { - this(name, null, serializer, deserializer); + public EntityPropertyImpl(String name, Class type, PropertySerializer serializer, PropertyDeserializer deserializer) { + this(name, null, type, serializer, deserializer); } + @SuppressWarnings("unchecked") public EntityPropertyImpl(String name, T defaultValue, PropertySerializer serializer, PropertyDeserializer deserializer) { + this(name, defaultValue, (Class) defaultValue.getClass(), serializer, deserializer); + } + + private EntityPropertyImpl(String name, T defaultValue, Class clazz, PropertySerializer serializer, PropertyDeserializer deserializer) { this.name = name.toUpperCase(); this.defaultValue = defaultValue; + this.clazz = clazz; this.serializer = serializer; this.deserializer = deserializer; BY_NAME.put(this.name, this); @@ -51,6 +58,10 @@ public class EntityPropertyImpl implements EntityProperty { return defaultValue; } + public Class getType() { + return clazz; + } + private final static Map> BY_NAME = new HashMap<>(); public static EntityPropertyImpl getByName(String name) { @@ -81,10 +92,10 @@ public class EntityPropertyImpl implements EntityProperty { private final static PropertyDeserializer DESCRIPTOR_DESERIALIZER = BaseSkinDescriptor::deserialize; public static EntityPropertyImpl SKIN_LAYERS = new EntityPropertyImpl<>("skin_layers", true, BOOLEAN_SERIALIZER, BOOLEAN_DESERIALIZER); - public static EntityPropertyImpl SKIN = new EntityPropertyImpl<>("skin", DESCRIPTOR_SERIALIZER, DESCRIPTOR_DESERIALIZER); - public static EntityPropertyImpl GLOW = new EntityPropertyImpl<>("glow", COLOR_SERIALIZER, COLOR_DESERIALIZER); + public static EntityPropertyImpl SKIN = new EntityPropertyImpl<>("skin", SkinDescriptor.class, DESCRIPTOR_SERIALIZER, DESCRIPTOR_DESERIALIZER); + public static EntityPropertyImpl GLOW = new EntityPropertyImpl<>("glow", NamedTextColor.class, COLOR_SERIALIZER, COLOR_DESERIALIZER); public static EntityPropertyImpl FIRE = new EntityPropertyImpl<>("fire", false, BOOLEAN_SERIALIZER, BOOLEAN_DESERIALIZER); public static EntityPropertyImpl INVISIBLE = new EntityPropertyImpl<>("invisible", false, BOOLEAN_SERIALIZER, BOOLEAN_DESERIALIZER); public static EntityPropertyImpl SILENT = new EntityPropertyImpl<>("silent", false, BOOLEAN_SERIALIZER, BOOLEAN_DESERIALIZER); - public static EntityPropertyImpl NAME = new EntityPropertyImpl<>("name", COMPONENT_SERIALIZER, COMPONENT_DESERIALIZER); + public static EntityPropertyImpl NAME = new EntityPropertyImpl<>("name", Component.class, COMPONENT_SERIALIZER, COMPONENT_DESERIALIZER); } \ No newline at end of file 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 8007187..4fe1b85 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/metadata/MetadataFactory.java @@ -26,10 +26,10 @@ import java.util.Map; * 1.19 https://wiki.vg/index.php?title=Entity_metadata */ public interface MetadataFactory { - EntityData skinLayers(); + EntityData skinLayers(boolean enabled); EntityData effects(boolean onFire, boolean glowing, boolean invisible); Collection name(Component name); - EntityData silent(); + EntityData silent(boolean enabled); MetadataFactory factory = get();