11package net .azalealibrary .configuration ;
22
33import net .azalealibrary .command .Arguments ;
4+ import net .azalealibrary .command .AzaleaException ;
45import net .azalealibrary .command .CommandNode ;
56import net .azalealibrary .configuration .property .ConfigurableProperty ;
7+ import org .bukkit .ChatColor ;
68import org .bukkit .command .CommandSender ;
79
810import java .util .ArrayList ;
11+ import java .util .Arrays ;
912import java .util .List ;
1013
1114public class ConfigureCommand extends CommandNode {
1215
13- private static final String SET = "set" ;
14- private static final String RESET = "reset" ;
15- private static final String INFO = "info" ;
16-
1716 public ConfigureCommand () {
1817 super ("configure" );
1918 }
@@ -22,17 +21,18 @@ public ConfigureCommand() {
2221 public List <String > complete (CommandSender sender , Arguments arguments ) {
2322 if (arguments .size () == 1 ) {
2423 return AzaleaConfigurationApi .getConfigurations ().stream ().map (Configurable ::getName ).toList ();
24+ } else if (arguments .size () == 2 ) {
25+ return Arrays .stream (Action .values ()).map (v -> v .name ().toLowerCase ()).toList ();
2526 } else {
2627 Configurable configuration = arguments .find (0 , "configuration" , AzaleaConfigurationApi ::getConfiguration );
28+ Action action = arguments .find (1 , "action" , input -> Action .valueOf (input .toUpperCase ()));
2729 List <ConfigurableProperty <?, ?>> properties = configuration .getProperties ();
2830
29- if (arguments .size () == 2 ) {
31+ if (arguments .size () == 3 ) {
3032 return properties .stream ().map (ConfigurableProperty ::getName ).toList ();
31- } else if (arguments .size () == 3 ) {
32- return List .of (SET , RESET , INFO );
33- } else if (arguments .size () > 3 && arguments .is (2 , SET )) {
33+ } else if (arguments .size () == 4 && action == Action .SET ) {
3434 return properties .stream ()
35- .filter (p -> p .getName ().equals (arguments .get (1 )))
35+ .filter (p -> p .getName ().equals (arguments .get (2 )))
3636 .findFirst ().map (p -> p .onComplete (sender , arguments .subArguments (3 )))
3737 .orElse (List .of ());
3838 }
@@ -43,21 +43,59 @@ public List<String> complete(CommandSender sender, Arguments arguments) {
4343 @ Override
4444 public void execute (CommandSender sender , Arguments arguments ) {
4545 Configurable configuration = arguments .find (0 , "configuration" , AzaleaConfigurationApi ::getConfiguration );
46- ConfigurableProperty <?, ?> property = arguments .find (1 , "property" , input -> configuration .getProperties ().stream ().filter (p -> p .getName ().equals (input )).findFirst ().orElse (null ));
47- String action = arguments .matchesAny (2 , "action" , SET , RESET , INFO );
46+ Action action = arguments .find (1 , "action" , input -> Action .valueOf (input .toUpperCase ()));
47+ Arguments sub = arguments .subArguments (3 );
48+ List <ConfigurableProperty <?, ?>> properties = arguments .find (2 , "property" , input -> configuration .getProperties ().stream ()
49+ .filter (c -> action .predicate .test (sender , sub , input , c ))
50+ .toList ());
51+
52+ if (properties .isEmpty ()) {
53+ throw new AzaleaException ("No properties found." );
54+ }
4855
4956 switch (action ) {
5057 case SET -> {
51- property .onExecute (sender , arguments .subArguments (3 ));
52- sender .sendMessage ("Property " + TextUtil .getName (property ) + " updated." );
58+ sender .sendMessage (getMessage (properties , "updated" ));
59+
60+ for (ConfigurableProperty <?, ?> property : properties ) {
61+ property .onExecute (sender , sub );
62+ sender .sendMessage (" " + TextUtil .getName (property ));
63+ }
5364 }
5465 case RESET -> {
55- property .reset ();
56- sender .sendMessage ("Property " + TextUtil .getName (property ) + " has been reset." );
66+ sender .sendMessage (getMessage (properties , "reset" ));
67+
68+ for (ConfigurableProperty <?, ?> property : properties ) {
69+ property .reset ();
70+ sender .sendMessage (" " + TextUtil .getName (property ));
71+ }
5772 }
5873 case INFO -> {
59- sender .sendMessage (TextUtil .getCommandInfo (property , 60 ).toArray (String []::new ));
74+ sender .sendMessage (TextUtil .getCommandInfo (properties . get ( 0 ) , 60 ).toArray (String []::new ));
6075 }
6176 }
6277 }
78+
79+ private static String getMessage (List <ConfigurableProperty <?, ?>> properties , String action ) {
80+ return String .valueOf (ChatColor .YELLOW ) + properties .size () + ChatColor .RESET +
81+ (properties .size () > 1 ? " properties" : " property" ) +
82+ (properties .size () > 1 ? " have" : " has" ) + " been " + action + ":" ;
83+ }
84+
85+ private enum Action {
86+ SET ((s , a , i , c ) -> c .getName ().matches (i ) && c .getType ().test (s , a )),
87+ RESET ((s , a , i , c ) -> c .getName ().matches (i )),
88+ INFO ((s , a , i , c ) -> c .getName ().equals (i ));
89+
90+ final ActionTester predicate ;
91+
92+ Action (ActionTester predicate ) {
93+ this .predicate = predicate ;
94+ }
95+
96+ @ FunctionalInterface
97+ private interface ActionTester {
98+ boolean test (CommandSender sender , Arguments arguments , String input , ConfigurableProperty <?, ?> property );
99+ }
100+ }
63101}
0 commit comments