Skip to content

Commit aa5dffd

Browse files
committed
Version 1.3.1
1 parent 868c5cc commit aa5dffd

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Version 1.3.1
2+
- Fix an issue with different context classes not being found
3+
----
14
Version 1.3.0
25
- Added `requires` to JSON parsing
36
- Better error reporting

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx1G
33
# Fabric Properties
44
# check these on https://fabricmc.net/use
55
# Project Properties
6-
version=1.3.0
6+
version=1.3.1
77
maven_group=com.oroarmor
88
archives_base_name=json-to-brigadier
99
project_name=Json To Brigadier

src/main/java/com/oroarmor/json/brigadier/JsonToBrigadier.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/test/java/com/oroarmor/json/brigadier/TestComplexCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void testParse() throws URISyntaxException {
8181
.executes(TestComplexCommand::runCommandDoubleDefault))
8282
.build();
8383

84-
CommandNode<Object> jsonCommandNode = JsonToBrigadier.parse(Paths.get(Objects.requireNonNull(TestComplexCommand.class.getClassLoader().getResource("com/oroarmor/json/brigadier/complex_command.json")).toURI())).build();
84+
CommandNode<Object> jsonCommandNode = JsonToBrigadier.parse(Paths.get(Objects.requireNonNull(TestComplexCommand.class.getClassLoader().getResource("com/oroarmor/json/brigadier/complex_command.json")).toURI()), Object.class).build();
8585

8686
assertTrue(CommandNodeEquals.equals(manualCommandNode, jsonCommandNode), "Parser correctly parses command from json");
8787
}
@@ -99,7 +99,7 @@ public void testRunCommand() throws URISyntaxException, CommandSyntaxException {
9999
.executes(TestComplexCommand::runCommandDouble))
100100
.executes(TestComplexCommand::runCommandDoubleDefault));
101101

102-
LiteralArgumentBuilder<Object> jsonCommandNode = (LiteralArgumentBuilder<Object>) JsonToBrigadier.parse(Paths.get(Objects.requireNonNull(TestComplexCommand.class.getClassLoader().getResource("com/oroarmor/json/brigadier/complex_command.json")).toURI()));
102+
LiteralArgumentBuilder<Object> jsonCommandNode = (LiteralArgumentBuilder<Object>) JsonToBrigadier.parse(Paths.get(Objects.requireNonNull(TestComplexCommand.class.getClassLoader().getResource("com/oroarmor/json/brigadier/complex_command.json")).toURI()), Object.class);
103103

104104

105105
CommandDispatcher<Object> dispatcher = new CommandDispatcher<>();
@@ -131,6 +131,6 @@ public void testParseToJson() {
131131
.executes(TestComplexCommand::runCommandDoubleDefault))
132132
.build();
133133

134-
assertTrue(CommandNodeEquals.equals(manualCommandNode, JsonToBrigadier.parse(BrigadierToJson.parse(manualCommandNode)).build()), "correct inverse parsing");
134+
assertTrue(CommandNodeEquals.equals(manualCommandNode, JsonToBrigadier.parse(BrigadierToJson.parse(manualCommandNode), Object.class).build()), "correct inverse parsing");
135135
}
136136
}

src/test/java/com/oroarmor/json/brigadier/TestSimpleCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void testParseFromJson() throws URISyntaxException {
5858
.executes(TestSimpleCommand::runCommand))
5959
.build();
6060

61-
CommandNode<Object> jsonCommandNode = JsonToBrigadier.parse(Paths.get(Objects.requireNonNull(TestSimpleCommand.class.getClassLoader().getResource("com/oroarmor/json/brigadier/test_command.json")).toURI())).build();
61+
CommandNode<Object> jsonCommandNode = JsonToBrigadier.parse(Paths.get(Objects.requireNonNull(TestSimpleCommand.class.getClassLoader().getResource("com/oroarmor/json/brigadier/test_command.json")).toURI()), Object.class).build();
6262

6363
Assertions.assertTrue(CommandNodeEquals.equals(manualCommandNode, jsonCommandNode), "Parser correctly parses command from json");
6464
}
@@ -69,7 +69,7 @@ public void testRunCommand() throws URISyntaxException, CommandSyntaxException {
6969
.then(argument("value", integer(0, 1))
7070
.executes(TestSimpleCommand::runCommand));
7171

72-
LiteralArgumentBuilder<Object> jsonCommandNode = (LiteralArgumentBuilder<Object>) JsonToBrigadier.parse(Paths.get(Objects.requireNonNull(TestSimpleCommand.class.getClassLoader().getResource("com/oroarmor/json/brigadier/test_command.json")).toURI()));
72+
LiteralArgumentBuilder<Object> jsonCommandNode = (LiteralArgumentBuilder<Object>) JsonToBrigadier.parse(Paths.get(Objects.requireNonNull(TestSimpleCommand.class.getClassLoader().getResource("com/oroarmor/json/brigadier/test_command.json")).toURI()), Object.class);
7373

7474

7575
CommandDispatcher<Object> dispatcher = new CommandDispatcher<>();
@@ -102,6 +102,6 @@ public String toString() {
102102
.build();
103103

104104
String json = new GsonBuilder().setPrettyPrinting().create().toJson(BrigadierToJson.parseObject(manualCommandNode));
105-
assertTrue(CommandNodeEquals.equals(manualCommandNode, JsonToBrigadier.parse(json).build()), "correct inverse parsing");
105+
assertTrue(CommandNodeEquals.equals(manualCommandNode, JsonToBrigadier.parse(json, Object.class).build()), "correct inverse parsing");
106106
}
107107
}

0 commit comments

Comments
 (0)