#306 Change help provider to non-static / add calls to help functionality

This commit is contained in:
ljacqu 2015-12-23 23:51:39 +01:00
parent a9fc658db0
commit cb2ffca6d3
8 changed files with 77 additions and 37 deletions

View File

@ -199,6 +199,13 @@ public class AuthMe extends JavaPlugin {
setPluginInfos();
// Load settings and custom configurations, if it fails, stop the server due to security reasons.
if (loadSettings()) {
server.shutdown();
setEnabled(false);
return;
}
// Set up messages
messages = Messages.getInstance();
@ -209,13 +216,6 @@ public class AuthMe extends JavaPlugin {
// Set up the module manager
setupModuleManager();
// Load settings and custom configurations, if it fails, stop the server due to security reasons.
if (loadSettings()) {
server.shutdown();
setEnabled(false);
return;
}
// Setup otherAccounts file
this.otherAccounts = OtherAccounts.getInstance();
@ -412,7 +412,7 @@ public class AuthMe extends JavaPlugin {
private CommandHandler initializeCommandHandler(PermissionsManager permissionsManager, Messages messages) {
HelpProvider helpProvider = new HelpProvider(permissionsManager);
Set<CommandDescription> baseCommands = CommandInitializer.buildCommands();
CommandMapper mapper = new CommandMapper(baseCommands, messages, permissionsManager);
CommandMapper mapper = new CommandMapper(baseCommands, messages, permissionsManager, helpProvider);
CommandService commandService = new CommandService(this, mapper, helpProvider, messages);
return new CommandHandler(commandService);
}

View File

@ -38,12 +38,14 @@ public class CommandMapper {
private final Set<CommandDescription> baseCommands;
private final Messages messages;
private final PermissionsManager permissionsManager;
private final HelpProvider helpProvider;
public CommandMapper(Set<CommandDescription> baseCommands, Messages messages,
PermissionsManager permissionsManager) {
PermissionsManager permissionsManager, HelpProvider helpProvider) {
this.baseCommands = baseCommands;
this.messages = messages;
this.permissionsManager = permissionsManager;
this.helpProvider = helpProvider;
}
public void outputStandardError(CommandSender sender, FoundCommandResult result) {
@ -97,7 +99,7 @@ public class CommandMapper {
// Show the command argument help
sender.sendMessage(ChatColor.DARK_RED + "Incorrect command arguments!");
List<String> lines = HelpProvider.printHelp(result, HelpProvider.SHOW_ARGUMENTS);
List<String> lines = helpProvider.printHelp(sender, result, HelpProvider.SHOW_ARGUMENTS);
for (String line : lines) {
sender.sendMessage(line);
}

View File

@ -20,6 +20,14 @@ public class CommandService {
private final HelpProvider helpProvider;
private final CommandMapper commandMapper;
/**
* Constructor.
*
* @param authMe The plugin instance
* @param commandMapper Command mapper
* @param helpProvider Help provider
* @param messages Messages instance
*/
public CommandService(AuthMe authMe, CommandMapper commandMapper, HelpProvider helpProvider, Messages messages) {
this.authMe = authMe;
this.messages = messages;
@ -27,27 +35,64 @@ public class CommandService {
this.commandMapper = commandMapper;
}
/**
* Send a message to a player.
*
* @param sender The command sender to send the message to
* @param messageKey The message key to send
*/
public void send(CommandSender sender, MessageKey messageKey) {
messages.send(sender, messageKey);
}
/**
* Map command parts to a command description.
*
* @param sender The command sender issuing the request (for permission check), or null to skip permissions
* @param commandParts The received command parts to map to a command
* @return The computed mapping result
*/
public FoundCommandResult mapPartsToCommand(CommandSender sender, List<String> commandParts) {
return commandMapper.mapPartsToCommand(sender, commandParts);
}
/**
* Output the standard error message for the status in the provided {@link FoundCommandResult} object.
* Does not output anything for successful mappings.
*
* @param sender The sender to output the error to
* @param result The mapping result to process
*/
public void outputMappingError(CommandSender sender, FoundCommandResult result) {
commandMapper.outputStandardError(sender, result);
}
/**
* Run the given task asynchronously with the Bukkit scheduler.
*
* @param task The task to run
*/
public void runTaskAsynchronously(Runnable task) {
authMe.getServer().getScheduler().runTaskAsynchronously(authMe, task);
}
/**
* Return the AuthMe data source.
*
* @return The used data source
*/
public DataSource getDataSource() {
// TODO ljacqu 20151222: Add getter for .database and rename the field to dataSource
return authMe.database;
}
/**
* Output 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}
*/
public void outputHelp(CommandSender sender, FoundCommandResult result, int options) {
List<String> lines = helpProvider.printHelp(sender, result, options);
for (String line : lines) {

View File

@ -3,11 +3,17 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AntiBot;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.help.HelpProvider;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import java.util.Arrays;
import java.util.List;
/**
* Display or change the status of the antibot mod.
*/
public class SwitchAntiBotCommand implements ExecutableCommand {
@Override
@ -25,16 +31,12 @@ public class SwitchAntiBotCommand implements ExecutableCommand {
sender.sendMessage("[AuthMe] AntiBot Manual Override: enabled!");
} else if ("OFF".equalsIgnoreCase(newState)) {
AntiBot.overrideAntiBotStatus(false);
sender.sendMessage("[AuthMe] AntiBotMod Manual Override: disabled!");
sender.sendMessage("[AuthMe] AntiBot Manual Override: disabled!");
} else {
sender.sendMessage(ChatColor.DARK_RED + "Invalid AntiBot mode!");
// FIXME #306: Restore help showing logic
/*
CommandHandler commandHandler = AuthMe.getInstance().getCommandHandler();
FoundCommandResult foundCommandResult =
commandHandler.mapPartsToCommand(Arrays.asList("authme", "antibot"));
HelpProvider.printHelp(foundCommandResult, HelpProvider.SHOW_ARGUMENTS);
sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/authme help antibot");*/
FoundCommandResult result = commandService.mapPartsToCommand(sender, Arrays.asList("authme", "antibot"));
commandService.outputHelp(sender, result, HelpProvider.SHOW_ARGUMENTS);
sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/authme help antibot");
}
}
}

View File

@ -2,8 +2,11 @@ package fr.xephi.authme.command.executable.email;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.command.FoundCommandResult;
import fr.xephi.authme.command.help.HelpProvider;
import org.bukkit.command.CommandSender;
import java.util.Collections;
import java.util.List;
/**
@ -13,7 +16,7 @@ public class EmailBaseCommand implements ExecutableCommand {
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// FIXME #306 use getCommandService().getHelpProvider();
// FIXME #306 HelpProvider.printHelp()
FoundCommandResult result = commandService.mapPartsToCommand(sender, Collections.singletonList("email"));
commandService.outputHelp(sender, result, HelpProvider.SHOW_CHILDREN);
}
}

View File

@ -49,28 +49,16 @@ public class HelpProvider {
this.permissionsManager = permissionsManager;
}
public static List<String> printHelp(FoundCommandResult foundCommand, int options) {
return printHelp(foundCommand, null, null, options);
}
public List<String> printHelp(CommandSender sender, FoundCommandResult result, int options) {
// FIXME don't overload and pass to the static method
// FIXME remove the static methods altogether
return printHelp(result, sender, permissionsManager, options);
}
// sender and permissions manager may be null if SHOW_PERMISSIONS is not set
public static List<String> printHelp(FoundCommandResult foundCommand, CommandSender sender,
PermissionsManager permissionsManager, int options) {
if (foundCommand.getCommandDescription() == null) {
if (result.getCommandDescription() == null) {
return singletonList(ChatColor.DARK_RED + "Failed to retrieve any help information!");
}
List<String> lines = new ArrayList<>();
lines.add(ChatColor.GOLD + "==========[ " + Settings.helpHeader + " HELP ]==========");
CommandDescription command = foundCommand.getCommandDescription();
List<String> labels = ImmutableList.copyOf(foundCommand.getLabels());
CommandDescription command = result.getCommandDescription();
List<String> labels = ImmutableList.copyOf(result.getLabels());
List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels));
if (!hasFlag(HIDE_COMMAND, options)) {

View File

@ -275,7 +275,6 @@ public class CommandInitializerTest {
* count of arguments.
*/
@Test
@Ignore // TODO #306 ljacqu 20151214: Un-ignore this test and fix the offending command
public void shouldPointToSameExecutableCommandWithConsistentArgumentCount() {
// given
final Map<Class<? extends ExecutableCommand>, Integer> mandatoryArguments = new HashMap<>();

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.command;
import fr.xephi.authme.command.help.HelpProvider;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.command.CommandSender;
@ -42,7 +43,7 @@ public class CommandMapperTest {
@Before
public void setUpMocks() {
permissionsManagerMock = mock(PermissionsManager.class);
mapper = new CommandMapper(commands, mock(Messages.class), permissionsManagerMock);
mapper = new CommandMapper(commands, mock(Messages.class), permissionsManagerMock, mock(HelpProvider.class));
}
// -----------