Adds a helper class for tab completion, and uses a TabExecutor for the give and give public commands
This commit is contained in:
		| @@ -19,7 +19,6 @@ import net.knarcraft.bookswithoutborders.command.CommandSetBookPrice; | ||||
| import net.knarcraft.bookswithoutborders.command.CommandSetLore; | ||||
| import net.knarcraft.bookswithoutborders.command.CommandSetTitle; | ||||
| import net.knarcraft.bookswithoutborders.command.CommandUnSign; | ||||
| import net.knarcraft.bookswithoutborders.command.GiveTabCompleter; | ||||
| import net.knarcraft.bookswithoutborders.listener.PlayerEventListener; | ||||
| import net.knarcraft.bookswithoutborders.listener.SignEventListener; | ||||
| import net.knarcraft.bookswithoutborders.state.BookDirectory; | ||||
| @@ -32,6 +31,7 @@ import org.bukkit.command.CommandSender; | ||||
| import org.bukkit.command.ConsoleCommandSender; | ||||
| import org.bukkit.command.PluginCommand; | ||||
| import org.bukkit.command.TabCompleter; | ||||
| import org.bukkit.command.TabExecutor; | ||||
| import org.bukkit.configuration.Configuration; | ||||
| import org.bukkit.entity.Player; | ||||
| import org.bukkit.inventory.ItemFactory; | ||||
| @@ -263,7 +263,10 @@ public class BooksWithoutBorders extends JavaPlugin { | ||||
|      * Registers all commands used by this plugin | ||||
|      */ | ||||
|     private void registerCommands() { | ||||
|         registerCommand("giveBook", new CommandGive(), new GiveTabCompleter()); | ||||
|         TabExecutor giveTabExecutor = new CommandGive(); | ||||
|         registerCommand("giveBook", giveTabExecutor, giveTabExecutor); | ||||
|         TabExecutor givePublicTabExecutor = new CommandGivePublic(); | ||||
|         registerCommand("givePublicBook", givePublicTabExecutor, givePublicTabExecutor); | ||||
|         registerCommand("decryptBook", new CommandDecrypt(), null); | ||||
|         registerCommand("groupEncryptBook", new CommandGroupEncrypt(), null); | ||||
|         registerCommand("deleteBook", new CommandDelete(), null); | ||||
| @@ -281,7 +284,6 @@ public class BooksWithoutBorders extends JavaPlugin { | ||||
|         registerCommand("loadPublicBook", new CommandLoadPublic(), null); | ||||
|         registerCommand("booksWithoutBorders", new CommandBooksWithoutBorders(), null); | ||||
|         registerCommand("reload", new CommandReload(), null); | ||||
|         registerCommand("givePublicBook", new CommandGivePublic(), null); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|   | ||||
| @@ -3,16 +3,21 @@ package net.knarcraft.bookswithoutborders.command; | ||||
| import net.knarcraft.bookswithoutborders.BooksWithoutBorders; | ||||
| import net.knarcraft.bookswithoutborders.utility.FileHelper; | ||||
| import net.knarcraft.bookswithoutborders.utility.InputCleaningHelper; | ||||
| import net.knarcraft.bookswithoutborders.utility.TabCompletionHelper; | ||||
| import org.bukkit.Server; | ||||
| 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 org.bukkit.inventory.ItemStack; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * Command executor for the give command | ||||
|  */ | ||||
| public class CommandGive implements CommandExecutor { | ||||
| public class CommandGive implements TabExecutor { | ||||
|  | ||||
|     private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); | ||||
|  | ||||
| @@ -98,4 +103,48 @@ public class CommandGive implements CommandExecutor { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { | ||||
|         return doTabCompletion(sender, args, false); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Performs the actual tab completion | ||||
|      * @param sender <p>The sender of the command</p> | ||||
|      * @param args <p>The arguments given</p> | ||||
|      * @param listPublic <p>Whether to list public files or player files</p> | ||||
|      * @return <p>A list of available choices</p> | ||||
|      */ | ||||
|     protected List<String> doTabCompletion(CommandSender sender, String[] args, boolean listPublic) { | ||||
|         Server server = booksWithoutBorders.getServer(); | ||||
|  | ||||
|         int argumentCount = args.length; | ||||
|  | ||||
|         if (argumentCount > 2) { | ||||
|             //Don't continue with autocompletion if the recipient is invalid | ||||
|             if (server.getPlayer(args[1]) == null) { | ||||
|                 return new ArrayList<>(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if (argumentCount == 1) { | ||||
|             //Return list of books | ||||
|             return BooksWithoutBorders.getAvailableBooks(sender, listPublic); | ||||
|         } else if (argumentCount == 2) { | ||||
|             //Return online players | ||||
|             return null; | ||||
|         } else if (argumentCount == 3) { | ||||
|             //Number of copies | ||||
|             return TabCompletionHelper.getBooleansAndNumbers(1, 64); | ||||
|         } else if (argumentCount == 4) { | ||||
|             //Signed | ||||
|             try { | ||||
|                 Integer.parseInt(args[2]); | ||||
|                 return TabCompletionHelper.getBooleans(); | ||||
|             } catch (NumberFormatException e) { | ||||
|                 return new ArrayList<>(); | ||||
|             } | ||||
|         } | ||||
|         return new ArrayList<>(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,17 +1,24 @@ | ||||
| package net.knarcraft.bookswithoutborders.command; | ||||
|  | ||||
| import org.bukkit.command.Command; | ||||
| import org.bukkit.command.CommandExecutor; | ||||
| import org.bukkit.command.CommandSender; | ||||
| import org.bukkit.command.TabExecutor; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * Command executor for the give public command | ||||
|  */ | ||||
| public class CommandGivePublic extends CommandGive implements CommandExecutor { | ||||
| public class CommandGivePublic extends CommandGive implements TabExecutor { | ||||
|  | ||||
|     @Override | ||||
|     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { | ||||
|         return giveBook(sender, args, true, "public"); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { | ||||
|         return doTabCompletion(sender, args, true); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,70 +0,0 @@ | ||||
| package net.knarcraft.bookswithoutborders.command; | ||||
|  | ||||
| import net.knarcraft.bookswithoutborders.BooksWithoutBorders; | ||||
| import org.bukkit.Server; | ||||
| import org.bukkit.command.Command; | ||||
| import org.bukkit.command.CommandSender; | ||||
| import org.bukkit.command.TabCompleter; | ||||
| import org.bukkit.entity.Player; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * Tab completer for the save command | ||||
|  */ | ||||
| public class GiveTabCompleter implements TabCompleter { | ||||
|  | ||||
|     private final BooksWithoutBorders booksWithoutBorders = BooksWithoutBorders.getInstance(); | ||||
|  | ||||
|     @Override | ||||
|     public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { | ||||
|         Server server = booksWithoutBorders.getServer(); | ||||
|         List<String> playerNames = new ArrayList<>(); | ||||
|         for (Player player : server.getOnlinePlayers()) { | ||||
|             playerNames.add(player.getName()); | ||||
|         } | ||||
|         List<String> booleans = new ArrayList<>(); | ||||
|         booleans.add("true"); | ||||
|         booleans.add("false"); | ||||
|         List<String> numbers = new ArrayList<>(); | ||||
|         numbers.add("1"); | ||||
|         numbers.add("2"); | ||||
|         numbers.add("3"); | ||||
|         numbers.add("4"); | ||||
|         numbers.add("5"); | ||||
|         List<String> boolAndNumbers = new ArrayList<>(); | ||||
|         boolAndNumbers.addAll(booleans); | ||||
|         boolAndNumbers.addAll(numbers); | ||||
|  | ||||
|         int argumentCount = args.length; | ||||
|  | ||||
|         if (argumentCount > 2) { | ||||
|             //Don't continue with autocompletion if the recipient is invalid | ||||
|             if (server.getPlayer(args[1]) == null) { | ||||
|                 return null; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         if (argumentCount == 1) { | ||||
|             //Return list of books | ||||
|             return BooksWithoutBorders.getAvailableBooks(sender, false); | ||||
|         } else if (argumentCount == 2) { | ||||
|             //Return online players | ||||
|             return playerNames; | ||||
|         } else if (argumentCount == 3) { | ||||
|             //Number of copies | ||||
|             return boolAndNumbers; | ||||
|         } else if (argumentCount == 4) { | ||||
|             //Signed | ||||
|             try { | ||||
|                 Integer.parseInt(args[2]); | ||||
|                 return booleans; | ||||
|             } catch (NumberFormatException e) { | ||||
|                 return null; | ||||
|             } | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,54 @@ | ||||
| package net.knarcraft.bookswithoutborders.utility; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * Helper class for getting string lists required for auto-completion | ||||
|  */ | ||||
| public class TabCompletionHelper { | ||||
|  | ||||
|     /** | ||||
|      * Gets a list of booleans | ||||
|      * | ||||
|      * @return <p>A list of booleans</p> | ||||
|      */ | ||||
|     public static List<String> getBooleans() { | ||||
|         List<String> booleans = new ArrayList<>(); | ||||
|         booleans.add("true"); | ||||
|         booleans.add("false"); | ||||
|         return booleans; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets a list of numbers in increasing order | ||||
|      * | ||||
|      * @param start <p>The start number</p> | ||||
|      * @param end   <p>The end number</p> | ||||
|      * @return <p>A list of numbers</p> | ||||
|      */ | ||||
|     public static List<String> getNumbers(int start, int end) { | ||||
|         List<String> numbers = new ArrayList<>(); | ||||
|         for (int i = start; i <= end; i++) { | ||||
|             numbers.add(String.valueOf(i)); | ||||
|         } | ||||
|         return numbers; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Gets a list of booleans and numbers | ||||
|      * | ||||
|      * @param start <p>The start number</p> | ||||
|      * @param end   <p>The end number</p> | ||||
|      * @return <p>A list of booleans and numbers</p> | ||||
|      */ | ||||
|     public static List<String> getBooleansAndNumbers(int start, int end) { | ||||
|         List<String> booleansAndNumbers = new ArrayList<>(); | ||||
|         List<String> booleans = getBooleans(); | ||||
|         List<String> numbers = getNumbers(start, end); | ||||
|         booleansAndNumbers.addAll(booleans); | ||||
|         booleansAndNumbers.addAll(numbers); | ||||
|         return booleansAndNumbers; | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user