55import cloud .commandframework .annotations .Argument ;
66import cloud .commandframework .annotations .MethodCommandExecutionHandler ;
77import cloud .commandframework .annotations .injection .ParameterInjectorRegistry ;
8- import cloud .commandframework .arguments .CommandArgument ;
9- import cloud .commandframework .arguments .parser .StandardParameters ;
10- import cloud .commandframework .arguments .standard .EnumArgument ;
8+ import cloud .commandframework .arguments .standard .EnumParser ;
119import cloud .commandframework .bukkit .BukkitCommandManager ;
1210import cloud .commandframework .bukkit .CloudBukkitCapabilities ;
1311import cloud .commandframework .captions .CaptionVariable ;
1412import cloud .commandframework .context .CommandContext ;
1513import cloud .commandframework .exceptions .ArgumentParseException ;
16- import cloud .commandframework .exceptions .CommandExecutionException ;
1714import cloud .commandframework .exceptions .InvalidCommandSenderException ;
1815import cloud .commandframework .exceptions .InvalidSyntaxException ;
1916import cloud .commandframework .exceptions .NoPermissionException ;
17+ import cloud .commandframework .exceptions .handling .ExceptionHandler ;
2018import cloud .commandframework .exceptions .parsing .ParserException ;
2119import cloud .commandframework .execution .CommandExecutionCoordinator ;
2220import cloud .commandframework .execution .FilteringCommandSuggestionProcessor ;
2321import cloud .commandframework .keys .CloudKey ;
24- import cloud .commandframework .keys .SimpleCloudKey ;
25- import cloud .commandframework .meta .CommandMeta ;
2622import cloud .commandframework .paper .PaperCommandManager ;
2723import cloud .commandframework .services .types .ConsumerService ;
2824import com .fastasyncworldedit .core .configuration .Caption ;
5955
6056public class CommandRegistry {
6157
62- public static final CloudKey <Snipe > SNIPE_KEY = createTypeKey (
58+ public static final CloudKey <Snipe > SNIPE_KEY = createCloudKey (
6359 "snipe" , Snipe .class
6460 );
65- public static final CloudKey <PerformerSnipe > PERFORMER_SNIPE_KEY = createTypeKey (
61+ public static final CloudKey <PerformerSnipe > PERFORMER_SNIPE_KEY = createCloudKey (
6662 "snipe" , PerformerSnipe .class
6763 );
68- public static final CloudKey <Toolkit > TOOLKIT_KEY = createTypeKey (
64+ public static final CloudKey <Toolkit > TOOLKIT_KEY = createCloudKey (
6965 "toolkit" , Toolkit .class
7066 );
7167
7268 private static final MethodHandles .Lookup LOOKUP = MethodHandles .lookup ();
7369 private static final String NO_DESCRIPTION = "No Description." ;
74- private static final CommandMeta . Key <Boolean > REQUIRE_TOOLKIT = createMetaKey (
70+ private static final CloudKey <Boolean > REQUIRE_TOOLKIT = createCloudKey (
7571 "require-toolkit" ,
7672 Boolean .class
7773 );
@@ -138,18 +134,16 @@ private BukkitCommandManager<SniperCommander> createCommandManager() throws Exce
138134 // Handles post-processor.
139135 commandManager .registerCommandPostProcessor (context -> {
140136 // Ensures that we are working with a voxel sniper annotated command.
141- CommandContext <SniperCommander > commandContext = context .getCommandContext ();
142- Command <SniperCommander > command = context .getCommand ();
143- if (!(commandContext .getSender () instanceof Sniper sniper )
144- || !(command .getCommandExecutionHandler () instanceof MethodCommandExecutionHandler <SniperCommander > handler )) {
137+ CommandContext <SniperCommander > commandContext = context .commandContext ();
138+ Command <SniperCommander > command = context .command ();
139+ if (!(commandContext .sender () instanceof Sniper sniper )
140+ || !(command .commandExecutionHandler () instanceof MethodCommandExecutionHandler <SniperCommander > handler )) {
145141 return ;
146142 }
147143
148144 Toolkit toolkit ;
149145 // Toolkit requirement relies on the custom annotation.
150- if (command .getCommandMeta ()
151- .get (REQUIRE_TOOLKIT )
152- .orElse (false )) {
146+ if (command .commandMeta ().getOrDefault (REQUIRE_TOOLKIT , false )) {
153147 if ((toolkit = sniper .getCurrentToolkit ()) == null ) {
154148 sniper .print (Caption .of ("voxelsniper.command.missing-toolkit" ));
155149 ConsumerService .interrupt ();
@@ -181,54 +175,49 @@ private BukkitCommandManager<SniperCommander> createCommandManager() throws Exce
181175 });
182176
183177 // Handles exceptions.
184- commandManager .registerExceptionHandler ( InvalidSyntaxException .class , ( commander , e ) ->
185- commander .print (Caption .of (
178+ commandManager .exceptionController (). registerHandler ( InvalidSyntaxException .class , ctx ->
179+ ctx . context (). sender () .print (Caption .of (
186180 "voxelsniper.command.invalid-command-syntax" ,
187- e .getCorrectSyntax ()
188- )));
189- commandManager . registerExceptionHandler (InvalidCommandSenderException .class , ( commander , e ) ->
190- commander .print (Caption .of (
181+ ctx . exception () .getCorrectSyntax ()
182+ ))
183+ ). registerHandler (InvalidCommandSenderException .class , ctx ->
184+ ctx . context (). sender () .print (Caption .of (
191185 "voxelsniper.command.invalid-sender-type" ,
192- e .getRequiredSender ().getSimpleName ()
193- )));
194- commandManager . registerExceptionHandler (NoPermissionException .class , ( commander , e ) ->
195- commander .print (Caption .of (
186+ ctx . exception () .getRequiredSender ().getSimpleName ()
187+ ))
188+ ). registerHandler (NoPermissionException .class , ctx ->
189+ ctx . context (). sender () .print (Caption .of (
196190 "voxelsniper.command.missing-permission" ,
197- e . getMissingPermission ()
198- )));
199- commandManager . registerExceptionHandler ( ArgumentParseException . class , ( commander , e ) -> {
200- Throwable t = e . getCause ();
201-
202- if ( t instanceof VoxelCommandElementParseException ve ) {
203- commander . print (ve . getErrorMessage ());
204- } else if ( t instanceof EnumArgument .EnumParseException ee ) {
205- commander .print (Caption .of (
191+ ctx . exception (). missingPermission ()
192+ ))
193+ ). registerHandler (
194+ ArgumentParseException . class ,
195+ ExceptionHandler . unwrappingHandler ()
196+ ). registerHandler ( VoxelCommandElementParseException . class , ctx ->
197+ ctx . context (). sender (). print (ctx . exception (). getErrorMessage ())
198+ ). registerHandler ( EnumParser .EnumParseException . class , ctx ->
199+ ctx . context (). sender () .print (Caption .of (
206200 "voxelsniper.command.invalid-enum" ,
207- ee .getInput (),
201+ ctx . exception () .getInput (),
208202 VoxelSniperText .formatList (
209- Arrays .stream (ee .getEnumClass ().getEnumConstants ()).toList (),
203+ Arrays .stream (ctx . exception () .getEnumClass ().getEnumConstants ()).toList (),
210204 (value , value2 ) -> Integer .compare (value .ordinal (), value2 .ordinal ()),
211205 value -> TextComponent .of (value .name ().toLowerCase (Locale .ROOT )),
212206 "voxelsniper.command.invalid-enum"
213207 )
214- ));
215- } else if ( t instanceof ParserException pe ) {
216- commander .print (Caption .of (
217- pe .errorCaption ()
218- .getKey ()
208+ ))
209+ ). registerHandler ( ParserException . class , ctx ->
210+ ctx . context (). sender () .print (Caption .of (
211+ ctx . exception () .errorCaption ()
212+ .key ()
219213 .replace ("argument.parse.failure." , "voxelsniper.command.invalid-" ),
220- Arrays .stream (pe .captionVariables ())
221- .map (CaptionVariable ::getValue )
214+ Arrays .stream (ctx . exception () .captionVariables ())
215+ .map (CaptionVariable ::value )
222216 .toArray (Object []::new )
223- ));
224- } else {
225- commander .print (Caption .of ("voxelsniper.error.unexpected" ));
226- e .printStackTrace ();
227- }
228- });
229- commandManager .registerExceptionHandler (CommandExecutionException .class , (commander , e ) -> {
230- commander .print (Caption .of ("voxelsniper.error.unexpected" ));
231- e .printStackTrace ();
217+ ))
218+ ).registerHandler (Throwable .class , ctx -> {
219+ ctx .context ().sender ().print (Caption .of ("voxelsniper.error.unexpected" ));
220+ ctx .exception ().printStackTrace ();
232221 });
233222
234223 return commandManager ;
@@ -238,11 +227,7 @@ private AnnotationParser<SniperCommander> createAnnotationParser(BukkitCommandMa
238227 // Creates the annotation parser.
239228 AnnotationParser <SniperCommander > annotationParser = new AnnotationParser <>(
240229 commandManager ,
241- SniperCommander .class ,
242- parserParameters -> CommandMeta .simple ()
243- .with (CommandMeta .DESCRIPTION , parserParameters
244- .get (StandardParameters .DESCRIPTION , NO_DESCRIPTION ))
245- .build ()
230+ SniperCommander .class
246231 );
247232
248233 // Handles the custom annotations.
@@ -290,7 +275,7 @@ private void handleDynamicRanges(
290275 MethodCommandExecutionHandler <SniperCommander > handler ,
291276 Object instance
292277 ) {
293- SniperCommander commander = commandContext .getSender ();
278+ SniperCommander commander = commandContext .sender ();
294279 MethodCommandExecutionHandler .CommandMethodContext <SniperCommander > methodContext = handler .context ();
295280
296281 // Dynamic range is based on executor instance fields, we must postprocess them manually.
@@ -310,9 +295,7 @@ private void handleDynamicRanges(
310295 argumentName = this .annotationParser .processString (argumentAnnotation .value ());
311296 }
312297
313- CommandArgument <SniperCommander , Number > argument =
314- (CommandArgument <SniperCommander , Number >) methodContext .commandArguments ().get (argumentName );
315- double number = commandContext .get (argument ).doubleValue ();
298+ double number = commandContext .<Number >get (argumentName ).doubleValue ();
316299
317300 DynamicRange dynamicRangeAnnotation = parameter .getAnnotation (DynamicRange .class );
318301 String min = dynamicRangeAnnotation .min ();
@@ -444,12 +427,8 @@ public AnnotationParser<SniperCommander> getAnnotationParser() {
444427 return annotationParser ;
445428 }
446429
447- private static <T > CloudKey <T > createTypeKey (String id , Class <T > clazz ) {
448- return SimpleCloudKey .of ("voxelsniper-" + id , TypeToken .get (clazz ));
449- }
450-
451- private static <T > CommandMeta .Key <T > createMetaKey (String id , Class <T > clazz ) {
452- return CommandMeta .Key .of (TypeToken .get (clazz ), "voxelsniper-" + id );
430+ private static <T > CloudKey <T > createCloudKey (String id , Class <T > clazz ) {
431+ return CloudKey .of ("voxelsniper-" + id , TypeToken .get (clazz ));
453432 }
454433
455434}
0 commit comments