Search in sources :

Example 1 with StatisticMap

use of net.glowstone.util.StatisticMap in project Glowstone by GlowstoneMC.

the class PlayerStatisticIoService method readStats.

/**
     * Reads the stats of a player from its statistics file and writes the values to the StatisticMap.
     *
     * @param player the player to read the statistics from
     */
public void readStats(GlowPlayer player) {
    File statsFile = getPlayerFile(player.getUniqueId());
    player.getStatisticMap().getValues().clear();
    if (statsFile.exists()) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(new FileReader(statsFile));
            for (Object obj : json.entrySet()) {
                Map.Entry<String, Object> entry = (Map.Entry<String, Object>) obj;
                Long longValue = null;
                if (entry.getValue() instanceof Long) {
                    longValue = (Long) entry.getValue();
                } else if (entry.getValue() instanceof JSONObject) {
                    JSONObject object = (JSONObject) entry.getValue();
                    if (object.containsKey("value")) {
                        longValue = (Long) object.get("value");
                    }
                } else {
                    GlowServer.logger.warning("Unknown statistic type for '" + entry.getKey() + "': " + entry.getValue() + " (" + entry.getValue().getClass().getSimpleName() + ")");
                }
                if (longValue != null) {
                    player.getStatisticMap().getValues().put(entry.getKey(), longValue.intValue());
                }
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : IOException(java.io.IOException) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) FileReader(java.io.FileReader) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) File(java.io.File) StatisticMap(net.glowstone.util.StatisticMap) Map(java.util.Map)

Example 2 with StatisticMap

use of net.glowstone.util.StatisticMap in project Glowstone by GlowstoneMC.

the class PlayerStatisticIoService method writeStats.

/**
     * Writes the statistics of the player into its statistics file.
     *
     * @param player the player to write the statistics file from
     */
public void writeStats(GlowPlayer player) {
    File file = getPlayerFile(player.getUniqueId());
    StatisticMap map = player.getStatisticMap();
    JSONObject json = new JSONObject(map.getValues());
    try {
        FileWriter writer = new FileWriter(file, false);
        writer.write(json.toJSONString());
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : StatisticMap(net.glowstone.util.StatisticMap) JSONObject(org.json.simple.JSONObject) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File)

Example 3 with StatisticMap

use of net.glowstone.util.StatisticMap in project Glowstone by GlowstoneMC.

the class JsonPlayerStatisticIoService method writeStatistics.

/**
 * Writes the statistics of the player into its statistics file.
 *
 * @param player the player to write the statistics file from
 */
@Override
public void writeStatistics(GlowPlayer player) {
    File file = getPlayerFile(player.getUniqueId());
    StatisticMap map = player.getStatisticMap();
    JSONObject json = new JSONObject(map.getValues());
    try {
        FileWriter writer = new FileWriter(file, false);
        writer.write(json.toJSONString());
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : StatisticMap(net.glowstone.util.StatisticMap) JSONObject(org.json.simple.JSONObject) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File)

Example 4 with StatisticMap

use of net.glowstone.util.StatisticMap in project Glowstone by GlowstoneMC.

the class JsonPlayerStatisticIoService method readStatistics.

/**
 * Reads the stats of a player from its statistics file and writes the values to the
 * StatisticMap.
 *
 * @param player the player to read the statistics from
 */
@Override
public void readStatistics(GlowPlayer player) {
    File statsFile = getPlayerFile(player.getUniqueId());
    player.getStatisticMap().getValues().clear();
    if (statsFile.exists()) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(new FileReader(statsFile));
            for (Object obj : json.entrySet()) {
                Map.Entry<String, Object> entry = (Map.Entry<String, Object>) obj;
                Long longValue = null;
                if (entry.getValue() instanceof Long) {
                    longValue = (Long) entry.getValue();
                } else if (entry.getValue() instanceof JSONObject) {
                    JSONObject object = (JSONObject) entry.getValue();
                    if (object.containsKey("value")) {
                        // NON-NLS
                        // NON-NLS
                        longValue = (Long) object.get("value");
                    }
                } else {
                    ConsoleMessages.Warn.Io.JSON_STAT_UNKNOWN.log(entry.getKey(), entry.getValue(), entry.getValue().getClass().getSimpleName());
                }
                if (longValue != null) {
                    player.getStatisticMap().getValues().put(entry.getKey(), longValue.intValue());
                }
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : IOException(java.io.IOException) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) FileReader(java.io.FileReader) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) File(java.io.File) StatisticMap(net.glowstone.util.StatisticMap) Map(java.util.Map)

Aggregations

File (java.io.File)4 IOException (java.io.IOException)4 StatisticMap (net.glowstone.util.StatisticMap)4 JSONObject (org.json.simple.JSONObject)4 FileReader (java.io.FileReader)2 FileWriter (java.io.FileWriter)2 Map (java.util.Map)2 JSONParser (org.json.simple.parser.JSONParser)2 ParseException (org.json.simple.parser.ParseException)2