Skip to content

Commit

Permalink
Add data saving
Browse files Browse the repository at this point in the history
  • Loading branch information
esotericenderman committed Jul 23, 2024
1 parent 14545ee commit ba02a16
Showing 1 changed file with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.bukkit.entity.Player;
Expand All @@ -14,21 +16,21 @@

public class PlayerDataManager {

private TemplatePaperPlugin plugin;
private final String playerDataFolderName = "player-data";
private final String playerDataFolderPath;

private final File playerDataFolder;

private Map<UUID, PlayerProfile> playerData;

public PlayerDataManager(TemplatePaperPlugin plugin) {
this.plugin = plugin;
playerDataFolderPath = plugin.getDataFolder().getPath() + File.pathSeparator + playerDataFolderName;
playerDataFolder = new File(playerDataFolderPath);

load();
}

private void load() {
String playerDataFolderName = "player-data";
String playerDataFolderPath = plugin.getDataFolder().getPath() + File.pathSeparator + playerDataFolderName;
File playerDataFolder = new File(playerDataFolderPath);

Gson gson = new Gson();

File[] playerDataFiles = playerDataFolder.listFiles();
Expand All @@ -54,6 +56,34 @@ private void load() {
}
}

private void save() {
Gson gson = new Gson();

for (Entry<UUID, PlayerProfile> entry : playerData.entrySet()) {
UUID uuid = entry.getKey();
PlayerProfile profile = entry.getValue();

File file = new File(playerDataFolderPath + File.pathSeparator + uuid.toString());

FileWriter writer;

try {
file.createNewFile();

writer = new FileWriter(file);

String json = gson.toJson(profile);

writer.write(json);

writer.close();
writer.flush();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}

public PlayerProfile getPlayerProfile(UUID uuid) {
return playerData.get(uuid);
}
Expand Down

0 comments on commit ba02a16

Please sign in to comment.