11package xyz .bitsquidd .bits .lib .command .util ;
22
33import org .jspecify .annotations .NullMarked ;
4+ import org .jspecify .annotations .Nullable ;
45
56import xyz .bitsquidd .bits .lib .command .BitsCommand ;
67import xyz .bitsquidd .bits .lib .command .annotation .Command ;
1516import java .lang .reflect .Constructor ;
1617import java .lang .reflect .Method ;
1718import java .lang .reflect .Parameter ;
18- import java .util .ArrayList ;
19- import java .util .Arrays ;
20- import java .util .Collections ;
21- import java .util .List ;
19+ import java .util .*;
2220import java .util .stream .Collectors ;
2321import java .util .stream .Stream ;
2422
2523@ NullMarked
2624public class BitsCommandBuilder {
25+ private @ Nullable BitsCommand commandInstance ;
2726 private final Class <? extends BitsCommand > commandClass ;
2827
2928 @ SuppressWarnings ("FieldCanBeLocal" )
@@ -35,6 +34,13 @@ public class BitsCommandBuilder {
3534
3635 private final String permissionString ;
3736
37+ // Allow us to build from an instance or a class.
38+ // Instances are used only for gathering extra requirements.
39+ public BitsCommandBuilder (@ Nullable BitsCommand commandInstance ) {
40+ this (Objects .requireNonNull (commandInstance ).getClass ());
41+ this .commandInstance = commandInstance ;
42+ }
43+
3844 public BitsCommandBuilder (Class <? extends BitsCommand > commandClass ) {
3945 this .commandClass = commandClass ;
4046
@@ -93,8 +99,8 @@ public Constructor<?> toConstructor() {
9399 }
94100
95101
96- public List <BitsCommandRequirement > getRequirements () {
97- List <BitsCommandRequirement > requirements = new ArrayList <>();
102+ public Set <BitsCommandRequirement > getRequirements () {
103+ Set <BitsCommandRequirement > requirements = new HashSet <>();
98104 if (commandName .isEmpty ()) requirements .add (PermissionRequirement .of (permissionString ));
99105
100106 // Gather permission strings and convert them to requirements.
@@ -113,6 +119,7 @@ public List<BitsCommandRequirement> getRequirements() {
113119 .toList ());
114120 }
115121
122+ if (commandInstance != null ) requirements .addAll (commandInstance .getAddedRequirements ());
116123 return requirements ;
117124 }
118125
0 commit comments