2016-03-02 19:28:01 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.command;
|
2015-10-19 17:43:46 +00:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
2019-12-12 22:02:33 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
2015-11-15 23:32:04 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.rank.Rank;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
|
|
|
import net.pravian.aero.command.AeroCommandBase;
|
|
|
|
import net.pravian.aero.command.executor.AbstractCommandExecutor;
|
|
|
|
import net.pravian.aero.command.executor.AeroCommandExecutor;
|
|
|
|
import net.pravian.aero.command.executor.AeroCommandExecutorFactory;
|
|
|
|
import net.pravian.aero.command.handler.AeroCommandHandler;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.command.PluginCommand;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
public class FreedomCommandExecutor<C extends AeroCommandBase<?>> extends AbstractCommandExecutor<C>
|
|
|
|
{
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
private final TotalFreedomMod plugin;
|
2019-12-12 22:02:33 +00:00
|
|
|
//
|
|
|
|
private Map<CommandSender, FreedomCommand> commandCooldown = new HashMap<>();
|
|
|
|
private final Timer timer = new Timer();
|
2016-05-12 19:40:39 +00:00
|
|
|
|
|
|
|
public FreedomCommandExecutor(TotalFreedomMod plugin, AeroCommandHandler<?> handler, String name, C command)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
super(handler, name, command);
|
2016-05-12 19:40:39 +00:00
|
|
|
this.plugin = plugin;
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected FreedomCommand getCommand()
|
|
|
|
{
|
2018-07-31 07:01:29 +00:00
|
|
|
return commandBase instanceof FreedomCommand ? (FreedomCommand)commandBase : null;
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setupCommand(PluginCommand pluginCommand)
|
|
|
|
{
|
|
|
|
final FreedomCommand command = getCommand();
|
|
|
|
if (command == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final CommandParameters params = command.getParams();
|
|
|
|
if (params == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
String aliasString = params.aliases();
|
|
|
|
|
|
|
|
if (aliasString.length() > 0)
|
|
|
|
{
|
|
|
|
pluginCommand.setAliases(Arrays.asList(params.aliases().split(",")));
|
|
|
|
}
|
2015-10-19 17:43:46 +00:00
|
|
|
pluginCommand.setDescription(params.description());
|
|
|
|
pluginCommand.setUsage(params.usage());
|
2016-05-12 19:40:39 +00:00
|
|
|
|
|
|
|
// Check if permisions are correctly set up
|
|
|
|
CommandPermissions perms = command.getPerms();
|
|
|
|
if (perms != null)
|
|
|
|
{
|
|
|
|
if (perms.level().isConsole())
|
|
|
|
{
|
|
|
|
FLog.warning("[Command] " + pluginCommand.getName() + " - permission is set to a console rank, "
|
|
|
|
+ "should be set to player variant with 'source = SourceType.ONLY_CONSOLE'");
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
|
|
|
|
{
|
|
|
|
if (!hasPermission(sender, true))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-12 22:02:33 +00:00
|
|
|
if (isOnCooldown(sender))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
try
|
|
|
|
{
|
2019-12-12 22:02:33 +00:00
|
|
|
boolean run = commandBase.runCommand(sender, command, label, args);
|
|
|
|
FreedomCommand c = getCommand();
|
|
|
|
CommandPermissions perms = c.getPerms();
|
|
|
|
if (perms.cooldown() > 0 && !plugin.al.isAdmin(sender))
|
|
|
|
{
|
|
|
|
commandCooldown.put(sender, c);
|
|
|
|
timer.schedule(new TimerTask()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
commandCooldown.remove(sender);
|
|
|
|
}
|
|
|
|
}, perms.cooldown() * 1000);
|
|
|
|
}
|
|
|
|
return run;
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
// If this is ever ran, TFM failed :
|
|
|
|
FLog.severe("Unhandled command exception: " + command.getName());
|
|
|
|
FLog.severe(ex);
|
|
|
|
sender.sendMessage(ChatColor.RED + "Unhandled Command Error: " + command.getName());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasPermission(CommandSender sender, boolean sendMsg)
|
|
|
|
{
|
|
|
|
final FreedomCommand command = getCommand();
|
|
|
|
if (command == null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
final CommandPermissions perms = command.getPerms();
|
|
|
|
if (perms == null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block host console
|
|
|
|
if (FUtil.isFromHostConsole(sender.getName())
|
|
|
|
&& perms.blockHostConsole())
|
|
|
|
{
|
|
|
|
if (sendMsg)
|
|
|
|
{
|
|
|
|
sender.sendMessage(handler.getPermissionMessage());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-31 07:01:29 +00:00
|
|
|
final Player player = sender instanceof Player ? (Player)sender : null;
|
2015-10-19 17:43:46 +00:00
|
|
|
|
|
|
|
// Only console
|
|
|
|
if (perms.source() == SourceType.ONLY_CONSOLE
|
|
|
|
&& player != null)
|
|
|
|
{
|
|
|
|
if (sendMsg)
|
|
|
|
{
|
|
|
|
sender.sendMessage(handler.getOnlyConsoleMessage());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only in game
|
|
|
|
if (perms.source() == SourceType.ONLY_IN_GAME
|
|
|
|
&& player == null)
|
|
|
|
{
|
|
|
|
if (sendMsg)
|
|
|
|
{
|
|
|
|
sender.sendMessage(handler.getOnlyPlayerMessage());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Player permissions
|
|
|
|
if (player != null)
|
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
Rank rank = plugin.rm.getRank(player);
|
2015-10-19 17:43:46 +00:00
|
|
|
boolean result = rank.isAtLeast(perms.level());
|
|
|
|
if (!result && sendMsg)
|
|
|
|
{
|
|
|
|
sender.sendMessage(handler.getPermissionMessage());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Console permissions
|
2016-05-12 19:40:39 +00:00
|
|
|
Rank rank = plugin.rm.getRank(sender);
|
2016-02-29 20:48:17 +00:00
|
|
|
boolean result = rank.isAtLeast(perms.level());
|
2015-10-19 17:43:46 +00:00
|
|
|
if (!result && sendMsg)
|
|
|
|
{
|
|
|
|
sender.sendMessage(handler.getPermissionMessage());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-12 22:02:33 +00:00
|
|
|
public boolean isOnCooldown(CommandSender sender)
|
|
|
|
{
|
|
|
|
final FreedomCommand command = getCommand();
|
|
|
|
if (commandCooldown.containsKey(sender) && commandCooldown.containsValue(command))
|
|
|
|
{
|
|
|
|
sender.sendMessage(ChatColor.RED + "You're on cooldown for this command.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public static class FreedomExecutorFactory implements AeroCommandExecutorFactory
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
private final TotalFreedomMod plugin;
|
|
|
|
|
|
|
|
public FreedomExecutorFactory(TotalFreedomMod plugin)
|
|
|
|
{
|
|
|
|
this.plugin = plugin;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
@Override
|
|
|
|
public AeroCommandExecutor<? extends AeroCommandBase<?>> newExecutor(AeroCommandHandler<?> handler, String name, AeroCommandBase<?> command)
|
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
return new FreedomCommandExecutor<>(plugin, handler, name, command);
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|