From b496a022c9c1e197fd6702dba47e2b6b4572cdf9 Mon Sep 17 00:00:00 2001 From: Roland von Rotz Date: Tue, 1 Oct 2019 23:09:53 +0200 Subject: [PATCH] Add time skip interval to config and more refactoring --- src/main/java/xyz/nkomarn/Harbor/Harbor.java | 5 -- .../Harbor/task/AccelerateNightTask.java | 2 +- .../java/xyz/nkomarn/Harbor/task/Checker.java | 6 +- .../java/xyz/nkomarn/Harbor/util/Config.java | 73 ++++++------------- src/main/resources/config.yml | 1 + 5 files changed, 28 insertions(+), 59 deletions(-) diff --git a/src/main/java/xyz/nkomarn/Harbor/Harbor.java b/src/main/java/xyz/nkomarn/Harbor/Harbor.java index 067ec17..c19a100 100644 --- a/src/main/java/xyz/nkomarn/Harbor/Harbor.java +++ b/src/main/java/xyz/nkomarn/Harbor/Harbor.java @@ -32,9 +32,4 @@ public class Harbor extends JavaPlugin { // Essentials hook essentials = (Essentials) Bukkit.getServer().getPluginManager().getPlugin("Essentials"); } - - public void onDisable() { - - } - } diff --git a/src/main/java/xyz/nkomarn/Harbor/task/AccelerateNightTask.java b/src/main/java/xyz/nkomarn/Harbor/task/AccelerateNightTask.java index 364c0a3..8bddeff 100644 --- a/src/main/java/xyz/nkomarn/Harbor/task/AccelerateNightTask.java +++ b/src/main/java/xyz/nkomarn/Harbor/task/AccelerateNightTask.java @@ -17,7 +17,7 @@ public class AccelerateNightTask extends BukkitRunnable { public void run() { final long time = world.getTime(); if (!(time >= 450 && time <= 1000)) { - world.setTime(time + 60); + world.setTime(time + Config.getInteger("values.timeSkipInterval")); } else { // Announce night skip and clear queue Message.SendRandomChatMessage("messages.chat.skipped"); diff --git a/src/main/java/xyz/nkomarn/Harbor/task/Checker.java b/src/main/java/xyz/nkomarn/Harbor/task/Checker.java index 89f28ff..f161c97 100644 --- a/src/main/java/xyz/nkomarn/Harbor/task/Checker.java +++ b/src/main/java/xyz/nkomarn/Harbor/task/Checker.java @@ -6,6 +6,7 @@ import org.bukkit.World; import org.bukkit.entity.Player; import xyz.nkomarn.Harbor.Harbor; import xyz.nkomarn.Harbor.util.Config; +import xyz.nkomarn.Harbor.util.Message; import java.util.ArrayList; import java.util.List; @@ -77,7 +78,8 @@ public class Checker implements Runnable { } private void accelerateNight(final World world) { - Bukkit.broadcastMessage(Config.getString("messages.chat.accelerateNight")); - new AccelerateNightTask(world).runTaskTimer(Harbor.instance, 20, 1); + Message.SendChatMessage(world, "messages.chat.accelerateNight", "", 0); + Message.SendActionbarMessage(world, "messages.actionbar.everyone", "", 0); + new AccelerateNightTask(world).runTaskTimer(Harbor.instance, 0L, 1); } } diff --git a/src/main/java/xyz/nkomarn/Harbor/util/Config.java b/src/main/java/xyz/nkomarn/Harbor/util/Config.java index 441a3e2..bbba8b2 100644 --- a/src/main/java/xyz/nkomarn/Harbor/util/Config.java +++ b/src/main/java/xyz/nkomarn/Harbor/util/Config.java @@ -4,84 +4,55 @@ import xyz.nkomarn.Harbor.Harbor; import java.util.ArrayList; import java.util.List; -import java.util.Objects; public class Config { - /** - * Report an error in reading the configuration - * @param e Exception generated from reading configuration - */ - private static void error(Exception e) { - e.printStackTrace(); - } - /** * Fetches a boolean from the configuration + * if location is not found, false is returned + * * @param location Configuration location of the boolean */ - public static boolean getBoolean(String location) { - try { - return Harbor.instance.getConfig().getBoolean(location); - } - catch (Exception e) { - error(e); - return false; - } + public static boolean getBoolean(final String location) { + return Harbor.instance.getConfig().getBoolean(location, false); } /** * Fetches a string from the configuration + * if location is not found, empty String is returned + * * @param location Configuration location of the string */ - public static String getString(String location) { - try { - return Harbor.instance.getConfig().getString(location); - } - catch (Exception e) { - error(e); - return ""; - } + public static String getString(final String location) { + return Harbor.instance.getConfig().getString(location, ""); } /** * Fetches an integer from the configuration + * if location is not found, 0 is returned + * * @param location Configuration location of the integer */ - public static int getInteger(String location) { - try { - return Harbor.instance.getConfig().getInt(location); - } - catch (Exception e) { - error(e); - return 0; - } + public static int getInteger(final String location) { + return Harbor.instance.getConfig().getInt(location, 0); } /** * Fetches a double from the configuration + * if location is not found, 0.0 is returned + * * @param location Configuration location of the double */ - public static double getDouble(String location) { - try { - return Double.parseDouble(Objects.requireNonNull(Harbor.instance.getConfig().getString(location))); - } - catch (Exception e) { - error(e); - return 0.0; - } + public static double getDouble(final String location) { + return Harbor.instance.getConfig().getDouble(location, 0.0); } /** - * Fetches a double from the configuration - * @param location Configuration location of the double + * Fetches a list from the configuration + * if location is not found, empty list is returned + * + * @param location Configuration location of the list */ - public static List getList(String location) { - try { - return Harbor.instance.getConfig().getStringList(location); - } - catch (Exception e) { - error(e); - return new ArrayList<>(); - } + public static List getList(final String location) { + return (List) Harbor.instance.getConfig().getList(location, new ArrayList<>()); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9d3aaf7..0202ce5 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -11,6 +11,7 @@ values: timer: 2 # How often to run the clock task (used to detect sleep, AFK players, time actionbar, etc.) percent: 100 # Percent of players that need to sleep to skip night (must be between 0 to 100) + timeSkipInterval: 60 # Time skip interval that is added when the night get accelerated. features: skip: true # Toggle night skipping feature. Configure amount of players needed to skip above (percent)