mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-22 16:48:21 +01:00
Fix minor checkstyle issues
- Add JavaDoc where checkstyle expects it - Fix line too long issues - ...
This commit is contained in:
parent
1a48348824
commit
1f8307c8f6
@ -5,7 +5,7 @@
|
||||
* ------------------------------------------------------- *
|
||||
* See AuthMeController for details. *
|
||||
* *
|
||||
* Source: https://github.com/AuthMe-Team/AuthMeReloaded/ *
|
||||
* Source: https://github.com/AuthMe/AuthMeReloaded/ *
|
||||
***********************************************************/
|
||||
class Pbkdf2 extends AuthMeController {
|
||||
|
||||
|
@ -21,7 +21,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The current AuthMeApi of AuthMe.
|
||||
* The current API of AuthMe.
|
||||
*
|
||||
* Recommended method of retrieving the AuthMeApi object:
|
||||
* <code>
|
||||
@ -44,8 +44,9 @@ public class AuthMeApi {
|
||||
* Constructor for AuthMeApi.
|
||||
*/
|
||||
@Inject
|
||||
AuthMeApi(AuthMe plugin, PluginHookService pluginHookService, DataSource dataSource, PasswordSecurity passwordSecurity,
|
||||
Management management, ValidationService validationService, PlayerCache playerCache, GeoIpService geoIpService) {
|
||||
AuthMeApi(AuthMe plugin, PluginHookService pluginHookService, DataSource dataSource, PlayerCache playerCache,
|
||||
PasswordSecurity passwordSecurity, Management management, ValidationService validationService,
|
||||
GeoIpService geoIpService) {
|
||||
this.plugin = plugin;
|
||||
this.pluginHookService = pluginHookService;
|
||||
this.dataSource = dataSource;
|
||||
|
@ -44,7 +44,7 @@ public class CommandMapper {
|
||||
* @param parts The parts to map to commands and arguments
|
||||
* @return The generated {@link FoundCommandResult}
|
||||
*/
|
||||
public FoundCommandResult mapPartsToCommand(CommandSender sender, final List<String> parts) {
|
||||
public FoundCommandResult mapPartsToCommand(CommandSender sender, List<String> parts) {
|
||||
if (Utils.isCollectionEmpty(parts)) {
|
||||
return new FoundCommandResult(null, parts, null, 0.0, MISSING_BASE_COMMAND);
|
||||
}
|
||||
@ -87,6 +87,14 @@ public class CommandMapper {
|
||||
return classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the command whose label matches the given parts the best. This method is called when
|
||||
* a successful mapping could not be performed.
|
||||
*
|
||||
* @param base the base command
|
||||
* @param parts the command parts
|
||||
* @return the closest result
|
||||
*/
|
||||
private static FoundCommandResult getCommandWithSmallestDifference(CommandDescription base, List<String> parts) {
|
||||
// Return the base command with incorrect arg count error if we only have one part
|
||||
if (parts.size() <= 1) {
|
||||
@ -189,14 +197,10 @@ public class CommandMapper {
|
||||
}
|
||||
|
||||
private static double getLabelDifference(CommandDescription command, String givenLabel) {
|
||||
double minDifference = Double.POSITIVE_INFINITY;
|
||||
for (String commandLabel : command.getLabels()) {
|
||||
double difference = StringUtils.getDifference(commandLabel, givenLabel);
|
||||
if (difference < minDifference) {
|
||||
minDifference = difference;
|
||||
}
|
||||
}
|
||||
return minDifference;
|
||||
return command.getLabels().stream()
|
||||
.map(label -> StringUtils.getDifference(label, givenLabel))
|
||||
.min(Double::compareTo)
|
||||
.orElseThrow(() -> new IllegalStateException("Command does not have any labels set"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,11 +7,20 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Utility functions for {@link CommandDescription} objects.
|
||||
*/
|
||||
public final class CommandUtils {
|
||||
|
||||
private CommandUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the minimum number of arguments required for running the command (= number of mandatory arguments).
|
||||
*
|
||||
* @param command the command to process
|
||||
* @return min number of arguments required by the command
|
||||
*/
|
||||
public static int getMinNumberOfArguments(CommandDescription command) {
|
||||
int mandatoryArguments = 0;
|
||||
for (CommandArgumentDescription argument : command.getArguments()) {
|
||||
@ -22,20 +31,16 @@ public final class CommandUtils {
|
||||
return mandatoryArguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum number of arguments the command accepts.
|
||||
*
|
||||
* @param command the command to process
|
||||
* @return max number of arguments that may be passed to the command
|
||||
*/
|
||||
public static int getMaxNumberOfArguments(CommandDescription command) {
|
||||
return command.getArguments().size();
|
||||
}
|
||||
|
||||
public static String constructCommandPath(CommandDescription command) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String prefix = "/";
|
||||
for (CommandDescription ancestor : constructParentList(command)) {
|
||||
sb.append(prefix).append(ancestor.getLabels().get(0));
|
||||
prefix = " ";
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a hierarchical list of commands for the given command. The commands are in order:
|
||||
* the parents of the given command precede the provided command. For example, given the command
|
||||
@ -54,6 +59,29 @@ public final class CommandUtils {
|
||||
return Lists.reverse(commands);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a textual representation of the command, e.g. {@code /authme register}.
|
||||
*
|
||||
* @param command the command to create the path for
|
||||
* @return the command string
|
||||
*/
|
||||
public static String constructCommandPath(CommandDescription command) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String prefix = "/";
|
||||
for (CommandDescription ancestor : constructParentList(command)) {
|
||||
sb.append(prefix).append(ancestor.getLabels().get(0));
|
||||
prefix = " ";
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a textual representation of the command, including its arguments.
|
||||
* For example: {@code /authme purge <days> [includeZero]}.
|
||||
*
|
||||
* @param command the command to create a usage string for
|
||||
* @return the command's path and arguments
|
||||
*/
|
||||
public static String buildSyntax(CommandDescription command) {
|
||||
String arguments = command.getArguments().stream()
|
||||
.map(arg -> formatArgument(arg))
|
||||
@ -73,12 +101,12 @@ public final class CommandUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a command argument with the proper type of brackets.
|
||||
* Formats a command argument with the proper type of brackets.
|
||||
*
|
||||
* @param argument the argument to format
|
||||
* @return the formatted argument
|
||||
*/
|
||||
public static String formatArgument(CommandArgumentDescription argument) {
|
||||
private static String formatArgument(CommandArgumentDescription argument) {
|
||||
if (argument.isOptional()) {
|
||||
return "[" + argument.getName() + "]";
|
||||
}
|
||||
|
@ -20,6 +20,9 @@ import static fr.xephi.authme.command.help.HelpProvider.SHOW_CHILDREN;
|
||||
import static fr.xephi.authme.command.help.HelpProvider.SHOW_COMMAND;
|
||||
import static fr.xephi.authme.command.help.HelpProvider.SHOW_DESCRIPTION;
|
||||
|
||||
/**
|
||||
* Displays help information to a user.
|
||||
*/
|
||||
public class HelpCommand implements ExecutableCommand {
|
||||
|
||||
@Inject
|
||||
@ -51,7 +54,8 @@ public class HelpCommand implements ExecutableCommand {
|
||||
|
||||
int mappedCommandLevel = result.getCommandDescription().getLabelCount();
|
||||
if (mappedCommandLevel == 1) {
|
||||
helpProvider.outputHelp(sender, result, SHOW_COMMAND | SHOW_DESCRIPTION | SHOW_CHILDREN | SHOW_ALTERNATIVES);
|
||||
helpProvider.outputHelp(sender, result,
|
||||
SHOW_COMMAND | SHOW_DESCRIPTION | SHOW_CHILDREN | SHOW_ALTERNATIVES);
|
||||
} else {
|
||||
helpProvider.outputHelp(sender, result, ALL_OPTIONS);
|
||||
}
|
||||
|
@ -68,6 +68,17 @@ class TestEmailSender implements DebugSection {
|
||||
return DebugSectionPermissions.TEST_EMAIL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the email address to use based on the sender and the arguments. If the arguments are empty,
|
||||
* we attempt to retrieve the email from the sender. If there is an argument, we verify that it is
|
||||
* an email address.
|
||||
* {@code null} is returned if no email address could be found. This method informs the sender of
|
||||
* the specific error in such cases.
|
||||
*
|
||||
* @param sender the command sender
|
||||
* @param arguments the provided arguments
|
||||
* @return the email to use, or null if none found
|
||||
*/
|
||||
private String getEmail(CommandSender sender, List<String> arguments) {
|
||||
if (arguments.isEmpty()) {
|
||||
DataSourceResult<String> emailResult = dataSource.getEmail(sender.getName());
|
||||
|
@ -97,6 +97,15 @@ public class RegisterCommand extends PlayerCommand {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the second argument is valid (based on the configuration)
|
||||
* to perform a password registration. The player is informed if the check
|
||||
* is unsuccessful.
|
||||
*
|
||||
* @param player the player to register
|
||||
* @param arguments the provided arguments
|
||||
* @return true if valid, false otherwise
|
||||
*/
|
||||
private boolean isSecondArgValidForPasswordRegistration(Player player, List<String> arguments) {
|
||||
RegisterSecondaryArgument secondArgType = commonService.getProperty(REGISTER_SECOND_ARGUMENT);
|
||||
// cases where args.size < 2
|
||||
@ -143,6 +152,15 @@ public class RegisterCommand extends PlayerCommand {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the second argument is valid (based on the configuration)
|
||||
* to perform an email registration. The player is informed if the check
|
||||
* is unsuccessful.
|
||||
*
|
||||
* @param player the player to register
|
||||
* @param arguments the provided arguments
|
||||
* @return true if valid, false otherwise
|
||||
*/
|
||||
private boolean isSecondArgValidForEmailRegistration(Player player, List<String> arguments) {
|
||||
RegisterSecondaryArgument secondArgType = commonService.getProperty(REGISTER_SECOND_ARGUMENT);
|
||||
// cases where args.size < 2
|
||||
|
@ -58,7 +58,15 @@ public class HelpProvider implements Reloadable {
|
||||
this.helpMessagesService = helpMessagesService;
|
||||
}
|
||||
|
||||
private List<String> printHelp(CommandSender sender, FoundCommandResult result, int options) {
|
||||
/**
|
||||
* Builds the help messages based on the provided arguments.
|
||||
*
|
||||
* @param sender the sender to evaluate permissions with
|
||||
* @param result the command result to create help for
|
||||
* @param options output options
|
||||
* @return the generated help messages
|
||||
*/
|
||||
private List<String> buildHelpOutput(CommandSender sender, FoundCommandResult result, int options) {
|
||||
if (result.getCommandDescription() == null) {
|
||||
return singletonList(ChatColor.DARK_RED + "Failed to retrieve any help information!");
|
||||
}
|
||||
@ -75,8 +83,7 @@ public class HelpProvider implements Reloadable {
|
||||
}
|
||||
|
||||
CommandDescription command = helpMessagesService.buildLocalizedDescription(result.getCommandDescription());
|
||||
List<String> labels = ImmutableList.copyOf(result.getLabels());
|
||||
List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels));
|
||||
List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, result.getLabels()));
|
||||
|
||||
if (hasFlag(SHOW_COMMAND, options)) {
|
||||
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.COMMAND) + ": "
|
||||
@ -91,30 +98,30 @@ public class HelpProvider implements Reloadable {
|
||||
lines.add(ChatColor.WHITE + " " + command.getDetailedDescription());
|
||||
}
|
||||
if (hasFlag(SHOW_ARGUMENTS, options)) {
|
||||
printArguments(command, lines);
|
||||
addArgumentsInfo(command, lines);
|
||||
}
|
||||
if (hasFlag(SHOW_PERMISSIONS, options) && sender != null) {
|
||||
printPermissions(command, sender, lines);
|
||||
addPermissionsInfo(command, sender, lines);
|
||||
}
|
||||
if (hasFlag(SHOW_ALTERNATIVES, options)) {
|
||||
printAlternatives(command, correctLabels, lines);
|
||||
addAlternativesInfo(command, correctLabels, lines);
|
||||
}
|
||||
if (hasFlag(SHOW_CHILDREN, options)) {
|
||||
printChildren(command, labels, lines);
|
||||
addChildrenInfo(command, correctLabels, lines);
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the help for a given command.
|
||||
* Outputs the help for a given command.
|
||||
*
|
||||
* @param sender The sender to output the help to
|
||||
* @param result The result to output information about
|
||||
* @param options Output options, see {@link HelpProvider}
|
||||
* @param sender the sender to output the help to
|
||||
* @param result the result to output information about
|
||||
* @param options output options
|
||||
*/
|
||||
public void outputHelp(CommandSender sender, FoundCommandResult result, int options) {
|
||||
List<String> lines = printHelp(sender, result, options);
|
||||
List<String> lines = buildHelpOutput(sender, result, options);
|
||||
for (String line : lines) {
|
||||
sender.sendMessage(line);
|
||||
}
|
||||
@ -134,6 +141,7 @@ public class HelpProvider implements Reloadable {
|
||||
* @param options the options to process
|
||||
* @return the options without any disabled sections
|
||||
*/
|
||||
@SuppressWarnings("checkstyle:BooleanExpressionComplexity")
|
||||
private int filterDisabledSections(int options) {
|
||||
if (enabledSections == null) {
|
||||
enabledSections = flagFor(HelpSection.COMMAND, SHOW_COMMAND)
|
||||
@ -151,7 +159,13 @@ public class HelpProvider implements Reloadable {
|
||||
return helpMessagesService.getMessage(section).isEmpty() ? 0 : flag;
|
||||
}
|
||||
|
||||
private void printArguments(CommandDescription command, List<String> lines) {
|
||||
/**
|
||||
* Adds help info about the given command's arguments into the provided list.
|
||||
*
|
||||
* @param command the command to generate arguments info for
|
||||
* @param lines the output collection to add the info to
|
||||
*/
|
||||
private void addArgumentsInfo(CommandDescription command, List<String> lines) {
|
||||
if (command.getArguments().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
@ -171,7 +185,14 @@ public class HelpProvider implements Reloadable {
|
||||
}
|
||||
}
|
||||
|
||||
private void printAlternatives(CommandDescription command, List<String> correctLabels, List<String> lines) {
|
||||
/**
|
||||
* Adds help info about the given command's alternative labels into the provided list.
|
||||
*
|
||||
* @param command the command for which to generate info about its labels
|
||||
* @param correctLabels labels used to access the command (sanitized)
|
||||
* @param lines the output collection to add the info to
|
||||
*/
|
||||
private void addAlternativesInfo(CommandDescription command, List<String> correctLabels, List<String> lines) {
|
||||
if (command.getLabels().size() <= 1) {
|
||||
return;
|
||||
}
|
||||
@ -199,7 +220,14 @@ public class HelpProvider implements Reloadable {
|
||||
}
|
||||
}
|
||||
|
||||
private void printPermissions(CommandDescription command, CommandSender sender, List<String> lines) {
|
||||
/**
|
||||
* Adds help info about the given command's permissions into the provided list.
|
||||
*
|
||||
* @param command the command to generate permissions info for
|
||||
* @param sender the command sender, used to evaluate permissions
|
||||
* @param lines the output collection to add the info to
|
||||
*/
|
||||
private void addPermissionsInfo(CommandDescription command, CommandSender sender, List<String> lines) {
|
||||
PermissionNode permission = command.getPermission();
|
||||
if (permission == null) {
|
||||
return;
|
||||
@ -240,13 +268,20 @@ public class HelpProvider implements Reloadable {
|
||||
return helpMessagesService.getMessage(HelpMessage.NO_PERMISSION);
|
||||
}
|
||||
|
||||
private void printChildren(CommandDescription command, List<String> parentLabels, List<String> lines) {
|
||||
/**
|
||||
* Adds help info about the given command's child command into the provided list.
|
||||
*
|
||||
* @param command the command for which to generate info about its child commands
|
||||
* @param correctLabels the labels used to access the given command (sanitized)
|
||||
* @param lines the output collection to add the info to
|
||||
*/
|
||||
private void addChildrenInfo(CommandDescription command, List<String> correctLabels, List<String> lines) {
|
||||
if (command.getChildren().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
lines.add(ChatColor.GOLD + helpMessagesService.getMessage(HelpSection.CHILDREN) + ":");
|
||||
String parentCommandPath = String.join(" ", parentLabels);
|
||||
String parentCommandPath = String.join(" ", correctLabels);
|
||||
for (CommandDescription child : command.getChildren()) {
|
||||
lines.add(" /" + parentCommandPath + " " + child.getLabels().get(0)
|
||||
+ ChatColor.GRAY + ChatColor.ITALIC + ": " + helpMessagesService.getDescription(child));
|
||||
@ -257,6 +292,23 @@ public class HelpProvider implements Reloadable {
|
||||
return (flag & options) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of labels for the given command, using the labels from the provided labels list
|
||||
* as long as they are correct.
|
||||
* <p>
|
||||
* Background: commands may have multiple labels (e.g. /authme register vs. /authme reg). It is interesting
|
||||
* for us to keep with which label the user requested the command. At the same time, when a user inputs a
|
||||
* non-existent label, we try to find the most similar one. This method keeps all labels that exists and will
|
||||
* default to the command's first label when an invalid label is encountered.
|
||||
* <p>
|
||||
* Examples:
|
||||
* command = "authme register", labels = {authme, egister}. Output: {authme, register}
|
||||
* command = "authme register", labels = {authme, reg}. Output: {authme, reg}
|
||||
*
|
||||
* @param command the command to compare the labels against
|
||||
* @param labels the labels as input by the user
|
||||
* @return list of correct labels, keeping the user's input where possible
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static List<String> filterCorrectLabels(CommandDescription command, List<String> labels) {
|
||||
List<CommandDescription> commands = CommandUtils.constructParentList(command);
|
||||
|
@ -234,6 +234,11 @@ public class PlayerAuth {
|
||||
private float yaw;
|
||||
private float pitch;
|
||||
|
||||
/**
|
||||
* Creates a PlayerAuth object.
|
||||
*
|
||||
* @return the generated PlayerAuth
|
||||
*/
|
||||
public PlayerAuth build() {
|
||||
PlayerAuth auth = new PlayerAuth();
|
||||
auth.nickname = checkNotNull(name).toLowerCase();
|
||||
@ -277,6 +282,12 @@ public class PlayerAuth {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the location info based on the argument.
|
||||
*
|
||||
* @param location the location info to set
|
||||
* @return this builder instance
|
||||
*/
|
||||
public Builder location(Location location) {
|
||||
this.x = location.getX();
|
||||
this.y = location.getY();
|
||||
|
@ -57,7 +57,7 @@ class DistributedFilesPersistenceHandler implements LimboPersistenceHandler {
|
||||
|
||||
@Override
|
||||
public LimboPlayer getLimboPlayer(Player player) {
|
||||
String uuid = PlayerUtils.getUUIDorName(player);
|
||||
String uuid = PlayerUtils.getUuidOrName(player);
|
||||
File file = getPlayerSegmentFile(uuid);
|
||||
Map<String, LimboPlayer> entries = readLimboPlayers(file);
|
||||
return entries == null ? null : entries.get(uuid);
|
||||
@ -65,7 +65,7 @@ class DistributedFilesPersistenceHandler implements LimboPersistenceHandler {
|
||||
|
||||
@Override
|
||||
public void saveLimboPlayer(Player player, LimboPlayer limbo) {
|
||||
String uuid = PlayerUtils.getUUIDorName(player);
|
||||
String uuid = PlayerUtils.getUuidOrName(player);
|
||||
File file = getPlayerSegmentFile(uuid);
|
||||
|
||||
Map<String, LimboPlayer> entries = null;
|
||||
@ -79,17 +79,17 @@ class DistributedFilesPersistenceHandler implements LimboPersistenceHandler {
|
||||
entries = new HashMap<>();
|
||||
}
|
||||
|
||||
entries.put(PlayerUtils.getUUIDorName(player), limbo);
|
||||
entries.put(PlayerUtils.getUuidOrName(player), limbo);
|
||||
saveEntries(entries, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeLimboPlayer(Player player) {
|
||||
String uuid = PlayerUtils.getUUIDorName(player);
|
||||
String uuid = PlayerUtils.getUuidOrName(player);
|
||||
File file = getPlayerSegmentFile(uuid);
|
||||
if (file.exists()) {
|
||||
Map<String, LimboPlayer> entries = readLimboPlayers(file);
|
||||
if (entries != null && entries.remove(PlayerUtils.getUUIDorName(player)) != null) {
|
||||
if (entries != null && entries.remove(PlayerUtils.getUuidOrName(player)) != null) {
|
||||
saveEntries(entries, file);
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ class IndividualFilesPersistenceHandler implements LimboPersistenceHandler {
|
||||
|
||||
@Override
|
||||
public LimboPlayer getLimboPlayer(Player player) {
|
||||
String id = PlayerUtils.getUUIDorName(player);
|
||||
String id = PlayerUtils.getUuidOrName(player);
|
||||
File file = new File(cacheDir, id + File.separator + "data.json");
|
||||
if (!file.exists()) {
|
||||
return null;
|
||||
@ -56,7 +56,7 @@ class IndividualFilesPersistenceHandler implements LimboPersistenceHandler {
|
||||
|
||||
@Override
|
||||
public void saveLimboPlayer(Player player, LimboPlayer limboPlayer) {
|
||||
String id = PlayerUtils.getUUIDorName(player);
|
||||
String id = PlayerUtils.getUuidOrName(player);
|
||||
try {
|
||||
File file = new File(cacheDir, id + File.separator + "data.json");
|
||||
Files.createParentDirs(file);
|
||||
@ -75,7 +75,7 @@ class IndividualFilesPersistenceHandler implements LimboPersistenceHandler {
|
||||
*/
|
||||
@Override
|
||||
public void removeLimboPlayer(Player player) {
|
||||
String id = PlayerUtils.getUUIDorName(player);
|
||||
String id = PlayerUtils.getUuidOrName(player);
|
||||
File file = new File(cacheDir, id);
|
||||
if (file.exists()) {
|
||||
FileUtils.purgeDirectory(file);
|
||||
|
@ -3,7 +3,7 @@ package fr.xephi.authme.datasource;
|
||||
/**
|
||||
* Wraps a value and allows to specify whether a value is missing or the player is not registered.
|
||||
*/
|
||||
public class DataSourceResult<T> {
|
||||
public final class DataSourceResult<T> {
|
||||
|
||||
/** Instance used when a player does not exist. */
|
||||
private static final DataSourceResult UNKNOWN_PLAYER = new DataSourceResult<>(null);
|
||||
|
@ -89,6 +89,11 @@ public class MySQL implements DataSource {
|
||||
setParameters(settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves various settings.
|
||||
*
|
||||
* @param settings the settings to read properties from
|
||||
*/
|
||||
private void setParameters(Settings settings) {
|
||||
this.host = settings.getProperty(DatabaseSettings.MYSQL_HOST);
|
||||
this.port = settings.getProperty(DatabaseSettings.MYSQL_PORT);
|
||||
@ -112,6 +117,9 @@ public class MySQL implements DataSource {
|
||||
this.useSsl = settings.getProperty(DatabaseSettings.MYSQL_USE_SSL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the connection arguments to the database.
|
||||
*/
|
||||
private void setConnectionArguments() {
|
||||
ds = new HikariDataSource();
|
||||
ds.setPoolName("AuthMeMYSQLPool");
|
||||
@ -159,6 +167,9 @@ public class MySQL implements DataSource {
|
||||
return ds.getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the table or any of its required columns if they don't exist.
|
||||
*/
|
||||
private void checkTablesAndColumns() throws SQLException {
|
||||
try (Connection con = getConnection(); Statement st = con.createStatement()) {
|
||||
// Create table with ID column if it doesn't exist
|
||||
|
@ -42,8 +42,9 @@ public class VAuthConverter implements Converter {
|
||||
} catch (Exception | NoSuchMethodError e) {
|
||||
pname = getName(UUID.fromString(name));
|
||||
}
|
||||
if (pname == null)
|
||||
if (pname == null) {
|
||||
continue;
|
||||
}
|
||||
auth = PlayerAuth.builder()
|
||||
.name(pname.toLowerCase())
|
||||
.realName(pname)
|
||||
|
@ -80,6 +80,14 @@ public class EmailService {
|
||||
return couldSendEmail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an email to the user with a recovery code for the password recovery process.
|
||||
*
|
||||
* @param name the name of the player
|
||||
* @param email the player's email address
|
||||
* @param code the recovery code
|
||||
* @return true if email could be sent, false otherwise
|
||||
*/
|
||||
public boolean sendRecoveryCode(String name, String email, String code) {
|
||||
HtmlEmail htmlEmail;
|
||||
try {
|
||||
|
@ -39,6 +39,13 @@ public class SendMailSsl {
|
||||
&& !settings.getProperty(MAIL_PASSWORD).isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link HtmlEmail} object configured as per the AuthMe config
|
||||
* with the given email address as recipient.
|
||||
*
|
||||
* @param emailAddress the email address the email is destined for
|
||||
* @return the created HtmlEmail object
|
||||
*/
|
||||
public HtmlEmail initializeMail(String emailAddress) throws EmailException {
|
||||
String senderMail = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_ADDRESS))
|
||||
? settings.getProperty(EmailSettings.MAIL_ACCOUNT)
|
||||
@ -66,6 +73,13 @@ public class SendMailSsl {
|
||||
return email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the given content to the HtmlEmail object and sends it.
|
||||
*
|
||||
* @param content the content to set
|
||||
* @param email the email object to send
|
||||
* @return true upon success, false otherwise
|
||||
*/
|
||||
public boolean sendEmail(String content, HtmlEmail email) {
|
||||
Thread.currentThread().setContextClassLoader(SendMailSsl.class.getClassLoader());
|
||||
// Issue #999: Prevent UnsupportedDataTypeException: no object DCH for MIME type multipart/alternative
|
||||
@ -93,6 +107,12 @@ public class SendMailSsl {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets properties to the given HtmlEmail object based on the port from which it will be sent.
|
||||
*
|
||||
* @param email the email object to configure
|
||||
* @param port the configured outgoing port
|
||||
*/
|
||||
private void setPropertiesForPort(HtmlEmail email, int port) throws EmailException {
|
||||
switch (port) {
|
||||
case 587:
|
||||
|
@ -12,14 +12,14 @@ import java.util.List;
|
||||
*/
|
||||
final class LogFilterHelper {
|
||||
|
||||
private static final String ISSUED_COMMAND_TEXT = "issued server command:";
|
||||
|
||||
@VisibleForTesting
|
||||
static final List<String> COMMANDS_TO_SKIP = withAndWithoutAuthMePrefix(
|
||||
"/login ", "/l ", "/log ", "/register ", "/reg ", "/unregister ", "/unreg ",
|
||||
"/changepassword ", "/cp ", "/changepass ", "/authme register ", "/authme reg ", "/authme r ",
|
||||
"/authme changepassword ", "/authme password ", "/authme changepass ", "/authme cp ");
|
||||
|
||||
private static final String ISSUED_COMMAND_TEXT = "issued server command:";
|
||||
|
||||
private LogFilterHelper() {
|
||||
// Util class
|
||||
}
|
||||
|
@ -23,14 +23,13 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* PermissionsManager.
|
||||
* </p><p>
|
||||
* <p>
|
||||
* A permissions manager, to manage and use various permissions systems.
|
||||
* This manager supports dynamic plugin hooking and various other features.
|
||||
* </p><p>
|
||||
* <p>
|
||||
* Written by Tim Visée.
|
||||
* </p>
|
||||
*
|
||||
* @author Tim Visée, http://timvisee.com
|
||||
* @version 0.3
|
||||
*/
|
||||
@ -211,8 +210,8 @@ public class PermissionsManager implements Reloadable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a player has permission for the given permission node. This is for offline player checks. If no permissions
|
||||
* system is used, then the player will not have permission.
|
||||
* Check if a player has permission for the given permission node. This is for offline player checks.
|
||||
* If no permissions system is used, then the player will not have permission.
|
||||
*
|
||||
* @param player The offline player
|
||||
* @param permissionNode The permission node to verify
|
||||
@ -250,11 +249,7 @@ public class PermissionsManager implements Reloadable {
|
||||
* @return True if the current permissions system supports groups, false otherwise.
|
||||
*/
|
||||
public boolean hasGroupSupport() {
|
||||
// If no permissions system is used, return false
|
||||
if (!isEnabled())
|
||||
return false;
|
||||
|
||||
return handler.hasGroupSupport();
|
||||
return isEnabled() && handler.hasGroupSupport();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -265,11 +260,7 @@ public class PermissionsManager implements Reloadable {
|
||||
* @return Permission groups, or an empty collection if this feature is not supported.
|
||||
*/
|
||||
public Collection<String> getGroups(Player player) {
|
||||
// If no permissions system is used, return an empty list
|
||||
if (!isEnabled())
|
||||
return Collections.emptyList();
|
||||
|
||||
return handler.getGroups(player);
|
||||
return isEnabled() ? handler.getGroups(player) : Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -280,11 +271,7 @@ public class PermissionsManager implements Reloadable {
|
||||
* @return The name of the primary permission group. Or null.
|
||||
*/
|
||||
public String getPrimaryGroup(Player player) {
|
||||
// If no permissions system is used, return an empty list
|
||||
if (!isEnabled())
|
||||
return null;
|
||||
|
||||
return handler.getPrimaryGroup(player);
|
||||
return isEnabled() ? handler.getPrimaryGroup(player) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -294,14 +281,10 @@ public class PermissionsManager implements Reloadable {
|
||||
* @param groupName The group name.
|
||||
*
|
||||
* @return True if the player is in the specified group, false otherwise.
|
||||
* False is also returned if groups aren't supported by the used permissions system.
|
||||
* False is also returned if groups aren't supported by the used permissions system.
|
||||
*/
|
||||
public boolean isInGroup(Player player, String groupName) {
|
||||
// If no permissions system is used, return false
|
||||
if (!isEnabled())
|
||||
return false;
|
||||
|
||||
return handler.isInGroup(player, groupName);
|
||||
return isEnabled() && handler.isInGroup(player, groupName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -311,7 +294,7 @@ public class PermissionsManager implements Reloadable {
|
||||
* @param groupName The name of the group.
|
||||
*
|
||||
* @return True if succeed, false otherwise.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
*/
|
||||
public boolean addGroup(Player player, String groupName) {
|
||||
if (!isEnabled() || StringUtils.isEmpty(groupName)) {
|
||||
@ -327,13 +310,10 @@ public class PermissionsManager implements Reloadable {
|
||||
* @param groupName The name of the group.
|
||||
*
|
||||
* @return True if succeed, false otherwise.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
*/
|
||||
public boolean removeGroups(Player player, String groupName) {
|
||||
if (!isEnabled())
|
||||
return false;
|
||||
|
||||
return handler.removeFromGroup(player, groupName);
|
||||
return isEnabled() && handler.removeFromGroup(player, groupName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -371,14 +351,10 @@ public class PermissionsManager implements Reloadable {
|
||||
* @param groupName The name of the group.
|
||||
*
|
||||
* @return True if succeed, false otherwise.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
* False is also returned if this feature isn't supported for the current permissions system.
|
||||
*/
|
||||
public boolean setGroup(Player player, String groupName) {
|
||||
// If no permissions system is used, return false
|
||||
if (!isEnabled())
|
||||
return false;
|
||||
|
||||
return handler.setGroup(player, groupName);
|
||||
return isEnabled() && handler.setGroup(player, groupName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -389,12 +365,13 @@ public class PermissionsManager implements Reloadable {
|
||||
* @param player The player to remove all groups from.
|
||||
*
|
||||
* @return True if succeed, false otherwise.
|
||||
* False will also be returned if this feature isn't supported for the used permissions system.
|
||||
* False will also be returned if this feature isn't supported for the used permissions system.
|
||||
*/
|
||||
public boolean removeAllGroups(Player player) {
|
||||
// If no permissions system is used, return false
|
||||
if (!isEnabled())
|
||||
if (!isEnabled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get a list of current groups
|
||||
Collection<String> groupNames = getGroups(player);
|
||||
|
@ -32,6 +32,12 @@ public class AsyncAddEmail implements AsynchronousProcess {
|
||||
|
||||
AsyncAddEmail() { }
|
||||
|
||||
/**
|
||||
* Handles the request to add the given email to the player's account.
|
||||
*
|
||||
* @param player the player to add the email to
|
||||
* @param email the email to add
|
||||
*/
|
||||
public void addEmail(Player player, String email) {
|
||||
String playerName = player.getName().toLowerCase();
|
||||
|
||||
|
@ -30,6 +30,13 @@ public class AsyncChangeEmail implements AsynchronousProcess {
|
||||
|
||||
AsyncChangeEmail() { }
|
||||
|
||||
/**
|
||||
* Handles the request to change the player's email address.
|
||||
*
|
||||
* @param player the player to change the email for
|
||||
* @param oldEmail provided old email
|
||||
* @param newEmail provided new email
|
||||
*/
|
||||
public void changeEmail(Player player, String oldEmail, String newEmail) {
|
||||
String playerName = player.getName().toLowerCase();
|
||||
if (playerCache.isAuthenticated(playerName)) {
|
||||
|
@ -269,6 +269,12 @@ public class AsynchronousLogin implements AsynchronousProcess {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends info about the other accounts owned by the given player to the configured users.
|
||||
*
|
||||
* @param auths the names of the accounts also owned by the player
|
||||
* @param player the player
|
||||
*/
|
||||
private void displayOtherAccounts(List<String> auths, Player player) {
|
||||
if (!service.getProperty(RestrictionSettings.DISPLAY_OTHER_ACCOUNTS) || auths.size() <= 1) {
|
||||
return;
|
||||
|
@ -12,6 +12,9 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Async task when a player wants to log out.
|
||||
*/
|
||||
public class AsynchronousLogout implements AsynchronousProcess {
|
||||
|
||||
@Inject
|
||||
@ -29,7 +32,12 @@ public class AsynchronousLogout implements AsynchronousProcess {
|
||||
AsynchronousLogout() {
|
||||
}
|
||||
|
||||
public void logout(final Player player) {
|
||||
/**
|
||||
* Handles a player's request to log out.
|
||||
*
|
||||
* @param player the player wanting to log out
|
||||
*/
|
||||
public void logout(Player player) {
|
||||
final String name = player.getName().toLowerCase();
|
||||
if (!playerCache.isAuthenticated(name)) {
|
||||
service.send(player, MessageKey.NOT_LOGGED_IN);
|
||||
|
@ -76,6 +76,11 @@ public class GeoIpService {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a thread which will attempt to download new data from the GeoLite website.
|
||||
*
|
||||
* @return the generated download thread
|
||||
*/
|
||||
private Thread createDownloadTask() {
|
||||
return new Thread(new Runnable() {
|
||||
@Override
|
||||
|
@ -90,7 +90,7 @@ public class PluginHookService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Query the CombatTagPlus plugin whether the given player is an NPC.
|
||||
* Queries the CombatTagPlus plugin whether the given player is an NPC.
|
||||
*
|
||||
* @param player The player to verify
|
||||
* @return True if the player is an NPC according to CombatTagPlus, false if not or if the plugin is unavailable
|
||||
@ -103,14 +103,23 @@ public class PluginHookService {
|
||||
// ------
|
||||
// "Is plugin available" methods
|
||||
// ------
|
||||
/**
|
||||
* @return true if we have a hook to Essentials, false otherwise
|
||||
*/
|
||||
public boolean isEssentialsAvailable() {
|
||||
return essentials != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if we have a hook to Multiverse, false otherwise
|
||||
*/
|
||||
public boolean isMultiverseAvailable() {
|
||||
return multiverse != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if we have a hook to CombatTagPlus, false otherwise
|
||||
*/
|
||||
public boolean isCombatTagPlusAvailable() {
|
||||
return combatTagPlus != null;
|
||||
}
|
||||
@ -118,6 +127,10 @@ public class PluginHookService {
|
||||
// ------
|
||||
// Hook methods
|
||||
// ------
|
||||
|
||||
/**
|
||||
* Attempts to create a hook into Essentials.
|
||||
*/
|
||||
public void tryHookToEssentials() {
|
||||
try {
|
||||
essentials = getPlugin(pluginManager, "Essentials", Essentials.class);
|
||||
@ -126,6 +139,9 @@ public class PluginHookService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to create a hook into CombatTagPlus.
|
||||
*/
|
||||
public void tryHookToCombatPlus() {
|
||||
try {
|
||||
combatTagPlus = getPlugin(pluginManager, "CombatTagPlus", CombatTagPlus.class);
|
||||
@ -134,6 +150,9 @@ public class PluginHookService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to create a hook into Multiverse.
|
||||
*/
|
||||
public void tryHookToMultiverse() {
|
||||
try {
|
||||
multiverse = getPlugin(pluginManager, "Multiverse-Core", MultiverseCore.class);
|
||||
@ -145,12 +164,23 @@ public class PluginHookService {
|
||||
// ------
|
||||
// Unhook methods
|
||||
// ------
|
||||
/**
|
||||
* Unhooks from Essentials.
|
||||
*/
|
||||
public void unhookEssentials() {
|
||||
essentials = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unhooks from CombatTagPlus.
|
||||
*/
|
||||
public void unhookCombatPlus() {
|
||||
combatTagPlus = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unhooks from Multiverse.
|
||||
*/
|
||||
public void unhookMultiverse() {
|
||||
multiverse = null;
|
||||
}
|
||||
|
@ -52,6 +52,7 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("checkstyle:BooleanExpressionComplexity")
|
||||
protected boolean performMigrations(PropertyResource resource, List<Property<?>> properties) {
|
||||
boolean changes = false;
|
||||
if ("[a-zA-Z0-9_?]*".equals(resource.getString(ALLOWED_NICKNAME_CHARACTERS.getPath()))) {
|
||||
@ -229,13 +230,19 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts old boolean configurations for registration to the new enum properties, if applicable.
|
||||
*
|
||||
* @param resource The property resource
|
||||
* @return True if the configuration has changed, false otherwise
|
||||
*/
|
||||
private static boolean convertToRegistrationType(PropertyResource resource) {
|
||||
String oldEmailRegistrationPath = "settings.registration.enableEmailRegistrationSystem";
|
||||
if (RegistrationSettings.REGISTRATION_TYPE.isPresent(resource) || !resource.contains(oldEmailRegistrationPath)) {
|
||||
String oldEmailRegisterPath = "settings.registration.enableEmailRegistrationSystem";
|
||||
if (RegistrationSettings.REGISTRATION_TYPE.isPresent(resource) || !resource.contains(oldEmailRegisterPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean useEmail = newProperty(oldEmailRegistrationPath, false).getValue(resource);
|
||||
boolean useEmail = newProperty(oldEmailRegisterPath, false).getValue(resource);
|
||||
RegistrationType registrationType = useEmail ? RegistrationType.EMAIL : RegistrationType.PASSWORD;
|
||||
|
||||
String useConfirmationPath = useEmail
|
||||
@ -253,6 +260,12 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrates old permission group settings to the new configurations.
|
||||
*
|
||||
* @param resource The property resource
|
||||
* @return True if the configuration has changed, false otherwise
|
||||
*/
|
||||
private static boolean mergeAndMovePermissionGroupSettings(PropertyResource resource) {
|
||||
boolean performedChanges;
|
||||
|
||||
@ -274,7 +287,7 @@ public class SettingsMigrationService extends PlainMigrationService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for an old property path and moves it to a new path if present.
|
||||
* Checks for an old property path and moves it to a new path if it is present and the new path is not yet set.
|
||||
*
|
||||
* @param oldProperty The old property (create a temporary {@link Property} object with the path)
|
||||
* @param newProperty The new property to move the value to
|
||||
|
@ -165,6 +165,8 @@ public class SpawnLoader implements Reloadable {
|
||||
case "authme":
|
||||
spawnLoc = getSpawn();
|
||||
break;
|
||||
default:
|
||||
// ignore
|
||||
}
|
||||
if (spawnLoc != null) {
|
||||
return spawnLoc;
|
||||
|
@ -2,11 +2,11 @@ package fr.xephi.authme.task.purge;
|
||||
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.service.PluginHookService;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.service.PluginHookService;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PurgeSettings;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.util.PlayerUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@ -67,8 +67,7 @@ public class PurgeExecutor {
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
File dataFolder = new File("." + File.separator + "plugins" + File.separator + "AntiXRayData"
|
||||
+ File.separator + "PlayerData");
|
||||
File dataFolder = new File(makePath(".", "plugins", "AntiXRayData", "PlayerData"));
|
||||
if (!dataFolder.exists() || !dataFolder.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
@ -102,8 +101,7 @@ public class PurgeExecutor {
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
File dataFolder = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative"
|
||||
+ File.separator + "inventories");
|
||||
File dataFolder = new File(makePath(".", "plugins", "LimitedCreative", "inventories"));
|
||||
if (!dataFolder.exists() || !dataFolder.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
@ -148,11 +146,11 @@ public class PurgeExecutor {
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
File dataFolder = new File(server.getWorldContainer()
|
||||
, makePath(settings.getProperty(PurgeSettings.DEFAULT_WORLD), "players"));
|
||||
File dataFolder = new File(server.getWorldContainer(),
|
||||
makePath(settings.getProperty(PurgeSettings.DEFAULT_WORLD), "players"));
|
||||
|
||||
for (OfflinePlayer offlinePlayer : cleared) {
|
||||
File playerFile = new File(dataFolder, PlayerUtils.getUUIDorName(offlinePlayer) + ".dat");
|
||||
File playerFile = new File(dataFolder, PlayerUtils.getUuidOrName(offlinePlayer) + ".dat");
|
||||
if (playerFile.delete()) {
|
||||
i++;
|
||||
}
|
||||
@ -184,7 +182,7 @@ public class PurgeExecutor {
|
||||
}
|
||||
|
||||
for (OfflinePlayer offlinePlayer : cleared) {
|
||||
File playerFile = new File(userDataFolder, PlayerUtils.getUUIDorName(offlinePlayer) + ".yml");
|
||||
File playerFile = new File(userDataFolder, PlayerUtils.getUuidOrName(offlinePlayer) + ".yml");
|
||||
if (playerFile.exists() && playerFile.delete()) {
|
||||
i++;
|
||||
}
|
||||
@ -193,7 +191,7 @@ public class PurgeExecutor {
|
||||
ConsoleLogger.info("AutoPurge: Removed " + i + " EssentialsFiles");
|
||||
}
|
||||
|
||||
// TODO: What is this method for? Is it correct?
|
||||
// TODO #676: What is this method for? Is it correct?
|
||||
synchronized void purgePermissions(Collection<OfflinePlayer> cleared) {
|
||||
if (!settings.getProperty(PurgeSettings.REMOVE_PERMISSIONS)) {
|
||||
return;
|
||||
|
@ -19,7 +19,7 @@ public final class PlayerUtils {
|
||||
*
|
||||
* @return player's UUID or Name in String.
|
||||
*/
|
||||
public static String getUUIDorName(OfflinePlayer player) {
|
||||
public static String getUuidOrName(OfflinePlayer player) {
|
||||
// We may made this configurable in future
|
||||
// so we can have uuid support.
|
||||
try {
|
||||
|
@ -6,7 +6,6 @@ import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
import fr.xephi.authme.initialization.HasCleanup;
|
||||
import fr.xephi.authme.listener.PlayerListener;
|
||||
import fr.xephi.authme.process.register.executors.RegistrationMethod;
|
||||
import fr.xephi.authme.security.crypts.Whirlpool;
|
||||
import fr.xephi.authme.util.expiring.ExpiringMap;
|
||||
|
250
src/test/java/fr/xephi/authme/api/NewAPITest.java
Normal file
250
src/test/java/fr/xephi/authme/api/NewAPITest.java
Normal file
@ -0,0 +1,250 @@
|
||||
package fr.xephi.authme.api;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.api.v3.AuthMeApi;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.process.Management;
|
||||
import fr.xephi.authme.security.PasswordSecurity;
|
||||
import fr.xephi.authme.service.PluginHookService;
|
||||
import fr.xephi.authme.service.ValidationService;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Test for {@link fr.xephi.authme.api.NewAPI}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class NewAPITest {
|
||||
|
||||
@InjectMocks
|
||||
private NewAPI api;
|
||||
|
||||
@Mock
|
||||
private PluginHookService pluginHookService;
|
||||
@Mock
|
||||
private ValidationService validationService;
|
||||
@Mock
|
||||
private DataSource dataSource;
|
||||
@Mock
|
||||
private Management management;
|
||||
@Mock
|
||||
private PasswordSecurity passwordSecurity;
|
||||
@Mock
|
||||
private PlayerCache playerCache;
|
||||
|
||||
@Test
|
||||
public void shouldReturnInstanceOrNull() {
|
||||
NewAPI result = NewAPI.getInstance();
|
||||
assertThat(result, sameInstance(api));
|
||||
|
||||
ReflectionTestUtils.setField(AuthMeApi.class, null, "singleton", null);
|
||||
assertThat(AuthMeApi.getInstance(), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnIfPlayerIsAuthenticated() {
|
||||
// given
|
||||
String name = "Bobby";
|
||||
Player player = mockPlayerWithName(name);
|
||||
given(playerCache.isAuthenticated(name)).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = api.isAuthenticated(player);
|
||||
|
||||
// then
|
||||
verify(playerCache).isAuthenticated(name);
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnIfPlayerIsNpc() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
given(pluginHookService.isNpc(player)).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = api.isNPC(player);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnIfPlayerIsUnrestricted() {
|
||||
// given
|
||||
String name = "Tester";
|
||||
Player player = mockPlayerWithName(name);
|
||||
given(validationService.isUnrestricted(name)).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = api.isUnrestricted(player);
|
||||
|
||||
// then
|
||||
verify(validationService).isUnrestricted(name);
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetLastLocation() {
|
||||
// given
|
||||
String name = "Gary";
|
||||
Player player = mockPlayerWithName(name);
|
||||
PlayerAuth auth = PlayerAuth.builder().name(name)
|
||||
.locWorld("world")
|
||||
.locX(12.4)
|
||||
.locY(24.6)
|
||||
.locZ(-438.2)
|
||||
.locYaw(3.41f)
|
||||
.locPitch(0.29f)
|
||||
.build();
|
||||
given(playerCache.getAuth(name)).willReturn(auth);
|
||||
Server server = mock(Server.class);
|
||||
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
|
||||
World world = mock(World.class);
|
||||
given(server.getWorld(auth.getWorld())).willReturn(world);
|
||||
|
||||
// when
|
||||
Location result = api.getLastLocation(player);
|
||||
|
||||
// then
|
||||
assertThat(result, not(nullValue()));
|
||||
assertThat(result.getX(), equalTo(auth.getQuitLocX()));
|
||||
assertThat(result.getY(), equalTo(auth.getQuitLocY()));
|
||||
assertThat(result.getZ(), equalTo(auth.getQuitLocZ()));
|
||||
assertThat(result.getWorld(), equalTo(world));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForUnavailablePlayer() {
|
||||
// given
|
||||
String name = "Numan";
|
||||
Player player = mockPlayerWithName(name);
|
||||
given(playerCache.getAuth(name)).willReturn(null);
|
||||
|
||||
// when
|
||||
Location result = api.getLastLocation(player);
|
||||
|
||||
// then
|
||||
assertThat(result, nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCheckForRegisteredName() {
|
||||
// given
|
||||
String name = "toaster";
|
||||
given(dataSource.isAuthAvailable(name)).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = api.isRegistered(name);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCheckPassword() {
|
||||
// given
|
||||
String playerName = "Robert";
|
||||
String password = "someSecretPhrase2983";
|
||||
given(passwordSecurity.comparePassword(password, playerName)).willReturn(true);
|
||||
|
||||
// when
|
||||
boolean result = api.checkPassword(playerName, password);
|
||||
|
||||
// then
|
||||
verify(passwordSecurity).comparePassword(password, playerName);
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnAuthNames() {
|
||||
// given
|
||||
String[] names = {"bobby", "peter", "elisabeth", "craig"};
|
||||
List<PlayerAuth> auths = Arrays.stream(names)
|
||||
.map(name -> PlayerAuth.builder().name(name).build())
|
||||
.collect(Collectors.toList());
|
||||
given(dataSource.getAllAuths()).willReturn(auths);
|
||||
|
||||
// when
|
||||
List<String> result = api.getRegisteredNames();
|
||||
|
||||
// then
|
||||
assertThat(result, contains(names));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnAuthRealNames() {
|
||||
// given
|
||||
String[] names = {"Bobby", "peter", "Elisabeth", "CRAIG"};
|
||||
List<PlayerAuth> auths = Arrays.stream(names)
|
||||
.map(name -> PlayerAuth.builder().name(name).realName(name).build())
|
||||
.collect(Collectors.toList());
|
||||
given(dataSource.getAllAuths()).willReturn(auths);
|
||||
|
||||
// when
|
||||
List<String> result = api.getRegisteredRealNames();
|
||||
|
||||
// then
|
||||
assertThat(result, contains(names));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUnregisterPlayer() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
String name = "Donald";
|
||||
given(player.getName()).willReturn(name);
|
||||
|
||||
// when
|
||||
api.forceUnregister(player);
|
||||
|
||||
// then
|
||||
verify(management).performUnregisterByAdmin(null, name, player);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUnregisterPlayerByName() {
|
||||
// given
|
||||
Server server = mock(Server.class);
|
||||
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
|
||||
String name = "tristan";
|
||||
Player player = mock(Player.class);
|
||||
given(server.getPlayer(name)).willReturn(player);
|
||||
|
||||
// when
|
||||
api.forceUnregister(name);
|
||||
|
||||
// then
|
||||
verify(management).performUnregisterByAdmin(null, name, player);
|
||||
}
|
||||
|
||||
private static Player mockPlayerWithName(String name) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
return player;
|
||||
}
|
||||
}
|
@ -212,6 +212,7 @@ public class CommandInitializerTest {
|
||||
testCollectionForCommand(command, CommandUtils.getMinNumberOfArguments(command), mandatoryArguments);
|
||||
testCollectionForCommand(command, CommandUtils.getMaxNumberOfArguments(command), totalArguments);
|
||||
}
|
||||
|
||||
private void testCollectionForCommand(CommandDescription command, int argCount,
|
||||
Map<Class<? extends ExecutableCommand>, Integer> collection) {
|
||||
final Class<? extends ExecutableCommand> clazz = command.getExecutableCommand();
|
||||
|
@ -82,7 +82,7 @@ public final class TestCommandsUtil {
|
||||
throw new IllegalStateException("Could not find command with label '" + label + "'");
|
||||
}
|
||||
|
||||
/** Shortcut command to initialize a new test command. */
|
||||
/* Shortcut command to initialize a new test command. */
|
||||
private static CommandDescription createCommand(PermissionNode permission, CommandDescription parent,
|
||||
List<String> labels, Class<? extends ExecutableCommand> commandClass,
|
||||
CommandArgumentDescription... arguments) {
|
||||
@ -103,7 +103,7 @@ public final class TestCommandsUtil {
|
||||
return command.register();
|
||||
}
|
||||
|
||||
/** Shortcut command to initialize a new argument description. */
|
||||
/* Shortcut command to initialize a new argument description. */
|
||||
private static CommandArgumentDescription newArgument(String label, boolean isOptional) {
|
||||
return new CommandArgumentDescription(label, "'" + label + "' argument description", isOptional);
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ public class AccountsCommandTest {
|
||||
// given
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
List<String> arguments = Collections.singletonList("123.45.67.89");
|
||||
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.<String>emptyList());
|
||||
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.emptyList());
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, arguments);
|
||||
|
@ -25,7 +25,7 @@ public class AuthMeCommandTest {
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.<String>emptyList());
|
||||
command.executeCommand(sender, Collections.emptyList());
|
||||
|
||||
// then
|
||||
ArgumentCaptor<String> messagesCaptor = ArgumentCaptor.forClass(String.class);
|
||||
|
@ -40,7 +40,7 @@ public class FirstSpawnCommandTest {
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(player, Collections.<String>emptyList());
|
||||
command.executeCommand(player, Collections.emptyList());
|
||||
|
||||
// then
|
||||
verify(player).teleport(firstSpawn);
|
||||
@ -54,7 +54,7 @@ public class FirstSpawnCommandTest {
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(player, Collections.<String>emptyList());
|
||||
command.executeCommand(player, Collections.emptyList());
|
||||
|
||||
// then
|
||||
verify(player).sendMessage(argThat(containsString("spawn has failed")));
|
||||
|
@ -90,15 +90,15 @@ public class LastLoginCommandTest {
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(sender.getName()).willReturn(name);
|
||||
|
||||
long lastLogin = System.currentTimeMillis() -
|
||||
(412 * DAY_IN_MSEC + 10 * HOUR_IN_MSEC - 9000);
|
||||
long lastLogin = System.currentTimeMillis()
|
||||
- (412 * DAY_IN_MSEC + 10 * HOUR_IN_MSEC - 9000);
|
||||
PlayerAuth auth = mock(PlayerAuth.class);
|
||||
given(auth.getLastLogin()).willReturn(lastLogin);
|
||||
given(auth.getIp()).willReturn("123.45.66.77");
|
||||
given(dataSource.getAuth(name)).willReturn(auth);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.<String>emptyList());
|
||||
command.executeCommand(sender, Collections.emptyList());
|
||||
|
||||
// then
|
||||
verify(dataSource).getAuth(name);
|
||||
|
@ -46,7 +46,7 @@ public class PurgeBannedPlayersCommandTest {
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.<String>emptyList());
|
||||
command.executeCommand(sender, Collections.emptyList());
|
||||
|
||||
// then
|
||||
verify(bukkitService).getBannedPlayers();
|
||||
|
@ -63,7 +63,7 @@ public class PurgeLastPositionCommandTest {
|
||||
given(dataSource.getAuth(player)).willReturn(auth);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.<String>emptyList());
|
||||
command.executeCommand(sender, Collections.emptyList());
|
||||
|
||||
// then
|
||||
verify(dataSource).getAuth(player);
|
||||
|
@ -39,7 +39,7 @@ public class SetFirstSpawnCommandTest {
|
||||
given(spawnLoader.setFirstSpawn(location)).willReturn(true);
|
||||
|
||||
// when
|
||||
command.executeCommand(player, Collections.<String>emptyList());
|
||||
command.executeCommand(player, Collections.emptyList());
|
||||
|
||||
// then
|
||||
verify(spawnLoader).setFirstSpawn(location);
|
||||
@ -55,7 +55,7 @@ public class SetFirstSpawnCommandTest {
|
||||
given(spawnLoader.setFirstSpawn(location)).willReturn(false);
|
||||
|
||||
// when
|
||||
command.executeCommand(player, Collections.<String>emptyList());
|
||||
command.executeCommand(player, Collections.emptyList());
|
||||
|
||||
// then
|
||||
verify(spawnLoader).setFirstSpawn(location);
|
||||
|
@ -41,7 +41,7 @@ public class SpawnCommandTest {
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(player, Collections.<String>emptyList());
|
||||
command.executeCommand(player, Collections.emptyList());
|
||||
|
||||
// then
|
||||
verify(player).teleport(spawn);
|
||||
@ -55,7 +55,7 @@ public class SpawnCommandTest {
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(player, Collections.<String>emptyList());
|
||||
command.executeCommand(player, Collections.emptyList());
|
||||
|
||||
// then
|
||||
verify(player).sendMessage(argThat(containsString("Spawn has failed")));
|
||||
|
@ -47,7 +47,7 @@ public class SwitchAntiBotCommandTest {
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.<String>emptyList());
|
||||
command.executeCommand(sender, Collections.emptyList());
|
||||
|
||||
// then
|
||||
verify(sender).sendMessage(argThat(containsString("status: ACTIVE")));
|
||||
|
@ -212,7 +212,7 @@ public class PlayerListenerTest {
|
||||
public void shouldNotCancelEventForAuthenticatedPlayer() {
|
||||
// given
|
||||
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false);
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Collections.<String>emptyList());
|
||||
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Collections.emptyList());
|
||||
Player player = playerWithMockedServer();
|
||||
// PlayerCommandPreprocessEvent#getPlayer is final, so create a spy instead of a mock
|
||||
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
|
||||
|
@ -44,7 +44,7 @@ public class PlayerUtilsTest {
|
||||
given(player.getUniqueId()).willReturn(uuid);
|
||||
|
||||
// when
|
||||
String result = PlayerUtils.getUUIDorName(player);
|
||||
String result = PlayerUtils.getUuidOrName(player);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(uuid.toString()));
|
||||
@ -59,7 +59,7 @@ public class PlayerUtilsTest {
|
||||
given(player.getName()).willReturn(name);
|
||||
|
||||
// when
|
||||
String result = PlayerUtils.getUUIDorName(player);
|
||||
String result = PlayerUtils.getUuidOrName(player);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(name));
|
||||
|
Loading…
Reference in New Issue
Block a user