2016-03-02 19:28:01 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.command;
|
2015-10-19 17:43:46 +00:00
|
|
|
|
2019-01-28 01:49:07 +00:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2015-10-19 17:43:46 +00:00
|
|
|
import lombok.Getter;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.admin.Admin;
|
|
|
|
import me.totalfreedom.totalfreedommod.player.PlayerData;
|
2016-05-12 19:40:39 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.rank.Rank;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
2016-05-12 19:40:39 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
2015-10-19 17:43:46 +00:00
|
|
|
import net.pravian.aero.command.AbstractCommandBase;
|
2016-03-02 19:28:01 +00:00
|
|
|
import net.pravian.aero.util.Players;
|
2015-10-19 17:43:46 +00:00
|
|
|
import org.bukkit.ChatColor;
|
2019-01-28 01:49:07 +00:00
|
|
|
import org.bukkit.Server;
|
2015-10-19 17:43:46 +00:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.command.PluginCommand;
|
|
|
|
import org.bukkit.entity.Player;
|
2019-01-28 01:49:07 +00:00
|
|
|
import org.bukkit.util.StringUtil;
|
2015-10-19 17:43:46 +00:00
|
|
|
|
|
|
|
public abstract class FreedomCommand extends AbstractCommandBase<TotalFreedomMod>
|
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static final String YOU_ARE_OP = ChatColor.YELLOW + "You are now op!";
|
|
|
|
public static final String YOU_ARE_NOT_OP = ChatColor.YELLOW + "You are no longer op!";
|
|
|
|
public static final String NOT_FROM_CONSOLE = "This command may not be used from the console.";
|
|
|
|
public static final String PLAYER_NOT_FOUND = ChatColor.GRAY + "Player not found!";
|
|
|
|
//
|
|
|
|
@Getter
|
|
|
|
private final CommandParameters params;
|
|
|
|
@Getter
|
|
|
|
private final CommandPermissions perms;
|
|
|
|
|
|
|
|
public FreedomCommand()
|
|
|
|
{
|
|
|
|
this.params = getClass().getAnnotation(CommandParameters.class);
|
|
|
|
if (params == null)
|
|
|
|
{
|
|
|
|
FLog.warning("Ignoring command usage for command " + getClass().getSimpleName() + ". Command is not annotated!");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.perms = getClass().getAnnotation(CommandPermissions.class);
|
|
|
|
if (perms == null)
|
|
|
|
{
|
|
|
|
FLog.warning("Ignoring permissions for command " + getClass().getSimpleName() + ". Command is not annotated!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final boolean runCommand(final CommandSender sender, final Command command, final String label, final String[] args)
|
|
|
|
{
|
|
|
|
setVariables(sender, command, label, args);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return run(sender, playerSender, command, label, args, isConsole());
|
|
|
|
}
|
2016-03-02 19:28:01 +00:00
|
|
|
catch (CommandFailException ex)
|
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
msg(ex.getMessage());
|
2016-03-02 19:28:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-19 17:43:46 +00:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
FLog.severe("Uncaught exception executing command: " + command.getName());
|
|
|
|
FLog.severe(ex);
|
|
|
|
sender.sendMessage(ChatColor.RED + "Command error: " + (ex.getMessage() == null ? "Unknown cause" : ex.getMessage()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 18:26:47 +00:00
|
|
|
protected abstract boolean run(final CommandSender sender, final Player playerSender, final Command cmd, final String commandLabel, final String[] args, final boolean senderIsConsole);
|
2015-10-19 17:43:46 +00:00
|
|
|
|
2019-01-28 01:49:07 +00:00
|
|
|
/*@Override
|
|
|
|
public List<String> tabComplete(CommandSender sender, Command command, String alias, String[] args)
|
|
|
|
{
|
|
|
|
return Arrays.asList("x", "d");
|
|
|
|
}*/
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
protected void checkConsole()
|
|
|
|
{
|
|
|
|
if (!isConsole())
|
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
throw new CommandFailException(getHandler().getOnlyConsoleMessage());
|
2016-03-02 19:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void checkPlayer()
|
|
|
|
{
|
|
|
|
if (isConsole())
|
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
throw new CommandFailException(getHandler().getOnlyPlayerMessage());
|
2016-03-02 19:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
protected void checkNotHostConsole()
|
|
|
|
{
|
|
|
|
if (isConsole() && FUtil.isFromHostConsole(sender.getName()))
|
|
|
|
{
|
|
|
|
throw new CommandFailException("This command can not be used from the host console.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void checkRank(Rank rank)
|
2016-03-02 19:28:01 +00:00
|
|
|
{
|
|
|
|
if (!plugin.rm.getRank(sender).isAtLeast(rank))
|
|
|
|
{
|
|
|
|
noPerms();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
protected boolean noPerms()
|
|
|
|
{
|
|
|
|
throw new CommandFailException(getHandler().getPermissionMessage());
|
2016-03-02 19:28:01 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
protected boolean isConsole()
|
|
|
|
{
|
2016-03-02 19:28:01 +00:00
|
|
|
return !(sender instanceof Player);
|
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
protected Player getPlayer(String name)
|
|
|
|
{
|
2016-03-02 19:28:01 +00:00
|
|
|
return Players.getPlayer(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void msg(final CommandSender sender, final String message, final ChatColor color)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
if (sender == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sender.sendMessage(color + message);
|
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
protected void msg(final String message, final ChatColor color)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
msg(sender, message, color);
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
protected void msg(final CommandSender sender, final String message)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2016-03-02 19:28:01 +00:00
|
|
|
msg(sender, message, ChatColor.GRAY);
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
protected void msg(final String message)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
msg(sender, message);
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
protected boolean isAdmin(CommandSender sender)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
return plugin.al.isAdmin(sender);
|
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
protected Admin getAdmin(CommandSender sender)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
return plugin.al.getAdmin(sender);
|
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
protected Admin getAdmin(Player player)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
return plugin.al.getAdmin(player);
|
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
protected PlayerData getData(Player player)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
return plugin.pl.getData(player);
|
|
|
|
}
|
|
|
|
|
2017-11-16 22:32:09 +00:00
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
public static FreedomCommand getFrom(Command command)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-07-31 07:01:29 +00:00
|
|
|
return (FreedomCommand)((FreedomCommandExecutor)(((PluginCommand)command).getExecutor())).getCommandBase();
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|