Fixes tab completion for the add and remove commands #7

This commit is contained in:
Kristian Knarvik 2022-02-19 00:00:50 +01:00
parent 6f35da03e7
commit d76d5cdf93
5 changed files with 40 additions and 44 deletions

View File

@ -3,8 +3,7 @@ package net.knarcraft.paidsigns;
import net.knarcraft.paidsigns.command.AddCommand; import net.knarcraft.paidsigns.command.AddCommand;
import net.knarcraft.paidsigns.command.AddTabCompleter; import net.knarcraft.paidsigns.command.AddTabCompleter;
import net.knarcraft.paidsigns.command.ReloadTabCommand; import net.knarcraft.paidsigns.command.ReloadTabCommand;
import net.knarcraft.paidsigns.command.RemoveCommand; import net.knarcraft.paidsigns.command.RemoveTabCommand;
import net.knarcraft.paidsigns.command.RemoveTabCompleter;
import net.knarcraft.paidsigns.listener.SignListener; import net.knarcraft.paidsigns.listener.SignListener;
import net.knarcraft.paidsigns.manager.EconomyManager; import net.knarcraft.paidsigns.manager.EconomyManager;
import net.knarcraft.paidsigns.manager.PaidSignManager; import net.knarcraft.paidsigns.manager.PaidSignManager;
@ -108,8 +107,9 @@ public final class PaidSigns extends JavaPlugin {
PluginCommand removeCommand = this.getCommand("removePaidSign"); PluginCommand removeCommand = this.getCommand("removePaidSign");
if (removeCommand != null) { if (removeCommand != null) {
removeCommand.setExecutor(new RemoveCommand()); TabExecutor removeTabExecutor = new RemoveTabCommand();
removeCommand.setTabCompleter(new RemoveTabCompleter()); removeCommand.setExecutor(removeTabExecutor);
removeCommand.setTabCompleter(removeTabExecutor);
} }
PluginCommand reloadCommand = this.getCommand("reload"); PluginCommand reloadCommand = this.getCommand("reload");

View File

@ -35,14 +35,15 @@ public class AddTabCompleter implements TabCompleter {
loadAvailablePermissions(); loadAvailablePermissions();
} }
List<String> arguments = Tokenizer.tokenize(String.join(" ", args)); List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
if (arguments.size() < 1) { if (argumentSize < 1) {
return names; return names;
} else if (arguments.size() < 2) { } else if (argumentSize < 2) {
return costs; return costs;
} else if (arguments.size() < 3) { } else if (argumentSize < 3) {
return tabCompletePermission(arguments.get(arguments.size() - 1)); return tabCompletePermission(arguments.get(arguments.size() - 1));
} else if (arguments.size() < 5) { } else if (argumentSize < 5) {
return options; return options;
} }
return new ArrayList<>(); return new ArrayList<>();

View File

@ -1,13 +1,16 @@
package net.knarcraft.paidsigns.command; package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.PaidSign;
import net.knarcraft.paidsigns.utility.Tokenizer; import net.knarcraft.paidsigns.utility.Tokenizer;
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.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
@ -16,15 +19,15 @@ import java.util.logging.Logger;
/** /**
* A representation of the command for removing a paid sign * A representation of the command for removing a paid sign
*/ */
public class RemoveCommand implements CommandExecutor { public class RemoveTabCommand implements TabExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) { @NotNull String[] args) {
if (args.length < 1) { List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
if (arguments.size() < 1) {
return false; return false;
} }
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
String name = arguments.get(0); String name = arguments.get(0);
try { try {
@ -44,4 +47,23 @@ public class RemoveCommand implements CommandExecutor {
return false; return false;
} }
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
@NotNull String[] args) {
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
if (argumentSize < 1) {
List<PaidSign> allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns();
List<String> signIds = new ArrayList<>();
for (PaidSign sign : allPaidSigns) {
signIds.add("\"" + sign.getName() + "\"");
}
return signIds;
} else {
return new ArrayList<>();
}
}
} }

View File

@ -1,31 +0,0 @@
package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.PaidSigns;
import net.knarcraft.paidsigns.container.PaidSign;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* The tab completer for the remove command
*/
public class RemoveTabCompleter implements TabCompleter {
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
@NotNull String[] args) {
List<PaidSign> allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns();
List<String> signIds = new ArrayList<>();
for (PaidSign sign : allPaidSigns) {
signIds.add("\"" + sign.getName() + "\"");
}
return signIds;
}
}

View File

@ -6,7 +6,11 @@ import java.util.List;
/** /**
* A tokenizer for being able to support quotes in commands * A tokenizer for being able to support quotes in commands
*/ */
public class Tokenizer { public final class Tokenizer {
private Tokenizer() {
}
/** /**
* Tokenizes a string * Tokenizes a string