Adds a helper class for tab completion, and uses a TabExecutor for the give and give public commands
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user