Search in sources :

Example 1 with ScoreboardOrder

use of au.com.mineauz.minigames.minigame.ScoreboardOrder in project Minigames by AddstarMC.

the class ScoreboardCommand method onCommand.

@Override
public boolean onCommand(final CommandSender sender, Minigame ignore, String label, String[] args) {
    if (args == null || args.length < 3) {
        return false;
    }
    // Decode arguments
    final Minigame minigame = plugin.mdata.getMinigame(args[0]);
    if (minigame == null) {
        sender.sendMessage(ChatColor.RED + "No Minigame found by the name " + args[0]);
        return true;
    }
    final MinigameStat stat = MinigameStats.getStat(args[1]);
    if (stat == null) {
        sender.sendMessage(ChatColor.RED + "No statistic found by the name " + args[1]);
        return true;
    }
    final StatSettings settings = minigame.getSettings(stat);
    StatValueField field = null;
    for (StatValueField f : settings.getFormat().getFields()) {
        if (f.name().equalsIgnoreCase(args[2])) {
            field = f;
            break;
        }
    }
    if (field == null) {
        sender.sendMessage(ChatColor.RED + "No field found by the name " + args[2] + " for the statistic " + stat.getDisplayName());
        return true;
    }
    // Prepare defaults for optionals
    ScoreboardOrder order;
    switch(field) {
        case Last:
        case Total:
        case Max:
            order = ScoreboardOrder.DESCENDING;
            break;
        case Min:
            order = ScoreboardOrder.ASCENDING;
            break;
        default:
            throw new AssertionError();
    }
    int start = 0;
    int length = 8;
    // Now the optionals
    for (int i = 3; i < args.length - 1; i += 2) {
        if (args[i].equalsIgnoreCase("-o")) {
            // Order
            if (args[i + 1].equalsIgnoreCase("asc") || args[i + 1].equalsIgnoreCase("ascending")) {
                order = ScoreboardOrder.ASCENDING;
            } else if (args[i + 1].equalsIgnoreCase("desc") || args[i + 1].equalsIgnoreCase("descending")) {
                order = ScoreboardOrder.DESCENDING;
            } else {
                sender.sendMessage(ChatColor.RED + "Unknown order " + args[i + 1] + ". Expected asc, ascending, desc, or descending.");
                return true;
            }
        } else if (args[i].equalsIgnoreCase("-l")) {
            // Length
            if (args[i + 1].matches("[1-9][0-9]*")) {
                length = Integer.parseInt(args[i + 1]);
            } else {
                sender.sendMessage(ChatColor.RED + "Unknown length " + args[i + 1] + ". Expected positive non-zero number");
                return true;
            }
        } else if (args[i].equalsIgnoreCase("-s")) {
            // Start
            if (args[i + 1].matches("[1-9][0-9]*")) {
                start = Integer.parseInt(args[i + 1]) - 1;
            } else {
                sender.sendMessage(ChatColor.RED + "Unknown start " + args[i + 1] + ". Expected positive non-zero number");
                return true;
            }
        } else {
            sender.sendMessage(ChatColor.RED + "Unknown option " + args[i] + ". Expected -o, -l, or -s");
            return true;
        }
    }
    final ScoreboardOrder fOrder = order;
    final StatValueField fField = field;
    sender.sendMessage(ChatColor.GRAY + "Loading scoreboard...");
    // Now load the values
    ListenableFuture<List<StoredStat>> future = plugin.getBackend().loadStats(minigame, stat, field, order, start, length);
    Futures.addCallback(future, new FutureCallback<List<StoredStat>>() {

        @Override
        public void onSuccess(List<StoredStat> result) {
            sender.sendMessage(ChatColor.GREEN + minigame.getName(true) + " Scoreboard: " + settings.getDisplayName() + " - " + fField.getTitle() + " " + fOrder.toString().toLowerCase());
            for (StoredStat playerStat : result) {
                sender.sendMessage(ChatColor.AQUA + playerStat.getPlayerDisplayName() + ": " + ChatColor.WHITE + stat.displayValue(playerStat.getValue(), settings));
            }
        }

        @Override
        public void onFailure(Throwable t) {
            sender.sendMessage(ChatColor.RED + "An internal error occured while loading the statistics");
            t.printStackTrace();
        }
    });
    return true;
}
Also used : StoredStat(au.com.mineauz.minigames.stats.StoredStat) StatValueField(au.com.mineauz.minigames.stats.StatValueField) MinigameStat(au.com.mineauz.minigames.stats.MinigameStat) ScoreboardOrder(au.com.mineauz.minigames.minigame.ScoreboardOrder) ArrayList(java.util.ArrayList) List(java.util.List) StatSettings(au.com.mineauz.minigames.stats.StatSettings) Minigame(au.com.mineauz.minigames.minigame.Minigame)

Aggregations

Minigame (au.com.mineauz.minigames.minigame.Minigame)1 ScoreboardOrder (au.com.mineauz.minigames.minigame.ScoreboardOrder)1 MinigameStat (au.com.mineauz.minigames.stats.MinigameStat)1 StatSettings (au.com.mineauz.minigames.stats.StatSettings)1 StatValueField (au.com.mineauz.minigames.stats.StatValueField)1 StoredStat (au.com.mineauz.minigames.stats.StoredStat)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1