Java tutorial
/* * 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.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 BanCommand implements CommandExecutor { private static final Logger LOGGER = Logger.getLogger(BanCommand.class.getName()); private final Client client; private final ExecutorService executor; private final CloudBansConfig config; public BanCommand(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) { // /ban <player> <reason> if (!sender.hasPermission("cloudbans.ban.ban")) { 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; } BanRequestBuilder builder = new BanRequestBuilder(); builder.setServer(config.getServerUuid()); if (args.length > 1) { String[] reason = Arrays.copyOfRange(args, 1, args.length); String finalReason = Joiner.on(" ").join(reason); builder.setDescription(finalReason); } if (sender instanceof Player) { builder.setIssuer(((Player) sender).getUniqueId()); } BanRequest request = builder.build(); CommandUtils.parseTarget(request, args[0]); Future<BanResponse> future = client.createBan(request); Futures.addCallback(JdkFutureAdapters.listenInPoolThread(future, executor), new FutureCallback<BanResponse>() { @Override public void onSuccess(BanResponse result) { switch (result.getBan().getDelayState()) { case EXECUTED: sender.sendMessage(ChatColor.GREEN + "Ban executed"); break; case QUEUED: sender.sendMessage(ChatColor.GREEN + "Ban will be executed soon."); break; } } @Override public void onFailure(Throwable t) { sender.sendMessage(ChatColor.RED + "Ban was not executed successfully."); LOGGER.log(Level.SEVERE, "An error occurred while executing ban request.", t); } }); return true; } }