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;
|
2022-03-19 03:33:23 +00:00
|
|
|
import dev.plex.command.annotation.System;
|
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;
|
2022-02-04 04:01:30 +00:00
|
|
|
import org.bukkit.entity.Player;
|
2022-01-27 05:28:30 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2022-02-04 04:49:05 +00:00
|
|
|
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-03-04 01:51:07 +00:00
|
|
|
@CommandParameters(name = "admin", usage = "/<command> <add <player> | remove <player> | setrank <player> <rank> | list>", aliases = "saconfig,slconfig,adminconfig,adminmanage", description = "Manage all admins")
|
2022-03-19 03:33:23 +00:00
|
|
|
@System("ranks")
|
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-05 21:17:14 +00:00
|
|
|
@Override
|
2022-02-04 04:49:05 +00:00
|
|
|
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args)
|
2020-11-05 21:17:14 +00:00
|
|
|
{
|
|
|
|
if (args.length == 0)
|
|
|
|
{
|
2022-02-14 05:55:50 +00:00
|
|
|
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))
|
2020-11-06 18:19:38 +00:00
|
|
|
{
|
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))
|
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
return messageComponent("playerIsAdmin");
|
2020-11-06 18:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2020-11-06 18:19:38 +00:00
|
|
|
{
|
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))
|
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
return messageComponent("playerNotAdmin");
|
2020-11-06 18:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2020-11-06 18:19:38 +00:00
|
|
|
{
|
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]))
|
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
return messageComponent("rankNotFound");
|
2020-11-05 21:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Rank rank = Rank.valueOf(args[2].toUpperCase());
|
|
|
|
|
|
|
|
if (!rank.isAtLeast(Rank.ADMIN))
|
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
return messageComponent("rankMustBeHigherThanAdmin");
|
2020-11-05 21:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
|
2020-11-06 18:19:38 +00:00
|
|
|
|
|
|
|
if (!isAdmin(plexPlayer))
|
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
return messageComponent("playerNotAdmin");
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:50 +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;
|
|
|
|
}
|
|
|
|
}
|