3333import org .bukkit .entity .Player ;
3434import org .bukkit .plugin .java .JavaPlugin ;
3535import org .bukkit .util .StringUtil ;
36+ import org .jetbrains .annotations .Nullable ;
3637import org .panda_lang .utilities .inject .DependencyInjectionException ;
3738import org .panda_lang .utilities .inject .MethodInjector ;
3839import 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}
0 commit comments