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();
}
}
}
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();
}
}
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();
}
}
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();
}
}
}
Aggregations