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;
|
2022-01-04 03:04:39 +00:00
|
|
|
import dev.plex.cache.DataUtils;
|
|
|
|
import dev.plex.command.PlexCommand;
|
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;
|
|
|
|
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;
|
|
|
|
|
2022-01-04 03:04:39 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2020-11-06 18:19:38 +00:00
|
|
|
@CommandPermissions(level = Rank.SENIOR_ADMIN, source = RequiredCommandSource.ANY)
|
2020-11-06 03:50:16 +00:00
|
|
|
@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
|
|
|
|
{
|
2020-11-06 18:19:38 +00:00
|
|
|
//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;
|
|
|
|
}
|
|
|
|
|
2020-11-06 18:19:38 +00:00
|
|
|
if (!sender.isConsoleSender())
|
|
|
|
{
|
2022-01-04 03:04:39 +00:00
|
|
|
sender.send(tl("consoleOnly"));
|
2021-06-20 08:02:07 +00:00
|
|
|
throw new ConsoleOnlyException();
|
2020-11-06 18:19:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:17:14 +00:00
|
|
|
UUID targetUUID = PlexUtils.getFromName(args[1]);
|
|
|
|
|
|
|
|
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
|
|
|
|
{
|
2020-11-06 07:07:28 +00:00
|
|
|
throw new PlayerNotFoundException();
|
2020-11-05 21:17:14 +00:00
|
|
|
}
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
|
2020-11-06 18:19:38 +00:00
|
|
|
|
|
|
|
if (isAdmin(plexPlayer))
|
|
|
|
{
|
2021-06-20 08:02:07 +00:00
|
|
|
sender.send(tl("playerIsAdmin"));
|
2020-11-06 18:19:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:17:14 +00:00
|
|
|
plexPlayer.setRank(Rank.ADMIN.name());
|
|
|
|
DataUtils.update(plexPlayer);
|
2020-11-06 18:19:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-06 18:19:38 +00:00
|
|
|
if (!sender.isConsoleSender())
|
|
|
|
{
|
2021-06-20 08:02:07 +00:00
|
|
|
throw new ConsoleOnlyException();
|
2020-11-06 18:19:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:17:14 +00:00
|
|
|
UUID targetUUID = PlexUtils.getFromName(args[1]);
|
|
|
|
|
|
|
|
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
|
|
|
|
{
|
2020-11-06 07:07:28 +00:00
|
|
|
throw new PlayerNotFoundException();
|
2020-11-05 21:17:14 +00:00
|
|
|
}
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
|
2020-11-06 18:19:38 +00:00
|
|
|
|
|
|
|
if (!isAdmin(plexPlayer))
|
|
|
|
{
|
2021-06-20 08:02:07 +00:00
|
|
|
sender.send(tl("playerNotAdmin"));
|
2020-11-06 18:19:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:17:14 +00:00
|
|
|
plexPlayer.setRank("");
|
|
|
|
DataUtils.update(plexPlayer);
|
2020-11-06 18:19:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-06 18:19:38 +00:00
|
|
|
if (!sender.isConsoleSender())
|
|
|
|
{
|
2021-06-20 08:02:07 +00:00
|
|
|
throw new ConsoleOnlyException();
|
2020-11-06 18:19:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 21:17:14 +00:00
|
|
|
UUID targetUUID = PlexUtils.getFromName(args[1]);
|
|
|
|
|
|
|
|
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
|
|
|
|
{
|
2020-11-06 07:07:28 +00:00
|
|
|
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);
|
2020-11-06 18:19:38 +00:00
|
|
|
|
|
|
|
if (!isAdmin(plexPlayer))
|
|
|
|
{
|
2021-06-20 08:02:07 +00:00
|
|
|
sender.send(tl("playerNotAdmin"));
|
2020-11-06 18:19:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:17:14 +00:00
|
|
|
plexPlayer.setRank(rank.name().toLowerCase());
|
|
|
|
DataUtils.update(plexPlayer);
|
|
|
|
|
2020-11-06 18:19:38 +00:00
|
|
|
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(), ", "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
}
|