Skip to content

Commit

Permalink
GH-81 Set permission on root command
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Sep 8, 2024
1 parent 55050b0 commit 3aecf7b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Add these declarations to your `pom.xml`.
<dependency>
<groupId>net.dzikoysk</groupId>
<artifactId>funnycommands</artifactId>
<version>0.7.1</version>
<version>0.7.4</version>
</dependency>
</dependencies>
```
Expand All @@ -37,7 +37,7 @@ repositories {
}
dependencies {
implementation "net.dzikoysk:funnycommands:0.7.1"
implementation "net.dzikoysk:funnycommands:0.7.4"
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ allprojects {
apply(plugin = "maven-publish")

group = "net.dzikoysk"
version = "0.7.1"
version = "0.7.4"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.Nullable;
import org.panda_lang.utilities.inject.DependencyInjectionException;
import org.panda_lang.utilities.inject.MethodInjector;
import panda.std.Option;
Expand All @@ -53,19 +54,16 @@ final class DynamicCommand extends Command {
this.funnyCommands = funnyCommands;
this.plugin = plugin;
this.root = root;

if (!StringUtils.isEmpty(commandInfo.getPermission())) {
this.setPermission(commandInfo.getPermission());
}
}

@SuppressWarnings("NullableProblems")
@Override
public boolean execute(CommandSender sender, String alias, String[] arguments) {
Option<Context> contextValue = createContext(sender, alias, arguments);

if (!contextValue.isDefined()) {
funnyCommands.getUsageHandler().accept(sender, root);
return true;
}

Context context = contextValue.get();
Context context = createContext(sender, alias, arguments);
CommandStructure matchedCommand = context.getCommandStructure();
CommandInfo commandInfo = matchedCommand.getMetadata().getCommandInfo();

Expand Down Expand Up @@ -149,15 +147,14 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
}
}

Option<Context> subcommandContext = createContext(sender, alias, arguments);
Context context = createContext(sender, alias, arguments);
CommandInfo commandInfo = context.getCommandStructure().getMetadata().getCommandInfo();

// list subcommands for root request
if (subcommandContext.isEmpty()) {
List<String> names = root.getSubcommandsNames();
return arguments.length == 0 ? names : StringUtil.copyPartialMatches(arguments[arguments.length - 1], names, new ArrayList<>());
// skip completion for unauthorized commands
if (!commandInfo.getPermission().isEmpty() && !sender.hasPermission(commandInfo.getPermission())) {
return Collections.emptyList();
}

Context context = subcommandContext.get();
String[] normalizedArguments = context.getArguments();

if (context.getCommandStructure().equals(root) && normalizedArguments.length == 1) {
Expand All @@ -168,13 +165,6 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
}
}

CommandInfo commandInfo = context.getCommandStructure().getMetadata().getCommandInfo();

// skip completion for unauthorized commands
if (!commandInfo.getPermission().isEmpty() && !sender.hasPermission(commandInfo.getPermission())) {
return Collections.emptyList();
}

// skip undefined completions
if (commandInfo.getCompletes().isEmpty()) {
return Collections.emptyList();
Expand Down Expand Up @@ -202,7 +192,7 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
return completer.apply(context, normalizedArguments[completerIndex]);
}

private Option<Context> createContext(CommandSender commandSender, String alias, String[] arguments) {
private Context createContext(CommandSender commandSender, String alias, String[] arguments) {
String[] normalizedArguments = CommandUtils.normalize(arguments);
CommandStructure commandStructure = root;
int index = 0;
Expand All @@ -218,9 +208,7 @@ private Option<Context> createContext(CommandSender commandSender, String alias,
}

String[] commandArguments = Arrays.copyOfRange(normalizedArguments, index, normalizedArguments.length);
Context context = new Context(funnyCommands, commandSender, commandStructure, alias, commandArguments);

return Option.of(context);
return new Context(funnyCommands, commandSender, commandStructure, alias, commandArguments);
}

private <T> T invoke(CommandMetadata metadata, MethodInjector method, Context context) throws Throwable {
Expand All @@ -239,5 +227,4 @@ private Option<DetailedExceptionHandler<? extends Exception>> resolveExceptionHa
Map<Class<? extends Exception>, DetailedExceptionHandler<? extends Exception>> exceptionHandlers = funnyCommands.getExceptionHandlers();
return ClassUtils.selectMostRelated(exceptionHandlers.keySet(), throwableClass).map(exceptionHandlers::get);
}

}
2 changes: 1 addition & 1 deletion funnycommands/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: FunnyCommands
main: net.dzikoysk.funnycommands.FunnyCommandsPlugin
version: 0.7.1
version: 0.7.4
author: dzikoysk

0 comments on commit 3aecf7b

Please sign in to comment.