package com.nicewuerfel.blockown.command; import com.nicewuerfel.blockown.Message; import com.nicewuerfel.blockown.Setting; import com.nicewuerfel.blockown.database.Database; import com.nicewuerfel.blockown.output.Output; import com.nicewuerfel.blockown.protection.Protection; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; import javax.annotation.Nonnull; public abstract class CommandExecutor implements org.bukkit.command.CommandExecutor { protected final Setting setting; protected final Database database; protected final Protection protection; public CommandExecutor(Setting setting, Database database, Protection protection) { this.setting = setting; this.database = database; this.protection = protection; } /** * Checks whether sender is instance of Player.<br> * If not, sends an appropriate message to the sender. * * @param sender the CommandSender * @return the player, or Optional.absent() */ boolean isPlayer(CommandSender sender) { if (sender instanceof Player) { return true; } else { sender.sendMessage(Message.COMMAND_PLAYERS_ONLY.getMessage()); return false; } } /** * If the sender is an instance of ConsoleCommandSender, returns true. If not, sends a message to * the sender and returns false. * * @param sender the CommandSender * @return true, if sender is console. */ boolean isConsole(@Nonnull CommandSender sender) { if (sender instanceof ConsoleCommandSender) { return true; } else { sender.sendMessage(Message.COMMAND_CONSOLE_ONLY.getMessage()); return false; } } boolean isValid(@Nonnull OfflinePlayer player) { return player.hasPlayedBefore() || player.isOnline(); } /* * @author Njol (bukkit forums) */ // protected <T extends Entity> T getTarget(Location eyeLocation, Iterable<T> entities) { // T target = null; // double threshold = 1; // for (T other : entities) { // Vector n = other.getLocation().toVector().subtract(eyeLocation.getDirection()); // if (eyeLocation.getDirection().normalize().crossProduct(n).lengthSquared() < threshold && // n.normalize().dot(eyeLocation.getDirection().normalize()) >= 0) { // if (target == null || target.getLocation().distanceSquared(eyeLocation) > // other.getLocation().distanceSquared(eyeLocation)) // target = other; // } // } // return target; // } protected Output getOutput() { return setting.getOutput(); } }