Skip to content

Commit

Permalink
Allow tab completion of map names
Browse files Browse the repository at this point in the history
Signed-off-by: Pugzy <[email protected]>
  • Loading branch information
Pugzy committed Nov 17, 2019
1 parent 2c5a496 commit ae6d170
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/java/tc/oc/pgm/commands/AdminCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<map>",
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();
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/tc/oc/pgm/commands/provider/PGMMapProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,7 +40,7 @@ public PGMMap get(CommandSender sender, CommandArgs args, List<? extends Annotat
PGMMap map = matchManager.getMatch(sender).getMap();

if (args.hasNext()) {
String mapName = getRemainingText(args);
String mapName = StringUtils.decodeSpaces(getRemainingText(args));
map = mapLibrary.getMapByNameOrId(mapName).orNull();
if (map == null) {
String fuzzyName = StringUtils.bestFuzzyMatch(mapName, mapLibrary.getMapNames(), 0.9);
Expand Down Expand Up @@ -82,4 +84,17 @@ private boolean isGoToNext(List<? extends Annotation> annotations) {
.filter(value -> Arrays.asList(((Default) value).value()).contains("next"))
.isPresent();
}

@Override
public List<String> getSuggestions(
String prefix, CommandSender sender, Namespace namespace, List<? extends Annotation> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -36,4 +38,18 @@ public TeamMatchModule get(
}
return teamMatchModule;
}

@Override
public List<String> getSuggestions(
String prefix, CommandSender sender, Namespace namespace, List<? extends Annotation> 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());
}
}
1 change: 1 addition & 0 deletions src/main/java/tc/oc/pgm/match/MatchManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/tc/oc/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, "", "").
*
Expand Down Expand Up @@ -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, ' ');
}
}

0 comments on commit ae6d170

Please sign in to comment.