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;
|
2022-04-11 17:56:26 +00:00
|
|
|
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;
|
2022-04-13 02:22:17 +00:00
|
|
|
import dev.plex.command.exception.CommandFailException;
|
|
|
|
import dev.plex.command.exception.ConsoleMustDefinePlayerException;
|
|
|
|
import dev.plex.command.exception.ConsoleOnlyException;
|
|
|
|
import dev.plex.command.exception.PlayerNotBannedException;
|
|
|
|
import dev.plex.command.exception.PlayerNotFoundException;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.command.source.RequiredCommandSource;
|
|
|
|
import dev.plex.player.PlexPlayer;
|
|
|
|
import dev.plex.rank.enums.Rank;
|
2022-01-29 22:35:48 +00:00
|
|
|
import dev.plex.util.PlexLog;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.util.PlexUtils;
|
2022-04-13 02:22:17 +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;
|
2022-03-17 22:16:17 +00:00
|
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
2022-01-27 09:00:50 +00:00
|
|
|
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-04-13 02:22:17 +00:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandMap;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.command.ConsoleCommandSender;
|
|
|
|
import org.bukkit.command.PluginIdentifiableCommand;
|
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;
|
2022-02-04 04:01:30 +00:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2020-10-31 04:51:22 +00:00
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Superclass for all commands
|
|
|
|
*/
|
2022-02-04 04:01:30 +00:00
|
|
|
public abstract class PlexCommand extends Command implements PluginIdentifiableCommand
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Returns the instance of the plugin
|
|
|
|
*/
|
2020-10-31 15:09:13 +00:00
|
|
|
protected static Plex plugin = Plex.get();
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* The parameters for the command
|
|
|
|
*/
|
2020-10-31 04:51:22 +00:00
|
|
|
private final CommandParameters params;
|
2022-02-05 23:14:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The permissions for the command
|
|
|
|
*/
|
2020-10-31 04:51:22 +00:00
|
|
|
private final CommandPermissions perms;
|
2020-10-31 08:55:27 +00:00
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Minimum required rank fetched from the permissions
|
|
|
|
*/
|
2020-10-31 04:51:22 +00:00
|
|
|
private final Rank level;
|
2022-02-05 23:14:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Required command source fetched from the permissions
|
|
|
|
*/
|
2020-10-31 08:55:27 +00:00
|
|
|
private final RequiredCommandSource commandSource;
|
2020-10-31 04:51:22 +00:00
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Creates an instance of the command
|
|
|
|
*/
|
2022-05-06 01:54:08 +00:00
|
|
|
public PlexCommand(boolean register)
|
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-05-06 01:54:08 +00:00
|
|
|
if (register)
|
|
|
|
{
|
|
|
|
getMap().register("plex", this);
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 05:08:45 +00:00
|
|
|
|
2022-05-06 01:54:08 +00:00
|
|
|
public PlexCommand()
|
|
|
|
{
|
|
|
|
this(true);
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Executes the command
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
|
|
|
* @param sender The sender of the command
|
2022-03-17 22:16:17 +00:00
|
|
|
* @param playerSender The player who executed the command (null if CommandSource is console or if CommandSource is any but console executed)
|
2022-02-07 05:58:55 +00:00
|
|
|
* @param args A Kyori Component to send to the sender (can be null)
|
2022-02-05 23:14:23 +00:00
|
|
|
*/
|
2022-02-04 04:01:30 +00:00
|
|
|
protected abstract Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args);
|
2020-10-31 08:55:27 +00:00
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
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
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
sender.sendMessage(messageComponent("noPermissionInGame"));
|
2020-10-31 08:55:27 +00:00
|
|
|
return true;
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
2022-02-14 05:55:50 +00:00
|
|
|
|
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-02-25 07:09:55 +00:00
|
|
|
send(sender, messageComponent("noPermissionConsole"));
|
2020-10-31 08:55:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-02-14 05:55:50 +00:00
|
|
|
}
|
2020-11-06 18:19:38 +00:00
|
|
|
|
2022-02-14 05:55:50 +00:00
|
|
|
if (sender instanceof Player player)
|
|
|
|
{
|
2020-10-31 08:55:27 +00:00
|
|
|
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
|
2022-01-29 22:35:48 +00:00
|
|
|
|
2022-02-01 06:31:06 +00:00
|
|
|
if (plugin.getSystem().equalsIgnoreCase("ranks"))
|
2022-01-29 22:35:48 +00:00
|
|
|
{
|
|
|
|
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
|
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
send(sender, messageComponent("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMessage())));
|
2022-02-05 21:51:15 +00:00
|
|
|
return true;
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
else
|
2022-04-07 07:37:31 +00:00
|
|
|
{
|
|
|
|
if (getLevel().isAtLeast(Rank.ADMIN) && !plexPlayer.isAdminActive())
|
|
|
|
{
|
|
|
|
send(sender, messageComponent("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMessage())));
|
|
|
|
return true;
|
|
|
|
}
|
2022-01-29 22:35:48 +00:00
|
|
|
}
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
|
2022-01-29 22:35:48 +00:00
|
|
|
{
|
2022-05-11 05:48:47 +00:00
|
|
|
if (!perms.permission().isEmpty() && !plugin.getPermissionHandler().hasPermission(player, perms.permission()))
|
2022-01-29 22:35:48 +00:00
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
send(sender, messageComponent("noPermissionNode", perms.permission()));
|
2022-02-05 21:51:15 +00:00
|
|
|
return true;
|
2022-01-29 22:35:48 +00:00
|
|
|
}
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
else
|
2022-04-10 09:31:07 +00:00
|
|
|
{
|
|
|
|
PlexLog.error("Neither permissions or ranks were selected to be used in the configuration file!");
|
|
|
|
send(sender, "There is a server misconfiguration. Please alert a developer or the owner");
|
|
|
|
return true;
|
2022-04-09 05:08:18 +00:00
|
|
|
}
|
2022-04-10 09:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sender instanceof ConsoleCommandSender && !sender.getName().equalsIgnoreCase("console")) //telnet
|
|
|
|
{
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(sender.getName());
|
|
|
|
|
|
|
|
if (plugin.getSystem().equalsIgnoreCase("ranks"))
|
|
|
|
{
|
|
|
|
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
|
|
|
|
{
|
|
|
|
send(sender, messageComponent("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMessage())));
|
|
|
|
return true;
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
else
|
2022-04-10 09:31:07 +00:00
|
|
|
{
|
|
|
|
if (getLevel().isAtLeast(Rank.ADMIN) && !plexPlayer.isAdminActive())
|
|
|
|
{
|
|
|
|
send(sender, messageComponent("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMessage())));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
|
2022-04-10 09:31:07 +00:00
|
|
|
{
|
2022-05-11 05:48:47 +00:00
|
|
|
if (!perms.permission().isEmpty() && !plugin.getPermissionHandler().hasPermission(Bukkit.getOfflinePlayer(plexPlayer.getName()), perms.permission()))
|
2022-04-10 09:31:07 +00:00
|
|
|
{
|
|
|
|
send(sender, messageComponent("noPermissionNode", perms.permission()));
|
|
|
|
return true;
|
2022-04-11 00:22:22 +00:00
|
|
|
}
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2022-01-29 22:35:48 +00:00
|
|
|
PlexLog.error("Neither permissions or ranks were selected to be used in the configuration file!");
|
|
|
|
send(sender, "There is a server misconfiguration. Please alert a developer or the owner");
|
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-04-13 02:22:17 +00:00
|
|
|
Component component = this.execute(sender, isConsole(sender) ? null : (Player)sender, args);
|
2022-01-27 09:00:50 +00:00
|
|
|
if (component != null)
|
|
|
|
{
|
|
|
|
send(sender, component);
|
|
|
|
}
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
catch (PlayerNotFoundException | CommandFailException | ConsoleOnlyException |
|
2022-04-19 20:16:07 +00:00
|
|
|
ConsoleMustDefinePlayerException | PlayerNotBannedException | NumberFormatException ex)
|
2020-11-03 00:19:26 +00:00
|
|
|
{
|
2022-04-10 01:53:27 +00:00
|
|
|
send(sender, PlexUtils.mmDeserialize(ex.getMessage()));
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
|
|
|
return true;
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
2022-03-17 22:16:17 +00:00
|
|
|
* Checks if the String given is a matching command
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-03-17 22:16:17 +00:00
|
|
|
* @param label The String to check
|
2022-02-05 23:14:23 +00:00
|
|
|
* @return true if the string is a command name or alias
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
}
|
2022-04-13 02:22:17 +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-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Gets a PlexPlayer from Player object
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param player The player object
|
2022-02-07 05:58:55 +00:00
|
|
|
* @return PlexPlayer Object
|
2022-02-05 23:14:23 +00:00
|
|
|
* @see PlexPlayer
|
|
|
|
*/
|
2022-01-27 09:00:50 +00:00
|
|
|
protected PlexPlayer getPlexPlayer(@NotNull Player player)
|
|
|
|
{
|
|
|
|
return DataUtils.getPlayer(player.getUniqueId());
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
2022-03-17 22:16:17 +00:00
|
|
|
* Sends a message to an Audience
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-03-17 22:16:17 +00:00
|
|
|
* @param audience The Audience to send the message to
|
2022-02-07 05:58:55 +00:00
|
|
|
* @param s The message to send
|
2022-02-05 23:14:23 +00:00
|
|
|
*/
|
2022-01-27 09:00:50 +00:00
|
|
|
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-02-05 23:14:23 +00:00
|
|
|
/**
|
2022-03-17 22:16:17 +00:00
|
|
|
* Sends a message to an Audience
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-03-17 22:16:17 +00:00
|
|
|
* @param audience The Audience to send the message to
|
|
|
|
* @param component The Component to send
|
2022-02-05 23:14:23 +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-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a sender has enough permissions or is high enough a rank
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-03-17 22:16:17 +00:00
|
|
|
* @param sender A CommandSender
|
2022-02-07 05:58:55 +00:00
|
|
|
* @param rank The rank to check (if the server is using ranks)
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param permission The permission to check (if the server is using permissions)
|
|
|
|
* @return true if the sender has enough permissions
|
|
|
|
* @see Rank
|
|
|
|
*/
|
2022-01-30 20:56:08 +00:00
|
|
|
protected boolean checkRank(CommandSender sender, Rank rank, String permission)
|
|
|
|
{
|
|
|
|
if (!isConsole(sender))
|
|
|
|
{
|
2022-04-13 02:22:17 +00:00
|
|
|
return checkRank((Player)sender, rank, permission);
|
2022-04-10 09:31:07 +00:00
|
|
|
}
|
|
|
|
if (!sender.getName().equalsIgnoreCase("console"))
|
|
|
|
{
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(sender.getName());
|
|
|
|
if (plugin.getSystem().equalsIgnoreCase("ranks"))
|
|
|
|
{
|
|
|
|
if (!plexPlayer.getRankFromString().isAtLeast(rank))
|
|
|
|
{
|
|
|
|
throw new CommandFailException(PlexUtils.messageString("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage())));
|
|
|
|
}
|
|
|
|
if (rank.isAtLeast(Rank.ADMIN) && !plexPlayer.isAdminActive())
|
|
|
|
{
|
|
|
|
throw new CommandFailException(PlexUtils.messageString("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage())));
|
|
|
|
}
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
|
2022-04-10 09:31:07 +00:00
|
|
|
{
|
2022-05-11 05:48:47 +00:00
|
|
|
if (!perms.permission().isEmpty() && !plugin.getPermissionHandler().hasPermission(Bukkit.getOfflinePlayer(plexPlayer.getName()), perms.permission()))
|
2022-04-10 09:31:07 +00:00
|
|
|
{
|
|
|
|
throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission));
|
2022-04-11 00:22:22 +00:00
|
|
|
}
|
2022-04-10 09:31:07 +00:00
|
|
|
}
|
2022-01-30 20:56:08 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a player has enough permissions or is high enough a rank
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
|
|
|
* @param player The player object
|
|
|
|
* @param rank The rank to check (if the server is using ranks)
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param permission The permission to check (if the server is using permissions)
|
|
|
|
* @return true if the sender has enough permissions
|
|
|
|
* @see Rank
|
|
|
|
*/
|
2022-01-30 00:43:44 +00:00
|
|
|
protected boolean checkRank(Player player, Rank rank, String permission)
|
2022-01-29 22:35:48 +00:00
|
|
|
{
|
2022-02-14 05:55:50 +00:00
|
|
|
if (player instanceof ConsoleCommandSender)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2022-01-29 22:35:48 +00:00
|
|
|
PlexPlayer plexPlayer = getPlexPlayer(player);
|
2022-02-01 06:31:06 +00:00
|
|
|
if (plugin.getSystem().equalsIgnoreCase("ranks"))
|
2022-01-29 22:35:48 +00:00
|
|
|
{
|
2022-02-22 06:55:59 +00:00
|
|
|
if (!plexPlayer.getRankFromString().isAtLeast(rank))
|
2022-01-30 00:43:44 +00:00
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
throw new CommandFailException(PlexUtils.messageString("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage())));
|
2022-01-30 00:43:44 +00:00
|
|
|
}
|
2022-04-07 07:37:31 +00:00
|
|
|
if (rank.isAtLeast(Rank.ADMIN) && !plexPlayer.isAdminActive())
|
|
|
|
{
|
|
|
|
throw new CommandFailException(PlexUtils.messageString("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage())));
|
|
|
|
}
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
|
2022-01-29 22:35:48 +00:00
|
|
|
{
|
2022-05-11 05:48:47 +00:00
|
|
|
if (!perms.permission().isEmpty() && !plugin.getPermissionHandler().hasPermission(player, permission))
|
2022-01-29 22:35:48 +00:00
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission));
|
2022-01-29 22:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-30 00:43:44 +00:00
|
|
|
return true;
|
2022-01-29 22:35:48 +00:00
|
|
|
}
|
2022-01-27 09:00:50 +00:00
|
|
|
|
2022-03-25 05:45:55 +00:00
|
|
|
protected boolean silentCheckRank(Player player, Rank rank, String permission)
|
|
|
|
{
|
|
|
|
if (player instanceof ConsoleCommandSender)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
PlexPlayer plexPlayer = getPlexPlayer(player);
|
|
|
|
if (plugin.getSystem().equalsIgnoreCase("ranks"))
|
|
|
|
{
|
2022-04-07 07:37:31 +00:00
|
|
|
return rank.isAtLeast(Rank.ADMIN) ? plexPlayer.isAdminActive() && plexPlayer.getRankFromString().isAtLeast(rank) : plexPlayer.getRankFromString().isAtLeast(rank);
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
|
2022-03-25 05:45:55 +00:00
|
|
|
{
|
2022-05-11 05:48:47 +00:00
|
|
|
return !perms.permission().isEmpty() && plugin.getPermissionHandler().hasPermission(player, permission);
|
2022-03-25 05:45:55 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-14 05:55:50 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a sender has enough permissions or is high enough a rank
|
|
|
|
*
|
|
|
|
* @param sender The player object
|
|
|
|
* @param rank The rank to check (if the server is using ranks)
|
|
|
|
* @param permission The permission to check (if the server is using permissions)
|
|
|
|
* @return true if the sender has enough permissions
|
|
|
|
* @see Rank
|
|
|
|
*/
|
|
|
|
protected boolean checkTab(CommandSender sender, Rank rank, String permission)
|
|
|
|
{
|
|
|
|
if (!isConsole(sender))
|
|
|
|
{
|
2022-04-13 02:22:17 +00:00
|
|
|
return checkTab((Player)sender, rank, permission);
|
2022-02-14 05:55:50 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether a player has enough permissions or is high enough a rank
|
|
|
|
*
|
|
|
|
* @param player The player object
|
|
|
|
* @param rank The rank to check (if the server is using ranks)
|
|
|
|
* @param permission The permission to check (if the server is using permissions)
|
|
|
|
* @return true if the sender has enough permissions
|
|
|
|
* @see Rank
|
|
|
|
*/
|
|
|
|
protected boolean checkTab(Player player, Rank rank, String permission)
|
|
|
|
{
|
|
|
|
PlexPlayer plexPlayer = getPlexPlayer(player);
|
|
|
|
if (plugin.getSystem().equalsIgnoreCase("ranks"))
|
|
|
|
{
|
2022-04-07 07:37:31 +00:00
|
|
|
return rank.isAtLeast(Rank.ADMIN) ? plexPlayer.isAdminActive() && plexPlayer.getRankFromString().isAtLeast(rank) : plexPlayer.getRankFromString().isAtLeast(rank);
|
2022-04-13 02:22:17 +00:00
|
|
|
}
|
|
|
|
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
|
2022-02-14 05:55:50 +00:00
|
|
|
{
|
2022-05-11 05:48:47 +00:00
|
|
|
return !perms.permission().isEmpty() && plugin.getPermissionHandler().hasPermission(player, permission);
|
2022-02-14 05:55:50 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Checks if a player is an admin
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param plexPlayer The PlexPlayer object
|
|
|
|
* @return true if the player is an admin
|
|
|
|
* @see PlexPlayer
|
|
|
|
*/
|
2020-11-06 03:50:16 +00:00
|
|
|
protected boolean isAdmin(PlexPlayer plexPlayer)
|
|
|
|
{
|
|
|
|
return Plex.get().getRankManager().isAdmin(plexPlayer);
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Checks if a sender is an admin
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param sender A command sender
|
|
|
|
* @return true if the sender is an admin or if console
|
|
|
|
*/
|
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);
|
2022-01-29 22:35:48 +00:00
|
|
|
return plugin.getRankManager().isAdmin(plexPlayer);
|
2022-01-27 09:00:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Checks if a username is an admin
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param name The username
|
|
|
|
* @return true if the username is an admin
|
|
|
|
*/
|
2020-11-06 03:50:16 +00:00
|
|
|
protected boolean isAdmin(String name)
|
|
|
|
{
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(name);
|
2022-01-29 22:35:48 +00:00
|
|
|
return plugin.getRankManager().isAdmin(plexPlayer);
|
2020-11-06 03:50:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Checks if a sender is a senior admin
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param sender A command sender
|
|
|
|
* @return true if the sender is a senior admin or if console
|
|
|
|
*/
|
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);
|
2022-01-29 22:35:48 +00:00
|
|
|
return plugin.getRankManager().isSeniorAdmin(plexPlayer);
|
2020-11-06 03:50:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Gets the UUID of the sender
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param sender A command sender
|
|
|
|
* @return A unique ID or null if the sender is console
|
|
|
|
* @see UUID
|
|
|
|
*/
|
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-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* The plugin
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +00:00
|
|
|
* @return The instance of the plugin
|
|
|
|
* @see Plex
|
|
|
|
*/
|
2022-02-04 04:01:30 +00:00
|
|
|
@Override
|
2022-02-05 23:14:23 +00:00
|
|
|
public @NotNull Plex getPlugin()
|
2022-02-04 04:01:30 +00:00
|
|
|
{
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a sender is console
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param sender A command sender
|
|
|
|
* @return true if the sender is console
|
|
|
|
*/
|
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-02-05 23:14:23 +00:00
|
|
|
/**
|
2022-02-25 07:36:46 +00:00
|
|
|
* Converts a message entry from the "messages.yml" to a Component
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
|
|
|
* @param s The message entry
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param objects Any objects to replace in order
|
2022-03-17 22:16:17 +00:00
|
|
|
* @return A Kyori Component
|
2022-02-05 23:14:23 +00:00
|
|
|
*/
|
2022-02-25 07:09:55 +00:00
|
|
|
protected Component messageComponent(String s, Object... objects)
|
2020-11-03 00:19:26 +00:00
|
|
|
{
|
2022-02-25 07:09:55 +00:00
|
|
|
return PlexUtils.messageComponent(s, objects);
|
2022-01-27 09:00:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 00:37:47 +00:00
|
|
|
/**
|
|
|
|
* Converts a message entry from the "messages.yml" to a Component
|
|
|
|
*
|
|
|
|
* @param s The message entry
|
|
|
|
* @param objects Any objects to replace in order
|
|
|
|
* @return A Kyori Component
|
|
|
|
*/
|
|
|
|
protected Component messageComponent(String s, Component... objects)
|
|
|
|
{
|
|
|
|
return PlexUtils.messageComponent(s, objects);
|
|
|
|
}
|
|
|
|
|
2022-02-25 07:36:46 +00:00
|
|
|
/**
|
|
|
|
* Converts a message entry from the "messages.yml" to a String
|
|
|
|
*
|
|
|
|
* @param s The message entry
|
|
|
|
* @param objects Any objects to replace in order
|
|
|
|
* @return A String
|
|
|
|
*/
|
|
|
|
protected String messageString(String s, Object... objects)
|
|
|
|
{
|
|
|
|
return PlexUtils.messageString(s, objects);
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
2022-03-17 22:16:17 +00:00
|
|
|
* Converts usage to a Component
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-03-17 22:16:17 +00:00
|
|
|
* @return A Kyori Component stating the usage
|
2022-02-14 05:55:50 +00:00
|
|
|
*/
|
|
|
|
protected Component usage()
|
|
|
|
{
|
2022-03-17 22:16:17 +00:00
|
|
|
return Component.text("Correct Usage: ").color(NamedTextColor.YELLOW).append(componentFromString(this.getUsage()).color(NamedTextColor.GRAY));
|
2022-02-14 05:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-17 22:16:17 +00:00
|
|
|
* Converts usage to a Component
|
2022-02-22 09:30:23 +00:00
|
|
|
* <p>
|
2022-02-14 05:55:50 +00:00
|
|
|
* s The usage to convert
|
2022-02-22 09:30:23 +00:00
|
|
|
*
|
2022-03-17 22:16:17 +00:00
|
|
|
* @return A Kyori Component stating the usage
|
2022-02-05 23:14:23 +00:00
|
|
|
*/
|
2022-01-27 09:00:50 +00:00
|
|
|
protected Component usage(String s)
|
|
|
|
{
|
2022-03-17 22:16:17 +00:00
|
|
|
return Component.text("Correct Usage: ").color(NamedTextColor.YELLOW).append(componentFromString(s).color(NamedTextColor.GRAY));
|
2020-11-03 00:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected Player getNonNullPlayer(String name)
|
|
|
|
{
|
2022-05-17 17:24:58 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
UUID uuid = UUID.fromString(name);
|
|
|
|
return Bukkit.getPlayer(uuid);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ignored)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:19:26 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-01-30 20:56:08 +00:00
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(uuid);
|
2020-11-03 00:19:26 +00:00
|
|
|
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-02-25 07:09:55 +00:00
|
|
|
throw new CommandFailException(PlexUtils.messageString("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-02-05 23:14:23 +00:00
|
|
|
/**
|
2022-03-17 22:16:17 +00:00
|
|
|
* Converts a String to a legacy Kyori Component
|
2022-02-07 05:58:55 +00:00
|
|
|
*
|
2022-03-17 22:16:17 +00:00
|
|
|
* @param s The String to convert
|
2022-02-25 07:36:46 +00:00
|
|
|
* @return A Kyori component
|
2022-02-05 23:14:23 +00:00
|
|
|
*/
|
2022-01-27 09:00:50 +00:00
|
|
|
protected Component componentFromString(String s)
|
|
|
|
{
|
2022-03-17 22:16:17 +00:00
|
|
|
return LegacyComponentSerializer.legacyAmpersand().deserialize(s).colorIfAbsent(NamedTextColor.GRAY);
|
2022-01-27 09:00:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 01:18:35 +00:00
|
|
|
protected Component noColorComponentFromString(String s)
|
|
|
|
{
|
|
|
|
return LegacyComponentSerializer.legacyAmpersand().deserialize(s);
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:54:11 +00:00
|
|
|
/**
|
2022-03-17 22:16:17 +00:00
|
|
|
* Converts a String to a MiniMessage Component
|
2022-02-25 09:54:11 +00:00
|
|
|
*
|
2022-03-17 22:16:17 +00:00
|
|
|
* @param s The String to convert
|
|
|
|
* @return A Kyori Component
|
2022-02-25 09:54:11 +00:00
|
|
|
*/
|
|
|
|
protected Component mmString(String s)
|
|
|
|
{
|
2022-04-10 01:53:27 +00:00
|
|
|
return PlexUtils.mmDeserialize(s);
|
2022-02-25 09:54:11 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|