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

200 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;
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())
{
sender.send("Console only");
return;
}
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))
{
sender.send("Player is an admin");
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())
{
sender.send("Console only");
return;
}
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))
{
sender.send("Player is not an admin");
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())
{
sender.send("Console only");
return;
}
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]))
{
sender.send("Rank not found");
return;
}
Rank rank = Rank.valueOf(args[2].toUpperCase());
if (!rank.isAtLeast(Rank.ADMIN))
{
sender.send("Must be admin+");
return;
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (!isAdmin(plexPlayer))
{
sender.send("Player is not an admin");
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;
}
}