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

Java tutorial

Introduction

Here is the source code for xyz.cloudbans.bukkit.command.NoticeCommand.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 org.bukkit.entity.Player;
import xyz.cloudbans.bukkit.CloudBansConfig;
import xyz.cloudbans.bukkit.utils.CommandUtils;
import xyz.cloudbans.client.Client;
import xyz.cloudbans.client.response.notice.NoticeResponse;
import xyz.cloudbans.entities.request.NoticeRequest;
import xyz.cloudbans.entities.request.builder.NoticeRequestBuilder;

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 NoticeCommand implements CommandExecutor {

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

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

    public NoticeCommand(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, String[] args) {
        // /notice <player> <notice>
        if (!sender.hasPermission("cloudbans.notice.notice")) {
            sender.sendMessage(ChatColor.RED + "You don't have enough permissions for that.");
            return true;
        }

        if (args.length < 2) {
            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;
        }

        NoticeRequestBuilder builder = new NoticeRequestBuilder();
        builder.setServer(config.getServerUuid());

        if (args.length > 1) {
            String[] notice = Arrays.copyOfRange(args, 1, args.length);
            String finalNotice = Joiner.on(" ").join(notice);
            builder.setNotice(finalNotice);
        }

        if (sender instanceof Player) {
            builder.setIssuer(((Player) sender).getUniqueId());
        }

        final NoticeRequest request = builder.build();
        CommandUtils.parseTarget(request, args[0]);

        Future<NoticeResponse> future = client.createNotice(request);
        Futures.addCallback(JdkFutureAdapters.listenInPoolThread(future, executor),
                new FutureCallback<NoticeResponse>() {
                    @Override
                    public void onSuccess(NoticeResponse result) {
                        switch (result.getNotice().getDelayState()) {
                        case EXECUTED:
                            sender.sendMessage(ChatColor.GREEN + "Notice executed");
                            break;
                        case QUEUED:
                            sender.sendMessage(ChatColor.GREEN + "Notice will be executed soon.");
                            break;
                        }
                    }

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