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 --- src/net/brysonsteck/Resurrection/Resurrection.java | 28 +++++++++++++ .../Resurrection/commands/CommandAbout.java | 10 ++--- .../Resurrection/commands/CommandBug.java | 15 +++++++ .../Resurrection/commands/CommandHowLong.java | 41 ++++++++++++++++++ .../Resurrection/commands/CommandResurrect.java | 49 ++++++++++++++++++++++ .../Resurrection/commands/CommandSource.java | 15 ++++++- .../Resurrection/player/PlayerData.java | 31 ++++++++++++++ .../Resurrection/player/PlayerListener.java | 42 ++++++++++++++++++- 8 files changed, 223 insertions(+), 8 deletions(-) diff --git a/src/net/brysonsteck/Resurrection/Resurrection.java b/src/net/brysonsteck/Resurrection/Resurrection.java index 01af324..3d37900 100644 --- a/src/net/brysonsteck/Resurrection/Resurrection.java +++ b/src/net/brysonsteck/Resurrection/Resurrection.java @@ -5,6 +5,7 @@ import net.brysonsteck.Resurrection.player.PlayerListener; import net.brysonsteck.Resurrection.startup.CheckForUpdate; import net.brysonsteck.Resurrection.startup.ParseSettings; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.event.Listener; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; @@ -100,16 +101,43 @@ public class Resurrection extends JavaPlugin implements Listener { System.out.println("[Resurrection] ---------------------------------------------------------"); if (parseSettings.isSettingsComplete() && !fileFail) { + boolean DEBUG = false; + if (Boolean.parseBoolean(parseSettings.getSetting("debug"))) { + DEBUG = true; + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Resurrection's debug mode has been enabled in the settings file."); + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: All debug messages will be broadcasted (sent to everyone) prefaced with the tag \"[Res. DEBUG]\" and sent in bold yellow text."); + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Several messages may be sent at a time. Therefore, debug mode should be disabled for anything other than, well, debugging."); + System.out.println("[Resurrection] ---------------------------------------------------------"); + } + System.out.println("[Resurrection] Registering listeners and adding commands..."); // register listener this.getServer().getPluginManager().registerEvents(new PlayerListener(parseSettings), this); + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player listener registered."); + } // register commands this.getCommand("about").setExecutor(new CommandAbout(parseSettings.getSetting("debug"), pluginInfo.getVersion(), outdated)); + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: `/about` registered."); + } this.getCommand("bug").setExecutor(new CommandBug(parseSettings.getSetting("debug"))); + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: `/bug` registered."); + } this.getCommand("resurrect").setExecutor(new CommandResurrect(parseSettings.getSetting("debug"))); + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: `/resurrect` registered."); + } this.getCommand("howlong").setExecutor(new CommandHowLong(parseSettings.getSetting("debug"))); + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: `/howlong` registered."); + } this.getCommand("source").setExecutor(new CommandSource(parseSettings.getSetting("debug"))); + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: `/source` registered."); + } System.out.println("[Resurrection] ---------------------------------------------------------"); System.out.println("[Resurrection] Successfully Started!"); diff --git a/src/net/brysonsteck/Resurrection/commands/CommandAbout.java b/src/net/brysonsteck/Resurrection/commands/CommandAbout.java index 08f0437..a820432 100644 --- a/src/net/brysonsteck/Resurrection/commands/CommandAbout.java +++ b/src/net/brysonsteck/Resurrection/commands/CommandAbout.java @@ -8,24 +8,24 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; public class CommandAbout implements CommandExecutor { - boolean debug; + boolean DEBUG; String currentVersion; boolean outdated; public CommandAbout(String debug, String currentVersion, boolean outdated) { - this.debug = Boolean.parseBoolean(debug); + this.DEBUG = Boolean.parseBoolean(debug); this.currentVersion = currentVersion; this.outdated = outdated; } @Override public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { - if (debug) { + if (DEBUG) { Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: The `/about` command was ran by " + commandSender.getName()); } if (commandSender instanceof Player) { - if (debug) { + if (DEBUG) { Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is a player."); } Player p = (Player) commandSender; @@ -40,7 +40,7 @@ public class CommandAbout implements CommandExecutor { p.sendMessage(ChatColor.YELLOW + "For more info on this plugin or to download it, visit the GitHub repository at " + ChatColor.AQUA + "https://github.com/brysonsteck/resurrection"); p.sendMessage(ChatColor.YELLOW + "\u00a9 2021 Bryson Steck"); } else { - if (debug) { + if (DEBUG) { Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is console."); } System.out.println("[Resurrection] --- Resurrection ---"); diff --git a/src/net/brysonsteck/Resurrection/commands/CommandBug.java b/src/net/brysonsteck/Resurrection/commands/CommandBug.java index 75ed5cf..c5c53a1 100644 --- a/src/net/brysonsteck/Resurrection/commands/CommandBug.java +++ b/src/net/brysonsteck/Resurrection/commands/CommandBug.java @@ -1,6 +1,7 @@ package net.brysonsteck.Resurrection.commands; import net.brysonsteck.Resurrection.Resurrection; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -10,13 +11,23 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; public class CommandBug implements CommandExecutor { + boolean DEBUG; public CommandBug(String debug) { + this.DEBUG = Boolean.parseBoolean(debug); } @Override public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: The `/bug` command was ran by " + commandSender.getName()); + } + if (commandSender instanceof Player) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is a player."); + } + commandSender.sendMessage(ChatColor.YELLOW + "Did you find a bug? Well that sucks for you."); new BukkitRunnable() { @Override @@ -30,6 +41,10 @@ public class CommandBug implements CommandExecutor { }.runTaskLater(JavaPlugin.getProvidingPlugin(Resurrection.class), 60); return true; } else { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is console"); + } + System.out.println("[Resurrection] Did you find a bug? Well that sucks for you."); new BukkitRunnable() { @Override diff --git a/src/net/brysonsteck/Resurrection/commands/CommandHowLong.java b/src/net/brysonsteck/Resurrection/commands/CommandHowLong.java index 29314d7..32662a0 100644 --- a/src/net/brysonsteck/Resurrection/commands/CommandHowLong.java +++ b/src/net/brysonsteck/Resurrection/commands/CommandHowLong.java @@ -10,20 +10,38 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; public class CommandHowLong implements CommandExecutor { + boolean DEBUG; public CommandHowLong(String debug) { + this.DEBUG = Boolean.parseBoolean(debug); } @Override public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: The `/howlong` command was ran by " + commandSender.getName()); + } + if (commandSender instanceof Player) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is a player."); + } + boolean self = false; boolean valid = false; if (strings.length == 0) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Valid; no arguments given. Assuming sender wants to check own time"); + } + self = true; valid = true; } else if (strings.length == 1) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Valid; one argument given. Assuming sender wants to check another player's time"); + } + valid = true; } @@ -37,6 +55,10 @@ public class CommandHowLong implements CommandExecutor { commandSender.sendMessage(ChatColor.RED + "ERROR: That player is not online/doesn't exist!"); return false; } + + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player " + p.getDisplayName() + " exists."); + } } PlayerData playerData = new PlayerData(); playerData.readData(); @@ -46,6 +68,10 @@ public class CommandHowLong implements CommandExecutor { if (players.startsWith(p.getDisplayName())) { String[] playerSplit = players.split(","); if (Boolean.parseBoolean(playerSplit[1])) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player is dead according to file. Calculating time until resurrection"); + } + long currentTime = System.currentTimeMillis(); long resurrectionTime = Long.parseLong(playerSplit[2]); @@ -70,12 +96,20 @@ public class CommandHowLong implements CommandExecutor { return false; } } else { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is console."); + } + boolean valid = false; if (strings.length == 0) { System.out.println("[Resurrection] ERROR: The /howlong command requires the name of a player when ran through the console."); return false; } else if (strings.length == 1) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Valid; console specified player to check."); + } + valid = true; } @@ -87,6 +121,10 @@ public class CommandHowLong implements CommandExecutor { return false; } + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player " + p.getDisplayName() + " exists."); + } + PlayerData playerData = new PlayerData(); playerData.readData(); String rawData = playerData.getRawData(); @@ -95,6 +133,9 @@ public class CommandHowLong implements CommandExecutor { if (players.startsWith(p.getDisplayName())) { String[] playerSplit = players.split(","); if (Boolean.parseBoolean(playerSplit[1])) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Player is dead according to file. Calculating time until resurrection"); + } long currentTime = System.currentTimeMillis(); long resurrectionTime = Long.parseLong(playerSplit[2]); diff --git a/src/net/brysonsteck/Resurrection/commands/CommandResurrect.java b/src/net/brysonsteck/Resurrection/commands/CommandResurrect.java index 9f97c29..3d206c4 100644 --- a/src/net/brysonsteck/Resurrection/commands/CommandResurrect.java +++ b/src/net/brysonsteck/Resurrection/commands/CommandResurrect.java @@ -12,22 +12,45 @@ import org.bukkit.entity.Player; import org.bukkit.potion.PotionEffect; public class CommandResurrect implements CommandExecutor { + boolean DEBUG; + public CommandResurrect(String debug) { + this.DEBUG = Boolean.parseBoolean(debug); } @Override public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: The `resurrect` command was ran by " + commandSender.getName()); + } + boolean valid = (strings.length == 1); + if (commandSender instanceof Player) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is a player."); + } + Player p = (Player) commandSender; if (valid) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Valid; an argument was specified. Assuming it as name of player to resurrect"); + } + Player resurrectPlayer = Bukkit.getPlayer(strings[0]); if (resurrectPlayer == null) { p.sendMessage(ChatColor.RED + "ERROR: That player is not online/doesn't exist! Failed to resurrect."); return false; } + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player \"" + resurrectPlayer.getDisplayName() + "\" exists"); + } + if (resurrectPlayer.getGameMode() == GameMode.SPECTATOR) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player in spectator mode, assuming dead"); + } for (PotionEffect effect : resurrectPlayer.getActivePotionEffects()) resurrectPlayer.removePotionEffect(effect.getType()); resurrectPlayer.setGameMode(GameMode.SURVIVAL); @@ -37,6 +60,10 @@ public class CommandResurrect implements CommandExecutor { Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + strings[0] + " has been resurrected manually by an admin!"); removeDeath(resurrectPlayer); if (p.getBedSpawnLocation() != null) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: A bed for the specified player was found. Teleporting"); + } + p.teleport(p.getBedSpawnLocation()); } return true; @@ -49,13 +76,28 @@ public class CommandResurrect implements CommandExecutor { return false; } } else { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is console."); + } + if (valid) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Valid; an argument was specified. Assuming it as name of player to resurrect"); + } + Player resurrectPlayer = Bukkit.getPlayer(strings[0]); if (resurrectPlayer == null) { System.out.println("[Resurrection] ERROR: That player is not online/doesn't exist! Failed to resurrect."); return false; } + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player \"" + resurrectPlayer.getDisplayName() + "\" exists"); + } + if (resurrectPlayer.getGameMode() == GameMode.SPECTATOR) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Specified player in spectator mode, assuming dead"); + } for (PotionEffect effect : resurrectPlayer.getActivePotionEffects()) resurrectPlayer.removePotionEffect(effect.getType()); resurrectPlayer.setGameMode(GameMode.SURVIVAL); @@ -65,6 +107,10 @@ public class CommandResurrect implements CommandExecutor { Bukkit.broadcastMessage(strings[0] + " has been resurrected manually by an admin!"); removeDeath(resurrectPlayer); if (resurrectPlayer.getBedSpawnLocation() != null) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: A bed for the specified player was found. Teleporting"); + } + resurrectPlayer.teleport(resurrectPlayer.getBedSpawnLocation()); } return true; @@ -87,6 +133,9 @@ public class CommandResurrect implements CommandExecutor { int index = 0; for (String players : rawPlayers) { if (players.startsWith(p.getDisplayName())) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: Removing the death from the resurrected player"); + } String[] playerSplit = players.split(","); playerSplit[1] = "false"; playerSplit[2] = "0"; diff --git a/src/net/brysonsteck/Resurrection/commands/CommandSource.java b/src/net/brysonsteck/Resurrection/commands/CommandSource.java index 92e51c6..5ac7666 100644 --- a/src/net/brysonsteck/Resurrection/commands/CommandSource.java +++ b/src/net/brysonsteck/Resurrection/commands/CommandSource.java @@ -1,6 +1,7 @@ package net.brysonsteck.Resurrection.commands; import net.brysonsteck.Resurrection.Resurrection; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -10,15 +11,22 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; public class CommandSource implements CommandExecutor { - boolean debug; + boolean DEBUG; public CommandSource(String debug) { - this.debug = Boolean.parseBoolean(debug); + this.DEBUG = Boolean.parseBoolean(debug); } @Override public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: The `/source` command was ran by " + commandSender.getName()); + } + if (commandSender instanceof Player) { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is a player"); + } commandSender.sendMessage(ChatColor.YELLOW + "Resurrection is FREE AND OPEN SOURCE under the"); commandSender.sendMessage(ChatColor.YELLOW + "GNU Affero General Public License v3.0 via GitHub."); commandSender.sendMessage(ChatColor.YELLOW + "You can view the repository at " + ChatColor.AQUA + "https://github.com/brysonsteck/resurrection"); @@ -26,6 +34,9 @@ public class CommandSource implements CommandExecutor { return true; } else { + if (DEBUG) { + Bukkit.broadcastMessage(ChatColor.YELLOW +""+ ChatColor.BOLD + "[Res. DEBUG]: CommandSender is console"); + } System.out.println("[Resurrection] Resurrection is FREE AND OPEN SOURCE under the"); System.out.println("[Resurrection] GNU Affero General Public License v3.0 via GitHub."); System.out.println("[Resurrection] You can view the repository at https://github.com/brysonsteck/resurrection"); 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