xyz.cloudbans.bukkit.command.UnbanCommand.java Source code

Java tutorial

Introduction

Here is the source code for xyz.cloudbans.bukkit.command.UnbanCommand.java

Source

/*
 * CloudBans Bukkit Plugin - Bukkit plugin for CloudBans
 *      Copyright (C) 2016  CloudBans (https://cloudbans.xyz)
 *
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package xyz.cloudbans.bukkit.command;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.JdkFutureAdapters;
import org.bukkit.ChatColor;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import xyz.cloudbans.bukkit.CloudBansConfig;
import xyz.cloudbans.client.Client;
import xyz.cloudbans.client.response.ban.BanResponse;
import xyz.cloudbans.entities.request.BanRequest;
import xyz.cloudbans.entities.request.builder.BanRequestBuilder;

import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UnbanCommand implements CommandExecutor {

    private static final Logger LOGGER = Logger.getLogger(UnbanCommand.class.getName());

    private final Client client;
    private final ExecutorService executor;
    private final CloudBansConfig config;

    public UnbanCommand(Client client, ExecutorService executor, CloudBansConfig config) {
        this.client = Preconditions.checkNotNull(client, "client must not be null.");
        this.executor = Preconditions.checkNotNull(executor, "executor must not be null.");
        this.config = Preconditions.checkNotNull(config, "config must not be null.");
    }

    @Override
    public boolean onCommand(final CommandSender sender, Command command, String label, final String[] args) {
        // unban <player> <reason>
        if (!sender.hasPermission("cloudbans.ban.unban")) {
            sender.sendMessage(ChatColor.RED + "You don't have enough permissions for that.");
            return true;
        }

        if (args.length < 1) {
            sender.sendMessage(ChatColor.RED + "Your missing arguments for this command.");
            return false;
        }

        if (sender instanceof BlockCommandSender) {
            sender.sendMessage(ChatColor.RED
                    + "For security reasons this command can only executed by a player or the console!");
            return true;
        }

        Future<BanResponse> future = client.getActiveBan(config.getServerUuid(), args[0]);
        Futures.addCallback(JdkFutureAdapters.listenInPoolThread(future, executor),
                new FutureCallback<BanResponse>() {
                    @Override
                    public void onSuccess(BanResponse result) {
                        BanRequestBuilder builder = new BanRequestBuilder();
                        builder.setId(result.getBan().getId());
                        if (args.length > 2) {
                            String[] reason = Arrays.copyOfRange(args, 2, args.length);
                            String unbanReason = Joiner.on(" ").join(reason);
                            builder.setDescription(result.getBan().getDescription() + " Unban: " + unbanReason);
                        }
                        builder.setEnabled(false);
                        BanRequest request = builder.build();
                        Future<BanResponse> unbanResponseFuture = client.updateBan(request);
                        Futures.addCallback(JdkFutureAdapters.listenInPoolThread(unbanResponseFuture, executor),
                                new FutureCallback<BanResponse>() {
                                    @Override
                                    public void onSuccess(BanResponse result) {
                                        switch (result.getBan().getDelayState()) {
                                        case EXECUTED:
                                            sender.sendMessage(ChatColor.GREEN + "Unban executed");
                                            break;
                                        case QUEUED:
                                            sender.sendMessage(ChatColor.GREEN + "Unban will be executed soon.");
                                            break;
                                        }
                                    }

                                    @Override
                                    public void onFailure(Throwable t) {
                                        sender.sendMessage(ChatColor.RED + "Unban was not executed successfully.");
                                        LOGGER.log(Level.SEVERE, "An error occurred while executing unban request.",
                                                t);
                                    }
                                });
                    }

                    @Override
                    public void onFailure(Throwable t) {

                    }
                });
        return true;
    }
}