11package dev .rollczi .litecommands .factory ;
22
33import dev .rollczi .litecommands .command .amount .AmountValidator ;
4+ import dev .rollczi .litecommands .meta .CommandMeta ;
45
56import java .lang .reflect .Method ;
67import java .util .ArrayList ;
@@ -19,80 +20,62 @@ public class CommandState implements CommandEditor.State {
1920 private final String name ;
2021 private final List <Route > routesBefore = new ArrayList <>();
2122 private final Set <String > aliases = new HashSet <>();
22- private final Set <String > permissions = new HashSet <>();
23- private final Set <String > executedPermissions = new HashSet <>();
2423 private final Set <CommandState > children = new HashSet <>();
2524 private final Map <Method , CommandState > executors = new HashMap <>();
26- private final AmountValidator validator ;
25+ private final CommandMeta meta ;
2726 private final boolean canceled ;
2827
2928 public CommandState () {
3029 this .name = null ;
31- this .validator = AmountValidator . none ();
30+ this .meta = CommandMeta . create ();
3231 this .canceled = false ;
3332 }
3433
35- private CommandState (String name , List <Route > routesBefore , Set <String > aliases , Set <String > permissions , Set < String > executedPermissions , Set < CommandState > children , Map <Method , CommandState > executors , AmountValidator validator , boolean canceled ) {
34+ private CommandState (String name , List <Route > routesBefore , Set <String > aliases , Set <CommandState > children , Map <Method , CommandState > executors , CommandMeta meta , boolean canceled ) {
3635 this .name = name ;
36+ this .meta = meta ;
3737 this .canceled = canceled ;
3838 this .routesBefore .addAll (routesBefore );
3939 this .aliases .addAll (aliases );
40- this .permissions .addAll (permissions );
41- this .executedPermissions .addAll (executedPermissions );
4240 this .children .addAll (children );
4341 this .executors .putAll (executors );
44- this .validator = validator ;
4542 }
4643
4744 @ Override
4845 public CommandState name (String name ) {
49- return new CommandState (name , routesBefore , aliases , permissions , executedPermissions , children , executors , validator , canceled );
46+ return new CommandState (name , routesBefore , aliases , children , executors , meta , canceled );
5047 }
5148
5249 public CommandState routeBefore (Route route ) {
5350 ArrayList <Route > routes = new ArrayList <>(routesBefore );
5451 routes .add (route );
5552
56- return new CommandState (name , routes , aliases , permissions , executedPermissions , children , executors , validator , canceled );
53+ return new CommandState (name , routes , aliases , children , executors , meta , canceled );
5754 }
5855
5956 public CommandState alias (String ... alias ) {
6057 Set <String > aliases = new HashSet <>(this .aliases );
6158 Collections .addAll (aliases , alias );
6259
63- return new CommandState (name , routesBefore , aliases , permissions , executedPermissions , children , executors , validator , canceled );
64- }
65-
66- public CommandState permission (String ... permission ) {
67- Set <String > permissions = new HashSet <>(this .permissions );
68- Collections .addAll (permissions , permission );
69-
70- return new CommandState (name , routesBefore , aliases , permissions , executedPermissions , children , executors , validator , canceled );
71- }
72-
73- public CommandState executedPermission (String ... permission ) {
74- Set <String > permissions = new HashSet <>(this .executedPermissions );
75- Collections .addAll (permissions , permission );
76-
77- return new CommandState (name , routesBefore , aliases , permissions , executedPermissions , children , executors , validator , canceled );
60+ return new CommandState (name , routesBefore , aliases , children , executors , meta , canceled );
7861 }
7962
8063 public CommandState child (CommandState ... child ) {
8164 Set <CommandState > children = new HashSet <>(this .children );
8265 Collections .addAll (children , child );
8366
84- return new CommandState (name , routesBefore , aliases , permissions , executedPermissions , children , executors , validator , canceled );
67+ return new CommandState (name , routesBefore , aliases , children , executors , meta , canceled );
8568 }
8669
8770 public CommandState executor (Method method , CommandState state ) {
8871 Map <Method , CommandState > executors = new HashMap <>(this .executors );
8972 executors .put (method , state );
9073
91- return new CommandState (name , routesBefore , aliases , permissions , executedPermissions , children , executors , validator , canceled );
74+ return new CommandState (name , routesBefore , aliases , children , executors , meta , canceled );
9275 }
9376
9477 public CommandState validator (Function <AmountValidator , AmountValidator > edit ) {
95- return new CommandState ( name , routesBefore , aliases , permissions , executedPermissions , children , executors , edit . apply ( this . validator ), canceled );
78+ return this . meta ( commandMeta -> commandMeta . applyAmountValidator ( edit ) );
9679 }
9780
9881 public String getName () {
@@ -107,14 +90,6 @@ public Set<String> getAliases() {
10790 return Collections .unmodifiableSet (aliases );
10891 }
10992
110- public Set <String > getPermissions () {
111- return Collections .unmodifiableSet (permissions );
112- }
113-
114- public Set <String > getExecutedPermissions () {
115- return Collections .unmodifiableSet (executedPermissions );
116- }
117-
11893 public Set <CommandState > getChildren () {
11994 return Collections .unmodifiableSet (children );
12095 }
@@ -123,10 +98,6 @@ public Map<Method, CommandState> getExecutors() {
12398 return Collections .unmodifiableMap (executors );
12499 }
125100
126- public AmountValidator getValidator () {
127- return validator ;
128- }
129-
130101 public CommandState mergeMethod (Method method , CommandState other ) {
131102 return this .mergeMethod (other .routesBefore .iterator (), method , other );
132103 }
@@ -155,7 +126,7 @@ public CommandState alias(Collection<String> aliases) {
155126 Set <String > aliasesSet = new HashSet <>(this .aliases );
156127 aliasesSet .addAll (aliases );
157128
158- return new CommandState (name , routesBefore , aliasesSet , permissions , executedPermissions , children , executors , validator , canceled );
129+ return new CommandState (name , routesBefore , aliasesSet , children , executors , meta , canceled );
159130 }
160131
161132 public boolean isCanceled () {
@@ -188,28 +159,50 @@ public CommandState aliases(Collection<String> aliases, boolean removeOld) {
188159 Set <String > aliasesSet = removeOld ? new HashSet <>() : new HashSet <>(this .aliases );
189160 aliasesSet .addAll (aliases );
190161
191- return new CommandState (name , routesBefore , aliasesSet , permissions , executedPermissions , children , executors , validator , canceled );
162+ return new CommandState (name , routesBefore , aliasesSet , children , executors , meta , canceled );
192163 }
193164
194165 @ Override
195166 public CommandState permission (Collection <String > permissions , boolean removeOld ) {
196- Set <String > perm = removeOld ? new HashSet <>() : new HashSet <>(this .permissions );
197- perm .addAll (permissions );
167+ return this .meta (commandMeta -> {
168+ if (removeOld ) {
169+ commandMeta .clearPermissions ();
170+ }
198171
199- return new CommandState (name , routesBefore , aliases , perm , executedPermissions , children , executors , validator , canceled );
172+ commandMeta .addPermission (permissions );
173+ return commandMeta ;
174+ });
200175 }
201176
202177 @ Override
203178 public CommandState permissionExcluded (Collection <String > executedPermissions , boolean removeOld ) {
204- Set <String > executedPerm = removeOld ? new HashSet <>() : new HashSet <>(this .executedPermissions );
205- executedPerm .addAll (executedPermissions );
179+ return this .meta (commandMeta -> {
180+ if (removeOld ) {
181+ commandMeta .clearExcludedPermissions ();
182+ }
206183
207- return new CommandState (name , routesBefore , aliases , permissions , executedPerm , children , executors , validator , canceled );
184+ commandMeta .addExcludedPermission (executedPermissions );
185+ return commandMeta ;
186+ });
208187 }
209188
210189 @ Override
211190 public CommandState cancel (boolean canceled ) {
212- return new CommandState (name , routesBefore , aliases , permissions , executedPermissions , children , executors , validator , canceled );
191+ return new CommandState (name , routesBefore , aliases , children , executors , meta , canceled );
192+ }
193+
194+ @ Override
195+ public CommandState meta (Function <CommandMeta , CommandMeta > edit ) {
196+ CommandMeta newMeta = CommandMeta .create ();
197+ newMeta .apply (this .meta );
198+
199+ CommandMeta metaData = edit .apply (newMeta );
200+
201+ return new CommandState (name , routesBefore , aliases , children , executors , metaData , canceled );
202+ }
203+
204+ public CommandMeta getMeta () {
205+ return meta ;
213206 }
214207
215208 public static class Route {
0 commit comments