removal of aero

This commit is contained in:
Super_
2020-06-30 21:51:06 -04:00
parent 9dad7c6d05
commit 9d71a7f4ae
104 changed files with 808 additions and 1010 deletions

View File

@ -1,7 +1,13 @@
package me.totalfreedom.totalfreedommod.command;
import com.google.common.collect.Lists;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import lombok.Getter;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.admin.Admin;
@ -9,90 +15,225 @@ import me.totalfreedom.totalfreedommod.player.PlayerData;
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.pravian.aero.command.AbstractCommandBase;
import net.pravian.aero.util.Players;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import org.spigotmc.SpigotConfig;
public abstract class FreedomCommand extends AbstractCommandBase<TotalFreedomMod>
public abstract class FreedomCommand implements CommandExecutor, TabCompleter
{
public static final String COMMAND_PREFIX = "Command_";
@Getter
private static CommandMap commandMap;
@Getter
private final String name;
private final String description;
private final String usage;
@Getter
private final String aliases;
private final Rank level;
private final SourceType source;
private final boolean blockHostConsole;
private final int cooldown;
private final CommandParameters params;
@Getter
private final CommandPermissions perms;
protected CommandSender sender;
protected final TotalFreedomMod plugin = TotalFreedomMod.getPlugin();
protected final Server server = plugin.getServer();
public static final String UNKNOWN_COMMAND = ChatColor.WHITE + SpigotConfig.unknownCommandMessage;
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 PLAYER_NOT_FOUND = ChatColor.GRAY + "Player not found!";
//
@Getter
private final CommandParameters params;
@Getter
private final CommandPermissions perms;
//
public static final String ONLY_CONSOLE = ChatColor.RED + "Only console senders may execute this command!";
public static final String ONLY_IN_GAME = ChatColor.RED + "Only in-game players may execute this command!";
public static final String NO_PERMISSION = ChatColor.RED + "You do not have permission to execute this command.";
public FreedomCommand()
public static final Timer timer = new Timer();
public static final Map<CommandSender, FreedomCommand> COOLDOWN_TIMERS = new HashMap<>();
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!");
}
params = getClass().getAnnotation(CommandParameters.class);
perms = getClass().getAnnotation(CommandPermissions.class);
this.name = getClass().getSimpleName().replace(COMMAND_PREFIX, "").toLowerCase();
this.description = params.description();
this.usage = params.usage();
this.aliases = params.aliases();
this.level = perms.level();
this.source = perms.source();
this.blockHostConsole = perms.blockHostConsole();
this.cooldown = perms.cooldown();
}
@Override
public final boolean runCommand(final CommandSender sender, final Command command, final String label, final String[] args)
public void register()
{
setVariables(sender, command, label, args);
try
{
return run(sender, playerSender, command, label, args, isConsole());
}
catch (CommandFailException ex)
{
msg(ex.getMessage());
return true;
}
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;
}
FCommand cmd = new FCommand(this.name);
if (this.aliases != null) cmd.setAliases(Arrays.asList(StringUtils.split(this.aliases, ",")));
if (this.description != null) cmd.setDescription(this.description);
if (this.usage != null) cmd.setUsage(this.usage);
getCommandMap().register("", cmd);
cmd.setExecutor(this);
}
protected List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args) {
return null;
public static final CommandMap getCommandMap()
{
if (commandMap == null)
{
try
{
final Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
f.setAccessible(true);
commandMap = (CommandMap) f.get(Bukkit.getServer());
return getCommandMap();
}
catch (Exception e)
{
e.printStackTrace();
}
}
else if (commandMap != null)
{
return commandMap;
}
return getCommandMap();
}
@Override
public List<String> tabComplete(CommandSender sender, Command command, String alias, String[] args)
private final class FCommand extends Command
{
List<String> options = getTabCompleteOptions(sender, command, alias, args);
if (options == null)
private FreedomCommand cmd = null;
private FCommand(String command)
{
super(command);
}
public void setExecutor(FreedomCommand cmd)
{
this.cmd = cmd;
}
public boolean execute(CommandSender sender, String commandLabel, String[] args)
{
if (cmd != null)
{
cmd.sender = sender;
if (COOLDOWN_TIMERS.containsKey(sender) && COOLDOWN_TIMERS.containsValue(cmd))
{
msg(ChatColor.RED + "You are on cooldown for this command.");
return true;
}
if (perms.blockHostConsole() && FUtil.isFromHostConsole(sender.getName()))
{
msg(ChatColor.RED + "Host console is not allowed to use this command!");
return true;
}
if (!plugin.rm.getRank(sender).isAtLeast(perms.level()))
{
msg(NO_PERMISSION);
return true;
}
if (perms.source() == SourceType.ONLY_CONSOLE && sender instanceof Player)
{
msg(ONLY_CONSOLE);
return true;
}
if (perms.source() == SourceType.ONLY_IN_GAME && sender instanceof ConsoleCommandSender)
{
msg(ONLY_IN_GAME);
return true;
}
if (perms.cooldown() != 0 && !isAdmin(sender))
{
COOLDOWN_TIMERS.put(sender, cmd);
timer.schedule(new TimerTask()
{
@Override
public void run()
{
COOLDOWN_TIMERS.remove(sender);
}
}, perms.cooldown() * 1000);
}
return cmd.onCommand(sender, this, commandLabel, args);
}
return false;
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args)
{
if (cmd != null)
{
return cmd.onTabComplete(sender, this, alias, args);
}
return null;
}
return StringUtil.copyPartialMatches(args[args.length - 1], options, Lists.<String>newArrayList());
}
protected abstract boolean run(final CommandSender sender, final Player playerSender, final Command cmd, final String commandLabel, final String[] args, final boolean senderIsConsole);
protected void msg(CommandSender sender, String message)
{
sender.sendMessage(ChatColor.GRAY + message);
}
protected void msg(Player player, String message)
{
player.sendMessage(ChatColor.GRAY + message);
}
protected void msg(Player player, String message, ChatColor color)
{
player.sendMessage(color + message);
}
protected void msg(String message)
{
msg(sender, message);
}
protected void msg(String message, ChatColor color)
{
msg(color + message);
}
protected void msg(String message, net.md_5.bungee.api.ChatColor color)
{
msg(color + message);
}
protected boolean isAdmin(Player player)
{
return plugin.al.isAdmin(player);
}
protected boolean isAdmin(CommandSender sender)
{
return plugin.al.isAdmin(sender);
}
protected void checkConsole()
{
if (!isConsole())
{
throw new CommandFailException(getHandler().getOnlyConsoleMessage());
throw new CommandFailException(ONLY_CONSOLE);
}
}
@ -100,15 +241,7 @@ public abstract class FreedomCommand extends AbstractCommandBase<TotalFreedomMod
{
if (isConsole())
{
throw new CommandFailException(getHandler().getOnlyPlayerMessage());
}
}
protected void checkNotHostConsole()
{
if (isConsole() && FUtil.isFromHostConsole(sender.getName()))
{
throw new CommandFailException("This command can not be used from the host console.");
throw new CommandFailException(ONLY_IN_GAME);
}
}
@ -120,72 +253,67 @@ public abstract class FreedomCommand extends AbstractCommandBase<TotalFreedomMod
}
}
protected boolean noPerms()
protected void checkNotHostConsole()
{
throw new CommandFailException(getHandler().getPermissionMessage());
if (isConsole() && FUtil.isFromHostConsole(sender.getName()))
{
throw new CommandFailException("This command can not be used from the host console.");
}
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
try
{
boolean run = run(sender, sender instanceof ConsoleCommandSender ? null : (Player) sender, cmd, commandLabel, args, sender instanceof ConsoleCommandSender);
if (!run)
{
msg(ChatColor.WHITE + cmd.getUsage().replace("<command>", cmd.getLabel()));
return true;
}
}
catch (CommandFailException ex)
{
msg(ChatColor.RED + ex.getMessage());
}
return false;
}
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args)
{
List<String> options = getTabCompleteOptions(sender, command, alias, args);
if (options == null)
{
return null;
}
return StringUtil.copyPartialMatches(args[args.length - 1], options, Lists.newArrayList());
}
public abstract boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole);
protected List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args)
{
return null;
}
protected boolean isConsole()
{
return !(sender instanceof Player);
return sender instanceof ConsoleCommandSender;
}
protected Player getPlayer(String name)
{
return Players.getPlayer(name);
return Bukkit.getPlayer(name);
}
protected Player getPlayer(String name, Boolean nullVanished)
{
Player player = Players.getPlayer(name);
Player player = Bukkit.getPlayer(name);
if (nullVanished && plugin.al.vanished.contains(player) && !plugin.al.isAdmin(sender))
{
return null;
}
return Players.getPlayer(name);
}
protected void msg(final CommandSender sender, final String message, final ChatColor color)
{
if (sender == null)
{
return;
}
sender.sendMessage(color + message);
}
protected void msg(final CommandSender sender, final String message, final net.md_5.bungee.api.ChatColor color)
{
if (sender == null)
{
return;
}
sender.sendMessage(color + message);
}
protected void msg(final String message, final net.md_5.bungee.api.ChatColor color)
{
msg(sender, message, color);
}
protected void msg(final String message, final ChatColor color)
{
msg(sender, message, color);
}
protected void msg(final CommandSender sender, final String message)
{
msg(sender, message, ChatColor.GRAY);
}
protected void msg(final String message)
{
msg(sender, message);
}
protected boolean isAdmin(CommandSender sender)
{
return plugin.al.isAdmin(sender);
return player;
}
protected Admin getAdmin(CommandSender sender)
@ -203,12 +331,16 @@ public abstract class FreedomCommand extends AbstractCommandBase<TotalFreedomMod
return plugin.pl.getData(player);
}
protected boolean noPerms()
{
throw new CommandFailException(NO_PERMISSION);
}
public static FreedomCommand getFrom(Command command)
{
try
{
return (FreedomCommand)((FreedomCommandExecutor)(((PluginCommand)command).getExecutor())).getCommandBase();
return (FreedomCommand) (((PluginCommand) command).getExecutor());
}
catch (Exception ex)
{