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 ;
1715import cloud .commandframework .exceptions .InvalidCommandSenderException ;
1816import cloud .commandframework .exceptions .InvalidSyntaxException ;
1917import cloud .commandframework .exceptions .NoPermissionException ;
18+ import cloud .commandframework .exceptions .handling .ExceptionHandler ;
2019import cloud .commandframework .exceptions .parsing .ParserException ;
2120import cloud .commandframework .execution .CommandExecutionCoordinator ;
2221import cloud .commandframework .execution .FilteringCommandSuggestionProcessor ;
2322import cloud .commandframework .keys .CloudKey ;
24- import cloud .commandframework .keys .SimpleCloudKey ;
25- import cloud .commandframework .meta .CommandMeta ;
2623import cloud .commandframework .paper .PaperCommandManager ;
2724import cloud .commandframework .services .types .ConsumerService ;
2825import com .fastasyncworldedit .core .configuration .Caption ;
5956
6057public class CommandRegistry {
6158
62- public static final CloudKey <Snipe > SNIPE_KEY = createTypeKey (
59+ public static final CloudKey <Snipe > SNIPE_KEY = createCloudKey (
6360 "snipe" , Snipe .class
6461 );
65- public static final CloudKey <PerformerSnipe > PERFORMER_SNIPE_KEY = createTypeKey (
62+ public static final CloudKey <PerformerSnipe > PERFORMER_SNIPE_KEY = createCloudKey (
6663 "snipe" , PerformerSnipe .class
6764 );
68- public static final CloudKey <Toolkit > TOOLKIT_KEY = createTypeKey (
65+ public static final CloudKey <Toolkit > TOOLKIT_KEY = createCloudKey (
6966 "toolkit" , Toolkit .class
7067 );
7168
7269 private static final MethodHandles .Lookup LOOKUP = MethodHandles .lookup ();
7370 private static final String NO_DESCRIPTION = "No Description." ;
74- private static final CommandMeta . Key <Boolean > REQUIRE_TOOLKIT = createMetaKey (
71+ private static final CloudKey <Boolean > REQUIRE_TOOLKIT = createCloudKey (
7572 "require-toolkit" ,
7673 Boolean .class
7774 );
@@ -140,16 +137,14 @@ private BukkitCommandManager<SniperCommander> createCommandManager() throws Exce
140137 // Ensures that we are working with a voxel sniper annotated command.
141138 CommandContext <SniperCommander > commandContext = context .getCommandContext ();
142139 Command <SniperCommander > command = context .getCommand ();
143- if (!(commandContext .getSender () instanceof Sniper sniper )
144- || !(command .getCommandExecutionHandler () instanceof MethodCommandExecutionHandler <SniperCommander > handler )) {
140+ if (!(commandContext .sender () instanceof Sniper sniper )
141+ || !(command .commandExecutionHandler () instanceof MethodCommandExecutionHandler <SniperCommander > handler )) {
145142 return ;
146143 }
147144
148145 Toolkit toolkit ;
149146 // Toolkit requirement relies on the custom annotation.
150- if (command .getCommandMeta ()
151- .get (REQUIRE_TOOLKIT )
152- .orElse (false )) {
147+ if (command .commandMeta ().getOrDefault (REQUIRE_TOOLKIT , false )) {
153148 if ((toolkit = sniper .getCurrentToolkit ()) == null ) {
154149 sniper .print (Caption .of ("voxelsniper.command.missing-toolkit" ));
155150 ConsumerService .interrupt ();
@@ -181,54 +176,49 @@ private BukkitCommandManager<SniperCommander> createCommandManager() throws Exce
181176 });
182177
183178 // Handles exceptions.
184- commandManager .registerExceptionHandler ( InvalidSyntaxException .class , ( commander , e ) ->
185- commander .print (Caption .of (
179+ commandManager .exceptionController (). registerHandler ( InvalidSyntaxException .class , ctx ->
180+ ctx . context (). sender () .print (Caption .of (
186181 "voxelsniper.command.invalid-command-syntax" ,
187- e .getCorrectSyntax ()
188- )));
189- commandManager . registerExceptionHandler (InvalidCommandSenderException .class , ( commander , e ) ->
190- commander .print (Caption .of (
182+ ctx . exception () .getCorrectSyntax ()
183+ ))
184+ ). registerHandler (InvalidCommandSenderException .class , ctx ->
185+ ctx . context (). sender () .print (Caption .of (
191186 "voxelsniper.command.invalid-sender-type" ,
192- e .getRequiredSender ().getSimpleName ()
193- )));
194- commandManager . registerExceptionHandler (NoPermissionException .class , ( commander , e ) ->
195- commander .print (Caption .of (
187+ ctx . exception () .getRequiredSender ().getSimpleName ()
188+ ))
189+ ). registerHandler (NoPermissionException .class , ctx ->
190+ ctx . context (). sender () .print (Caption .of (
196191 "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 (
192+ ctx . exception (). missingPermission ()
193+ ))
194+ ). registerHandler (
195+ ArgumentParseException . class ,
196+ ExceptionHandler . unwrappingHandler ()
197+ ). registerHandler ( VoxelCommandElementParseException . class , ctx ->
198+ ctx . context (). sender (). print (ctx . exception (). getErrorMessage ())
199+ ). registerHandler ( EnumParser .EnumParseException . class , ctx ->
200+ ctx . context (). sender () .print (Caption .of (
206201 "voxelsniper.command.invalid-enum" ,
207- ee .getInput (),
202+ ctx . exception () .getInput (),
208203 VoxelSniperText .formatList (
209- Arrays .stream (ee .getEnumClass ().getEnumConstants ()).toList (),
204+ Arrays .stream (ctx . exception () .getEnumClass ().getEnumConstants ()).toList (),
210205 (value , value2 ) -> Integer .compare (value .ordinal (), value2 .ordinal ()),
211206 value -> TextComponent .of (value .name ().toLowerCase (Locale .ROOT )),
212207 "voxelsniper.command.invalid-enum"
213208 )
214- ));
215- } else if ( t instanceof ParserException pe ) {
216- commander .print (Caption .of (
217- pe .errorCaption ()
218- .getKey ()
209+ ))
210+ ). registerHandler ( ParserException . class , ctx ->
211+ ctx . context (). sender () .print (Caption .of (
212+ ctx . exception () .errorCaption ()
213+ .key ()
219214 .replace ("argument.parse.failure." , "voxelsniper.command.invalid-" ),
220- Arrays .stream (pe .captionVariables ())
221- .map (CaptionVariable ::getValue )
215+ Arrays .stream (ctx . exception () .captionVariables ())
216+ .map (CaptionVariable ::value )
222217 .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 ();
218+ ))
219+ ).registerHandler (Throwable .class , ctx -> {
220+ ctx .context ().sender ().print (Caption .of ("voxelsniper.error.unexpected" ));
221+ ctx .exception ().printStackTrace ();
232222 });
233223
234224 return commandManager ;
@@ -238,11 +228,7 @@ private AnnotationParser<SniperCommander> createAnnotationParser(BukkitCommandMa
238228 // Creates the annotation parser.
239229 AnnotationParser <SniperCommander > annotationParser = new AnnotationParser <>(
240230 commandManager ,
241- SniperCommander .class ,
242- parserParameters -> CommandMeta .simple ()
243- .with (CommandMeta .DESCRIPTION , parserParameters
244- .get (StandardParameters .DESCRIPTION , NO_DESCRIPTION ))
245- .build ()
231+ SniperCommander .class
246232 );
247233
248234 // Handles the custom annotations.
@@ -290,7 +276,7 @@ private void handleDynamicRanges(
290276 MethodCommandExecutionHandler <SniperCommander > handler ,
291277 Object instance
292278 ) {
293- SniperCommander commander = commandContext .getSender ();
279+ SniperCommander commander = commandContext .sender ();
294280 MethodCommandExecutionHandler .CommandMethodContext <SniperCommander > methodContext = handler .context ();
295281
296282 // Dynamic range is based on executor instance fields, we must postprocess them manually.
@@ -310,9 +296,7 @@ private void handleDynamicRanges(
310296 argumentName = this .annotationParser .processString (argumentAnnotation .value ());
311297 }
312298
313- CommandArgument <SniperCommander , Number > argument =
314- (CommandArgument <SniperCommander , Number >) methodContext .commandArguments ().get (argumentName );
315- double number = commandContext .get (argument ).doubleValue ();
299+ double number = commandContext .<Number >get (argumentName ).doubleValue ();
316300
317301 DynamicRange dynamicRangeAnnotation = parameter .getAnnotation (DynamicRange .class );
318302 String min = dynamicRangeAnnotation .min ();
@@ -444,12 +428,8 @@ public AnnotationParser<SniperCommander> getAnnotationParser() {
444428 return annotationParser ;
445429 }
446430
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 );
431+ private static <T > CloudKey <T > createCloudKey (String id , Class <T > clazz ) {
432+ return CloudKey .of ("voxelsniper-" + id , TypeToken .get (clazz ));
453433 }
454434
455435}
0 commit comments