Skip to content

Commit

Permalink
feat: improved error logging when loading/saving statistics of a part…
Browse files Browse the repository at this point in the history
…icular player; changed how the configuration section is loaded/saved
  • Loading branch information
Misat11 committed Feb 4, 2024
1 parent 59b6104 commit 0cd9524
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.screamingsandals.bedwars.statistics;

import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.screamingsandals.bedwars.Main;
import org.screamingsandals.bedwars.api.events.BedwarsSavePlayerStatisticEvent;
import org.bukkit.OfflinePlayer;
Expand All @@ -32,7 +33,6 @@
import java.io.File;
import java.sql.*;
import java.util.*;
import java.util.stream.Collectors;

public class PlayerStatisticManager implements PlayerStatisticsManager {
private File databaseFile = null;
Expand Down Expand Up @@ -199,15 +199,22 @@ private PlayerStatistic loadYamlStatistic(UUID uuid) {
return playerStatistic;
}

if (!this.fileDatabase.isConfigurationSection("data." + uuid.toString())) {
Main.getInstance().getLogger().warning("Statistics of player with UUID " + uuid + " are not properly saved and the plugin cannot load them!");
Object confSection = this.fileDatabase.get("data." + uuid.toString());

if (!(confSection instanceof ConfigurationSection) && !(confSection instanceof Map)) {
Main.getInstance().getLogger().warning("Statistics of player with UUID " + uuid + " are not properly saved and the plugin cannot load them! Expected " + ConfigurationSection.class.getName() + ", got " + (confSection != null ? confSection.getClass().getName() : "null"));
PlayerStatistic playerStatistic = new PlayerStatistic(uuid);
this.playerStatistic.put(uuid, playerStatistic);
return playerStatistic;
}

HashMap<String, Object> deserialize = new HashMap<>();
deserialize.putAll(this.fileDatabase.getConfigurationSection("data." + uuid.toString()).getValues(false));
Map<String, Object> deserialize;
if (confSection instanceof ConfigurationSection) {
deserialize = new HashMap<>(((ConfigurationSection) confSection).getValues(false));
} else {
//noinspection unchecked
deserialize = (Map<String, Object>) confSection;
}
PlayerStatistic playerStatistic = new PlayerStatistic(deserialize);
playerStatistic.setId(uuid);
Player player = Main.getInstance().getServer().getPlayer(uuid);
Expand Down Expand Up @@ -285,11 +292,13 @@ public void storeStatistic(PlayerStatistic statistic) {
}

private synchronized void storeYamlStatistic(PlayerStatistic statistic) {
this.fileDatabase.set("data." + statistic.getId().toString(), statistic.serialize());
this.fileDatabase.set("data." + statistic.getId().toString(), null);
this.fileDatabase.createSection("data." + statistic.getId().toString(), statistic.serialize());
try {
this.fileDatabase.save(this.databaseFile);
} catch (Exception ex) {
Main.getInstance().getLogger().warning("Couldn't store statistic data for player with uuid: " + statistic.getId().toString());
ex.printStackTrace();
}
}

Expand Down

0 comments on commit 0cd9524

Please sign in to comment.