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

181 lines
5.3 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;
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.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;
2022-01-27 21:23:01 +00:00
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
2022-01-27 05:28:30 +00:00
import net.kyori.adventure.text.Component;
2020-11-05 21:17:14 +00:00
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
2022-01-27 05:28:30 +00:00
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
2022-01-27 05:28:30 +00:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2020-11-05 21:17:14 +00:00
2022-02-22 06:57:26 +00:00
@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.ANY)
2022-01-27 05:28:30 +00:00
@CommandParameters(name = "admin", 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-05 21:17:14 +00:00
@Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args)
2020-11-05 21:17:14 +00:00
{
if (args.length == 0)
{
return usage();
2020-11-05 21:17:14 +00:00
}
if (args[0].equalsIgnoreCase("add"))
{
if (args.length != 2)
{
2022-01-27 05:28:30 +00:00
return usage("/admin add <player>");
2020-11-05 21:17:14 +00:00
}
2022-01-27 05:28:30 +00:00
if (!isConsole(sender))
{
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))
{
2022-01-27 05:28:30 +00:00
return tl("playerIsAdmin");
}
Bukkit.getServer().getPluginManager().callEvent(new AdminAddEvent(sender, plexPlayer));
2022-01-27 05:28:30 +00:00
return null;
2020-11-05 21:17:14 +00:00
}
if (args[0].equalsIgnoreCase("remove"))
{
if (args.length != 2)
{
2022-01-27 05:28:30 +00:00
return usage("/admin remove <player>");
2020-11-05 21:17:14 +00:00
}
2022-01-27 05:28:30 +00:00
if (!isConsole(sender))
{
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))
{
2022-01-27 05:28:30 +00:00
return tl("playerNotAdmin");
}
Bukkit.getServer().getPluginManager().callEvent(new AdminRemoveEvent(sender, plexPlayer));
2022-01-27 05:28:30 +00:00
return null;
2020-11-05 21:17:14 +00:00
}
if (args[0].equalsIgnoreCase("setrank"))
{
if (args.length != 3)
{
2022-01-27 05:28:30 +00:00
return usage("/admin setrank <player> <rank>");
2020-11-05 21:17:14 +00:00
}
2022-01-27 05:28:30 +00:00
if (!isConsole(sender))
{
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]))
{
2022-01-27 05:28:30 +00:00
return tl("rankNotFound");
2020-11-05 21:17:14 +00:00
}
Rank rank = Rank.valueOf(args[2].toUpperCase());
if (!rank.isAtLeast(Rank.ADMIN))
{
2022-01-27 05:28:30 +00:00
return tl("rankMustBeHigherThanAdmin");
2020-11-05 21:17:14 +00:00
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (!isAdmin(plexPlayer))
{
2022-01-27 05:28:30 +00:00
return tl("playerNotAdmin");
}
Bukkit.getServer().getPluginManager().callEvent(new AdminSetRankEvent(sender, plexPlayer, rank));
2020-11-05 21:17:14 +00:00
2022-01-27 05:28:30 +00:00
return null;
2020-11-05 21:17:14 +00:00
}
if (args[0].equalsIgnoreCase("list"))
{
if (args.length != 1)
{
2022-01-27 05:28:30 +00:00
return usage("/admin list");
2020-11-05 21:17:14 +00:00
}
return componentFromString("Admins: " + StringUtils.join(plugin.getAdminList().getAllAdmins(), ", "));
2020-11-05 21:17:14 +00:00
}
2022-01-27 05:28:30 +00:00
return null;
2020-11-05 21:17:14 +00:00
}
@Override
2022-01-27 05:28:30 +00:00
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
2020-11-06 01:29:38 +00:00
{
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();
}
2022-01-27 05:28:30 +00:00
2020-11-05 21:17:14 +00:00
private boolean rankExists(String rank)
{
for (Rank ranks : Rank.values())
{
if (ranks.name().equalsIgnoreCase(rank))
{
return true;
}
}
return false;
}
}