Removes some redundancy between tab completers

This commit is contained in:
Kristian Knarvik 2022-02-25 20:38:10 +01:00
parent 7e01d77723
commit 8a030276c5
4 changed files with 34 additions and 16 deletions

View File

@ -1,10 +1,8 @@
package net.knarcraft.paidsigns.command; package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.utility.TabCompleteHelper; import net.knarcraft.paidsigns.utility.TabCompleteHelper;
import net.knarcraft.paidsigns.utility.Tokenizer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -14,7 +12,7 @@ import java.util.List;
/** /**
* The tab completer for the add paid sign condition command * The tab completer for the add paid sign condition command
*/ */
public class AddConditionTabCompleter implements TabCompleter { public class AddConditionTabCompleter extends TokenizedTabCompleter {
private List<String> lineIndices; private List<String> lineIndices;
private List<String> stringsToMatch; private List<String> stringsToMatch;
@ -25,12 +23,10 @@ public class AddConditionTabCompleter implements TabCompleter {
@Override @Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
@NotNull String[] args) { @NotNull String[] args) {
super.onTabComplete(sender, command, alias, args);
if (lineIndices == null) { if (lineIndices == null) {
initializeValues(); initializeValues();
} }
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
if (argumentSize < 1) { if (argumentSize < 1) {
return TabCompleteHelper.getPaidSignNames(); return TabCompleteHelper.getPaidSignNames();
} else if (argumentSize < 2) { } else if (argumentSize < 2) {

View File

@ -1,11 +1,9 @@
package net.knarcraft.paidsigns.command; package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.utility.TabCompleteHelper; import net.knarcraft.paidsigns.utility.TabCompleteHelper;
import net.knarcraft.paidsigns.utility.Tokenizer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -19,7 +17,7 @@ import java.util.StringJoiner;
/** /**
* The tab completer for the add paid sign command * The tab completer for the add paid sign command
*/ */
public class AddTabCompleter implements TabCompleter { public class AddTabCompleter extends TokenizedTabCompleter {
private static List<String> names; private static List<String> names;
private static List<String> costs; private static List<String> costs;
@ -31,12 +29,11 @@ public class AddTabCompleter implements TabCompleter {
@Override @Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
@NotNull String[] args) { @NotNull String[] args) {
super.onTabComplete(sender, command, alias, args);
if (names == null) { if (names == null) {
initializeValues(); initializeValues();
loadAvailablePermissions(); loadAvailablePermissions();
} }
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
if (argumentSize < 1) { if (argumentSize < 1) {
return names; return names;

View File

@ -1,10 +1,8 @@
package net.knarcraft.paidsigns.command; package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.utility.TabCompleteHelper; import net.knarcraft.paidsigns.utility.TabCompleteHelper;
import net.knarcraft.paidsigns.utility.Tokenizer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -14,7 +12,7 @@ import java.util.List;
/** /**
* The tab completer for the remove paid sign condition command * The tab completer for the remove paid sign condition command
*/ */
public class RemoveConditionTabCompleter implements TabCompleter { public class RemoveConditionTabCompleter extends TokenizedTabCompleter {
private List<String> lineIndices; private List<String> lineIndices;
@ -22,11 +20,10 @@ public class RemoveConditionTabCompleter implements TabCompleter {
@Override @Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
@NotNull String[] args) { @NotNull String[] args) {
super.onTabComplete(sender, command, alias, args);
if (lineIndices == null) { if (lineIndices == null) {
initializeValues(); initializeValues();
} }
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
if (argumentSize < 1) { if (argumentSize < 1) {
return TabCompleteHelper.getPaidSignNames(); return TabCompleteHelper.getPaidSignNames();

View File

@ -0,0 +1,28 @@
package net.knarcraft.paidsigns.command;
import net.knarcraft.paidsigns.utility.Tokenizer;
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.List;
/**
* A tab completer with tokenized arguments
*/
public class TokenizedTabCompleter implements TabCompleter {
protected List<String> arguments;
protected int argumentSize;
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
arguments = Tokenizer.tokenize(String.join(" ", args));
argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
return null;
}
}