TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/FreedomCommand.java

431 lines
12 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
2019-01-28 01:49:07 +00:00
import com.google.common.collect.Lists;
2020-07-01 01:51:06 +00:00
import java.lang.reflect.Field;
2020-12-30 04:06:12 +00:00
import java.util.ArrayList;
2020-07-01 01:51:06 +00:00
import java.util.Arrays;
import java.util.HashMap;
2019-01-28 01:49:07 +00:00
import java.util.List;
2020-07-01 01:51:06 +00:00
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.player.PlayerData;
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.util.FUtil;
2020-07-01 01:51:06 +00:00
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
2020-07-01 01:51:06 +00:00
import org.bukkit.Server;
import org.bukkit.command.Command;
2020-07-01 01:51:06 +00:00
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
2020-07-01 01:51:06 +00:00
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.PluginCommand;
2020-07-01 01:51:06 +00:00
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
2019-01-28 01:49:07 +00:00
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
2020-07-01 01:51:06 +00:00
public abstract class FreedomCommand implements CommandExecutor, TabCompleter
{
2020-07-01 01:51:06 +00:00
public static final String COMMAND_PREFIX = "Command_";
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!";
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 static final Timer timer = new Timer();
public static final Map<CommandSender, FreedomCommand> COOLDOWN_TIMERS = new HashMap<>();
2020-07-01 01:51:06 +00:00
private static CommandMap commandMap;
protected final TotalFreedomMod plugin = TotalFreedomMod.getPlugin();
protected final Server server = plugin.getServer();
2020-07-01 01:51:06 +00:00
private final String name;
private final String description;
private final String usage;
private final String aliases;
private final Rank level;
private final SourceType source;
private final boolean blockHostConsole;
private final int cooldown;
private final CommandParameters params;
private final CommandPermissions perms;
2020-07-01 01:51:06 +00:00
protected CommandSender sender;
FreedomCommand()
{
2020-07-01 01:51:06 +00:00
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();
}
public static CommandMap getCommandMap()
2020-07-01 01:51:06 +00:00
{
if (commandMap == null)
{
2020-07-01 01:51:06 +00:00
try
{
final Field f = Bukkit.getServer().getPluginManager().getClass().getDeclaredField("commandMap");
2020-07-01 01:51:06 +00:00
f.setAccessible(true);
2020-08-04 08:14:42 +00:00
commandMap = (CommandMap)f.get(Bukkit.getServer().getPluginManager());
2020-07-01 01:51:06 +00:00
}
catch (Exception e)
{
e.printStackTrace();
}
}
return commandMap;
}
public static FreedomCommand getFrom(Command command)
{
try
{
return (FreedomCommand)(((PluginCommand)command).getExecutor());
}
catch (Exception ex)
{
return null;
}
}
2020-07-01 01:51:06 +00:00
public static String getCommandPrefix()
{
return COMMAND_PREFIX;
}
2020-07-01 01:51:06 +00:00
public void register()
{
FCommand cmd = new FCommand(this.name);
if (this.aliases != null)
{
cmd.setAliases(Arrays.asList(StringUtils.split(this.aliases, ",")));
2020-07-01 01:51:06 +00:00
}
if (this.description != null)
2020-07-01 01:51:06 +00:00
{
cmd.setDescription(this.description);
}
if (this.usage != null)
{
cmd.setUsage(this.usage);
}
getCommandMap().register("totalfreedommod", cmd);
cmd.setExecutor(this);
}
2020-07-01 01:51:06 +00:00
protected void msg(CommandSender sender, String message)
{
sender.sendMessage(ChatColor.GRAY + message);
2019-01-29 04:57:41 +00:00
}
2020-07-01 01:51:06 +00:00
protected void msg(Player player, String message)
2019-01-29 04:57:41 +00:00
{
2020-07-01 01:51:06 +00:00
player.sendMessage(ChatColor.GRAY + message);
2019-01-29 04:57:41 +00:00
}
2020-07-01 01:51:06 +00:00
protected void msg(Player player, String message, ChatColor color)
{
player.sendMessage(color + message);
}
2019-01-28 01:49:07 +00:00
2020-07-01 01:51:06 +00:00
protected void msg(String message)
{
2020-07-01 01:51:06 +00:00
msg(sender, message);
}
2020-07-01 01:51:06 +00:00
protected void msg(String message, ChatColor color)
{
2020-07-01 01:51:06 +00:00
msg(color + message);
}
2020-07-01 01:51:06 +00:00
protected void msg(String message, net.md_5.bungee.api.ChatColor color)
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
{
2020-07-01 01:51:06 +00:00
msg(color + message);
TotalFreedomMod Electrum Version 5.0 This TotalFreedomMod release implements many changes. Most notably, the internals have been completely revamped. TotalFreedomMod now relies on the Aero library for core mechanics such as command handling and services. Another important change is the UUID system. In TotalFreedomMod Electrum, it has been completely removed. The core reason for this is that the system as a whole was very bugged. Additionally, it did not solve the primary reason for its conception: preserving player data when the player changes their username. This is because TotalFreedomMod servers usually run in offline-mode. This meaning that some of the players joining do not have a registerd Mojang UUID whatsoever. All in all, the UUID system was buggy, and it did not fix the reason it was implemented, so it has been completely removed. The admin list and the ban list now use usernames and IPs again. Lastly, many smaller changes have been implemented. Due to the amount of changes, they have not been named individualy. Please refer to the issues below for more details. Fixes #342 Fixes #350 Fixes #380 Fixes #684 Fixes #704 Fixes #716 Fixes #735 Fixes #745 Fixes #784 Fixes #765 Fixes #791 Fixes #805 Fixes #826 Fixes #883 Fixes #1524 Fixes #1534 Fixes #1536 Fixes #1538 Fixes #1545 Fixes #1546 Fixes #1568 Fixes #1627 Resolves #403 Resolves #435 Resolves #597 Resolves #603 Resolves #628 Resolves #690 Resolves #708 Resolves #747 Resolves #748 Resolves #749 Resolves #764 Resolves #767 Resolves #782 Resolves #809 Resolves #803 Resolves #811 Resolves #813 Resolves #830 Resolves #848 Resolves #856 Resolves #876 Resolves #908 Resolves #992 Resolves #1018 Resolves #1432 Resolves #1446 Resolves #1494 Resolves #1501 Resolves #1526 Resolves #1540 Resolves #1550 Resolves #1560 Resolves #1561 Resolves #1578 Resolves #1613
2016-05-12 19:40:39 +00:00
}
protected boolean isAdmin(Player player)
{
return plugin.al.isAdmin(player);
}
protected boolean isAdmin(CommandSender sender)
{
return plugin.al.isAdmin(sender);
}
2020-07-01 01:51:06 +00:00
protected void checkConsole()
{
2020-07-01 01:51:06 +00:00
if (!isConsole())
{
throw new CommandFailException(ONLY_CONSOLE);
}
}
2020-07-01 01:51:06 +00:00
protected void checkPlayer()
{
2020-07-01 01:51:06 +00:00
if (isConsole())
{
throw new CommandFailException(ONLY_IN_GAME);
}
}
2020-07-01 01:51:06 +00:00
protected void checkRank(Rank rank)
2020-04-23 11:18:03 +00:00
{
2020-07-01 01:51:06 +00:00
if (!plugin.rm.getRank(sender).isAtLeast(rank))
2020-04-23 11:18:03 +00:00
{
2020-07-01 01:51:06 +00:00
noPerms();
2020-04-23 11:18:03 +00:00
}
}
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String commandLabel, @NotNull String[] args)
2020-06-30 07:25:38 +00:00
{
2020-07-01 01:51:06 +00:00
try
{
boolean run = run(sender, sender instanceof ConsoleCommandSender ? null : (Player)sender, cmd, commandLabel, args, sender instanceof ConsoleCommandSender);
2020-07-01 01:51:06 +00:00
if (!run)
{
msg(ChatColor.WHITE + cmd.getUsage().replace("<command>", cmd.getLabel()));
return true;
}
}
catch (CommandFailException ex)
2020-06-30 07:25:38 +00:00
{
2020-07-01 01:51:06 +00:00
msg(ChatColor.RED + ex.getMessage());
2020-06-30 07:25:38 +00:00
}
2020-07-01 01:51:06 +00:00
return false;
2020-06-30 07:25:38 +00:00
}
2020-12-30 04:06:12 +00:00
@NotNull
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args)
2020-06-30 07:25:38 +00:00
{
2020-07-01 01:51:06 +00:00
List<String> options = getTabCompleteOptions(sender, command, alias, args);
if (options == null)
{
2020-12-30 04:06:12 +00:00
return new ArrayList<>();
2020-07-01 01:51:06 +00:00
}
return StringUtil.copyPartialMatches(args[args.length - 1], options, Lists.newArrayList());
2020-06-30 07:25:38 +00:00
}
2020-07-01 01:51:06 +00:00
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)
{
2020-07-01 03:21:44 +00:00
return FUtil.getPlayerList();
}
2020-07-01 01:51:06 +00:00
protected boolean isConsole()
{
2020-07-01 01:51:06 +00:00
return sender instanceof ConsoleCommandSender;
}
2020-07-01 01:51:06 +00:00
protected Player getPlayer(String name)
{
2020-07-01 01:51:06 +00:00
return Bukkit.getPlayer(name);
}
2020-07-01 01:51:06 +00:00
protected Player getPlayer(String name, Boolean nullVanished)
{
2020-07-01 01:51:06 +00:00
Player player = Bukkit.getPlayer(name);
2020-07-22 21:40:58 +00:00
if (player != null)
2020-07-01 01:51:06 +00:00
{
if (nullVanished && plugin.al.isVanished(player.getName()) && !plugin.al.isAdmin(sender))
2020-07-22 21:40:58 +00:00
{
return null;
}
2020-07-01 01:51:06 +00:00
}
return player;
}
protected Admin getAdmin(CommandSender sender)
{
return plugin.al.getAdmin(sender);
}
protected Admin getAdmin(Player player)
{
return plugin.al.getAdmin(player);
}
protected PlayerData getData(Player player)
{
return plugin.pl.getData(player);
}
2020-07-01 01:51:06 +00:00
protected boolean noPerms()
{
throw new CommandFailException(NO_PERMISSION);
}
public String getName()
{
return name;
}
public String getDescription()
{
return description;
}
public String getUsage()
{
return usage;
}
public String getAliases()
{
return aliases;
}
public Rank getLevel()
{
return level;
}
public SourceType getSource()
{
return source;
}
public boolean isBlockHostConsole()
{
return blockHostConsole;
}
public int getCooldown()
{
return cooldown;
}
public CommandParameters getParams()
{
return params;
}
public CommandPermissions getPerms()
{
return perms;
}
private final class FCommand extends Command
{
private FreedomCommand cmd = null;
private FCommand(String command)
{
super(command);
}
public void setExecutor(FreedomCommand cmd)
{
this.cmd = cmd;
}
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, String[] args)
{
if (cmd != null)
{
cmd.sender = sender;
2020-12-30 04:06:12 +00:00
if (func4())
{
return true;
}
2020-12-30 04:06:12 +00:00
if (func1())
{
return true;
}
2020-12-30 04:06:12 +00:00
if (func2())
{
return true;
}
func3();
return cmd.onCommand(sender, this, commandLabel, args);
}
return false;
}
2020-12-30 04:06:12 +00:00
public boolean func1()
{
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;
}
return false;
}
2020-12-30 04:06:12 +00:00
public boolean func2()
{
if (!plugin.rm.getRank(sender).isAtLeast(perms.level()))
{
msg(NO_PERMISSION);
return true;
}
if (perms.blockHostConsole() && FUtil.isFromHostConsole(sender.getName()) && !FUtil.inDeveloperMode())
{
msg(ChatColor.RED + "Host console is not allowed to use this command!");
return true;
}
return false;
}
2020-12-30 04:06:12 +00:00
public void func3()
{
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() * 1000L);
}
}
2020-12-30 04:06:12 +00:00
public boolean func4()
{
if (COOLDOWN_TIMERS.containsKey(sender) && COOLDOWN_TIMERS.containsValue(cmd))
{
msg(ChatColor.RED + "You are on cooldown for this command.");
return true;
}
return false;
}
@NotNull
@Override
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, String[] args)
{
if (cmd != null)
{
return cmd.onTabComplete(sender, this, alias, args);
}
2020-12-30 04:06:12 +00:00
return new ArrayList<>();
}
}
2020-07-22 21:40:58 +00:00
}