#306 Change help provider to non-static / add calls to help functionality
This commit is contained in:
parent
a9fc658db0
commit
cb2ffca6d3
@ -199,6 +199,13 @@ public class AuthMe extends JavaPlugin {
|
|||||||
|
|
||||||
setPluginInfos();
|
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
|
// Set up messages
|
||||||
messages = Messages.getInstance();
|
messages = Messages.getInstance();
|
||||||
|
|
||||||
@ -209,13 +216,6 @@ public class AuthMe extends JavaPlugin {
|
|||||||
// Set up the module manager
|
// Set up the module manager
|
||||||
setupModuleManager();
|
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
|
// Setup otherAccounts file
|
||||||
this.otherAccounts = OtherAccounts.getInstance();
|
this.otherAccounts = OtherAccounts.getInstance();
|
||||||
|
|
||||||
@ -412,7 +412,7 @@ public class AuthMe extends JavaPlugin {
|
|||||||
private CommandHandler initializeCommandHandler(PermissionsManager permissionsManager, Messages messages) {
|
private CommandHandler initializeCommandHandler(PermissionsManager permissionsManager, Messages messages) {
|
||||||
HelpProvider helpProvider = new HelpProvider(permissionsManager);
|
HelpProvider helpProvider = new HelpProvider(permissionsManager);
|
||||||
Set<CommandDescription> baseCommands = CommandInitializer.buildCommands();
|
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);
|
CommandService commandService = new CommandService(this, mapper, helpProvider, messages);
|
||||||
return new CommandHandler(commandService);
|
return new CommandHandler(commandService);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,12 +38,14 @@ public class CommandMapper {
|
|||||||
private final Set<CommandDescription> baseCommands;
|
private final Set<CommandDescription> baseCommands;
|
||||||
private final Messages messages;
|
private final Messages messages;
|
||||||
private final PermissionsManager permissionsManager;
|
private final PermissionsManager permissionsManager;
|
||||||
|
private final HelpProvider helpProvider;
|
||||||
|
|
||||||
public CommandMapper(Set<CommandDescription> baseCommands, Messages messages,
|
public CommandMapper(Set<CommandDescription> baseCommands, Messages messages,
|
||||||
PermissionsManager permissionsManager) {
|
PermissionsManager permissionsManager, HelpProvider helpProvider) {
|
||||||
this.baseCommands = baseCommands;
|
this.baseCommands = baseCommands;
|
||||||
this.messages = messages;
|
this.messages = messages;
|
||||||
this.permissionsManager = permissionsManager;
|
this.permissionsManager = permissionsManager;
|
||||||
|
this.helpProvider = helpProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void outputStandardError(CommandSender sender, FoundCommandResult result) {
|
public void outputStandardError(CommandSender sender, FoundCommandResult result) {
|
||||||
@ -97,7 +99,7 @@ public class CommandMapper {
|
|||||||
|
|
||||||
// Show the command argument help
|
// Show the command argument help
|
||||||
sender.sendMessage(ChatColor.DARK_RED + "Incorrect command arguments!");
|
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) {
|
for (String line : lines) {
|
||||||
sender.sendMessage(line);
|
sender.sendMessage(line);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,6 +20,14 @@ public class CommandService {
|
|||||||
private final HelpProvider helpProvider;
|
private final HelpProvider helpProvider;
|
||||||
private final CommandMapper commandMapper;
|
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) {
|
public CommandService(AuthMe authMe, CommandMapper commandMapper, HelpProvider helpProvider, Messages messages) {
|
||||||
this.authMe = authMe;
|
this.authMe = authMe;
|
||||||
this.messages = messages;
|
this.messages = messages;
|
||||||
@ -27,27 +35,64 @@ public class CommandService {
|
|||||||
this.commandMapper = commandMapper;
|
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) {
|
public void send(CommandSender sender, MessageKey messageKey) {
|
||||||
messages.send(sender, 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) {
|
public FoundCommandResult mapPartsToCommand(CommandSender sender, List<String> commandParts) {
|
||||||
return commandMapper.mapPartsToCommand(sender, 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) {
|
public void outputMappingError(CommandSender sender, FoundCommandResult result) {
|
||||||
commandMapper.outputStandardError(sender, 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) {
|
public void runTaskAsynchronously(Runnable task) {
|
||||||
authMe.getServer().getScheduler().runTaskAsynchronously(authMe, task);
|
authMe.getServer().getScheduler().runTaskAsynchronously(authMe, task);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the AuthMe data source.
|
||||||
|
*
|
||||||
|
* @return The used data source
|
||||||
|
*/
|
||||||
public DataSource getDataSource() {
|
public DataSource getDataSource() {
|
||||||
// TODO ljacqu 20151222: Add getter for .database and rename the field to dataSource
|
// TODO ljacqu 20151222: Add getter for .database and rename the field to dataSource
|
||||||
return authMe.database;
|
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) {
|
public void outputHelp(CommandSender sender, FoundCommandResult result, int options) {
|
||||||
List<String> lines = helpProvider.printHelp(sender, result, options);
|
List<String> lines = helpProvider.printHelp(sender, result, options);
|
||||||
for (String line : lines) {
|
for (String line : lines) {
|
||||||
|
|||||||
@ -3,11 +3,17 @@ package fr.xephi.authme.command.executable.authme;
|
|||||||
import fr.xephi.authme.AntiBot;
|
import fr.xephi.authme.AntiBot;
|
||||||
import fr.xephi.authme.command.CommandService;
|
import fr.xephi.authme.command.CommandService;
|
||||||
import fr.xephi.authme.command.ExecutableCommand;
|
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.ChatColor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display or change the status of the antibot mod.
|
||||||
|
*/
|
||||||
public class SwitchAntiBotCommand implements ExecutableCommand {
|
public class SwitchAntiBotCommand implements ExecutableCommand {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -25,16 +31,12 @@ public class SwitchAntiBotCommand implements ExecutableCommand {
|
|||||||
sender.sendMessage("[AuthMe] AntiBot Manual Override: enabled!");
|
sender.sendMessage("[AuthMe] AntiBot Manual Override: enabled!");
|
||||||
} else if ("OFF".equalsIgnoreCase(newState)) {
|
} else if ("OFF".equalsIgnoreCase(newState)) {
|
||||||
AntiBot.overrideAntiBotStatus(false);
|
AntiBot.overrideAntiBotStatus(false);
|
||||||
sender.sendMessage("[AuthMe] AntiBotMod Manual Override: disabled!");
|
sender.sendMessage("[AuthMe] AntiBot Manual Override: disabled!");
|
||||||
} else {
|
} else {
|
||||||
sender.sendMessage(ChatColor.DARK_RED + "Invalid AntiBot mode!");
|
sender.sendMessage(ChatColor.DARK_RED + "Invalid AntiBot mode!");
|
||||||
// FIXME #306: Restore help showing logic
|
FoundCommandResult result = commandService.mapPartsToCommand(sender, Arrays.asList("authme", "antibot"));
|
||||||
/*
|
commandService.outputHelp(sender, result, HelpProvider.SHOW_ARGUMENTS);
|
||||||
CommandHandler commandHandler = AuthMe.getInstance().getCommandHandler();
|
sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/authme help antibot");
|
||||||
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");*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,8 +2,11 @@ package fr.xephi.authme.command.executable.email;
|
|||||||
|
|
||||||
import fr.xephi.authme.command.CommandService;
|
import fr.xephi.authme.command.CommandService;
|
||||||
import fr.xephi.authme.command.ExecutableCommand;
|
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 org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -13,7 +16,7 @@ public class EmailBaseCommand implements ExecutableCommand {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
|
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
|
||||||
// FIXME #306 use getCommandService().getHelpProvider();
|
FoundCommandResult result = commandService.mapPartsToCommand(sender, Collections.singletonList("email"));
|
||||||
// FIXME #306 HelpProvider.printHelp()
|
commandService.outputHelp(sender, result, HelpProvider.SHOW_CHILDREN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -49,28 +49,16 @@ public class HelpProvider {
|
|||||||
this.permissionsManager = permissionsManager;
|
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) {
|
public List<String> printHelp(CommandSender sender, FoundCommandResult result, int options) {
|
||||||
// FIXME don't overload and pass to the static method
|
if (result.getCommandDescription() == null) {
|
||||||
// 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) {
|
|
||||||
return singletonList(ChatColor.DARK_RED + "Failed to retrieve any help information!");
|
return singletonList(ChatColor.DARK_RED + "Failed to retrieve any help information!");
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> lines = new ArrayList<>();
|
List<String> lines = new ArrayList<>();
|
||||||
lines.add(ChatColor.GOLD + "==========[ " + Settings.helpHeader + " HELP ]==========");
|
lines.add(ChatColor.GOLD + "==========[ " + Settings.helpHeader + " HELP ]==========");
|
||||||
|
|
||||||
CommandDescription command = foundCommand.getCommandDescription();
|
CommandDescription command = result.getCommandDescription();
|
||||||
List<String> labels = ImmutableList.copyOf(foundCommand.getLabels());
|
List<String> labels = ImmutableList.copyOf(result.getLabels());
|
||||||
List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels));
|
List<String> correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels));
|
||||||
|
|
||||||
if (!hasFlag(HIDE_COMMAND, options)) {
|
if (!hasFlag(HIDE_COMMAND, options)) {
|
||||||
|
|||||||
@ -275,7 +275,6 @@ public class CommandInitializerTest {
|
|||||||
* count of arguments.
|
* count of arguments.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
@Ignore // TODO #306 ljacqu 20151214: Un-ignore this test and fix the offending command
|
|
||||||
public void shouldPointToSameExecutableCommandWithConsistentArgumentCount() {
|
public void shouldPointToSameExecutableCommandWithConsistentArgumentCount() {
|
||||||
// given
|
// given
|
||||||
final Map<Class<? extends ExecutableCommand>, Integer> mandatoryArguments = new HashMap<>();
|
final Map<Class<? extends ExecutableCommand>, Integer> mandatoryArguments = new HashMap<>();
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package fr.xephi.authme.command;
|
package fr.xephi.authme.command;
|
||||||
|
|
||||||
|
import fr.xephi.authme.command.help.HelpProvider;
|
||||||
import fr.xephi.authme.output.Messages;
|
import fr.xephi.authme.output.Messages;
|
||||||
import fr.xephi.authme.permission.PermissionsManager;
|
import fr.xephi.authme.permission.PermissionsManager;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@ -42,7 +43,7 @@ public class CommandMapperTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUpMocks() {
|
public void setUpMocks() {
|
||||||
permissionsManagerMock = mock(PermissionsManager.class);
|
permissionsManagerMock = mock(PermissionsManager.class);
|
||||||
mapper = new CommandMapper(commands, mock(Messages.class), permissionsManagerMock);
|
mapper = new CommandMapper(commands, mock(Messages.class), permissionsManagerMock, mock(HelpProvider.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------
|
// -----------
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user