Finish ListCommand changes

This commit is contained in:
Spencer 2017-12-30 21:39:52 -05:00 committed by Alexander Meech
parent af3328f6c0
commit ffc019eb8c

View File

@ -1,6 +1,9 @@
package mineplex.mapparser.command; package mineplex.mapparser.command;
import java.io.File; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -17,11 +20,15 @@ import mineplex.mapparser.MapParser;
*/ */
public class ListCommand extends BaseCommand public class ListCommand extends BaseCommand
{ {
private boolean _colorSwitch = false; //private static final List<String> AUTHOR_ALISES = Arrays.asList("author", "creator", "a");
private static final List<String> NAME_ALIASES = Arrays.asList("name", "mapname", "title", "n");
private static final List<String> TYPE_ALIASES = Arrays.asList("type", "game", "gametype", "g");
public ListCommand(MapParser plugin) public ListCommand(MapParser plugin)
{ {
super(plugin, "list"); super(plugin, "list");
setUsage("/list <author|name|type> <search term(s)>");
} }
@Override @Override
@ -36,26 +43,58 @@ public class ListCommand extends BaseCommand
for (GameType gameType : GameType.values()) for (GameType gameType : GameType.values())
{ {
if (gameType == GameType.InProgress) if (gameType == GameType.InProgress)
{
continue; continue;
}
if (listMaps(player, gameType, colorSwitch)) if (listMapsForGameType(player, gameType, colorSwitch))
{
colorSwitch = !colorSwitch; colorSwitch = !colorSwitch;
}
} }
} }
else if (args.length == 1) else if (args.length == 1)
{ {
String input = args[0]; message(player, F.elem(getUsage()));
}
else
{
String subCommand = args[0].toLowerCase();
GameType gameType = getGameType(input); if (TYPE_ALIASES.contains(subCommand))
if (gameType != null)
{ {
String input = args[1];
GameType gameType = getGameType(input);
if (gameType == null)
{
getPlugin().sendValidGameTypes(player);
return true;
}
UtilPlayerBase.message(player, F.main("Parser", "Listing Maps for gametype " + F.elem(gameType.GetName()))); UtilPlayerBase.message(player, F.main("Parser", "Listing Maps for gametype " + F.elem(gameType.GetName())));
listMaps(player, gameType, false); listMapsForGameType(player, gameType, false);
return true;
} }
else
{
String search = Arrays.asList(args).subList(1, args.length).stream().collect(Collectors.joining());
if (NAME_ALIASES.contains(subCommand))
{
List<String> mapNames = GameType.getAllMapNames()
.stream()
.filter(n -> n.toLowerCase().contains(search))
.collect(Collectors.toList());
message(player, "Listing maps whose name contains " + F.elem(search) + C.mBody + ":");
message(player, getMapsMessage(mapNames, false));
}
else
{
message(player, "Invalid sub-command.");
}
}
} }
return true; return true;
@ -83,44 +122,68 @@ public class ListCommand extends BaseCommand
return gameType; return gameType;
} }
private boolean listMaps(Player player, GameType gameType, boolean colorSwitch) private String getMapsMessage(List<String> mapNames, boolean colorSwitch)
{ {
String maps = ""; StringBuilder maps = new StringBuilder();
ChatColor color = ChatColor.AQUA; ChatColor color = ChatColor.AQUA;
if (colorSwitch) if (colorSwitch)
color = ChatColor.GREEN; color = ChatColor.GREEN;
File mapsFolder = new File("map" + File.separator + gameType.GetName()); for (String name : mapNames)
if (!mapsFolder.exists()) {
return false; maps.append(color).append(name).append(" ");
for (File file : mapsFolder.listFiles()) if (color == ChatColor.AQUA)
color = ChatColor.DARK_AQUA;
else if (color == ChatColor.DARK_AQUA)
color = ChatColor.AQUA;
else if (color == ChatColor.GREEN)
color = ChatColor.DARK_GREEN;
else color = ChatColor.GREEN;
}
return maps.toString();
}
private boolean listMapsForGameType(Player player, GameType gameType, boolean colorSwitch)
{
List<String> mapNames = gameType.getMapNames();
if (mapNames == null)
{ {
if (!file.isDirectory()) return false;
continue;
maps += color + file.getName() + " ";
if (color == ChatColor.AQUA)
color = ChatColor.DARK_AQUA;
else if (color == ChatColor.DARK_AQUA)
color = ChatColor.AQUA;
else if (color == ChatColor.GREEN)
color = ChatColor.DARK_GREEN;
else if (color == ChatColor.DARK_GREEN)
color = ChatColor.GREEN;
} }
String maps = getMapsMessage(mapNames, colorSwitch);
// Print line of maps for specific gametype // Print line of maps for specific gametype
if (maps.length() > 0) if (maps.length() > 0)
{ {
maps = F.elem(ChatColor.RESET + C.Scramble + "!" + ChatColor.RESET + C.Bold + gameType.name()) + "> " + maps; player.sendMessage(F.elem(ChatColor.RESET + C.Scramble + "!" + ChatColor.RESET + C.Bold + gameType.name()) + "> " + maps);
player.sendMessage(maps);
return true; return true;
} }
return false; return false;
} }
public List<MapData> getAllMapData()
{
List<String> mapNames = GameType.getAllMapNames();
List<MapData> mapData = new ArrayList<>();
for (String mapName : mapNames)
{
MapData data = getPlugin().getData(mapName);
if (data == null)
{
continue;
}
mapData.add(data);
}
return mapData;
}
} }