@@ -47,36 +47,38 @@ public final class JsonToBrigadier {
4747 /**
4848 * Parses a file at the path
4949 *
50- * @param path The path to the JSON file
51- * @param <T> The command context type
52- * @param <S> The {@link ArgumentBuilder} self type
50+ * @param path The path to the JSON file
51+ * @param contextClass The class for the context that the command executes in
52+ * @param <T> The command context type
53+ * @param <S> The {@link ArgumentBuilder} self type
5354 * @return An {@link ArgumentBuilder} for the JSON file
5455 */
55- public static <T , S extends ArgumentBuilder <T , S >> ArgumentBuilder <T , S > parse (Path path ) {
56+ public static <T , S extends ArgumentBuilder <T , S >> ArgumentBuilder <T , S > parse (Path path , Class < T > contextClass ) {
5657 String file ;
5758 try {
5859 file = String .join ("\n " , Files .readAllLines (path ));
5960 } catch (IOException e ) {
6061 System .err .println ("Invalid path to JSON file" );
6162 throw new RuntimeException (e );
6263 }
63- return parse (file );
64+ return parse (file , contextClass );
6465 }
6566
6667 /**
6768 * Parses a json string
6869 *
69- * @param json The string for the json
70- * @param <T> The command context type
71- * @param <S> The {@link ArgumentBuilder} self type
70+ * @param json The string for the json
71+ * @param contextClass The class for the context that the command executes in
72+ * @param <T> The command context type
73+ * @param <S> The {@link ArgumentBuilder} self type
7274 * @return An {@link ArgumentBuilder} for the JSON file
7375 */
74- public static <T , S extends ArgumentBuilder <T , S >> ArgumentBuilder <T , S > parse (String json ) {
76+ public static <T , S extends ArgumentBuilder <T , S >> ArgumentBuilder <T , S > parse (String json , Class < T > contextClass ) {
7577 JsonObject commandObject = JsonParser .parseString (json ).getAsJsonObject ();
76- return parseCommand (commandObject );
78+ return parseCommand (commandObject , contextClass );
7779 }
7880
79- private static <T , S extends ArgumentBuilder <T , S >> ArgumentBuilder <T , S > parseCommand (JsonObject commandObject ) {
81+ private static <T , S extends ArgumentBuilder <T , S >> ArgumentBuilder <T , S > parseCommand (JsonObject commandObject , Class < T > contextClass ) {
8082 if (!commandObject .has (ARGUMENT )) {
8183 throw new IllegalArgumentException ("Command is missing an argument type" );
8284 }
@@ -86,7 +88,11 @@ private static <T, S extends ArgumentBuilder<T, S>> ArgumentBuilder<T, S> parseC
8688 }
8789
8890 ArgumentBuilder <T , S > builder = JsonArgumentParsers .get (commandObject .get ("argument" ).getAsJsonObject ().get ("type" ).getAsString ()).parse (commandObject );
89- addChildren (builder , commandObject );
91+ if (commandObject .has (CHILDREN )) {
92+ for (JsonElement child : commandObject .get (CHILDREN ).getAsJsonArray ()) {
93+ builder .then (parseCommand (child .getAsJsonObject (), contextClass ));
94+ }
95+ }
9096
9197 if (commandObject .has (EXECUTES )) {
9298 String [] description = commandObject .get (EXECUTES ).getAsString ().split ("::" );
@@ -123,7 +129,7 @@ public String toString() {
123129 Class <?> executeClass ;
124130 try {
125131 executeClass = Thread .currentThread ().getContextClassLoader ().loadClass (description [0 ]);
126- final Method method = executeClass .getDeclaredMethod (description [1 ], Object . class );
132+ final Method method = executeClass .getDeclaredMethod (description [1 ], contextClass );
127133 builder .requires (new Predicate <T >() {
128134 public boolean test (T context ) {
129135 try {
@@ -149,14 +155,4 @@ public String toString() {
149155
150156 return builder ;
151157 }
152-
153- private static <T , S extends ArgumentBuilder <T , S >> void addChildren (ArgumentBuilder <T , S > argumentBuilder , JsonObject commandObject ) {
154- if (!commandObject .has (CHILDREN )) {
155- return ;
156- }
157-
158- for (JsonElement child : commandObject .get (CHILDREN ).getAsJsonArray ()) {
159- argumentBuilder .then (parseCommand (child .getAsJsonObject ()));
160- }
161- }
162158}
0 commit comments