From 9e8c0c2a881100939a63ba4d610af5d26b53938d Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Mon, 13 Sep 2021 17:14:31 -0600 Subject: found lack of saving data if player joins during dead state and resurrects --- .../Resurrection/player/PlayerData.java | 31 ++++++++++++++++ .../Resurrection/player/PlayerListener.java | 42 +++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) (limited to 'src/net/brysonsteck/Resurrection/player') diff --git a/src/net/brysonsteck/Resurrection/player/PlayerData.java b/src/net/brysonsteck/Resurrection/player/PlayerData.java index eea7c3a..4771af5 100644 --- a/src/net/brysonsteck/Resurrection/player/PlayerData.java +++ b/src/net/brysonsteck/Resurrection/player/PlayerData.java @@ -2,7 +2,9 @@ package net.brysonsteck.Resurrection.player; import net.brysonsteck.Resurrection.Resurrection; +import net.brysonsteck.Resurrection.startup.ParseSettings; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.plugin.java.JavaPlugin; import java.io.*; @@ -11,20 +13,38 @@ import java.util.Hashtable; public class PlayerData { Hashtable> playerData = new Hashtable<>(); String rawData; + boolean DEBUG = Boolean.parseBoolean(new ParseSettings() + .getSetting("debug")); public void saveData(String write) { try { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Attempting to save player data"); + } FileWriter writer = new FileWriter("plugins/playerData.resurrection"); writer.write(write); 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"); + } + System.out.println("[Resurrection] There was an issue saving the player data file."); e.printStackTrace(); + System.out.println("[Resurrection] Resurrection will continue to run despite this error, but avoid shutting down the server until a successful save occurs."); + System.out.println("[Resurrection] In the mean time, check to make sure the playerData file exists and you have permissions to write to it."); } readData(); + + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player data saved successfully"); + } } public void readData() { try { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Attempting to read player data"); + } rawData = ""; BufferedReader reader = new BufferedReader(new FileReader("plugins/playerData.resurrection")); String line; @@ -40,13 +60,24 @@ public class PlayerData { playerHash.put("dead", playerData[1]); playerHash.put("timeLeft", playerData[2]); this.playerData.put(playerData[0], playerHash); + if (DEBUG) { + TimeCheck timeCheck = new TimeCheck(Long.parseLong(playerData[2])); + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: player: " + playerData[0] + " | dead: " + playerData[1] + " | ms to resurrect at: " + playerData[2]); + } } } 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"); + } + System.out.println("[Resurrection] There was an issue reading the player data file."); e.printStackTrace(); System.out.println("[Resurrection] This file is crucial to Resurrection. Since the file could not be read, the plugin will now stop."); Bukkit.getPluginManager().disablePlugin(JavaPlugin.getProvidingPlugin(Resurrection.class)); } + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player data read successfully"); + } } // public Hashtable> getPlayers() { diff --git a/src/net/brysonsteck/Resurrection/player/PlayerListener.java b/src/net/brysonsteck/Resurrection/player/PlayerListener.java index 4705171..28bc619 100644 --- a/src/net/brysonsteck/Resurrection/player/PlayerListener.java +++ b/src/net/brysonsteck/Resurrection/player/PlayerListener.java @@ -25,14 +25,20 @@ public class PlayerListener implements Listener { Location spawn = world.getSpawnLocation(); Hashtable playerSpawns = new Hashtable<>(); ParseSettings parseSettings; + boolean DEBUG; public PlayerListener(ParseSettings parseSettings) { this.parseSettings = parseSettings; + this.DEBUG = Boolean.parseBoolean(parseSettings.getSetting("debug")); } @EventHandler public void onJoin(PlayerJoinEvent e) { Player p = e.getPlayer(); + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player " + p.getDisplayName() + " has joined"); + } + PlayerData playerData = new PlayerData(); playerData.readData(); String rawData = playerData.getRawData(); @@ -43,12 +49,19 @@ public class PlayerListener implements Listener { long timeToResurrection = 0; for (String players : rawPlayers) { if (players.startsWith(p.getDisplayName())) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player " + p.getDisplayName() + " was found in the player data"); + } found = true; String[] playerSplit = players.split(","); boolean dead = Boolean.parseBoolean(playerSplit[1]); timeToResurrection = Long.parseLong(playerSplit[2]); if (!dead) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player " + p.getDisplayName() + " is not dead; making sure they are in survival"); + } + for (PotionEffect effect : p.getActivePotionEffects()) p.removePotionEffect(effect.getType()); p.setGameMode(GameMode.SURVIVAL); @@ -65,9 +78,16 @@ public class PlayerListener implements Listener { index++; } if (!found) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player " + p.getDisplayName() + " was not found in the player data; registering"); + } + playerData.saveData(rawData + ";" + p.getDisplayName() + ",false,0"); } if (resumeDeath && !timerRunning) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player " + p.getDisplayName() + " is dead; pushing into dead state until resurrection"); + } // spawn = p.getLocation(); p.sendMessage(ChatColor.RED + "You are still dead. To check how long you have left before you are resurrected, "); p.sendMessage(ChatColor.RED + "run the \"/howlong\" command in chat."); @@ -91,6 +111,21 @@ public class PlayerListener implements Listener { new BukkitRunnable() { @Override public void run() { + String rawData = playerData.getRawData(); + int index = 0; + for (String players : rawPlayers) { + if (players.startsWith(p.getDisplayName())) { + String[] playerSplit = players.split(","); + playerSplit[1] = "false"; + playerSplit[2] = "0"; + + rawPlayers[index] = String.join(",", playerSplit); + rawData = String.join(";", rawPlayers); + playerData.saveData(rawData); + break; + } + index++; + } stillDead = false; for (PotionEffect effect : p.getActivePotionEffects()) p.removePotionEffect(effect.getType()); @@ -110,11 +145,16 @@ public class PlayerListener implements Listener { @EventHandler public void onDeath(PlayerDeathEvent e) { - System.out.println("[Resurrection] A player has died!"); Player p = e.getEntity(); stillDead = true; + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player " + p.getDisplayName() + " has died, reading resurrection_time in settings"); + } + + // get human readable form of resurrection time TimeCheck timeCheck = new TimeCheck(Long.parseLong(parseSettings.getSetting("resurrection_time"))); + // calculate time that player will resurrect at long resurrectionTime = System.currentTimeMillis() + Long.parseLong(parseSettings.getSetting("resurrection_time")); e.setDeathMessage(e.getDeathMessage() + " and will respawn in the next " + timeCheck.formatTime('f')); -- cgit v1.2.3