Plex/src/main/java/dev/plex/command/impl/AdminCMD.java
spacerocket62 8202021f07 Migrate (what I could find) legacy component system uses to kyori component system
Create List command
Remove fionn command
Remove test command
Add Mojang Utils
Auto add Plex Players back to cache on start if any are online
2022-01-27 01:00:50 -08:00

187 lines
5.5 KiB
Java

package dev.plex.command.impl;
import com.google.common.collect.ImmutableList;
import dev.plex.cache.DataUtils;
import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.ConsoleOnlyException;
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;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@CommandPermissions(level = Rank.SENIOR_ADMIN, source = RequiredCommandSource.ANY)
@CommandParameters(name = "admin", usage = "/<command> <add | remove | setrank | list> [player] [rank]", aliases = "saconfig,slconfig,adminconfig,adminmanage", description = "Manage all admins")
public class AdminCMD extends PlexCommand
{
//TODO: Better return messages
@Override
public Component execute(CommandSender sender, String[] args)
{
if (args.length == 0)
{
return usage(getUsage());
}
if (args[0].equalsIgnoreCase("add"))
{
if (args.length != 2)
{
return usage("/admin add <player>");
}
if (!isConsole(sender))
{
throw new ConsoleOnlyException();
}
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{
throw new PlayerNotFoundException();
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (isAdmin(plexPlayer))
{
return tl("playerIsAdmin");
}
plexPlayer.setRank(Rank.ADMIN.name());
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminAddEvent(sender, plexPlayer));
return null;
}
if (args[0].equalsIgnoreCase("remove"))
{
if (args.length != 2)
{
return usage("/admin remove <player>");
}
if (!isConsole(sender))
{
throw new ConsoleOnlyException();
}
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{
throw new PlayerNotFoundException();
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (!isAdmin(plexPlayer))
{
return tl("playerNotAdmin");
}
plexPlayer.setRank("");
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminRemoveEvent(sender, plexPlayer));
return null;
}
if (args[0].equalsIgnoreCase("setrank"))
{
if (args.length != 3)
{
return usage("/admin setrank <player> <rank>");
}
if (!isConsole(sender))
{
throw new ConsoleOnlyException();
}
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{
throw new PlayerNotFoundException();
}
if (!rankExists(args[2]))
{
return tl("rankNotFound");
}
Rank rank = Rank.valueOf(args[2].toUpperCase());
if (!rank.isAtLeast(Rank.ADMIN))
{
return tl("rankMustBeHigherThanAdmin");
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (!isAdmin(plexPlayer))
{
return tl("playerNotAdmin");
}
plexPlayer.setRank(rank.name().toLowerCase());
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminSetRankEvent(sender, plexPlayer, rank));
return null;
}
if (args[0].equalsIgnoreCase("list"))
{
if (args.length != 1)
{
return usage("/admin list");
}
return componentFromString("Admins: " + StringUtils.join(plugin.getAdminList().getAllAdmins(), ", "));
}
return null;
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
if (args.length == 1)
{
return Arrays.asList("add", "remove", "setrank", "list");
}
else if (args.length == 2 && !args[0].equalsIgnoreCase("list"))
{
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;
}
}