Skip to content

Commit 3aecf7b

Browse files
committed
GH-81 Set permission on root command
1 parent 55050b0 commit 3aecf7b

4 files changed

Lines changed: 17 additions & 30 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Add these declarations to your `pom.xml`.
2424
<dependency>
2525
<groupId>net.dzikoysk</groupId>
2626
<artifactId>funnycommands</artifactId>
27-
<version>0.7.1</version>
27+
<version>0.7.4</version>
2828
</dependency>
2929
</dependencies>
3030
```
@@ -37,7 +37,7 @@ repositories {
3737
}
3838
3939
dependencies {
40-
implementation "net.dzikoysk:funnycommands:0.7.1"
40+
implementation "net.dzikoysk:funnycommands:0.7.4"
4141
}
4242
```
4343

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ allprojects {
1616
apply(plugin = "maven-publish")
1717

1818
group = "net.dzikoysk"
19-
version = "0.7.1"
19+
version = "0.7.4"
2020

2121
repositories {
2222
mavenCentral()

funnycommands/src/main/java/net/dzikoysk/funnycommands/commands/DynamicCommand.java

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.bukkit.entity.Player;
3434
import org.bukkit.plugin.java.JavaPlugin;
3535
import org.bukkit.util.StringUtil;
36+
import org.jetbrains.annotations.Nullable;
3637
import org.panda_lang.utilities.inject.DependencyInjectionException;
3738
import org.panda_lang.utilities.inject.MethodInjector;
3839
import panda.std.Option;
@@ -53,19 +54,16 @@ final class DynamicCommand extends Command {
5354
this.funnyCommands = funnyCommands;
5455
this.plugin = plugin;
5556
this.root = root;
57+
58+
if (!StringUtils.isEmpty(commandInfo.getPermission())) {
59+
this.setPermission(commandInfo.getPermission());
60+
}
5661
}
5762

5863
@SuppressWarnings("NullableProblems")
5964
@Override
6065
public boolean execute(CommandSender sender, String alias, String[] arguments) {
61-
Option<Context> contextValue = createContext(sender, alias, arguments);
62-
63-
if (!contextValue.isDefined()) {
64-
funnyCommands.getUsageHandler().accept(sender, root);
65-
return true;
66-
}
67-
68-
Context context = contextValue.get();
66+
Context context = createContext(sender, alias, arguments);
6967
CommandStructure matchedCommand = context.getCommandStructure();
7068
CommandInfo commandInfo = matchedCommand.getMetadata().getCommandInfo();
7169

@@ -149,15 +147,14 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
149147
}
150148
}
151149

152-
Option<Context> subcommandContext = createContext(sender, alias, arguments);
150+
Context context = createContext(sender, alias, arguments);
151+
CommandInfo commandInfo = context.getCommandStructure().getMetadata().getCommandInfo();
153152

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

160-
Context context = subcommandContext.get();
161158
String[] normalizedArguments = context.getArguments();
162159

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

171-
CommandInfo commandInfo = context.getCommandStructure().getMetadata().getCommandInfo();
172-
173-
// skip completion for unauthorized commands
174-
if (!commandInfo.getPermission().isEmpty() && !sender.hasPermission(commandInfo.getPermission())) {
175-
return Collections.emptyList();
176-
}
177-
178168
// skip undefined completions
179169
if (commandInfo.getCompletes().isEmpty()) {
180170
return Collections.emptyList();
@@ -202,7 +192,7 @@ public List<String> tabComplete(CommandSender sender, String alias, String[] arg
202192
return completer.apply(context, normalizedArguments[completerIndex]);
203193
}
204194

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

220210
String[] commandArguments = Arrays.copyOfRange(normalizedArguments, index, normalizedArguments.length);
221-
Context context = new Context(funnyCommands, commandSender, commandStructure, alias, commandArguments);
222-
223-
return Option.of(context);
211+
return new Context(funnyCommands, commandSender, commandStructure, alias, commandArguments);
224212
}
225213

226214
private <T> T invoke(CommandMetadata metadata, MethodInjector method, Context context) throws Throwable {
@@ -239,5 +227,4 @@ private Option<DetailedExceptionHandler<? extends Exception>> resolveExceptionHa
239227
Map<Class<? extends Exception>, DetailedExceptionHandler<? extends Exception>> exceptionHandlers = funnyCommands.getExceptionHandlers();
240228
return ClassUtils.selectMostRelated(exceptionHandlers.keySet(), throwableClass).map(exceptionHandlers::get);
241229
}
242-
243230
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: FunnyCommands
22
main: net.dzikoysk.funnycommands.FunnyCommandsPlugin
3-
version: 0.7.1
3+
version: 0.7.4
44
author: dzikoysk

0 commit comments

Comments
 (0)