From ae6d1708de563eaf93885905a3ac5075fcd67b51 Mon Sep 17 00:00:00 2001 From: Pugzy Date: Sun, 17 Nov 2019 21:46:06 +0000 Subject: [PATCH] Allow tab completion of map names Signed-off-by: Pugzy --- .../java/tc/oc/pgm/commands/AdminCommands.java | 4 ++-- .../pgm/commands/provider/PGMMapProvider.java | 17 ++++++++++++++++- .../provider/TeamMatchModuleProvider.java | 16 ++++++++++++++++ .../java/tc/oc/pgm/match/MatchManagerImpl.java | 1 + src/main/java/tc/oc/util/StringUtils.java | 10 ++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/main/java/tc/oc/pgm/commands/AdminCommands.java b/src/main/java/tc/oc/pgm/commands/AdminCommands.java index 57f02137e0..cd1b35b0f5 100644 --- a/src/main/java/tc/oc/pgm/commands/AdminCommands.java +++ b/src/main/java/tc/oc/pgm/commands/AdminCommands.java @@ -124,10 +124,10 @@ public static void end(CommandSender sender, Match match, @Nullable @Text String aliases = {"setnext", "sn"}, desc = "Sets the next map. Note that the rotation will go to this map then resume as normal.", - usage = "[map name]", + usage = "", flags = "f", perms = Permissions.SETNEXT) - public static void setNext(CommandSender sender, @Switch('f') boolean force, @Text PGMMap map) + public static void setNext(CommandSender sender, PGMMap map, @Switch('f') boolean force) throws CommandException { MatchManager mm = PGM.getMatchManager(); boolean restartQueued = RestartManager.get().isRestartRequested(); diff --git a/src/main/java/tc/oc/pgm/commands/provider/PGMMapProvider.java b/src/main/java/tc/oc/pgm/commands/provider/PGMMapProvider.java index 0418988e33..e179c31ade 100644 --- a/src/main/java/tc/oc/pgm/commands/provider/PGMMapProvider.java +++ b/src/main/java/tc/oc/pgm/commands/provider/PGMMapProvider.java @@ -3,11 +3,13 @@ import app.ashcon.intake.argument.ArgumentException; import app.ashcon.intake.argument.CommandArgs; import app.ashcon.intake.argument.MissingArgumentException; +import app.ashcon.intake.argument.Namespace; import app.ashcon.intake.bukkit.parametric.provider.BukkitProvider; import app.ashcon.intake.parametric.annotation.Default; import java.lang.annotation.Annotation; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import org.bukkit.command.CommandSender; import tc.oc.pgm.AllTranslations; import tc.oc.pgm.api.match.MatchManager; @@ -38,7 +40,7 @@ public PGMMap get(CommandSender sender, CommandArgs args, List annotations) { .filter(value -> Arrays.asList(((Default) value).value()).contains("next")) .isPresent(); } + + @Override + public List getSuggestions( + String prefix, CommandSender sender, Namespace namespace, List mods) { + + final String convertedPrefix = StringUtils.encodeSpaces(prefix); + + return mapLibrary.getMapNames().stream() + .map(StringUtils::encodeSpaces) + .filter(name -> name.toLowerCase().startsWith(convertedPrefix.toLowerCase())) + .sorted() + .collect(Collectors.toList()); + } } diff --git a/src/main/java/tc/oc/pgm/commands/provider/TeamMatchModuleProvider.java b/src/main/java/tc/oc/pgm/commands/provider/TeamMatchModuleProvider.java index da77cd1d31..95958f7b1c 100644 --- a/src/main/java/tc/oc/pgm/commands/provider/TeamMatchModuleProvider.java +++ b/src/main/java/tc/oc/pgm/commands/provider/TeamMatchModuleProvider.java @@ -1,10 +1,12 @@ package tc.oc.pgm.commands.provider; import app.ashcon.intake.argument.CommandArgs; +import app.ashcon.intake.argument.Namespace; import app.ashcon.intake.bukkit.parametric.provider.BukkitProvider; import app.ashcon.intake.parametric.ProvisionException; import java.lang.annotation.Annotation; import java.util.List; +import java.util.stream.Collectors; import javax.annotation.Nullable; import org.bukkit.command.CommandSender; import tc.oc.pgm.AllTranslations; @@ -36,4 +38,18 @@ public TeamMatchModule get( } return teamMatchModule; } + + @Override + public List getSuggestions( + String prefix, CommandSender sender, Namespace namespace, List mods) { + + TeamMatchModule teamMatchModule = + matchManager.getMatch(sender).getMatchModule(TeamMatchModule.class); + + return teamMatchModule.getTeams().stream() + .map(team -> team.getName().replace(' ', '\u2508')) + .filter(team -> team.toLowerCase().startsWith(prefix.toLowerCase())) + .sorted() + .collect(Collectors.toList()); + } } diff --git a/src/main/java/tc/oc/pgm/match/MatchManagerImpl.java b/src/main/java/tc/oc/pgm/match/MatchManagerImpl.java index 7f9aa2018c..c23b2d4284 100644 --- a/src/main/java/tc/oc/pgm/match/MatchManagerImpl.java +++ b/src/main/java/tc/oc/pgm/match/MatchManagerImpl.java @@ -117,6 +117,7 @@ private World createMatchWorld(String worldName, MapInfo info, TerrainModule ter world.setPVP(true); world.setSpawnFlags(false, false); world.setAutoSave(false); + world.setGameRuleValue("keepInventory", "false"); if (info.difficulty != null) { world.setDifficulty(info.difficulty); diff --git a/src/main/java/tc/oc/util/StringUtils.java b/src/main/java/tc/oc/util/StringUtils.java index 0c0b522eb6..20839ca89a 100644 --- a/src/main/java/tc/oc/util/StringUtils.java +++ b/src/main/java/tc/oc/util/StringUtils.java @@ -8,6 +8,8 @@ public class StringUtils { public static final int GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH = 55; // Will never wrap, even with the largest characters + public static final char FAKE_SPACE_CHAR = '\u2508'; + /** * Shorthand for listToEnglishCompound(list, "", ""). * @@ -456,4 +458,12 @@ public static String percentage(double completion) { return percent + "%"; } } + + public static String encodeSpaces(String input) { + return input.replace(' ', FAKE_SPACE_CHAR); + } + + public static String decodeSpaces(String input) { + return input.replace(FAKE_SPACE_CHAR, ' '); + } }