2013-03-01 00:52:01 -05:00
|
|
|
package com.gmail.nossr50.util.commands;
|
|
|
|
|
2013-10-15 13:03:33 -04:00
|
|
|
import java.util.ArrayList;
|
2013-03-28 12:57:49 -04:00
|
|
|
import java.util.List;
|
|
|
|
|
2013-09-23 20:22:32 +02:00
|
|
|
import org.bukkit.OfflinePlayer;
|
2013-03-01 00:52:01 -05:00
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2014-01-03 11:07:13 -05:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2013-05-16 19:31:12 -04:00
|
|
|
import com.gmail.nossr50.config.Config;
|
2013-03-12 16:25:42 -04:00
|
|
|
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
2013-03-01 00:52:01 -05:00
|
|
|
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
|
|
|
import com.gmail.nossr50.datatypes.skills.SkillType;
|
|
|
|
import com.gmail.nossr50.locale.LocaleLoader;
|
2013-03-12 16:25:42 -04:00
|
|
|
import com.gmail.nossr50.util.Misc;
|
|
|
|
import com.gmail.nossr50.util.StringUtils;
|
|
|
|
import com.gmail.nossr50.util.player.UserManager;
|
2013-03-01 00:52:01 -05:00
|
|
|
import com.gmail.nossr50.util.skills.SkillUtils;
|
|
|
|
|
2013-03-28 12:57:49 -04:00
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
|
2013-03-01 00:52:01 -05:00
|
|
|
public final class CommandUtils {
|
2013-03-28 12:57:49 -04:00
|
|
|
public static final List<String> TRUE_FALSE_OPTIONS = ImmutableList.of("on", "off", "true", "false", "enabled", "disabled");
|
|
|
|
public static final List<String> RESET_OPTIONS = ImmutableList.of("clear", "reset");
|
|
|
|
|
2013-03-01 00:52:01 -05:00
|
|
|
private CommandUtils() {}
|
|
|
|
|
2013-03-12 16:25:42 -04:00
|
|
|
public static boolean isChildSkill(CommandSender sender, SkillType skill) {
|
2013-10-29 15:38:20 -04:00
|
|
|
if (skill == null || !skill.isChildSkill()) {
|
2013-03-12 16:25:42 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sender.sendMessage("Child skills are not supported by this command."); // TODO: Localize this
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean inspectOffline(CommandSender sender, PlayerProfile profile, boolean hasPermission) {
|
|
|
|
if (unloadedProfile(sender, profile)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasPermission) {
|
|
|
|
sender.sendMessage(LocaleLoader.getString("Inspect.Offline"));
|
2013-03-01 00:52:01 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-12 16:25:42 -04:00
|
|
|
public static boolean tooFar(CommandSender sender, Player target, boolean hasPermission) {
|
2013-05-16 19:31:12 -04:00
|
|
|
if (sender instanceof Player && !Misc.isNear(((Player) sender).getLocation(), target.getLocation(), Config.getInstance().getInspectDistance()) && !hasPermission) {
|
2013-03-12 16:25:42 -04:00
|
|
|
sender.sendMessage(LocaleLoader.getString("Inspect.TooFar"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-02 12:42:18 +02:00
|
|
|
public static boolean hidden(CommandSender sender, Player target, boolean hasPermission) {
|
2014-01-20 13:58:40 -08:00
|
|
|
return sender instanceof Player && !((Player) sender).canSee(target) && !hasPermission;
|
2013-06-02 12:42:18 +02:00
|
|
|
}
|
|
|
|
|
2013-03-12 16:25:42 -04:00
|
|
|
public static boolean noConsoleUsage(CommandSender sender) {
|
|
|
|
if (sender instanceof Player) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sender.sendMessage(LocaleLoader.getString("Commands.NoConsole"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-23 20:22:32 +02:00
|
|
|
public static boolean isOffline(CommandSender sender, OfflinePlayer player) {
|
2013-03-12 16:25:42 -04:00
|
|
|
if (player.isOnline()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sender.sendMessage(LocaleLoader.getString("Commands.Offline"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-23 20:22:32 +02:00
|
|
|
/**
|
|
|
|
* Checks if there is a valid mcMMOPlayer object.
|
|
|
|
*
|
|
|
|
* @param sender CommandSender who used the command
|
|
|
|
* @param playerName name of the target player
|
|
|
|
* @param mcMMOPlayer mcMMOPlayer object of the target player
|
|
|
|
*
|
|
|
|
* @return true if the player is online and a valid mcMMOPlayer object was found
|
|
|
|
*/
|
2013-03-12 16:25:42 -04:00
|
|
|
public static boolean checkPlayerExistence(CommandSender sender, String playerName, McMMOPlayer mcMMOPlayer) {
|
|
|
|
if (mcMMOPlayer != null) {
|
2013-03-21 22:04:52 +01:00
|
|
|
return true;
|
2013-03-12 16:25:42 -04:00
|
|
|
}
|
|
|
|
|
2013-10-28 13:04:06 -04:00
|
|
|
PlayerProfile profile = new PlayerProfile(playerName, false);
|
|
|
|
|
|
|
|
if (unloadedProfile(sender, profile)) {
|
|
|
|
return false;
|
2013-03-12 16:25:42 -04:00
|
|
|
}
|
|
|
|
|
2013-09-23 20:22:32 +02:00
|
|
|
sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist"));
|
2013-03-21 22:04:52 +01:00
|
|
|
return false;
|
2013-03-12 16:25:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean unloadedProfile(CommandSender sender, PlayerProfile profile) {
|
|
|
|
if (profile.isLoaded()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-23 20:22:32 +02:00
|
|
|
sender.sendMessage(LocaleLoader.getString("Commands.Offline"));
|
2013-03-12 16:25:42 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isInvalidInteger(CommandSender sender, String value) {
|
|
|
|
if (StringUtils.isInt(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sender.sendMessage("That is not a valid integer."); // TODO: Localize
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isInvalidDouble(CommandSender sender, String value) {
|
|
|
|
if (StringUtils.isDouble(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sender.sendMessage("That is not a valid percentage."); // TODO: Localize
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isInvalidSkill(CommandSender sender, String skillName) {
|
2013-11-19 21:00:29 -05:00
|
|
|
if (SkillUtils.isSkill(skillName)) {
|
2013-03-12 16:25:42 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sender.sendMessage(LocaleLoader.getString("Commands.Skill.Invalid"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean shouldEnableToggle(String arg) {
|
|
|
|
return arg.equalsIgnoreCase("on") || arg.equalsIgnoreCase("true") || arg.equalsIgnoreCase("enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean shouldDisableToggle(String arg) {
|
|
|
|
return arg.equalsIgnoreCase("off") || arg.equalsIgnoreCase("false") || arg.equalsIgnoreCase("disabled");
|
|
|
|
}
|
|
|
|
|
2013-03-01 00:52:01 -05:00
|
|
|
/**
|
|
|
|
* Print out details on Gathering skills. Only for online players.
|
|
|
|
*
|
|
|
|
* @param inspect The player to retrieve stats for
|
|
|
|
* @param display The sender to display stats to
|
|
|
|
*/
|
2013-03-12 16:25:42 -04:00
|
|
|
public static void printGatheringSkills(Player inspect, CommandSender display) {
|
2013-10-15 13:03:33 -04:00
|
|
|
printGroupedSkillData(inspect, display, LocaleLoader.getString("Stats.Header.Gathering"), SkillType.GATHERING_SKILLS);
|
2013-03-01 00:52:01 -05:00
|
|
|
}
|
|
|
|
|
2013-03-12 16:25:42 -04:00
|
|
|
public static void printGatheringSkills(Player player) {
|
|
|
|
printGatheringSkills(player, player);
|
2013-03-01 00:52:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print out details on Combat skills. Only for online players.
|
|
|
|
*
|
|
|
|
* @param inspect The player to retrieve stats for
|
|
|
|
* @param display The sender to display stats to
|
|
|
|
*/
|
2013-03-12 16:25:42 -04:00
|
|
|
public static void printCombatSkills(Player inspect, CommandSender display) {
|
2013-10-15 13:03:33 -04:00
|
|
|
printGroupedSkillData(inspect, display, LocaleLoader.getString("Stats.Header.Combat"), SkillType.COMBAT_SKILLS);
|
2013-03-01 00:52:01 -05:00
|
|
|
}
|
|
|
|
|
2013-03-12 16:25:42 -04:00
|
|
|
public static void printCombatSkills(Player player) {
|
|
|
|
printCombatSkills(player, player);
|
2013-03-01 00:52:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print out details on Misc skills. Only for online players.
|
|
|
|
*
|
|
|
|
* @param inspect The player to retrieve stats for
|
|
|
|
* @param display The sender to display stats to
|
|
|
|
*/
|
2013-03-12 16:25:42 -04:00
|
|
|
public static void printMiscSkills(Player inspect, CommandSender display) {
|
2013-10-15 13:03:33 -04:00
|
|
|
printGroupedSkillData(inspect, display, LocaleLoader.getString("Stats.Header.Misc"), SkillType.MISC_SKILLS);
|
2013-03-12 16:25:42 -04:00
|
|
|
}
|
2013-03-01 00:52:01 -05:00
|
|
|
|
2013-03-12 16:25:42 -04:00
|
|
|
public static void printMiscSkills(Player player) {
|
|
|
|
printMiscSkills(player, player);
|
|
|
|
}
|
2013-03-01 00:52:01 -05:00
|
|
|
|
2013-10-15 13:03:33 -04:00
|
|
|
public static String displaySkill(PlayerProfile profile, SkillType skill) {
|
|
|
|
if (skill.isChildSkill()) {
|
2014-01-20 13:58:40 -08:00
|
|
|
return LocaleLoader.getString("Skills.ChildStats", LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".Listener"), " ", profile.getSkillLevel(skill));
|
2013-03-01 00:52:01 -05:00
|
|
|
}
|
2013-10-15 13:03:33 -04:00
|
|
|
|
2014-01-20 13:58:40 -08:00
|
|
|
return LocaleLoader.getString("Skills.Stats", LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".Listener"), " ", profile.getSkillLevel(skill), profile.getSkillXpLevel(skill), profile.getXpToLevel(skill));
|
2013-03-01 00:52:01 -05:00
|
|
|
}
|
|
|
|
|
2013-10-15 13:03:33 -04:00
|
|
|
private static void printGroupedSkillData(Player inspect, CommandSender display, String header, List<SkillType> skillGroup) {
|
|
|
|
PlayerProfile profile = UserManager.getPlayer(inspect).getProfile();
|
|
|
|
|
|
|
|
List<String> displayData = new ArrayList<String>();
|
|
|
|
displayData.add(header);
|
|
|
|
|
|
|
|
for (SkillType skill : skillGroup) {
|
2013-10-28 13:04:06 -04:00
|
|
|
if (skill.getPermissions(inspect)) {
|
2013-10-15 13:03:33 -04:00
|
|
|
displayData.add(displaySkill(profile, skill));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int size = displayData.size();
|
|
|
|
|
|
|
|
if (size > 1) {
|
|
|
|
display.sendMessage(displayData.toArray(new String[size]));
|
|
|
|
}
|
2013-03-01 00:52:01 -05:00
|
|
|
}
|
2013-10-15 13:03:33 -04:00
|
|
|
|
2014-01-03 11:07:13 -05:00
|
|
|
/**
|
|
|
|
* Get a matched player name if one was found in the database.
|
|
|
|
*
|
|
|
|
* @param partialName Name to match
|
|
|
|
*
|
|
|
|
* @return Matched name or {@code partialName} if no match was found
|
|
|
|
*/
|
|
|
|
public static String getMatchedPlayerName(String partialName) {
|
|
|
|
if (Config.getInstance().getMatchOfflinePlayers()) {
|
|
|
|
List<String> matches = matchPlayer(partialName);
|
|
|
|
|
|
|
|
if (matches.size() == 1) {
|
|
|
|
partialName = matches.get(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Player player = mcMMO.p.getServer().getPlayer(partialName);
|
|
|
|
|
|
|
|
if (player != null) {
|
|
|
|
partialName = player.getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return partialName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to match any player names with the given name, and returns a list of all possibly matches.
|
|
|
|
*
|
|
|
|
* This list is not sorted in any particular order.
|
|
|
|
* If an exact match is found, the returned list will only contain a single result.
|
|
|
|
*
|
|
|
|
* @param partialName Name to match
|
|
|
|
* @return List of all possible names
|
|
|
|
*/
|
|
|
|
private static List<String> matchPlayer(String partialName) {
|
|
|
|
List<String> matchedPlayers = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (OfflinePlayer offlinePlayer : mcMMO.p.getServer().getOfflinePlayers()) {
|
|
|
|
String playerName = offlinePlayer.getName();
|
|
|
|
|
|
|
|
if (partialName.equalsIgnoreCase(playerName)) {
|
|
|
|
// Exact match
|
|
|
|
matchedPlayers.clear();
|
|
|
|
matchedPlayers.add(playerName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (playerName.toLowerCase().contains(partialName.toLowerCase())) {
|
|
|
|
// Partial match
|
|
|
|
matchedPlayers.add(playerName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchedPlayers;
|
|
|
|
}
|
2013-03-01 00:52:01 -05:00
|
|
|
}
|