diff --git a/src/main/java/com/graywolf336/jail/Util.java b/src/main/java/com/graywolf336/jail/Util.java index ddd60cf..614cf96 100644 --- a/src/main/java/com/graywolf336/jail/Util.java +++ b/src/main/java/com/graywolf336/jail/Util.java @@ -3,6 +3,8 @@ package com.graywolf336.jail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -300,6 +302,23 @@ public class Util { public static String[] getSignLines() { return signLines; } + + public static List getUnusedItems(List items, String[] args, boolean useP) { + List used = new ArrayList(); + for(String s : args) + if(s.contains("-")) + used.add(s.replace("-", "")); + + List unused = new ArrayList(); + for(String t : items) + if(!used.contains(t)) //don't add it if it is already used + if(!t.equalsIgnoreCase("p") || (useP && t.equalsIgnoreCase("p")))//don't add -p unless otherwise stated + unused.add("-" + t); + + Collections.sort(unused); + + return unused; + } /** * Converts the player inventory to a String array of Base64 strings. First string is the content and second string is the armor. diff --git a/src/main/java/com/graywolf336/jail/command/subcommands/JailCommand.java b/src/main/java/com/graywolf336/jail/command/subcommands/JailCommand.java index c66349f..011bb4e 100644 --- a/src/main/java/com/graywolf336/jail/command/subcommands/JailCommand.java +++ b/src/main/java/com/graywolf336/jail/command/subcommands/JailCommand.java @@ -271,8 +271,8 @@ public class JailCommand implements Command { String previous = args[args.length - 2]; jm.getPlugin().debug("args[args.length - 2]: " + previous); - if(previous.equalsIgnoreCase("-p")) return getPlayers(jm, ""); - else if(previous.equalsIgnoreCase("-j")) return jm.getJailsByPrefix(""); + if(previous.equalsIgnoreCase("-p")) return getPlayers(jm, last); + else if(previous.equalsIgnoreCase("-j")) return jm.getJailsByPrefix(last); else if(previous.equalsIgnoreCase("-c")) { //Since we need to give them a list of the cells in a jail //we need to get the jail they're giving @@ -280,15 +280,15 @@ public class JailCommand implements Command { if(jailIndex != -1) { String jail = args[jailIndex + 1]; jm.getPlugin().debug("The jail is: " + jail); - if(jm.isValidJail(jail)) return getCells(jm, jail, ""); + if(jm.isValidJail(jail)) return getCells(jm, jail, last); } }else if(previous.endsWith("r")) return Collections.emptyList(); - else if(!commands.contains(args[args.length - 2].replace("-", ""))) return getUnusedCommands(args); + else if(!commands.contains(args[args.length - 2].replace("-", ""))) return Util.getUnusedItems(commands, args, false); } }else if(last.equalsIgnoreCase("-")) { //add some smart checking so that it only returns a list of what isn't already //in the command :) - return getUnusedCommands(args); + return Util.getUnusedItems(commands, args, false); }else { return getPlayers(jm, last); } @@ -321,21 +321,4 @@ public class JailCommand implements Command { return results; } - - private List getUnusedCommands(String[] args) { - List used = new ArrayList(); - for(String s : args) - if(s.contains("-")) - used.add(s.replace("-", "")); - - List unused = new ArrayList(); - for(String t : commands) - if(!used.contains(t) //don't add it if it is already used - && !t.equalsIgnoreCase("p"))//don't add -p - unused.add("-" + t); - - Collections.sort(unused); - - return unused; - } } diff --git a/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferCommand.java b/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferCommand.java index bb3afde..d760560 100644 --- a/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferCommand.java +++ b/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferCommand.java @@ -1,14 +1,19 @@ package com.graywolf336.jail.command.subcommands; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import org.apache.commons.lang3.ArrayUtils; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.util.StringUtil; import com.graywolf336.jail.JailManager; +import com.graywolf336.jail.Util; import com.graywolf336.jail.beans.Cell; import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Prisoner; @@ -29,6 +34,8 @@ import com.lexicalscope.jewel.cli.CliFactory; usage = "/jail transfer [-p player] (-j jail) (-c cell)" ) public class JailTransferCommand implements Command { + private List commands = Arrays.asList(new String[] { "p", "j", "c" }); + public boolean execute(JailManager jm, CommandSender sender, String... args) throws Exception { if(jm.getJails().isEmpty()) { sender.sendMessage(Lang.NOJAILS.get()); @@ -137,7 +144,62 @@ public class JailTransferCommand implements Command { } public List provideTabCompletions(JailManager jm, CommandSender sender, String... args) throws Exception { - //TODO implement + //by the time it gets to this command it'll have at least two arguments + String last = args[args.length - 1]; + + if(last.isEmpty() || !commands.contains(last.replace("-", ""))) { + //the current part is empty. Need to look at their previous + //item and if it is a valid option, then provide them a valid tab complete option + if(args.length - 2 > -1) { + String previous = args[args.length - 2]; + jm.getPlugin().debug("args[args.length - 2]: " + previous); + + if(previous.equalsIgnoreCase("-p")) return getPlayers(jm, last); + else if(previous.equalsIgnoreCase("-j")) return jm.getJailsByPrefix(last); + else if(previous.equalsIgnoreCase("-c")) { + //Since we need to give them a list of the cells in a jail + //we need to get the jail they're giving + int jailIndex = ArrayUtils.indexOf(args, "-j"); + if(jailIndex != -1) { + String jail = args[jailIndex + 1]; + jm.getPlugin().debug("The jail is: " + jail); + if(jm.isValidJail(jail)) return getCells(jm, jail, last); + } + }else if(!commands.contains(args[args.length - 2].replace("-", ""))) return Util.getUnusedItems(commands, args, true); + } + }else if(last.equalsIgnoreCase("-")) { + //add some smart checking so that it only returns a list of what isn't already + //in the command :) + return Util.getUnusedItems(commands, args, true); + }else { + return getPlayers(jm, last); + } + return Collections.emptyList(); } + + private List getPlayers(JailManager jm, String first) { + List results = new ArrayList(); + + for(Player p : jm.getPlugin().getServer().getOnlinePlayers()) + if(first.isEmpty() || StringUtil.startsWithIgnoreCase(p.getName(), first)) + results.add(p.getName()); + + Collections.sort(results); + + return results; + } + + private List getCells(JailManager jm, String jail, String cell) { + List results = new ArrayList(); + + for(Cell c : jm.getJail(jail).getCells()) + if(!c.hasPrisoner()) + if(cell.isEmpty() || StringUtil.startsWithIgnoreCase(c.getName(), cell)) + results.add(c.getName()); + + Collections.sort(results); + + return results; + } }