Skip to content

Commit

Permalink
Begin works on docs json
Browse files Browse the repository at this point in the history
  • Loading branch information
iBlitzkriegi authored and iBlitzkriegi committed Jun 8, 2018
1 parent 4675dd8 commit 2bfc3e3
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Registration register(String name, String ending, Class type, Clas
for (int i = 0; i < patterns.length; i++) {
patterns[i] += " " + ending;
}
return Vixio.getInstance().registerEvent(name, type, clazz, patterns);
return Vixio.getInstance().registerEvent(name, type, clazz, patterns).setEvent(clazz);
}

@Override
Expand Down
120 changes: 108 additions & 12 deletions src/main/java/me/iblitzkriegi/vixio/registration/Documentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -134,6 +135,7 @@ public static void setupSyntaxFile() {
}
bw.flush();
bw.close();
generateJson();
//getEventValues(bw, 0, EventJDAEvent.class, EventGuildMessageReceived.class, AnyEvent.class);

} catch (IOException e) {
Expand All @@ -142,28 +144,122 @@ public static void setupSyntaxFile() {

}

private static void getEventValues(String syntax, BufferedWriter bw, int time, Class<? extends Event>... classes) throws IOException {
bw.write("-=Events=-");
bw.newLine();
public static void generateJson() {
File file = new File(Vixio.getInstance().getDataFolder(), "syntaxes.json");
try {
if (!file.exists()) {
file.createNewFile();
} else {
file.delete();
}
} catch (IOException x) {

}
try {
FileWriter fw;
fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
// Effects
bw.write("\t{");
bw.newLine();
bw.write("\t\t\"effects\": [{");
int effects = 0;
for (Registration reg : Vixio.getInstance().effects) {
effects = effects + 1;
bw.newLine();
bw.write("\t\t{");
bw.newLine();
bw.write("\t\t\t\"examples\": [");
bw.newLine();
bw.write("\t\t\t\t[\"" + reg.getExample() + "\"]");
bw.newLine();
bw.write("\t\t\t],");
bw.newLine();
bw.write("\t\t\t\"name\": \"" + reg.getName() + "\",");
bw.newLine();
bw.write("\t\t\t\"description\": \"" + reg.getDesc() + "\",");
bw.newLine();
bw.write("\t\t\t\"syntaxes\": [{");
bw.newLine();
if (reg.getUserFacing() != null) {
bw.write("\t\t\t\t\"syntax\": \"" + reg.getUserFacing() + "\"");
bw.newLine();
} else {
boolean multipleSyntax = reg.getSyntaxes().length == 2;
if (multipleSyntax) {
int multiSyntax = 0;
for (String syntax : reg.getSyntaxes()) {
multiSyntax = multiSyntax + 1;
if (multiSyntax == 1) {
bw.write("\t\t\t\t\"syntax\": \"" + syntax + "\"");
bw.newLine();
bw.write("\t\t\t}, {");
bw.newLine();
} else {
if (multiSyntax != reg.getSyntaxes().length) {
bw.write("\t\t\t\t\"syntax\": \"" + syntax + "\"");
bw.newLine();
} else {
bw.write("\t\t\t\t\"syntax\": \"" + syntax + "\"");
bw.newLine();
}
}
}
} else {
bw.write("\t\t\t\t\"syntax\": \"" + reg.getSyntax() + "\"");
bw.newLine();
}
}
if (effects != Vixio.getInstance().effects.size()) {
bw.write("\t\t\t}],");
bw.newLine();
bw.write("\t\t},");
} else {
bw.write("\t\t\t}]");
bw.newLine();
bw.write("\t\t}");
}
}
bw.newLine();
bw.write("\t\t}],");
bw.newLine();
bw.write("\t},");


// bw.write("-=Events=-");
// bw.newLine();
// for (Registration reg : Vixio.getInstance().events) {
// if (reg.getEvent() != null) {
// bw.write("syntax: " + reg.getSyntax());
// bw.newLine();
// List<String> values = getEventValues(reg.getEvent());
// bw.write("Event values: " + values.subList(0, values.size()));
// bw.newLine();
// }
// }
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private static List<String> getEventValues(Class<? extends Event>... classes) {

Method m = getMethod(EventValues.class, "getEventValuesList", int.class);
List<?> values = invokeMethod(m, null, time);
List<?> values = invokeMethod(m, null, 0);
List<String> eventValues = new ArrayList<>();
if (values != null)
for (Class<?> c : classes) {
bw.write("on " + syntax);
bw.newLine();
bw.write("\tEvent values:");
bw.newLine();
for (Object eventValue : values) {
Class<?> event = getField(eventValue.getClass(), eventValue, "event");
if (event != null && (c.isAssignableFrom(event) || event.isAssignableFrom(c))) {
Class<?> ret = getField(eventValue.getClass(), eventValue, "c");
bw.write("\t\t- event-" + ret.getSimpleName().toLowerCase());
bw.newLine();
eventValues.add("event-" + ret.getSimpleName().toLowerCase().replaceAll("updatingmessage", "message"));
}
}
}
bw.flush();
bw.close();
return eventValues;
}

public static Method getMethod(Class<?> clz, String method, Class<?>... parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import ch.njol.util.StringUtils;

import java.util.ArrayList;

/**
* Created by Blitz on 7/22/2017.
*/
Expand All @@ -14,6 +16,7 @@ public class Registration {
private Class<?> clazz;
private String[] syntaxes;
private String userFacing;
private Class event;

public Registration(Class<?> cls, String... syntaxes) {
clazz = cls;
Expand Down Expand Up @@ -64,7 +67,10 @@ public String getExample() {
}

public Registration setExample(String... s) {
return setExample(StringUtils.join(s, "\n"));
String example = StringUtils.join(s, ",")
.replaceAll("\t", "\\\\t")
.replaceAll("\"", "\\\\\"");
return setExample(example);
}

public String getUserFacing() {
Expand All @@ -81,5 +87,12 @@ public Registration setUserFacing(String... patterns) {
return this;
}

public Registration setEvent(Class clazz) {
this.event = clazz;
return this;
}

public Class getEvent() {
return event;
}
}

0 comments on commit 2bfc3e3

Please sign in to comment.