resurrection/src/net/brysonsteck/Resurrection/player/PlayerData.java

93 lines
3.9 KiB
Java
Raw Normal View History

2021-06-15 18:43:25 -06:00
package net.brysonsteck.Resurrection.player;
2021-06-05 23:10:04 -06:00
2021-08-19 21:12:47 -06:00
import net.brysonsteck.Resurrection.Resurrection;
import net.brysonsteck.Resurrection.startup.ParseSettings;
2021-08-19 21:12:47 -06:00
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
2021-08-19 21:12:47 -06:00
import org.bukkit.plugin.java.JavaPlugin;
2021-06-05 23:10:04 -06:00
import java.io.*;
import java.util.Hashtable;
import java.util.logging.Logger;
2021-06-05 23:10:04 -06:00
public class PlayerData {
Hashtable<String, Hashtable<String, String>> playerData = new Hashtable<>();
String rawData;
boolean DEBUG = Boolean.parseBoolean(new ParseSettings()
.getSetting("debug"));
Logger log = JavaPlugin.getProvidingPlugin(Resurrection.class).getLogger();
2021-06-05 23:10:04 -06:00
public void saveData(String write) {
try {
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Attempting to save player data");
}
2021-07-04 21:23:50 -06:00
FileWriter writer = new FileWriter("plugins/playerData.resurrection");
2021-06-16 16:50:42 -06:00
writer.write(write);
2021-06-05 23:10:04 -06:00
writer.close();
} catch (IOException e) {
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Error occurred while trying to save player data, avoid shutting down the server");
}
log.warning("There was an issue saving the player data file.");
2021-06-05 23:10:04 -06:00
e.printStackTrace();
log.warning("Resurrection will continue to run despite this error, but avoid shutting down the server until a successful save occurs.");
log.warning("In the mean time, check to make sure the playerData file exists and you have permissions to write to it.");
2021-06-05 23:10:04 -06:00
}
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player data saved successfully, rereading data to ensure Resurrection is up to date");
}
readData();
2021-06-05 23:10:04 -06:00
}
public void readData() {
try {
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Attempting to read player data");
}
rawData = "";
2021-07-04 21:23:50 -06:00
BufferedReader reader = new BufferedReader(new FileReader("plugins/playerData.resurrection"));
2021-07-04 21:56:15 -06:00
String line;
2021-06-05 23:10:04 -06:00
String[] playerData;
while (true) {
line = reader.readLine();
if (line == null) {
break;
}
rawData = rawData + line;
2021-06-05 23:10:04 -06:00
playerData = line.split(",");
Hashtable<String, String> playerHash = new Hashtable<>();
playerHash.put("dead", playerData[1]);
playerHash.put("timeLeft", playerData[2]);
this.playerData.put(playerData[0], playerHash);
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: player: " + playerData[0].replaceFirst(";","") + " | dead: " + playerData[1] + " | ms to resurrect at: " + playerData[2]);
}
2021-06-05 23:10:04 -06:00
}
2022-01-01 22:46:37 -07:00
reader.close();
2021-06-05 23:10:04 -06:00
} catch (IOException e) {
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Error occurred while trying to read player data. Resurrection is shutting down");
}
log.severe("There was an issue reading the player data file.");
2021-06-05 23:10:04 -06:00
e.printStackTrace();
log.severe("This file is crucial to Resurrection. Since the file could not be read, the plugin will now stop.");
2021-08-19 21:12:47 -06:00
Bukkit.getPluginManager().disablePlugin(JavaPlugin.getProvidingPlugin(Resurrection.class));
2021-06-05 23:10:04 -06:00
}
if (DEBUG) {
Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player data read successfully");
}
2021-06-05 23:10:04 -06:00
}
2021-07-04 21:56:15 -06:00
// public Hashtable<String, Hashtable<String, String>> getPlayers() {
// return playerData;
// }
public String getRawData() {
return rawData;
}
2021-06-05 23:10:04 -06:00
}