#1034 Create debug command structure + utility to see permission groups

- Relevant to current work... :)
This commit is contained in:
ljacqu
2017-01-29 19:47:14 +01:00
parent 350ef9b5e6
commit 24162ad94b
7 changed files with 142 additions and 32 deletions
@@ -0,0 +1,46 @@
package fr.xephi.authme.command.executable.authme.debug;
import fr.xephi.authme.command.ExecutableCommand;
import org.bukkit.command.CommandSender;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Debug command main.
*/
public class DebugCommand implements ExecutableCommand {
@Inject
private PermissionGroups permissionGroups;
private Map<String, DebugSection> sections;
@PostConstruct
private void collectSections() {
Map<String, DebugSection> sections = Stream.of(permissionGroups)
.collect(Collectors.toMap(DebugSection::getName, Function.identity()));
this.sections = sections;
}
@Override
public void executeCommand(CommandSender sender, List<String> arguments) {
if (arguments.isEmpty()) {
sender.sendMessage("Available sections:");
sections.values()
.forEach(e -> sender.sendMessage("- " + e.getName() + ": " + e.getDescription()));
} else {
DebugSection debugSection = sections.get(arguments.get(0).toLowerCase());
if (debugSection == null) {
sender.sendMessage("Unknown subcommand");
} else {
debugSection.execute(sender, arguments.subList(1, arguments.size()));
}
}
}
}
@@ -0,0 +1,30 @@
package fr.xephi.authme.command.executable.authme.debug;
import org.bukkit.command.CommandSender;
import java.util.List;
/**
* A debug section: "child" command of the debug command.
*/
interface DebugSection {
/**
* @return the name to get to this child command
*/
String getName();
/**
* @return short description of the child command
*/
String getDescription();
/**
* Executes the debug child command.
*
* @param sender the sender executing the command
* @param arguments the arguments, without the label of the child command
*/
void execute(CommandSender sender, List<String> arguments);
}
@@ -0,0 +1,40 @@
package fr.xephi.authme.command.executable.authme.debug;
import fr.xephi.authme.permission.PermissionsManager;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
/**
* Outputs the permission groups of a player.
*/
class PermissionGroups implements DebugSection {
@Inject
private PermissionsManager permissionsManager;
@Override
public String getName() {
return "groups";
}
@Override
public String getDescription() {
return "Show permission groups a player belongs to";
}
@Override
public void execute(CommandSender sender, List<String> arguments) {
String name = arguments.isEmpty() ? sender.getName() : arguments.get(0);
Player player = Bukkit.getPlayer(name);
if (player == null) {
sender.sendMessage("Player " + name + " could not be found");
} else {
sender.sendMessage("Player " + name + " has permission groups: "
+ String.join(", ", permissionsManager.getGroups(player)));
}
}
}