aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2021-03-05 21:19:07 +0000
committerLuck <git@lucko.me>2021-03-05 21:19:07 +0000
commit020c10292bc1dc211443c1fb0927c262a2826d91 (patch)
treeb896865ca6d848ef7e1fd4ec87630f96669e98a0
parenta2f2e6dd592d9275bdc1c3e4749b076b87c98c70 (diff)
downloadspark-020c10292bc1dc211443c1fb0927c262a2826d91.tar.gz
spark-020c10292bc1dc211443c1fb0927c262a2826d91.tar.bz2
spark-020c10292bc1dc211443c1fb0927c262a2826d91.zip
Add permissions for each individual subcommand
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java32
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/command/Command.java4
2 files changed, 27 insertions, 9 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java
index f03bab2..cf3b0ee 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java
@@ -163,10 +163,20 @@ public class SparkPlatform {
return this.serverNormalOperationStartTime;
}
+ private List<Command> getAvailableCommands(CommandSender sender) {
+ if (sender.hasPermission("spark")) {
+ return this.commands;
+ }
+ return this.commands.stream()
+ .filter(c -> sender.hasPermission("spark." + c.primaryAlias()))
+ .collect(Collectors.toList());
+ }
+
public void executeCommand(CommandSender sender, String[] args) {
CommandResponseHandler resp = new CommandResponseHandler(this, sender);
+ List<Command> commands = getAvailableCommands(sender);
- if (!sender.hasPermission("spark")) {
+ if (commands.isEmpty()) {
resp.replyPrefixed(text("You do not have permission to use this command.", RED));
return;
}
@@ -197,7 +207,7 @@ public class SparkPlatform {
ArrayList<String> rawArgs = new ArrayList<>(Arrays.asList(args));
String alias = rawArgs.remove(0).toLowerCase();
- for (Command command : this.commands) {
+ for (Command command : commands) {
if (command.aliases().contains(alias)) {
try {
command.executor().execute(this, sender, resp, new Arguments(rawArgs));
@@ -208,25 +218,29 @@ public class SparkPlatform {
}
}
- sendUsage(resp);
+ sendUsage(commands, resp);
}
public List<String> tabCompleteCommand(CommandSender sender, String[] args) {
- if (!sender.hasPermission("spark")) {
+ List<Command> commands = getAvailableCommands(sender);
+ if (commands.isEmpty()) {
return Collections.emptyList();
}
List<String> arguments = new ArrayList<>(Arrays.asList(args));
if (args.length <= 1) {
- List<String> mainCommands = this.commands.stream().map(c -> c.aliases().get(0)).collect(Collectors.toList());
+ List<String> mainCommands = commands.stream()
+ .map(Command::primaryAlias)
+ .collect(Collectors.toList());
+
return TabCompleter.create()
.at(0, CompletionSupplier.startsWith(mainCommands))
.complete(arguments);
}
String alias = arguments.remove(0);
- for (Command command : this.commands) {
+ for (Command command : commands) {
if (command.aliases().contains(alias)) {
return command.tabCompleter().completions(this, sender, arguments);
}
@@ -235,15 +249,15 @@ public class SparkPlatform {
return Collections.emptyList();
}
- private void sendUsage(CommandResponseHandler sender) {
+ private void sendUsage(List<Command> commands, CommandResponseHandler sender) {
sender.replyPrefixed(text()
.append(text("spark", WHITE))
.append(space())
.append(text("v" + getPlugin().getVersion(), GRAY))
.build()
);
- for (Command command : this.commands) {
- String usage = "/" + getPlugin().getCommandName() + " " + command.aliases().get(0);
+ for (Command command : commands) {
+ String usage = "/" + getPlugin().getCommandName() + " " + command.primaryAlias();
ClickEvent clickEvent = ClickEvent.suggestCommand(usage);
sender.reply(text()
.append(text(">", GOLD, BOLD))
diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/Command.java b/spark-common/src/main/java/me/lucko/spark/common/command/Command.java
index db23454..e1a5146 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/command/Command.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/command/Command.java
@@ -62,6 +62,10 @@ public class Command {
return this.tabCompleter;
}
+ public String primaryAlias() {
+ return this.aliases.get(0);
+ }
+
public static final class Builder {
private final ImmutableList.Builder<String> aliases = ImmutableList.builder();
private final ImmutableList.Builder<ArgumentInfo> arguments = ImmutableList.builder();