co.mcme.warps.commands.WarpListCommand.java Source code

Java tutorial

Introduction

Here is the source code for co.mcme.warps.commands.WarpListCommand.java

Source

/**
 * This file is part of Warps, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2015 Henry Slawniak <http://fortkickass.co/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package co.mcme.warps.commands;

import co.mcme.warps.Warps;
import co.mcme.warps.storage.PlayerWarp;
import co.mcme.warps.storage.WarpDatabase;
import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.ChatPaginator;

public class WarpListCommand implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        ArrayList<PlayerWarp> list = new ArrayList();
        if (sender instanceof Player) {
            for (PlayerWarp warp : WarpDatabase.getWarps().values()) {
                if (warp.canWarp((Player) sender)) {
                    list.add(warp);
                }
            }
        } else {
            list.addAll(WarpDatabase.getWarps().values());
        }
        StringBuilder lines = new StringBuilder();
        boolean first = true;
        for (PlayerWarp warp : list) {
            if (!first) {
                lines.append("\n");
            }
            ChatColor col = ChatColor.GREEN;
            if (warp.isInviteonly()) {
                col = ChatColor.RED;
            }
            Date date = new Date(warp.getCreateStamp());
            lines.append(col).append(warp.getName()).append(ChatColor.GRAY).append(" by ").append(ChatColor.AQUA)
                    .append(warp.getOwner()).append(ChatColor.GRAY).append(" on ").append(ChatColor.AQUA)
                    .append(Warps.getShortDateformat().format(date));
            if (first) {
                first = false;
            }
        }

        if (args.length > 0 && StringUtils.isNumeric(args[0])) {
            ChatPaginator.ChatPage page = ChatPaginator.paginate(lines.toString(), Integer.valueOf(args[0]),
                    ChatPaginator.AVERAGE_CHAT_PAGE_WIDTH, 8);
            sender.sendMessage(ChatColor.GRAY + "Warp List page " + ChatColor.AQUA + page.getPageNumber()
                    + ChatColor.GRAY + " of " + ChatColor.AQUA + page.getTotalPages());
            for (String line : page.getLines()) {
                sender.sendMessage(line);
            }
            return true;
        } else {
            ChatPaginator.ChatPage page = ChatPaginator.paginate(lines.toString(), 1,
                    ChatPaginator.AVERAGE_CHAT_PAGE_WIDTH, 8);
            sender.sendMessage(ChatColor.GRAY + "Warp List page " + ChatColor.AQUA + page.getPageNumber()
                    + ChatColor.GRAY + " of " + ChatColor.AQUA + page.getTotalPages());
            for (String line : page.getLines()) {
                sender.sendMessage(line);
            }
            return true;
        }
    }
}