Plex/src/main/java/dev/plex/command/PlexCommand.java

262 lines
7.4 KiB
Java
Raw Normal View History

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