From db869c0c5b0d8248ed30234a82bfd9f65144be4c Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 25 Feb 2022 22:16:11 +0100 Subject: [PATCH] Adds a tab completer for the list command --- .../net/knarcraft/paidsigns/PaidSigns.java | 3 +- .../paidsigns/command/AddCommand.java | 18 +++++++++++- .../paidsigns/command/ListTabCompleter.java | 29 +++++++++++++++++++ .../paidsigns/utility/TabCompleteHelper.java | 6 +++- 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java diff --git a/src/main/java/net/knarcraft/paidsigns/PaidSigns.java b/src/main/java/net/knarcraft/paidsigns/PaidSigns.java index 9f9b1e5..367eee1 100644 --- a/src/main/java/net/knarcraft/paidsigns/PaidSigns.java +++ b/src/main/java/net/knarcraft/paidsigns/PaidSigns.java @@ -5,6 +5,7 @@ import net.knarcraft.paidsigns.command.AddConditionCommand; import net.knarcraft.paidsigns.command.AddConditionTabCompleter; import net.knarcraft.paidsigns.command.AddTabCompleter; import net.knarcraft.paidsigns.command.ListCommand; +import net.knarcraft.paidsigns.command.ListTabCompleter; import net.knarcraft.paidsigns.command.ReloadTabCommand; import net.knarcraft.paidsigns.command.RemoveConditionCommand; import net.knarcraft.paidsigns.command.RemoveConditionTabCompleter; @@ -113,7 +114,7 @@ public final class PaidSigns extends JavaPlugin { PluginCommand listCommand = this.getCommand("listPaidSigns"); if (listCommand != null) { listCommand.setExecutor(new ListCommand()); - //TODO: Add tab completer + listCommand.setTabCompleter(new ListTabCompleter()); } PluginCommand addConditionCommand = this.getCommand("addPaidSignCondition"); diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java index 91f9ed5..20f396f 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java @@ -25,7 +25,6 @@ public class AddCommand extends TokenizedCommand { if (args.length < 3) { return false; } - PaidSignManager manager = PaidSigns.getInstance().getSignManager(); String signName = arguments.get(0); double cost; @@ -45,6 +44,23 @@ public class AddCommand extends TokenizedCommand { ignoreColor = OptionState.fromString(arguments.get(4)); } + return createPaidSign(sender, signName, cost, permission, ignoreCase, ignoreColor); + } + + /** + * Creates a new paid sign with the given user input + * + * @param sender

The command sender that called the add command

+ * @param signName

The name of the new paid sign

+ * @param cost

The cost of the new paid sign

+ * @param permission

The permission required for creating the sign represented by the paid sign

+ * @param ignoreCase

Whether to ignore case for the paid sign's conditions

+ * @param ignoreColor

Whether to ignore color for the paid sign's conditions

+ * @return

True if the paid sign was successfully created and registered

+ */ + private boolean createPaidSign(CommandSender sender, String signName, double cost, String permission, + OptionState ignoreCase, OptionState ignoreColor) { + PaidSignManager manager = PaidSigns.getInstance().getSignManager(); try { PaidSign sign = new PaidSign(signName, cost, permission, ignoreCase, ignoreColor); if (manager.getPaidSign(signName) != null) { diff --git a/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java new file mode 100644 index 0000000..ca4628f --- /dev/null +++ b/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java @@ -0,0 +1,29 @@ +package net.knarcraft.paidsigns.command; + +import net.knarcraft.paidsigns.utility.TabCompleteHelper; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +/** + * The tab completer for the list paid signs command + */ +public class ListTabCompleter extends TokenizedTabCompleter { + + @Nullable + @Override + public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, + @NotNull String[] args) { + if (argumentSize < 1) { + return TabCompleteHelper.getPaidSignNames(); + } else if (argumentSize < 2) { + return TabCompleteHelper.getSignLines(); + } + return new ArrayList<>(); + } + +} diff --git a/src/main/java/net/knarcraft/paidsigns/utility/TabCompleteHelper.java b/src/main/java/net/knarcraft/paidsigns/utility/TabCompleteHelper.java index a898ca7..73349a3 100644 --- a/src/main/java/net/knarcraft/paidsigns/utility/TabCompleteHelper.java +++ b/src/main/java/net/knarcraft/paidsigns/utility/TabCompleteHelper.java @@ -8,7 +8,11 @@ import java.util.List; /** * A helper class for providing common tab complete options */ -public class TabCompleteHelper { +public final class TabCompleteHelper { + + private TabCompleteHelper() { + + } /** * Gets the available boolean values for tab completion