Adds tab-complete filtering #12

This commit is contained in:
Kristian Knarvik 2023-04-18 14:13:07 +02:00
parent cf0962ef70
commit 58e25bcb30
12 changed files with 65 additions and 26 deletions

View File

@ -11,6 +11,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Supplier; import java.util.function.Supplier;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* An abstract class for an arena joining tab-completer * An abstract class for an arena joining tab-completer
*/ */
@ -34,13 +36,13 @@ public abstract class JoinArenaTabCompleter implements TabCompleter {
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String label, @NotNull String[] arguments) { @NotNull String label, @NotNull String[] arguments) {
if (arguments.length == 1) { if (arguments.length == 1) {
return arenaNameSupplier.get(); return filterMatchingContains(arenaNameSupplier.get(), arguments[0]);
} else if (arguments.length == 2) { } else if (arguments.length == 2) {
List<String> gameModes = new ArrayList<>(); List<String> gameModes = new ArrayList<>();
for (ArenaGameMode gameMode : gameMode.getValues()) { for (ArenaGameMode gameMode : gameMode.getValues()) {
gameModes.add(gameMode.name().toLowerCase()); gameModes.add(gameMode.name().toLowerCase());
} }
return gameModes; return filterMatchingContains(gameModes, arguments[1]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -14,6 +14,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The command for listing groups and the stages within * The command for listing groups and the stages within
*/ */
@ -84,7 +86,7 @@ public class DropperGroupListCommand implements TabExecutor {
for (DropperArenaGroup group : MiniGames.getInstance().getDropperArenaHandler().getAllGroups()) { for (DropperArenaGroup group : MiniGames.getInstance().getDropperArenaHandler().getAllGroups()) {
groupNames.add(group.getGroupName()); groupNames.add(group.getGroupName());
} }
return groupNames; return filterMatchingContains(groupNames, arguments[0]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -15,6 +15,8 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The command for setting the group of an arena * The command for setting the group of an arena
*/ */
@ -62,7 +64,7 @@ public class DropperGroupSetCommand implements TabExecutor {
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
if (arguments.length == 1) { if (arguments.length == 1) {
return TabCompleteHelper.getDropperArenas(); return filterMatchingContains(TabCompleteHelper.getDropperArenas(), arguments[0]);
} else if (arguments.length == 2) { } else if (arguments.length == 2) {
List<String> possibleValues = new ArrayList<>(); List<String> possibleValues = new ArrayList<>();
possibleValues.add("none"); possibleValues.add("none");
@ -70,7 +72,7 @@ public class DropperGroupSetCommand implements TabExecutor {
for (DropperArenaGroup group : MiniGames.getInstance().getDropperArenaHandler().getAllGroups()) { for (DropperArenaGroup group : MiniGames.getInstance().getDropperArenaHandler().getAllGroups()) {
possibleValues.add(group.getGroupName()); possibleValues.add(group.getGroupName());
} }
return possibleValues; return filterMatchingContains(possibleValues, arguments[1]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -14,6 +14,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The command for swapping the order of two arenas in a group * The command for swapping the order of two arenas in a group
*/ */
@ -63,9 +65,9 @@ public class DropperGroupSwapCommand implements TabExecutor {
for (DropperArena dropperArena : arenaHandler.getArenasInAGroup()) { for (DropperArena dropperArena : arenaHandler.getArenasInAGroup()) {
arenaNames.add(dropperArena.getArenaName()); arenaNames.add(dropperArena.getArenaName());
} }
return arenaNames; return filterMatchingContains(arenaNames, arguments[0]);
} else if (arguments.length == 2) { } else if (arguments.length == 2) {
return getArenaNamesInSameGroup(arguments[0]); return filterMatchingContains(getArenaNamesInSameGroup(arguments[0]), arguments[1]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -10,6 +10,8 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The tab-completer for the edit arena command * The tab-completer for the edit arena command
*/ */
@ -17,12 +19,12 @@ public class EditDropperArenaTabCompleter implements TabCompleter {
@Override @Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String label, @NotNull String[] args) { @NotNull String label, @NotNull String[] arguments) {
if (args.length == 1) { if (arguments.length == 1) {
return TabCompleteHelper.getDropperArenas(); return filterMatchingContains(TabCompleteHelper.getDropperArenas(), arguments[0]);
} else if (args.length == 2) { } else if (arguments.length == 2) {
return TabCompleteHelper.getDropperArenaProperties(); return filterMatchingContains(TabCompleteHelper.getDropperArenaProperties(), arguments[1]);
} else if (args.length == 3) { } else if (arguments.length == 3) {
//TODO: Tab-complete possible values for the given property //TODO: Tab-complete possible values for the given property
return null; return null;
} else { } else {

View File

@ -10,6 +10,8 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The tab-completer for the remove arena command * The tab-completer for the remove arena command
*/ */
@ -20,7 +22,7 @@ public class RemoveDropperArenaTabCompleter implements TabCompleter {
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
if (arguments.length == 1) { if (arguments.length == 1) {
return TabCompleteHelper.getDropperArenas(); return filterMatchingContains(TabCompleteHelper.getDropperArenas(), arguments[0]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -10,6 +10,8 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The tab-completer for the edit arena command * The tab-completer for the edit arena command
*/ */
@ -17,12 +19,12 @@ public class EditParkourArenaTabCompleter implements TabCompleter {
@Override @Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
@NotNull String label, @NotNull String[] args) { @NotNull String label, @NotNull String[] arguments) {
if (args.length == 1) { if (arguments.length == 1) {
return TabCompleteHelper.getParkourArenas(); return filterMatchingContains(TabCompleteHelper.getParkourArenas(), arguments[0]);
} else if (args.length == 2) { } else if (arguments.length == 2) {
return TabCompleteHelper.getParkourArenaProperties(); return filterMatchingContains(TabCompleteHelper.getParkourArenaProperties(), arguments[1]);
} else if (args.length == 3) { } else if (arguments.length == 3) {
//TODO: Tab-complete possible values for the given property //TODO: Tab-complete possible values for the given property
return null; return null;
} else { } else {

View File

@ -15,6 +15,8 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The command for listing groups and the stages within * The command for listing groups and the stages within
*/ */
@ -86,7 +88,7 @@ public class ParkourGroupListCommand implements TabExecutor {
for (ParkourArenaGroup group : arenaGroups) { for (ParkourArenaGroup group : arenaGroups) {
groupNames.add(group.getGroupName()); groupNames.add(group.getGroupName());
} }
return groupNames; return filterMatchingContains(groupNames, arguments[0]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -15,6 +15,8 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The command for setting the group of an arena * The command for setting the group of an arena
*/ */
@ -62,7 +64,7 @@ public class ParkourGroupSetCommand implements TabExecutor {
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
if (arguments.length == 1) { if (arguments.length == 1) {
return TabCompleteHelper.getParkourArenas(); return filterMatchingContains(TabCompleteHelper.getParkourArenas(), arguments[0]);
} else if (arguments.length == 2) { } else if (arguments.length == 2) {
List<String> possibleValues = new ArrayList<>(); List<String> possibleValues = new ArrayList<>();
possibleValues.add("none"); possibleValues.add("none");
@ -70,7 +72,7 @@ public class ParkourGroupSetCommand implements TabExecutor {
for (ParkourArenaGroup group : MiniGames.getInstance().getParkourArenaHandler().getAllGroups()) { for (ParkourArenaGroup group : MiniGames.getInstance().getParkourArenaHandler().getAllGroups()) {
possibleValues.add(group.getGroupName()); possibleValues.add(group.getGroupName());
} }
return possibleValues; return filterMatchingContains(possibleValues, arguments[1]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -14,6 +14,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The command for swapping the order of two arenas in a group * The command for swapping the order of two arenas in a group
*/ */
@ -63,9 +65,9 @@ public class ParkourGroupSwapCommand implements TabExecutor {
for (ParkourArena parkourArena : arenaHandler.getArenasInAGroup()) { for (ParkourArena parkourArena : arenaHandler.getArenasInAGroup()) {
arenaNames.add(parkourArena.getArenaName()); arenaNames.add(parkourArena.getArenaName());
} }
return arenaNames; return filterMatchingContains(arenaNames, arguments[0]);
} else if (arguments.length == 2) { } else if (arguments.length == 2) {
return getArenaNamesInSameGroup(arguments[0]); return filterMatchingContains(getArenaNamesInSameGroup(arguments[0]), arguments[1]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -10,6 +10,8 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.knarcraft.minigames.util.TabCompleteHelper.filterMatchingContains;
/** /**
* The tab-completer for the remove arena command * The tab-completer for the remove arena command
*/ */
@ -20,7 +22,7 @@ public class RemoveParkourArenaTabCompleter implements TabCompleter {
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] arguments) { @NotNull String[] arguments) {
if (arguments.length == 1) { if (arguments.length == 1) {
return TabCompleteHelper.getParkourArenas(); return filterMatchingContains(TabCompleteHelper.getParkourArenas(), arguments[0]);
} else { } else {
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -76,4 +76,21 @@ public final class TabCompleteHelper {
return arenaProperties; return arenaProperties;
} }
/**
* Finds tab complete values that contain the typed text
*
* @param values <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values that contain the player's typed text</p>
*/
public static List<String> filterMatchingContains(@NotNull List<String> values, @NotNull String typedText) {
List<String> configValues = new ArrayList<>();
for (String value : values) {
if (value.toLowerCase().contains(typedText.toLowerCase())) {
configValues.add(value);
}
}
return configValues;
}
} }