2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.command;
|
2020-10-31 04:51:22 +00:00
|
|
|
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.Plex;
|
2022-01-04 03:04:39 +00:00
|
|
|
import dev.plex.cache.DataUtils;
|
|
|
|
import dev.plex.cache.PlayerCache;
|
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.CommandArgumentException;
|
|
|
|
import dev.plex.command.exception.CommandFailException;
|
2022-01-27 05:19:05 +00:00
|
|
|
import dev.plex.command.exception.ConsoleMustDefinePlayerException;
|
|
|
|
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.player.PlexPlayer;
|
|
|
|
import dev.plex.rank.enums.Rank;
|
|
|
|
import dev.plex.util.PlexUtils;
|
2022-01-27 21:22:28 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.UUID;
|
2022-01-27 09:00:50 +00:00
|
|
|
import net.kyori.adventure.audience.Audience;
|
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
2020-11-03 00:19:26 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.World;
|
2022-01-27 21:22:28 +00:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandMap;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.command.ConsoleCommandSender;
|
2020-10-31 08:55:27 +00:00
|
|
|
import org.bukkit.entity.Player;
|
2022-01-27 09:00:50 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2020-10-31 04:51:22 +00:00
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
public abstract class PlexCommand extends Command
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2020-10-31 15:09:13 +00:00
|
|
|
protected static Plex plugin = Plex.get();
|
|
|
|
|
2020-10-31 04:51:22 +00:00
|
|
|
private final CommandParameters params;
|
|
|
|
private final CommandPermissions perms;
|
2020-10-31 08:55:27 +00:00
|
|
|
|
2020-10-31 04:51:22 +00:00
|
|
|
private final Rank level;
|
2020-10-31 08:55:27 +00:00
|
|
|
private final RequiredCommandSource commandSource;
|
2020-10-31 04:51:22 +00:00
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
public PlexCommand()
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
super("");
|
2020-10-31 08:55:27 +00:00
|
|
|
this.params = getClass().getAnnotation(CommandParameters.class);
|
|
|
|
this.perms = getClass().getAnnotation(CommandPermissions.class);
|
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
setName(this.params.name());
|
|
|
|
setLabel(this.params.name());
|
2020-10-31 08:55:27 +00:00
|
|
|
setDescription(params.description());
|
2022-01-27 09:00:50 +00:00
|
|
|
setUsage(params.usage().replace("<command>", this.params.name()));
|
2020-10-31 08:55:27 +00:00
|
|
|
if (params.aliases().split(",").length > 0)
|
|
|
|
{
|
|
|
|
setAliases(Arrays.asList(params.aliases().split(",")));
|
|
|
|
}
|
|
|
|
this.level = perms.level();
|
|
|
|
this.commandSource = perms.source();
|
|
|
|
|
2022-01-04 03:04:39 +00:00
|
|
|
getMap().register("plex", this);
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
protected abstract Component execute(CommandSender sender, String[] args);
|
2020-10-31 08:55:27 +00:00
|
|
|
|
2020-10-31 04:51:22 +00:00
|
|
|
|
2020-10-31 08:55:27 +00:00
|
|
|
@Override
|
2022-01-27 09:00:50 +00:00
|
|
|
public boolean execute(@NotNull CommandSender sender, @NotNull String label, String[] args)
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2020-11-06 01:29:38 +00:00
|
|
|
if (!matches(label))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-06 18:19:38 +00:00
|
|
|
|
2020-11-03 00:19:26 +00:00
|
|
|
if (commandSource == RequiredCommandSource.CONSOLE && sender instanceof Player)
|
2020-10-31 08:55:27 +00:00
|
|
|
{
|
2020-11-05 21:17:14 +00:00
|
|
|
sender.sendMessage(tl("noPermissionInGame"));
|
2020-10-31 08:55:27 +00:00
|
|
|
return true;
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
|
|
|
if (commandSource == RequiredCommandSource.IN_GAME)
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2020-11-03 00:19:26 +00:00
|
|
|
if (sender instanceof ConsoleCommandSender)
|
2020-10-31 08:55:27 +00:00
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
send(sender, tl("noPermissionConsole"));
|
2020-10-31 08:55:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-11-06 01:29:38 +00:00
|
|
|
Player player = (Player)sender;
|
2020-11-06 18:19:38 +00:00
|
|
|
|
2020-10-31 08:55:27 +00:00
|
|
|
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
|
2020-11-02 00:06:08 +00:00
|
|
|
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
send(sender, tl("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMSG())));
|
2020-10-31 08:55:27 +00:00
|
|
|
return true;
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-03 00:19:26 +00:00
|
|
|
try
|
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
Component component = this.execute(sender, args);
|
|
|
|
if (component != null)
|
|
|
|
{
|
|
|
|
send(sender, component);
|
|
|
|
}
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
|
|
|
catch (CommandArgumentException ex)
|
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
send(sender, getUsage().replace("<command>", getLabel()));
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
2022-01-27 21:22:28 +00:00
|
|
|
catch (PlayerNotFoundException | CommandFailException
|
|
|
|
| ConsoleOnlyException | ConsoleMustDefinePlayerException ex)
|
2020-11-03 00:19:26 +00:00
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
send(sender, ex.getMessage());
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
|
|
|
return true;
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 08:55:27 +00:00
|
|
|
private boolean matches(String label)
|
|
|
|
{
|
|
|
|
if (params.aliases().split(",").length > 0)
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2020-10-31 08:55:27 +00:00
|
|
|
for (String alias : params.aliases().split(","))
|
|
|
|
{
|
|
|
|
if (alias.equalsIgnoreCase(label) || getName().equalsIgnoreCase(label))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
|
|
|
else if (params.aliases().split(",").length < 1)
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2020-10-31 08:55:27 +00:00
|
|
|
return getName().equalsIgnoreCase(label);
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
2020-10-31 08:55:27 +00:00
|
|
|
return false;
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
protected PlexPlayer getPlexPlayer(@NotNull Player player)
|
|
|
|
{
|
|
|
|
return DataUtils.getPlayer(player.getUniqueId());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void send(Audience audience, String s)
|
2020-11-03 00:19:26 +00:00
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
audience.sendMessage(componentFromString(s));
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
protected void send(Audience audience, Component component)
|
2020-11-03 00:19:26 +00:00
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
audience.sendMessage(component);
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
|
2020-11-06 03:50:16 +00:00
|
|
|
protected boolean isAdmin(PlexPlayer plexPlayer)
|
|
|
|
{
|
|
|
|
return Plex.get().getRankManager().isAdmin(plexPlayer);
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
protected boolean isAdmin(CommandSender sender)
|
|
|
|
{
|
2022-01-27 21:22:28 +00:00
|
|
|
if (!(sender instanceof Player player))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2022-01-27 09:00:50 +00:00
|
|
|
PlexPlayer plexPlayer = getPlexPlayer(player);
|
|
|
|
return Plex.get().getRankManager().isAdmin(plexPlayer);
|
|
|
|
}
|
|
|
|
|
2020-11-06 03:50:16 +00:00
|
|
|
protected boolean isAdmin(String name)
|
|
|
|
{
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(name);
|
|
|
|
return Plex.get().getRankManager().isAdmin(plexPlayer);
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
protected boolean isSeniorAdmin(CommandSender sender)
|
2020-11-06 03:50:16 +00:00
|
|
|
{
|
2022-01-27 21:22:28 +00:00
|
|
|
if (!(sender instanceof Player player))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2022-01-27 09:00:50 +00:00
|
|
|
PlexPlayer plexPlayer = getPlexPlayer(player);
|
|
|
|
return Plex.get().getRankManager().isSeniorAdmin(plexPlayer);
|
2020-11-06 03:50:16 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
protected UUID getUUID(CommandSender sender)
|
2020-11-06 03:50:16 +00:00
|
|
|
{
|
2022-01-27 21:22:28 +00:00
|
|
|
if (!(sender instanceof Player player))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-01-27 09:00:50 +00:00
|
|
|
return player.getUniqueId();
|
2020-11-06 03:50:16 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
|
|
|
|
protected boolean isConsole(CommandSender sender)
|
2020-11-05 21:17:14 +00:00
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
return !(sender instanceof Player);
|
2020-11-05 21:17:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
protected Component tl(String s, Object... objects)
|
2020-11-03 00:19:26 +00:00
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
return componentFromString(PlexUtils.tl(s, objects));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Component usage(String s)
|
|
|
|
{
|
|
|
|
return componentFromString(ChatColor.YELLOW + "Correct Usage: " + ChatColor.GRAY + s);
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected Player getNonNullPlayer(String name)
|
|
|
|
{
|
|
|
|
Player player = Bukkit.getPlayer(name);
|
|
|
|
if (player == null)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-03 00:19:26 +00:00
|
|
|
throw new PlayerNotFoundException();
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-03 00:19:26 +00:00
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected PlexPlayer getOnlinePlexPlayer(String name)
|
|
|
|
{
|
|
|
|
Player player = getNonNullPlayer(name);
|
|
|
|
PlexPlayer plexPlayer = PlayerCache.getPlexPlayer(player.getUniqueId());
|
|
|
|
if (plexPlayer == null)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-03 00:19:26 +00:00
|
|
|
throw new PlayerNotFoundException();
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-03 00:19:26 +00:00
|
|
|
return plexPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected PlexPlayer getOfflinePlexPlayer(UUID uuid)
|
|
|
|
{
|
|
|
|
PlexPlayer plexPlayer = PlayerCache.getPlexPlayer(uuid);
|
|
|
|
if (plexPlayer == null)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-03 00:19:26 +00:00
|
|
|
throw new PlayerNotFoundException();
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-03 00:19:26 +00:00
|
|
|
return plexPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected World getNonNullWorld(String name)
|
|
|
|
{
|
|
|
|
World world = Bukkit.getWorld(name);
|
|
|
|
if (world == null)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2022-01-27 09:00:50 +00:00
|
|
|
throw new CommandFailException(PlexUtils.tl("worldNotFound"));
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-03 00:19:26 +00:00
|
|
|
return world;
|
|
|
|
}
|
2020-10-31 08:55:27 +00:00
|
|
|
|
2022-01-27 09:00:50 +00:00
|
|
|
protected Component componentFromString(String s)
|
|
|
|
{
|
|
|
|
return LegacyComponentSerializer.legacyAmpersand().deserialize(s);
|
|
|
|
}
|
|
|
|
|
2020-10-31 08:55:27 +00:00
|
|
|
public Rank getLevel()
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2020-10-31 08:55:27 +00:00
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CommandMap getMap()
|
|
|
|
{
|
|
|
|
return Plex.get().getServer().getCommandMap();
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
|
|
|
}
|