Search in sources :

Example 1 with DataInit

use of me.shadorc.shadbot.data.annotation.DataInit in project Shadbot by Shadorc.

the class StatsManager method init.

@DataInit
public static void init() throws IOException {
    if (!FILE.exists()) {
        try (FileWriter writer = new FileWriter(FILE)) {
            writer.write(new JSONObject().toString(Config.JSON_INDENT_FACTOR));
        }
    }
    JSONObject statsObj;
    try (InputStream stream = FILE.toURI().toURL().openStream()) {
        statsObj = new JSONObject(new JSONTokener(stream));
    }
    Reflections reflections = new Reflections(StatsManager.class.getPackage().getName(), new MethodAnnotationsScanner());
    for (Method initMethod : reflections.getMethodsAnnotatedWith(StatsInit.class)) {
        StatsInit annotation = initMethod.getAnnotation(StatsInit.class);
        try {
            initMethod.invoke(null, statsObj.has(annotation.name()) ? statsObj.getJSONObject(annotation.name()) : new JSONObject());
        } catch (Exception err) {
            LogUtils.error(err, String.format("An error occurred while initializing statistics %s.", initMethod.getDeclaringClass().getSimpleName()));
        }
    }
}
Also used : JSONTokener(org.json.JSONTokener) StatsInit(me.shadorc.shadbot.data.stats.annotation.StatsInit) JSONObject(org.json.JSONObject) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) Method(java.lang.reflect.Method) IOException(java.io.IOException) JSONException(org.json.JSONException) Reflections(org.reflections.Reflections) DataInit(me.shadorc.shadbot.data.annotation.DataInit)

Example 2 with DataInit

use of me.shadorc.shadbot.data.annotation.DataInit in project Shadbot by Shadorc.

the class PremiumManager method init.

@DataInit
public static void init() throws JSONException, IOException {
    if (!FILE.exists()) {
        try (FileWriter writer = new FileWriter(FILE)) {
            JSONObject defaultObj = new JSONObject().put(DONATORS, new JSONObject()).put(UNUSED_RELICS, new JSONArray());
            writer.write(defaultObj.toString(Config.JSON_INDENT_FACTOR));
        }
    }
    try (InputStream stream = FILE.toURI().toURL().openStream()) {
        premiumObj = new JSONObject(new JSONTokener(stream));
    }
    premiumObj.getJSONArray(UNUSED_RELICS).forEach(relicObj -> UNUSED_RELICS_LIST.add(new Relic((JSONObject) relicObj)));
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) JSONArray(org.json.JSONArray) DataInit(me.shadorc.shadbot.data.annotation.DataInit)

Example 3 with DataInit

use of me.shadorc.shadbot.data.annotation.DataInit in project Shadbot by Shadorc.

the class LottoManager method init.

@DataInit
public static void init() throws JSONException, IOException {
    if (!FILE.exists()) {
        try (FileWriter writer = new FileWriter(FILE)) {
            JSONObject defaultObj = new JSONObject().put(USERS, new JSONArray()).put(POOL, 0);
            writer.write(defaultObj.toString(Config.JSON_INDENT_FACTOR));
        }
    }
    try (InputStream stream = FILE.toURI().toURL().openStream()) {
        dataObj = new JSONObject(new JSONTokener(stream));
    }
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) JSONArray(org.json.JSONArray) DataInit(me.shadorc.shadbot.data.annotation.DataInit)

Aggregations

FileWriter (java.io.FileWriter)3 InputStream (java.io.InputStream)3 DataInit (me.shadorc.shadbot.data.annotation.DataInit)3 JSONObject (org.json.JSONObject)3 JSONTokener (org.json.JSONTokener)3 JSONArray (org.json.JSONArray)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 StatsInit (me.shadorc.shadbot.data.stats.annotation.StatsInit)1 JSONException (org.json.JSONException)1 Reflections (org.reflections.Reflections)1 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)1