Plex/src/main/java/me/totalfreedom/plex/command/PlexCommand.java

160 lines
4.7 KiB
Java
Raw Normal View History

2020-10-31 04:58:12 +00:00
package me.totalfreedom.plex.command;
2020-10-31 04:51:22 +00:00
import com.google.common.collect.ImmutableList;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.command.annotations.CommandParameters;
import me.totalfreedom.plex.command.annotations.CommandPermissions;
import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.player.PlexPlayer;
2020-10-31 04:51:22 +00:00
import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
2020-10-31 04:51:22 +00:00
import java.util.Arrays;
import java.util.List;
public abstract class PlexCommand extends Command implements TabExecutor, IPlexCommand
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(String name)
2020-10-31 04:51:22 +00:00
{
super(name);
this.params = getClass().getAnnotation(CommandParameters.class);
this.perms = getClass().getAnnotation(CommandPermissions.class);
setName(name);
setLabel(name);
setDescription(params.description());
setUsage(params.usage());
if (params.aliases().split(",").length > 0)
{
setAliases(Arrays.asList(params.aliases().split(",")));
}
this.level = perms.level();
this.commandSource = perms.source();
getMap().register("", this);
2020-10-31 04:51:22 +00:00
}
@Override
public boolean execute(CommandSender sender, String label, String[] args)
2020-10-31 04:51:22 +00:00
{
onCommand(sender, this, label, args);
return true;
2020-10-31 04:51:22 +00:00
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
2020-10-31 04:51:22 +00:00
{
if (!matches(label)) return false;
if (commandSource == RequiredCommandSource.CONSOLE)
{
if (sender instanceof Player)
{
//TODO: Enter console only msg
return true;
}
execute(sender, args);
return true;
} else if (commandSource == RequiredCommandSource.IN_GAME)
2020-10-31 04:51:22 +00:00
{
if (!(sender instanceof Player))
{
//TODO: Enter player only msg
return true;
}
Player player = (Player) sender;
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (!plexPlayer.getRankFromString().isAtleast(getLevel()))
2020-10-31 04:51:22 +00:00
{
//TODO: Enter <insert level> only and higher msg
return true;
2020-10-31 04:51:22 +00:00
}
execute(sender, args);
return true;
} else {
if (!(sender instanceof Player))
2020-10-31 04:51:22 +00:00
{
execute(sender, args);
return true;
} else {
Player player = (Player) sender;
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (!plexPlayer.getRankFromString().isAtleast(getLevel()))
{
//TODO: Enter <insert level> only and higher msg
return true;
}
execute(sender, args);
return true;
2020-10-31 04:51:22 +00:00
}
}
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args)
{
if (!matches(alias)) return ImmutableList.of();
if (sender instanceof Player)
2020-10-31 04:51:22 +00:00
{
Player player = (Player) sender;
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (plexPlayer.getRankFromString().isAtleast(getLevel()))
{
return onTabComplete(sender, args);
} else {
return ImmutableList.of();
}
} else {
return onTabComplete(sender, args);
2020-10-31 04:51:22 +00:00
}
}
2020-10-31 04:51:22 +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
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;
}
}
} 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
}
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
}
}