From 83c639b7f6dc0239b7398a9eca1e7a4594ce086e Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 8 Sep 2021 15:36:42 +0200 Subject: [PATCH] Adds tab completion to the delete command --- .../command/CommandDelete.java | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java index af160f3..d87cf28 100644 --- a/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java +++ b/src/main/java/net/knarcraft/bookswithoutborders/command/CommandDelete.java @@ -4,11 +4,12 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders; import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.TabExecutor; import org.bukkit.entity.Player; import java.io.File; +import java.util.ArrayList; import java.util.List; import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder; @@ -17,7 +18,7 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS /** * Command executor for the delete command */ -public class CommandDelete implements CommandExecutor { +public class CommandDelete implements TabExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -46,19 +47,14 @@ public class CommandDelete implements CommandExecutor { //Delete the file if (args.length == 1) { List availableBooks = BooksWithoutBorders.getAvailableBooks(sender, deletePublic); - if (availableBooks == null) { - BooksWithoutBorders.sendErrorMessage(sender, "You must first use /deletebook to create a list of delete-able files!"); - return false; + if (!availableBooks.isEmpty()) { + performBookDeletion(sender, args[0], deletePublic); + //Update the book list + BooksWithoutBorders.updateBooks(sender, deletePublic); + return true; } else { - if (!availableBooks.isEmpty()) { - performBookDeletion(sender, args[0], deletePublic); - //Update the book list - BooksWithoutBorders.updateBooks(sender, deletePublic); - return true; - } else { - BooksWithoutBorders.sendErrorMessage(sender, "No files available to delete!"); - return false; - } + BooksWithoutBorders.sendErrorMessage(sender, "No files available to delete!"); + return false; } } BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!"); @@ -110,4 +106,24 @@ public class CommandDelete implements CommandExecutor { } } + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + return doTabCompletion(sender, args, false); + } + + /** + * Performs tab completion + * @param sender

The sender of the command

+ * @param args

The arguments given

+ * @param deletePublic

Whether to delete a public book

+ * @return

A list of available arguments

+ */ + protected List doTabCompletion(CommandSender sender, String[] args, boolean deletePublic) { + int argumentCount = args.length; + if (argumentCount == 1) { + return BooksWithoutBorders.getAvailableBooks(sender, deletePublic); + } + return new ArrayList<>(); + } + }