command cooldowns

This commit is contained in:
Super_ 2019-12-12 17:02:33 -05:00
parent 69fb21f57c
commit ee44b5fb7f
4 changed files with 43 additions and 2 deletions

View File

@ -13,4 +13,6 @@ public @interface CommandPermissions
SourceType source();
boolean blockHostConsole() default false;
int cooldown() default 0;
}

View File

@ -7,7 +7,7 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = Rank.SUPER_ADMIN, source = SourceType.BOTH)
@CommandPermissions(level = Rank.OP, source = SourceType.BOTH, cooldown = 30)
@CommandParameters(description = "Op everyone on the server, optionally change everyone's gamemode at the same time.", usage = "/<command> [-c | -s | -a]")
public class Command_opall extends FreedomCommand
{

View File

@ -2,6 +2,7 @@ package me.totalfreedom.totalfreedommod.command;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Timer;
import lombok.Getter;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.admin.Admin;

View File

@ -1,6 +1,10 @@
package me.totalfreedom.totalfreedommod.command;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.util.FLog;
@ -20,6 +24,9 @@ public class FreedomCommandExecutor<C extends AeroCommandBase<?>> extends Abstra
{
private final TotalFreedomMod plugin;
//
private Map<CommandSender, FreedomCommand> commandCooldown = new HashMap<>();
private final Timer timer = new Timer();
public FreedomCommandExecutor(TotalFreedomMod plugin, AeroCommandHandler<?> handler, String name, C command)
{
@ -76,9 +83,29 @@ public class FreedomCommandExecutor<C extends AeroCommandBase<?>> extends Abstra
return true;
}
if (isOnCooldown(sender))
{
return true;
}
try
{
return commandBase.runCommand(sender, command, label, args);
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;
}
catch (Exception ex)
{
@ -162,6 +189,17 @@ public class FreedomCommandExecutor<C extends AeroCommandBase<?>> extends Abstra
return result;
}
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;
}
public static class FreedomExecutorFactory implements AeroCommandExecutorFactory
{