diff options
author | Bryson Steck <steck.bryson@gmail.com> | 2021-08-19 21:58:40 -0600 |
---|---|---|
committer | Bryson Steck <steck.bryson@gmail.com> | 2021-08-19 21:58:40 -0600 |
commit | f75681144085e729b4e9972908c1fc859670bb4b (patch) | |
tree | 6c5c493288cfc5c241b68a803402b4c8763df41c /src/net | |
parent | 4af9e11bf4abae7f866b62af0877b4ce8bd94365 (diff) | |
download | resurrection-f75681144085e729b4e9972908c1fc859670bb4b.tar resurrection-f75681144085e729b4e9972908c1fc859670bb4b.tar.gz resurrection-f75681144085e729b4e9972908c1fc859670bb4b.tar.bz2 |
added h and f mods for timecheck.formattime
Diffstat (limited to 'src/net')
5 files changed, 42 insertions, 33 deletions
diff --git a/src/net/brysonsteck/Resurrection/ParseSettings.java b/src/net/brysonsteck/Resurrection/ParseSettings.java index bd476e7..8682bd2 100644 --- a/src/net/brysonsteck/Resurrection/ParseSettings.java +++ b/src/net/brysonsteck/Resurrection/ParseSettings.java @@ -3,15 +3,12 @@ package net.brysonsteck.Resurrection; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; +import java.io.*; import java.util.Hashtable; import java.util.Locale; public class ParseSettings { Hashtable<String, String> settings = new Hashtable<>(); - // <setting that failed / does it exist? (true = the value is wrong, false = the setting is missing)> String failedSetting; boolean settingsComplete; @@ -20,8 +17,19 @@ public class ParseSettings { public ParseSettings() { try { + File settingsFile = new File("data/settings.resurrection"); + if (!settingsFile.exists()) { + // create default settings file + FileWriter writer = new FileWriter(settingsFile); + writer.write("# This is the default settings file. All lines starting with a '#' are treated as comments and will be ignored.\n" + + "# 'resurrection_time' is the amount of time in milliseconds Resurrection will force the player to wait. Default value is 8640000 milliseconds (24 hours).\n" + + "resurrection_time=86400000\n" + + "# 'debug' enables debug messages in the console and players' chat as the plugin runs. The only valid values are 'true' and 'false'. Default value is false.\n" + + "debug=false"); + writer.close(); + } String rawData = ""; - BufferedReader reader = new BufferedReader(new FileReader("plugins/settings.resurrection")); + BufferedReader reader = new BufferedReader(new FileReader("data/settings.resurrection")); String line; String[] setting; while (true) { @@ -39,12 +47,12 @@ public class ParseSettings { if (!settingsComplete) { System.out.println("[Resurrection] The setting \"" + failedSetting + "\" is not present in the settings file.\n" + "[Resurrection] Please double check the settings file to make sure the setting exists and a valid corresponding value is set.\n" + - "[Resurrection] Example: \"resurrection_time=8640000\"\n" + + "[Resurrection] Example: \"resurrection_time=86400000\"\n" + "[Resurrection] Example: \"debug=false\""); } else if (!valuesComplete) { System.out.println("[Resurrection] The setting \"" + failedSetting + "\" contains an invalid or empty value.\n" + "[Resurrection] Please double check the settings file to make sure that a valid value is set for this setting.\n" + - "[Resurrection] Example: \"resurrection_time=8640000\"\n" + + "[Resurrection] Example: \"resurrection_time=86400000\"\n" + "[Resurrection] Example: \"debug=false\""); } System.out.println("[Resurrection] This file is crucial to Resurrection. Since the file is not complete, the plugin will now stop."); diff --git a/src/net/brysonsteck/Resurrection/Resurrection.java b/src/net/brysonsteck/Resurrection/Resurrection.java index b487fdf..f481572 100644 --- a/src/net/brysonsteck/Resurrection/Resurrection.java +++ b/src/net/brysonsteck/Resurrection/Resurrection.java @@ -88,15 +88,8 @@ public class Resurrection extends JavaPlugin implements Listener { } if (!settingsFile.exists()) { System.out.println("[Resurrection] Settings file does not exist. (This file is new with the 0.2 beta if you upgraded.) Creating now in the \"plugins\" directory..."); - try { - settingsFile.createNewFile(); - System.out.println("[Resurrection] Settings file created successfully."); - } catch (IOException e) { - System.out.println("[Resurrection] An error has occurred creating the settings file!"); - e.printStackTrace(); - System.out.println("[Resurrection] This file is crucial to Resurrection. Since the file could not be created, the plugin will now stop."); - Bukkit.getPluginManager().disablePlugin(this); - } + ParseSettings parseSettings = new ParseSettings(); + System.out.println("[Resurrection] Settings file created successfully."); } else { System.out.println("[Resurrection] The settings file has also been found!"); } diff --git a/src/net/brysonsteck/Resurrection/commands/CommandHowLong.java b/src/net/brysonsteck/Resurrection/commands/CommandHowLong.java index 7e1c27d..012d23e 100644 --- a/src/net/brysonsteck/Resurrection/commands/CommandHowLong.java +++ b/src/net/brysonsteck/Resurrection/commands/CommandHowLong.java @@ -48,9 +48,9 @@ public class CommandHowLong implements CommandExecutor { TimeCheck timeCheck = new TimeCheck(resurrectionTime - currentTime); if (self) { - commandSender.sendMessage("You will respawn in " + timeCheck.formatTime()); + commandSender.sendMessage("You will respawn in " + timeCheck.formatTime('f')); } else { - commandSender.sendMessage(p.getDisplayName() + " will respawn in " + timeCheck.formatTime()); + commandSender.sendMessage(p.getDisplayName() + " will respawn in " + timeCheck.formatTime('f')); } return true; } else { @@ -97,7 +97,7 @@ public class CommandHowLong implements CommandExecutor { TimeCheck timeCheck = new TimeCheck(resurrectionTime - currentTime); - System.out.println("[Resurrection] " + p.getDisplayName() + " will respawn in " + timeCheck.formatTime()); + System.out.println("[Resurrection] " + p.getDisplayName() + " will respawn in " + timeCheck.formatTime('f')); return true; } else { diff --git a/src/net/brysonsteck/Resurrection/player/PlayerListener.java b/src/net/brysonsteck/Resurrection/player/PlayerListener.java index 749d6b6..09ff124 100644 --- a/src/net/brysonsteck/Resurrection/player/PlayerListener.java +++ b/src/net/brysonsteck/Resurrection/player/PlayerListener.java @@ -1,5 +1,6 @@ package net.brysonsteck.Resurrection.player; +import net.brysonsteck.Resurrection.ParseSettings; import net.brysonsteck.Resurrection.Resurrection; import org.bukkit.*; import org.bukkit.block.Block; @@ -24,6 +25,11 @@ public class PlayerListener implements Listener { World world = Bukkit.getWorlds().get(0); Location spawn = world.getSpawnLocation(); Hashtable<String, Location> playerSpawns = new Hashtable<>(); + ParseSettings parseSettings; + + public PlayerListener() { + parseSettings = new ParseSettings(); + } @EventHandler public void onJoin(PlayerJoinEvent e) { @@ -108,16 +114,11 @@ public class PlayerListener implements Listener { System.out.println("Resurrection: A player has died!"); Player p = e.getEntity(); stillDead = true; -// -// TimeCheck death = new TimeCheck(timeOfDeath); -// TimeCheck resurrect = new TimeCheck((timeOfDeath + 86400000) - timeOfDeath); -// -// String deathFormatted = death.formatTime(); -// String resurrectFormatted = resurrect.formatTime(); -// long timeOfDeath = System.currentTimeMillis(); + TimeCheck timeCheck = new TimeCheck(Long.parseLong(parseSettings.getSetting("resurrection_time"))); + long resurrectionTime = System.currentTimeMillis() + 86400000; - p.sendMessage("You have died!! You will be able to respawn in the next 24 hours."); + p.sendMessage("You have died!! You will be able to respawn in the next " + timeCheck.formatTime('h')); timerRunning = true; // save death state diff --git a/src/net/brysonsteck/Resurrection/player/TimeCheck.java b/src/net/brysonsteck/Resurrection/player/TimeCheck.java index a12bb16..4012e48 100644 --- a/src/net/brysonsteck/Resurrection/player/TimeCheck.java +++ b/src/net/brysonsteck/Resurrection/player/TimeCheck.java @@ -9,11 +9,18 @@ public class TimeCheck { this.millis = millis; } - public String formatTime() { - return String.format("%d hrs, %d min, %d sec", - TimeUnit.MILLISECONDS.toHours(millis), - TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), - TimeUnit.MILLISECONDS.toSeconds(millis) - - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); + public String formatTime(char format) { + // h = hours only, f = full time + if (format == 'f') { + return String.format("%d hrs, %d min, %d sec", + TimeUnit.MILLISECONDS.toHours(millis), + TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), + TimeUnit.MILLISECONDS.toSeconds(millis) - + TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); + } else if (format == 'h') { + return String.format("%d hours", + TimeUnit.MILLISECONDS.toHours(millis)); + } + return null; } } |