From 446761c52c36252dd8a798d669631aa7a5523143 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Wed, 23 Aug 2023 12:14:57 +0200 Subject: [PATCH] action clear command --- .../java/lol/pyr/znpcsplus/ZNpcsPlus.java | 6 ++-- .../commands/action/ActionClearCommand.java | 36 +++++++++++++++++++ .../java/lol/pyr/znpcsplus/npc/NpcImpl.java | 4 +++ .../main/resources/help-messages/action.txt | 1 + 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionClearCommand.java diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java index a9715c1..09d7226 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/ZNpcsPlus.java @@ -15,10 +15,7 @@ import lol.pyr.director.common.message.Message; import lol.pyr.znpcsplus.api.NpcApiProvider; import lol.pyr.znpcsplus.api.interaction.InteractionType; import lol.pyr.znpcsplus.commands.*; -import lol.pyr.znpcsplus.commands.action.ActionAddCommand; -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.action.*; import lol.pyr.znpcsplus.commands.hologram.*; import lol.pyr.znpcsplus.commands.property.PropertyRemoveCommand; import lol.pyr.znpcsplus.commands.property.PropertySetCommand; @@ -321,6 +318,7 @@ public class ZNpcsPlus extends JavaPlugin { .addSubcommand("refreshdelay", new HoloRefreshDelayCommand(npcRegistry))) .addSubcommand("action", new MultiCommand(loadHelpMessage("action")) .addSubcommand("add", new ActionAddCommand(npcRegistry, actionRegistry)) + .addSubcommand("clear", new ActionClearCommand(npcRegistry)) .addSubcommand("delete", new ActionDeleteCommand(npcRegistry)) .addSubcommand("edit", new ActionEditCommand(npcRegistry, actionRegistry)) .addSubcommand("list", new ActionListCommand(npcRegistry))) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionClearCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionClearCommand.java new file mode 100644 index 0000000..2aa709a --- /dev/null +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/action/ActionClearCommand.java @@ -0,0 +1,36 @@ +package lol.pyr.znpcsplus.commands.action; + +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.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 ActionClearCommand implements CommandHandler { + private final NpcRegistryImpl npcRegistry; + + public ActionClearCommand(NpcRegistryImpl npcRegistry) { + this.npcRegistry = npcRegistry; + } + + @Override + public void run(CommandContext context) throws CommandExecutionException { + context.setUsage(context.getLabel() + " action clear "); + NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); + if (npc.getActions().size() == 0) context.halt(Component.text("That npc doesn't have any actions", NamedTextColor.RED)); + npc.clearActions(); + context.send(Component.text("Removed all actions from the npc", NamedTextColor.GREEN)); + } + + @Override + public List suggest(CommandContext context) throws CommandExecutionException { + if (context.argSize() == 1) return context.suggestCollection(npcRegistry.getModifiableIds()); + return Collections.emptyList(); + } +} diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java index eb54de6..78f76b8 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/npc/NpcImpl.java @@ -171,6 +171,10 @@ public class NpcImpl extends Viewable implements Npc { actions.remove(index); } + public void clearActions() { + actions.clear(); + } + public void addAction(InteractionActionImpl action) { actions.add(action); } diff --git a/plugin/src/main/resources/help-messages/action.txt b/plugin/src/main/resources/help-messages/action.txt index efa6835..eb63aa1 100644 --- a/plugin/src/main/resources/help-messages/action.txt +++ b/plugin/src/main/resources/help-messages/action.txt @@ -3,6 +3,7 @@ Hover over any command for more info * /npc action add + * /npc action clear * /npc action delete * /npc action edit * /npc action list