Finish ListCommand changes
This commit is contained in:
parent
af3328f6c0
commit
ffc019eb8c
@ -1,6 +1,9 @@
|
||||
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.entity.Player;
|
||||
@ -17,11 +20,15 @@ import mineplex.mapparser.MapParser;
|
||||
*/
|
||||
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)
|
||||
{
|
||||
super(plugin, "list");
|
||||
|
||||
setUsage("/list <author|name|type> <search term(s)>");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -36,26 +43,58 @@ public class ListCommand extends BaseCommand
|
||||
for (GameType gameType : GameType.values())
|
||||
{
|
||||
if (gameType == GameType.InProgress)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (listMaps(player, gameType, colorSwitch))
|
||||
if (listMapsForGameType(player, gameType, colorSwitch))
|
||||
{
|
||||
colorSwitch = !colorSwitch;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (args.length == 1)
|
||||
{
|
||||
String input = args[0];
|
||||
message(player, F.elem(getUsage()));
|
||||
}
|
||||
else
|
||||
{
|
||||
String subCommand = args[0].toLowerCase();
|
||||
|
||||
if (TYPE_ALIASES.contains(subCommand))
|
||||
{
|
||||
String input = args[1];
|
||||
|
||||
GameType gameType = getGameType(input);
|
||||
|
||||
if (gameType != null)
|
||||
if (gameType == null)
|
||||
{
|
||||
UtilPlayerBase.message(player, F.main("Parser", "Listing Maps for gametype " + F.elem(gameType.GetName())));
|
||||
listMaps(player, gameType, false);
|
||||
getPlugin().sendValidGameTypes(player);
|
||||
return true;
|
||||
}
|
||||
|
||||
UtilPlayerBase.message(player, F.main("Parser", "Listing Maps for gametype " + F.elem(gameType.GetName())));
|
||||
listMapsForGameType(player, gameType, false);
|
||||
}
|
||||
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;
|
||||
@ -83,44 +122,68 @@ public class ListCommand extends BaseCommand
|
||||
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;
|
||||
if (colorSwitch)
|
||||
color = ChatColor.GREEN;
|
||||
|
||||
File mapsFolder = new File("map" + File.separator + gameType.GetName());
|
||||
if (!mapsFolder.exists())
|
||||
return false;
|
||||
|
||||
for (File file : mapsFolder.listFiles())
|
||||
for (String name : mapNames)
|
||||
{
|
||||
if (!file.isDirectory())
|
||||
continue;
|
||||
|
||||
maps += color + file.getName() + " ";
|
||||
maps.append(color).append(name).append(" ");
|
||||
|
||||
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;
|
||||
else color = ChatColor.GREEN;
|
||||
}
|
||||
|
||||
return maps.toString();
|
||||
}
|
||||
|
||||
private boolean listMapsForGameType(Player player, GameType gameType, boolean colorSwitch)
|
||||
{
|
||||
List<String> mapNames = gameType.getMapNames();
|
||||
|
||||
if (mapNames == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
String maps = getMapsMessage(mapNames, colorSwitch);
|
||||
|
||||
// Print line of maps for specific gametype
|
||||
if (maps.length() > 0)
|
||||
{
|
||||
maps = F.elem(ChatColor.RESET + C.Scramble + "!" + ChatColor.RESET + C.Bold + gameType.name()) + "> " + maps;
|
||||
player.sendMessage(maps);
|
||||
player.sendMessage(F.elem(ChatColor.RESET + C.Scramble + "!" + ChatColor.RESET + C.Bold + gameType.name()) + "> " + maps);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user