Plex/src/main/java/dev/plex/command/impl/AdminCMD.java

198 lines
5.6 KiB
Java
Raw Normal View History

2021-01-03 07:21:15 +00:00
package dev.plex.command.impl;
2020-11-05 21:17:14 +00:00
import com.google.common.collect.ImmutableList;
2021-01-03 07:21:15 +00:00
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
2021-06-20 08:02:07 +00:00
import dev.plex.command.exception.ConsoleOnlyException;
2021-01-03 07:21:15 +00:00
import dev.plex.command.exception.PlayerNotFoundException;
import dev.plex.command.source.CommandSource;
import dev.plex.command.source.RequiredCommandSource;
2020-11-06 01:29:38 +00:00
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
2021-01-03 07:21:15 +00:00
import dev.plex.cache.DataUtils;
import dev.plex.command.PlexCommand;
import dev.plex.event.AdminAddEvent;
import dev.plex.event.AdminRemoveEvent;
import dev.plex.event.AdminSetRankEvent;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
2020-11-05 21:17:14 +00:00
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
@CommandPermissions(level = Rank.SENIOR_ADMIN, source = RequiredCommandSource.ANY)
@CommandParameters(usage = "/<command> <add | remove | setrank | list> [player] [rank]", aliases = "saconfig,slconfig,adminconfig,adminmanage", description = "Manage all admins")
2020-11-05 21:17:14 +00:00
public class AdminCMD extends PlexCommand
{
//TODO: Better return messages
2020-11-06 01:29:38 +00:00
public AdminCMD()
{
2020-11-05 21:17:14 +00:00
super("admin");
}
@Override
public void execute(CommandSource sender, String[] args)
{
if (args.length == 0)
{
sender.send(usage(getUsage()));
return;
}
if (args[0].equalsIgnoreCase("add"))
{
if (args.length != 2)
{
sender.send(usage("/admin add <player>"));
return;
}
if (!sender.isConsoleSender())
{
2021-06-20 08:02:07 +00:00
throw new ConsoleOnlyException();
}
2020-11-05 21:17:14 +00:00
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{
throw new PlayerNotFoundException();
2020-11-05 21:17:14 +00:00
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (isAdmin(plexPlayer))
{
2021-06-20 08:02:07 +00:00
sender.send(tl("playerIsAdmin"));
return;
}
2020-11-05 21:17:14 +00:00
plexPlayer.setRank(Rank.ADMIN.name());
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminAddEvent(sender, plexPlayer));
2020-11-05 21:17:14 +00:00
return;
}
if (args[0].equalsIgnoreCase("remove"))
{
if (args.length != 2)
{
sender.send(usage("/admin remove <player>"));
return;
}
if (!sender.isConsoleSender())
{
2021-06-20 08:02:07 +00:00
throw new ConsoleOnlyException();
}
2020-11-05 21:17:14 +00:00
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{
throw new PlayerNotFoundException();
2020-11-05 21:17:14 +00:00
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (!isAdmin(plexPlayer))
{
2021-06-20 08:02:07 +00:00
sender.send(tl("playerNotAdmin"));
return;
}
2020-11-05 21:17:14 +00:00
plexPlayer.setRank("");
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminRemoveEvent(sender, plexPlayer));
2020-11-05 21:17:14 +00:00
return;
}
if (args[0].equalsIgnoreCase("setrank"))
{
if (args.length != 3)
{
sender.send(usage("/admin setrank <player> <rank>"));
return;
}
if (!sender.isConsoleSender())
{
2021-06-20 08:02:07 +00:00
throw new ConsoleOnlyException();
}
2020-11-05 21:17:14 +00:00
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{
throw new PlayerNotFoundException();
2020-11-05 21:17:14 +00:00
}
if (!rankExists(args[2]))
{
2021-06-20 08:02:07 +00:00
sender.send(tl("rankNotFound"));
2020-11-05 21:17:14 +00:00
return;
}
Rank rank = Rank.valueOf(args[2].toUpperCase());
if (!rank.isAtLeast(Rank.ADMIN))
{
2021-06-20 08:02:07 +00:00
sender.send(tl("rankMustBeHigherThanAdmin"));
2020-11-05 21:17:14 +00:00
return;
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (!isAdmin(plexPlayer))
{
2021-06-20 08:02:07 +00:00
sender.send(tl("playerNotAdmin"));
return;
}
2020-11-05 21:17:14 +00:00
plexPlayer.setRank(rank.name().toLowerCase());
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminSetRankEvent(sender, plexPlayer, rank));
2020-11-05 21:17:14 +00:00
return;
}
if (args[0].equalsIgnoreCase("list"))
{
if (args.length != 1)
{
sender.send(usage("/admin list"));
return;
}
sender.send("Admins: " + StringUtils.join(plugin.getAdminList().getAllAdmins(), ", "));
return;
}
}
@Override
2020-11-06 01:29:38 +00:00
public List<String> onTabComplete(CommandSource sender, String[] args)
{
2020-11-05 21:17:14 +00:00
if (args.length == 1)
{
return Arrays.asList("add", "remove", "setrank", "list");
2020-11-06 01:29:38 +00:00
}
else if (args.length == 2 && !args[0].equalsIgnoreCase("list"))
2020-11-05 21:17:14 +00:00
{
return PlexUtils.getPlayerNameList();
}
return ImmutableList.of();
}
private boolean rankExists(String rank)
{
for (Rank ranks : Rank.values())
{
if (ranks.name().equalsIgnoreCase(rank))
{
return true;
}
}
return false;
}
}