Adds tab completion to the delete command

This commit is contained in:
Kristian Knarvik 2021-09-08 15:36:42 +02:00
parent 620894f0b1
commit 83c639b7f6

View File

@ -4,11 +4,12 @@ import net.knarcraft.bookswithoutborders.BooksWithoutBorders;
import net.knarcraft.bookswithoutborders.utility.FileHelper; import net.knarcraft.bookswithoutborders.utility.FileHelper;
import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder; import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getBookFolder;
@ -17,7 +18,7 @@ import static net.knarcraft.bookswithoutborders.BooksWithoutBordersSettings.getS
/** /**
* Command executor for the delete command * Command executor for the delete command
*/ */
public class CommandDelete implements CommandExecutor { public class CommandDelete implements TabExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -46,19 +47,14 @@ public class CommandDelete implements CommandExecutor {
//Delete the file //Delete the file
if (args.length == 1) { if (args.length == 1) {
List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, deletePublic); List<String> availableBooks = BooksWithoutBorders.getAvailableBooks(sender, deletePublic);
if (availableBooks == null) { if (!availableBooks.isEmpty()) {
BooksWithoutBorders.sendErrorMessage(sender, "You must first use /deletebook to create a list of delete-able files!"); performBookDeletion(sender, args[0], deletePublic);
return false; //Update the book list
BooksWithoutBorders.updateBooks(sender, deletePublic);
return true;
} else { } else {
if (!availableBooks.isEmpty()) { BooksWithoutBorders.sendErrorMessage(sender, "No files available to delete!");
performBookDeletion(sender, args[0], deletePublic); return false;
//Update the book list
BooksWithoutBorders.updateBooks(sender, deletePublic);
return true;
} else {
BooksWithoutBorders.sendErrorMessage(sender, "No files available to delete!");
return false;
}
} }
} }
BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!"); BooksWithoutBorders.sendErrorMessage(sender, "Incorrect number of arguments for this command!");
@ -110,4 +106,24 @@ public class CommandDelete implements CommandExecutor {
} }
} }
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
return doTabCompletion(sender, args, false);
}
/**
* Performs tab completion
* @param sender <p>The sender of the command</p>
* @param args <p>The arguments given</p>
* @param deletePublic <p>Whether to delete a public book</p>
* @return <p>A list of available arguments</p>
*/
protected List<String> doTabCompletion(CommandSender sender, String[] args, boolean deletePublic) {
int argumentCount = args.length;
if (argumentCount == 1) {
return BooksWithoutBorders.getAvailableBooks(sender, deletePublic);
}
return new ArrayList<>();
}
} }