2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.command;
|
2020-10-31 04:51:22 +00:00
|
|
|
|
2020-10-31 08:55:27 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
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;
|
|
|
|
import dev.plex.command.exception.PlayerNotFoundException;
|
|
|
|
import dev.plex.command.source.CommandSource;
|
|
|
|
import dev.plex.command.source.RequiredCommandSource;
|
|
|
|
import dev.plex.player.PlexPlayer;
|
|
|
|
import dev.plex.rank.enums.Rank;
|
|
|
|
import dev.plex.util.PlexUtils;
|
2020-11-03 00:19:26 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.World;
|
2022-01-04 03:04:39 +00:00
|
|
|
import org.bukkit.command.*;
|
2020-10-31 08:55:27 +00:00
|
|
|
import org.bukkit.entity.Player;
|
2020-10-31 04:51:22 +00:00
|
|
|
|
2022-01-04 03:04:39 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2020-10-31 08:55:27 +00:00
|
|
|
public abstract class PlexCommand extends Command implements TabExecutor, IPlexCommand
|
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-11-03 00:19:26 +00:00
|
|
|
private CommandSource sender;
|
2020-10-31 08:55:27 +00:00
|
|
|
private final RequiredCommandSource commandSource;
|
2020-10-31 04:51:22 +00:00
|
|
|
|
2020-10-31 08:55:27 +00:00
|
|
|
public PlexCommand(String name)
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2020-10-31 08:55:27 +00:00
|
|
|
super(name);
|
|
|
|
this.params = getClass().getAnnotation(CommandParameters.class);
|
|
|
|
this.perms = getClass().getAnnotation(CommandPermissions.class);
|
|
|
|
|
|
|
|
setName(name);
|
|
|
|
setLabel(name);
|
|
|
|
setDescription(params.description());
|
2020-11-10 02:47:03 +00:00
|
|
|
setUsage(params.usage().replace("<command>", 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
|
|
|
}
|
|
|
|
|
2020-10-31 08:55:27 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean execute(CommandSender sender, String label, String[] args)
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2020-10-31 08:55:27 +00:00
|
|
|
onCommand(sender, this, label, args);
|
|
|
|
return true;
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 08:55:27 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCommand(CommandSender sender, Command cmd, 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
|
|
|
{
|
2020-11-05 21:17:14 +00:00
|
|
|
sender.sendMessage(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
|
|
|
|
|
|
|
this.sender = new CommandSource(player);
|
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
|
|
|
{
|
2020-11-05 21:17:14 +00:00
|
|
|
sender.sendMessage(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
|
|
|
|
{
|
2020-11-06 18:19:38 +00:00
|
|
|
this.sender = new CommandSource(sender);
|
2020-11-03 00:19:26 +00:00
|
|
|
execute(this.sender, args);
|
|
|
|
}
|
|
|
|
catch (CommandArgumentException ex)
|
|
|
|
{
|
|
|
|
send(getUsage().replace("<command>", getLabel()));
|
|
|
|
}
|
|
|
|
catch (PlayerNotFoundException | CommandFailException ex)
|
|
|
|
{
|
|
|
|
send(ex.getMessage());
|
|
|
|
}
|
|
|
|
return true;
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 08:55:27 +00:00
|
|
|
@Override
|
|
|
|
public List<String> tabComplete(CommandSender sender, String alias, String[] args)
|
|
|
|
{
|
2020-11-06 01:29:38 +00:00
|
|
|
if (!matches(alias))
|
|
|
|
{
|
|
|
|
return ImmutableList.of();
|
|
|
|
}
|
2020-10-31 08:55:27 +00:00
|
|
|
if (sender instanceof Player)
|
2020-10-31 04:51:22 +00:00
|
|
|
{
|
2020-11-06 01:29:38 +00:00
|
|
|
Player player = (Player)sender;
|
2020-11-06 18:19:38 +00:00
|
|
|
|
|
|
|
this.sender = new CommandSource(player);
|
|
|
|
|
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 08:55:27 +00:00
|
|
|
{
|
2020-11-03 00:19:26 +00:00
|
|
|
return onTabComplete(this.sender, args);
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-31 08:55:27 +00:00
|
|
|
return ImmutableList.of();
|
|
|
|
}
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-06 18:19:38 +00:00
|
|
|
this.sender = new CommandSource(sender);
|
2020-11-03 00:19:26 +00:00
|
|
|
return onTabComplete(this.sender, args);
|
2020-10-31 04:51:22 +00:00
|
|
|
}
|
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
|
|
|
|
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args)
|
|
|
|
{
|
|
|
|
return tabComplete(sender, label, args);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-11-03 00:19:26 +00:00
|
|
|
protected void send(String s, CommandSource sender)
|
|
|
|
{
|
|
|
|
sender.send(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void send(String s, Player player)
|
|
|
|
{
|
|
|
|
player.sendMessage(s);
|
|
|
|
}
|
|
|
|
|
2020-11-06 03:50:16 +00:00
|
|
|
protected boolean isAdmin(PlexPlayer plexPlayer)
|
|
|
|
{
|
|
|
|
return Plex.get().getRankManager().isAdmin(plexPlayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean isAdmin(String name)
|
|
|
|
{
|
|
|
|
PlexPlayer plexPlayer = DataUtils.getPlayer(name);
|
|
|
|
return Plex.get().getRankManager().isAdmin(plexPlayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean isConsole()
|
|
|
|
{
|
2020-11-06 18:19:38 +00:00
|
|
|
return !(sender instanceof Player);
|
2020-11-06 03:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected String tl(String s, Object... objects)
|
|
|
|
{
|
|
|
|
return PlexUtils.tl(s, objects);
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:17:14 +00:00
|
|
|
protected String usage(String s)
|
|
|
|
{
|
|
|
|
return ChatColor.YELLOW + "Correct Usage: " + ChatColor.GRAY + s;
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:19:26 +00:00
|
|
|
protected void send(String s)
|
|
|
|
{
|
|
|
|
if (sender == null)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-03 00:19:26 +00:00
|
|
|
return;
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-03 00:19:26 +00:00
|
|
|
send(s, sender);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-11-03 00:19:26 +00:00
|
|
|
throw new CommandFailException(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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|