From 6dab7a13db9168a366d34577fdb45ef8e772a753 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Wed, 28 Jun 2023 00:31:47 +0200 Subject: [PATCH] add explicit property remove command --- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 6 ++- .../property/PropertyRemoveCommand.java | 42 +++++++++++++++++++ .../PropertySetCommand.java} | 10 ++--- .../main/resources/help-messages/property.txt | 7 ++++ .../src/main/resources/help-messages/root.txt | 2 +- 5 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertyRemoveCommand.java rename plugin/src/main/java/lol/pyr/znpcsplus/commands/{PropertyCommand.java => property/PropertySetCommand.java} (91%) create mode 100644 plugin/src/main/resources/help-messages/property.txt diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index 9c21191..ab1760d 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -20,6 +20,8 @@ import lol.pyr.znpcsplus.commands.action.ActionDeleteCommand; import lol.pyr.znpcsplus.commands.action.ActionEditCommand; import lol.pyr.znpcsplus.commands.action.ActionListCommand; import lol.pyr.znpcsplus.commands.hologram.*; +import lol.pyr.znpcsplus.commands.property.PropertyRemoveCommand; +import lol.pyr.znpcsplus.commands.property.PropertySetCommand; import lol.pyr.znpcsplus.commands.storage.ImportCommand; import lol.pyr.znpcsplus.commands.storage.LoadAllCommand; import lol.pyr.znpcsplus.commands.storage.SaveAllCommand; @@ -283,11 +285,13 @@ public class ZNpcsPlus extends JavaPlugin { .addSubcommand("skin", new SkinCommand(skinCache, npcRegistry, typeRegistry, propertyRegistry)) .addSubcommand("delete", new DeleteCommand(npcRegistry, adventure)) .addSubcommand("move", new MoveCommand(npcRegistry)) - .addSubcommand("property", new PropertyCommand(npcRegistry)) .addSubcommand("teleport", new TeleportCommand(npcRegistry)) .addSubcommand("list", new ListCommand(npcRegistry)) .addSubcommand("near", new NearCommand(npcRegistry)) .addSubcommand("type", new TypeCommand(npcRegistry, typeRegistry)) + .addSubcommand("property", new MultiCommand(loadHelpMessage("property")) + .addSubcommand("set", new PropertySetCommand(npcRegistry)) + .addSubcommand("remove", new PropertyRemoveCommand(npcRegistry))) .addSubcommand("storage", new MultiCommand(loadHelpMessage("storage")) .addSubcommand("save", new SaveAllCommand(npcRegistry)) .addSubcommand("reload", new LoadAllCommand(npcRegistry)) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertyRemoveCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertyRemoveCommand.java new file mode 100644 index 0000000..4bf648d --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertyRemoveCommand.java @@ -0,0 +1,42 @@ +package lol.pyr.znpcsplus.commands.property; + +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.api.entity.EntityProperty; +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 net.kyori.adventure.text.format.NamedTextColor; + +import java.util.Collections; +import java.util.List; + +public class PropertyRemoveCommand implements CommandHandler { + private final NpcRegistryImpl npcRegistry; + + public PropertyRemoveCommand(NpcRegistryImpl npcRegistry) { + this.npcRegistry = npcRegistry; + } + + @Override + public void run(CommandContext context) throws CommandExecutionException { + context.setUsage(context.getLabel() + " property remove "); + NpcEntryImpl entry = context.parse(NpcEntryImpl.class); + NpcImpl npc = entry.getNpc(); + EntityPropertyImpl property = context.parse(EntityPropertyImpl.class); + if (!npc.hasProperty(property)) context.halt(Component.text("This npc doesn't have the " + property.getName() + " property set", NamedTextColor.RED)); + npc.removeProperty(property); + context.send(Component.text("Removed property " + property.getName() + " from NPC " + entry.getId(), NamedTextColor.GREEN)); + } + + @Override + public List suggest(CommandContext context) throws CommandExecutionException { + if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); + if (context.argSize() == 2) return context.suggestStream(context.suggestionParse(0, NpcEntryImpl.class) + .getNpc().getAppliedProperties().stream().map(EntityProperty::getName)); + return Collections.emptyList(); + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertyCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertySetCommand.java similarity index 91% rename from plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertyCommand.java rename to plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertySetCommand.java index 7616bdf..43af13c 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/PropertyCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/property/PropertySetCommand.java @@ -1,4 +1,4 @@ -package lol.pyr.znpcsplus.commands; +package lol.pyr.znpcsplus.commands.property; import com.github.retrooper.packetevents.protocol.item.ItemStack; import io.github.retrooper.packetevents.util.SpigotConversionUtil; @@ -19,16 +19,16 @@ import org.bukkit.Color; import java.util.Collections; import java.util.List; -public class PropertyCommand implements CommandHandler { +public class PropertySetCommand implements CommandHandler { private final NpcRegistryImpl npcRegistry; - public PropertyCommand(NpcRegistryImpl npcRegistry) { + public PropertySetCommand(NpcRegistryImpl npcRegistry) { this.npcRegistry = npcRegistry; } @Override public void run(CommandContext context) throws CommandExecutionException { - context.setUsage(context.getLabel() + " property "); + context.setUsage(context.getLabel() + " property set "); NpcEntryImpl entry = context.parse(NpcEntryImpl.class); NpcImpl npc = entry.getNpc(); EntityPropertyImpl property = context.parse(EntityPropertyImpl.class); @@ -70,7 +70,7 @@ public class PropertyCommand implements CommandHandler { public List suggest(CommandContext context) throws CommandExecutionException { if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); if (context.argSize() == 2) return context.suggestStream(context.suggestionParse(0, NpcEntryImpl.class) - .getNpc().getType().getAllowedProperties().stream().map(EntityProperty::getName)); + .getNpc().getType().getAllowedProperties().stream().map(EntityProperty::getName)); if (context.argSize() >= 3) { EntityPropertyImpl property = context.suggestionParse(1, EntityPropertyImpl.class); Class type = property.getType(); diff --git a/plugin/src/main/resources/help-messages/property.txt b/plugin/src/main/resources/help-messages/property.txt new file mode 100644 index 0000000..61671d7 --- /dev/null +++ b/plugin/src/main/resources/help-messages/property.txt @@ -0,0 +1,7 @@ + +ZNPCsPlus v${version} Click to view the main help message'>[BACK] +Hover over any command more info + + * /npc property set + * /npc property remove + diff --git a/plugin/src/main/resources/help-messages/root.txt b/plugin/src/main/resources/help-messages/root.txt index a0daed0..ba24f1d 100644 --- a/plugin/src/main/resources/help-messages/root.txt +++ b/plugin/src/main/resources/help-messages/root.txt @@ -12,9 +12,9 @@ * /npc move * /npc teleport - * /npc property * /npc skin + * Npc property commands
Click to view full list'>/npc property help * Npc hologram commands
Click to view full list'>/npc holo help * Player interaction commands
Click to view full list'>/npc action help * Npc data storage commands
Click to view full list'>/npc storage help