From 36ddb10fe26aca1f36d270342524ccdb5ffbbf10 Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 02:25:45 -0400 Subject: [PATCH 01/12] add RequestFunctions class --- .../requests/RequestFunctions.java | 45 +++++++++++++++++++ .../requests/SunriseSunsetRequestObject.java | 24 +++------- .../requests/WeatherRequestObject.java | 44 +++++++----------- 3 files changed, 66 insertions(+), 47 deletions(-) create mode 100644 src/main/java/io/github/jack1424/realtimeweather/requests/RequestFunctions.java diff --git a/src/main/java/io/github/jack1424/realtimeweather/requests/RequestFunctions.java b/src/main/java/io/github/jack1424/realtimeweather/requests/RequestFunctions.java new file mode 100644 index 0000000..119e55c --- /dev/null +++ b/src/main/java/io/github/jack1424/realtimeweather/requests/RequestFunctions.java @@ -0,0 +1,45 @@ +package io.github.jack1424.realtimeweather.requests; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Scanner; + +public class RequestFunctions { + public static Object makeRequest(String URLString) throws IOException, HTTPResponseException, ParseException { + int responseCode = getResponseCode(URLString); + if (responseCode > 399) + throw new HTTPResponseException(responseCode); + + Scanner scanner = new Scanner(new URL(URLString).openStream()); + StringBuilder response = new StringBuilder(); + while (scanner.hasNextLine()) + response.append(scanner.nextLine()); + scanner.close(); + + return new JSONParser().parse(response.toString()); + } + + public static int getResponseCode(String URLString) throws IOException { + URL url = new URL(URLString); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + con.connect(); + return con.getResponseCode(); + } + + public static String getLatestVersion() throws Exception { + return ((JSONObject) ((JSONArray) makeRequest("https://api.modrinth.com/v2/project/WRA6ODcm/version")).get(0)).get("version_number").toString(); + } + + public static class HTTPResponseException extends Exception { + public HTTPResponseException(int responseCode) { + super(String.valueOf(responseCode)); + } + } +} diff --git a/src/main/java/io/github/jack1424/realtimeweather/requests/SunriseSunsetRequestObject.java b/src/main/java/io/github/jack1424/realtimeweather/requests/SunriseSunsetRequestObject.java index d503fe0..70bd9d9 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/requests/SunriseSunsetRequestObject.java +++ b/src/main/java/io/github/jack1424/realtimeweather/requests/SunriseSunsetRequestObject.java @@ -1,14 +1,10 @@ package io.github.jack1424.realtimeweather.requests; import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import javax.naming.ConfigurationException; -import javax.net.ssl.HttpsURLConnection; import java.io.IOException; -import java.net.ProtocolException; -import java.net.URL; import java.time.LocalDate; import java.time.LocalTime; import java.time.ZoneId; @@ -20,23 +16,13 @@ public class SunriseSunsetRequestObject { private String sunriseTime, sunsetTime; public SunriseSunsetRequestObject(TimeZone timeZone, String lat, String lon) throws IOException, ParseException, ConfigurationException { - URL url = new URL(String.format("https://api.sunrisesunset.io/json?lat=%s&lng=%s&timezone=UTC", lat, lon)); - - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - con.setRequestMethod("GET"); - con.connect(); - int responseCode = con.getResponseCode(); - if (responseCode > 399) - throw new ProtocolException("Server/client error (HTTP error " + responseCode + ")"); - - Scanner scanner = new Scanner(url.openStream()); - StringBuilder data = new StringBuilder(); - while (scanner.hasNext()) { - data.append(scanner.nextLine()); + JSONObject response; + try { + response = (JSONObject) ((JSONObject) RequestFunctions.makeRequest(String.format("https://api.sunrisesunset.io/json?lat=%s&lng=%s&timezone=UTC", lat, lon))).get("results"); + } catch (RequestFunctions.HTTPResponseException e) { + throw new IOException("Server/client error (HTTP error " + e.getMessage() + ")"); } - scanner.close(); - JSONObject response = (JSONObject) ((JSONObject) new JSONParser().parse(data.toString())).get("results"); sunriseTime = response.get("sunrise").toString(); sunsetTime = response.get("sunset").toString(); diff --git a/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java b/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java index c1873d3..bdacf7d 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java +++ b/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java @@ -2,46 +2,34 @@ package io.github.jack1424.realtimeweather.requests; import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import javax.naming.ConfigurationException; -import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.net.ProtocolException; -import java.net.URL; -import java.util.Scanner; public class WeatherRequestObject { private boolean rain = false, thunder = false; public WeatherRequestObject(String apiKey, String lat, String lon) throws IOException, ParseException, ConfigurationException { - URL url = new URL(String.format("https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s", lat, lon, apiKey)); + JSONArray conditions; + try { + conditions = (JSONArray) ((JSONObject) RequestFunctions.makeRequest(String.format("https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s", lat, lon, apiKey))).get("weather"); + } catch (RequestFunctions.HTTPResponseException e) { + int responseCode = Integer.parseInt(e.getMessage()); + if (responseCode > 499) { + throw new ProtocolException("Server/client error (HTTP error " + responseCode + ")"); + } else if (responseCode > 399) { + String message = "Error when getting weather information: "; - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - con.setRequestMethod("GET"); - con.connect(); - int responseCode = con.getResponseCode(); - if (responseCode > 499) { - throw new ProtocolException("Server/client error (HTTP error " + responseCode + ")"); + if (responseCode == 401) + throw new ConfigurationException(message + "API key invalid. Check the Wiki for troubleshooting steps."); + else + throw new ProtocolException(message + "Unknown error"); + } else { + throw new IOException("Server/client error (HTTP error " + e.getMessage() + ")"); + } } - else if (responseCode > 399) { - String message = "Error when getting weather information: "; - - if (responseCode == 401) - throw new ConfigurationException(message + "API key invalid. Check the Wiki for troubleshooting steps."); - else - throw new ProtocolException(message + "Unknown error"); - } - - Scanner scanner = new Scanner(url.openStream()); - StringBuilder data = new StringBuilder(); - while (scanner.hasNext()) { - data.append(scanner.nextLine()); - } - scanner.close(); - - JSONArray conditions = (JSONArray) ((JSONObject) new JSONParser().parse(data.toString())).get("weather"); for (Object rawCondition : conditions) { int id = Integer.parseInt(String.valueOf(((JSONObject) rawCondition).get("id"))); From 20bf2d74fcb318a033f43a6d96518a1c72b4df89 Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 02:26:56 -0400 Subject: [PATCH 02/12] add update checks --- .../realtimeweather/ConfigManager.java | 13 +++++++++- .../realtimeweather/RealTimeWeather.java | 26 +++++++++++++++++++ src/main/resources/config.yml | 8 +++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java b/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java index 5a296b7..51f3970 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java +++ b/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java @@ -19,7 +19,7 @@ public class ConfigManager { private final FileConfiguration configFile; private TimeZone timeZone; private boolean debug, timeEnabled, weatherEnabled, blockTimeSetCommand, blockWeatherCommand, disableBedsAtNight, disableBedsDuringThunder; - private long timeSyncInterval, weatherSyncInterval; + private long timeSyncInterval, weatherSyncInterval, updateCheckInterval; private String sunriseSunset, sunriseSunsetLatitude, sunriseSunsetLongitude, apiKey, weatherLatitude, weatherLongitude, disableBedsAtNightMessage, disableBedsDuringThunderMessage, sunriseCustomTime, sunsetCustomTime; public ConfigManager(RealTimeWeather rtw) { @@ -71,6 +71,8 @@ public class ConfigManager { setWeatherEnabled(false); } + + setUpdateCheckInterval(configFile.getLong("updateCheckInterval")); } public boolean debugEnabled() { @@ -308,4 +310,13 @@ public class ConfigManager { weatherLongitude = value; rtw.debug("Longitude set to " + value); } + + public void setUpdateCheckInterval(long value) { + updateCheckInterval = value; + rtw.debug("updateCheckInterval set to " + value); + } + + public long getUpdateCheckInterval() { + return updateCheckInterval; + } } diff --git a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java index 7d4328e..8782bec 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java +++ b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java @@ -43,6 +43,15 @@ public final class RealTimeWeather extends JavaPlugin { metrics.addCustomChart(new SimplePie("time_sync_enabled", () -> String.valueOf(config.isTimeEnabled()))); logger.info("Started!"); + + logger.info("Checking for updates..."); + logger.info(getUpdateCheck()); + + long updateCheckInterval = config.getUpdateCheckInterval(); + if (config.getUpdateCheckInterval() > 0) + getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> { + logger.info(getUpdateCheck()); + }, updateCheckInterval, updateCheckInterval); } @Override @@ -168,6 +177,23 @@ public final class RealTimeWeather extends JavaPlugin { } } + public String getUpdateCheck() { + String currentVersion = this.getDescription().getVersion(); + String latestVersion; + try { + debug("Getting latest version..."); + latestVersion = RequestFunctions.getLatestVersion(); + } catch (Exception exception) { + debug(exception.getMessage()); + return "There was an error getting the latest version"; + } + + if (currentVersion.equals(latestVersion)) { + return String.format("RealTimeWeather (v%s) is up to date!", currentVersion); + } else + return String.format("RealTimeWeather (v%s) is outdated! v%s is the latest version.", currentVersion, latestVersion); + } + public ConfigManager getConfigurator() { return config; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 452c66e..ada6a06 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,4 +1,4 @@ -# RealTimeWeather Configuration File (v1.3.0) +# RealTimeWeather Configuration File (v1.4.0) # You can find detailed instructions at: https://github.com/Jack1424/RealTimeWeather/wiki#editing-the-configuration-file ################################# TIME SYNC SETTINGS ################################################################# @@ -72,6 +72,12 @@ WeatherLongitude: '0' # # ###################################################################################################################### +# By default, RealTimeWeather will check if an update is available every 24 hours (1734000 ticks) +# You can change the interval here (or set to 0 to disable update checks) +# If this is disabled, RealTimeWeather will still check for updates on startup +# You can find a handy tick calculator here: https://mapmaking.fr/tick/ +updateCheckInterval: 1734000 + # Set to true for various console messages when time and weather sync are executed # This is useful if the plugin is not working, and you want to find out what's wrong # This will also provide error messages even when an error is caught and managed by RTM From 6db07a376b47fa943c09cff6b9379ec7f2fc23d4 Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 02:27:39 -0400 Subject: [PATCH 03/12] cleaned up some code --- .../github/jack1424/realtimeweather/RealTimeWeather.java | 8 ++++---- .../realtimeweather/requests/WeatherRequestObject.java | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java index 8782bec..2d56f80 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java +++ b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java @@ -106,9 +106,9 @@ public final class RealTimeWeather extends JavaPlugin { world.setTime(calculateWorldTime(cal, "5:02:27 AM", "6:36:36 PM")); return; } - } else if (config.getSunriseSunset().equals("custom")) + } else if (config.getSunriseSunset().equals("custom")) { world.setTime(calculateWorldTime(cal, config.getSunriseCustomTime(), config.getSunsetCustomTime())); - else + } else world.setTime(calculateWorldTime(cal, "5:02:27 AM", "6:36:36 PM")); } }, 0L, config.getTimeSyncInterval()); @@ -170,9 +170,9 @@ public final class RealTimeWeather extends JavaPlugin { if (currentMinutes >= sunriseMinutes && currentMinutes < sunsetMinutes) { return ((currentMinutes - sunriseMinutes) / (sunsetMinutes - sunriseMinutes) * 13569) + 23041; } else { - if (currentMinutes < sunriseMinutes) { + if (currentMinutes < sunriseMinutes) currentMinutes += 1440; - } + return ((currentMinutes - sunsetMinutes) / (1440 - sunsetMinutes + sunriseMinutes) * 13569) + 12610; } } diff --git a/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java b/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java index bdacf7d..ed4e960 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java +++ b/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java @@ -33,7 +33,6 @@ public class WeatherRequestObject { for (Object rawCondition : conditions) { int id = Integer.parseInt(String.valueOf(((JSONObject) rawCondition).get("id"))); - while (id >= 10) id /= 10; From 97bd8df2dec9a3d0d02e51c88a0ca9e26eb03385 Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 02:28:28 -0400 Subject: [PATCH 04/12] removed unnecessary if statements from WeatherRequestObject --- .../realtimeweather/requests/WeatherRequestObject.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java b/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java index ed4e960..0a215d3 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java +++ b/src/main/java/io/github/jack1424/realtimeweather/requests/WeatherRequestObject.java @@ -36,10 +36,8 @@ public class WeatherRequestObject { while (id >= 10) id /= 10; - if (!rain) - rain = id == 2 || id == 3 || id == 5 || id == 6; - if (!thunder) - thunder = id == 2; + rain = id == 2 || id == 3 || id == 5 || id == 6; + thunder = id == 2; } } From 796097f3e20ed2475874b57283b7643f7d8e6f28 Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 02:30:39 -0400 Subject: [PATCH 05/12] shortened task scheduling line for update checks --- .../io/github/jack1424/realtimeweather/RealTimeWeather.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java index 2d56f80..30f903b 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java +++ b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java @@ -49,9 +49,7 @@ public final class RealTimeWeather extends JavaPlugin { long updateCheckInterval = config.getUpdateCheckInterval(); if (config.getUpdateCheckInterval() > 0) - getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> { - logger.info(getUpdateCheck()); - }, updateCheckInterval, updateCheckInterval); + getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> logger.info(getUpdateCheck()), updateCheckInterval, updateCheckInterval); } @Override From 334471b37b7e384f86208f0da58f5fec107705bd Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:49:46 -0400 Subject: [PATCH 06/12] moved updateCheckInterval and Debug to top of config.yml --- .../jack1424/realtimeweather/ConfigManager.java | 2 +- src/main/resources/config.yml | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java b/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java index 51f3970..76979a3 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java +++ b/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java @@ -72,7 +72,7 @@ public class ConfigManager { setWeatherEnabled(false); } - setUpdateCheckInterval(configFile.getLong("updateCheckInterval")); + setUpdateCheckInterval(configFile.getLong("UpdateCheckInterval")); } public boolean debugEnabled() { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index ada6a06..a44f8a7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,6 +1,21 @@ # RealTimeWeather Configuration File (v1.4.0) # You can find detailed instructions at: https://github.com/Jack1424/RealTimeWeather/wiki#editing-the-configuration-file +######################################## Real Time Weather Settings ################################################## +# By default, RealTimeWeather will check if an update is available every 24 hours (1734000 ticks) # +# You can change the interval here (or set to 0 to disable update checks) # +# If this is disabled, RealTimeWeather will still check for updates on startup # +# You can find a handy tick calculator here: https://mapmaking.fr/tick/ # +UpdateCheckInterval: 1734000 +# +# Set to true for various console messages when time and weather sync are executed # +# This is useful if the plugin is not working, and you want to find out what's wrong # +# This will also provide error messages even when an error is caught and managed by RTM # +# Note: There will be no messages during a time sync because they happen very frequently # +# Note: Unhandled plugin errors will still be logged regardless of the debug value # +Debug: false +###################################################################################################################### + ################################# TIME SYNC SETTINGS ################################################################# # Set to true to enable time syncing, or false to disable # # All time-related settings will be ignored if this is set to false # From 27dc240597584ec7dc7f8947437b893d01058b08 Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:50:34 -0400 Subject: [PATCH 07/12] moved updateCheckInterval and Debug to top of config.yml --- src/main/resources/config.yml | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index a44f8a7..c83c8df 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -85,17 +85,4 @@ APIKey: 'API_KEY' WeatherLatitude: '0' WeatherLongitude: '0' # # -###################################################################################################################### - -# By default, RealTimeWeather will check if an update is available every 24 hours (1734000 ticks) -# You can change the interval here (or set to 0 to disable update checks) -# If this is disabled, RealTimeWeather will still check for updates on startup -# You can find a handy tick calculator here: https://mapmaking.fr/tick/ -updateCheckInterval: 1734000 - -# Set to true for various console messages when time and weather sync are executed -# This is useful if the plugin is not working, and you want to find out what's wrong -# This will also provide error messages even when an error is caught and managed by RTM -# Note: There will be no messages during a time sync because they happen very frequently -# Note: Unhandled plugin errors will still be logged regardless of the debug value -Debug: false +###################################################################################################################### \ No newline at end of file From 549541c74aebc68434dfa1dc2411e934dc9a3233 Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:51:23 -0400 Subject: [PATCH 08/12] moved updateCheckInterval and Debug to top of config.yml part 3 --- .../github/jack1424/realtimeweather/ConfigManager.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java b/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java index 76979a3..42a6f3e 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java +++ b/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java @@ -310,13 +310,4 @@ public class ConfigManager { weatherLongitude = value; rtw.debug("Longitude set to " + value); } - - public void setUpdateCheckInterval(long value) { - updateCheckInterval = value; - rtw.debug("updateCheckInterval set to " + value); - } - - public long getUpdateCheckInterval() { - return updateCheckInterval; - } } From 1ffa9d81bb5e998beabee7bf9972caad3f9ffb8e Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:52:43 -0400 Subject: [PATCH 09/12] added support for enabling/disabling time and weather sync in specific worlds (closes #7) --- .../realtimeweather/ConfigManager.java | 82 ++++++++++++++++++- .../realtimeweather/RealTimeWeather.java | 62 +++++++------- src/main/resources/config.yml | 22 +++++ 3 files changed, 130 insertions(+), 36 deletions(-) diff --git a/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java b/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java index 42a6f3e..9dda9fe 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java +++ b/src/main/java/io/github/jack1424/realtimeweather/ConfigManager.java @@ -1,6 +1,7 @@ package io.github.jack1424.realtimeweather; import io.github.jack1424.realtimeweather.requests.WeatherRequestObject; +import org.bukkit.World; import org.bukkit.configuration.file.FileConfiguration; import org.json.simple.parser.ParseException; @@ -11,6 +12,7 @@ import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.zone.ZoneRulesException; +import java.util.HashSet; import java.util.Objects; import java.util.TimeZone; @@ -18,9 +20,10 @@ public class ConfigManager { private final RealTimeWeather rtw; private final FileConfiguration configFile; private TimeZone timeZone; - private boolean debug, timeEnabled, weatherEnabled, blockTimeSetCommand, blockWeatherCommand, disableBedsAtNight, disableBedsDuringThunder; - private long timeSyncInterval, weatherSyncInterval, updateCheckInterval; + private boolean debug, timeEnabled, weatherEnabled, timeSyncAllWorlds, weatherSyncAllWorlds, blockTimeSetCommand, blockWeatherCommand, disableBedsAtNight, disableBedsDuringThunder; + private long updateCheckInterval, timeSyncInterval, weatherSyncInterval; private String sunriseSunset, sunriseSunsetLatitude, sunriseSunsetLongitude, apiKey, weatherLatitude, weatherLongitude, disableBedsAtNightMessage, disableBedsDuringThunderMessage, sunriseCustomTime, sunsetCustomTime; + private HashSet timeSyncWorlds, weatherSyncWorlds; public ConfigManager(RealTimeWeather rtw) { this.rtw = rtw; @@ -33,6 +36,16 @@ public class ConfigManager { setTimeEnabled(configFile.getBoolean("SyncTime")); if (isTimeEnabled()) try { + timeSyncWorlds = new HashSet<>(); + setTimeSyncAllWorlds(configFile.getBoolean("TimeSyncAllWorlds")); + if (getTimeSyncAllWorlds()) { + for (World world : rtw.getServer().getWorlds()) + if (world.getEnvironment() == World.Environment.NORMAL) + addTimeSyncWorld(world.getName()); + } else { + for (String worldName : configFile.getStringList("TimeSyncWorlds")) + addTimeSyncWorld(worldName); + } setBlockTimeSetCommand(configFile.getBoolean("BlockTimeSetCommand")); setDisableBedsAtNight(configFile.getBoolean("DisableBedsAtNight")); setDisableBedsAtNightMessage(configFile.getString("DisableBedsAtNightMessage")); @@ -57,6 +70,16 @@ public class ConfigManager { setWeatherEnabled(configFile.getBoolean("SyncWeather")); if (isWeatherEnabled()) try { + weatherSyncWorlds = new HashSet<>(); + setWeatherSyncAllWorlds(configFile.getBoolean("WeatherSyncAllWorlds")); + if (getWeatherSyncAllWorlds()) { + for (World world : rtw.getServer().getWorlds()) + if (world.getEnvironment() == World.Environment.NORMAL) + addWeatherSyncWorld(world.getName()); + } else { + for (String worldName : configFile.getStringList("WeatherSyncWorlds")) + addWeatherSyncWorld(worldName); + } setBlockWeatherCommand(configFile.getBoolean("BlockWeatherCommand")); setDisableBedsDuringThunder(configFile.getBoolean("DisableBedsDuringThunder")); setDisableBedsDuringThunderMessage(configFile.getString("DisableBedsDuringThunderMessage")); @@ -75,6 +98,15 @@ public class ConfigManager { setUpdateCheckInterval(configFile.getLong("UpdateCheckInterval")); } + public long getUpdateCheckInterval() { + return updateCheckInterval; + } + + public void setUpdateCheckInterval(long value) { + updateCheckInterval = value; + rtw.debug("updateCheckInterval set to " + value); + } + public boolean debugEnabled() { return debug; } @@ -93,6 +125,29 @@ public class ConfigManager { rtw.debug("SyncTime set to " + value); } + public boolean getTimeSyncAllWorlds() { + return timeSyncAllWorlds; + } + + public void setTimeSyncAllWorlds(boolean value) { + timeSyncAllWorlds = value; + rtw.debug("TimeSyncAllWorlds set to " + value); + } + + public HashSet getTimeSyncWorlds() { + return timeSyncWorlds; + } + + public void addTimeSyncWorld(String worldName) throws ConfigurationException { + World world = rtw.getServer().getWorld(worldName); + + if (world == null) + throw new ConfigurationException("World \"" + worldName + "\" cannot be found"); + + timeSyncWorlds.add(world); + rtw.debug("World \"" + worldName + "\" added to TimeSyncWorlds"); + } + public boolean getBlockTimeSetCommand() { return blockTimeSetCommand; } @@ -215,6 +270,29 @@ public class ConfigManager { rtw.debug("SyncWeather set to " + value); } + public boolean getWeatherSyncAllWorlds() { + return weatherSyncAllWorlds; + } + + public void setWeatherSyncAllWorlds(boolean value) { + weatherSyncAllWorlds = value; + rtw.debug("WeatherSyncAllWorlds set to " + value); + } + + public HashSet getWeatherSyncWorlds() { + return weatherSyncWorlds; + } + + public void addWeatherSyncWorld(String worldName) throws ConfigurationException { + World world = rtw.getServer().getWorld(worldName); + + if (world == null) + throw new ConfigurationException("World \"" + worldName + "\" cannot be found"); + + weatherSyncWorlds.add(world); + rtw.debug("World \"" + worldName + "\" added to WeatherSyncWorlds"); + } + public boolean getBlockWeatherCommand() { return blockWeatherCommand; } diff --git a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java index 30f903b..b889fa8 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java +++ b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java @@ -77,37 +77,35 @@ public final class RealTimeWeather extends JavaPlugin { if (config.getSunriseSunset().equals("custom")) debug("Using custom sunrise/sunset times. Sunrise: " + config.getSunriseCustomTime() + ", Sunset: " + config.getSunsetCustomTime()); - for (World world : getServer().getWorlds()) - if (world.getEnvironment().equals(World.Environment.NORMAL)) - world.setGameRuleValue("doDaylightCycle", "false"); + for (World world : config.getTimeSyncWorlds()) + world.setGameRuleValue("doDaylightCycle", "false"); getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> { if (config.isTimeEnabled()) { Calendar cal = Calendar.getInstance(config.getTimeZone()); - for (World world : getServer().getWorlds()) - if (world.getEnvironment().equals(World.Environment.NORMAL)) - if (config.getSunriseSunset().equals("real")) { - SunriseSunsetRequestObject sunriseSunset; + for (World world : config.getTimeSyncWorlds()) + if (config.getSunriseSunset().equals("real")) { + SunriseSunsetRequestObject sunriseSunset; + try { + sunriseSunset = new SunriseSunsetRequestObject(config.getTimeZone(), config.getWeatherLatitude(), config.getWeatherLongitude()); + world.setTime(calculateWorldTime(cal, sunriseSunset.getSunriseTime(), sunriseSunset.getSunsetTime())); + } catch (Exception e) { + logger.severe(e.getMessage()); + logger.severe("Error getting sunrise/sunset times, using default sunrise/sunset times"); + try { - sunriseSunset = new SunriseSunsetRequestObject(config.getTimeZone(), config.getWeatherLatitude(), config.getWeatherLongitude()); - world.setTime(calculateWorldTime(cal, sunriseSunset.getSunriseTime(), sunriseSunset.getSunsetTime())); - } catch (Exception e) { - logger.severe(e.getMessage()); - logger.severe("Error getting sunrise/sunset times, using default sunrise/sunset times"); - - try { - config.setSunriseSunset("default"); - } catch (ConfigurationException ex) { - throw new RuntimeException(ex); - } - - world.setTime(calculateWorldTime(cal, "5:02:27 AM", "6:36:36 PM")); - return; + config.setSunriseSunset("default"); + } catch (ConfigurationException ex) { + throw new RuntimeException(ex); } - } else if (config.getSunriseSunset().equals("custom")) { - world.setTime(calculateWorldTime(cal, config.getSunriseCustomTime(), config.getSunsetCustomTime())); - } else + world.setTime(calculateWorldTime(cal, "5:02:27 AM", "6:36:36 PM")); + return; + } + } else if (config.getSunriseSunset().equals("custom")) { + world.setTime(calculateWorldTime(cal, config.getSunriseCustomTime(), config.getSunsetCustomTime())); + } else + world.setTime(calculateWorldTime(cal, "5:02:27 AM", "6:36:36 PM")); } }, 0L, config.getTimeSyncInterval()); } @@ -123,11 +121,8 @@ public final class RealTimeWeather extends JavaPlugin { return; } - for (World world : getServer().getWorlds()) - if (world.getEnvironment().equals(World.Environment.NORMAL)) - world.setGameRuleValue("doWeatherCycle", "false"); - - debug("Enabling weather sync..."); + for (World world : config.getWeatherSyncWorlds()) + world.setGameRuleValue("doWeatherCycle", "false"); getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> { debug("Syncing weather..."); @@ -136,11 +131,10 @@ public final class RealTimeWeather extends JavaPlugin { WeatherRequestObject request = new WeatherRequestObject(config.getAPIKey(), config.getWeatherLatitude(), config.getWeatherLongitude()); debug("Setting weather (Rain: " + request.isRaining() + ", Thunder: " + request.isThundering() + ")..."); - for (World world : getServer().getWorlds()) - if (world.getEnvironment().equals(World.Environment.NORMAL)) { - world.setStorm(request.isRaining()); - world.setThundering(request.isThundering()); - } + for (World world : config.getWeatherSyncWorlds()) { + world.setStorm(request.isRaining()); + world.setThundering(request.isThundering()); + } } catch (Exception e) { logger.severe("There was an error when attempting to get weather information"); debug(e.getMessage()); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index c83c8df..4cb1acf 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -21,6 +21,17 @@ Debug: false # All time-related settings will be ignored if this is set to false # SyncTime: false # # +# By default, RealTimeWeather will apply the time sync settings below to all worlds # +# End and nether worlds will not be synced # +# If you ony want to enable time syncing in some worlds on your server, set this to false # +TimeSyncAllWorlds: true +# # +# List the worlds that you want RealTimeWeather to sync the time in # +# End and nether worlds will not be synced # +# This only works if TimeSyncAllWorlds is set to false # +TimeSyncWorlds: + - world +# # # Set to false to enable the /time set command (not recommended) # BlockTimeSetCommand: true # # @@ -60,6 +71,17 @@ SunsetCustomTime: '6:36:36 PM' # All weather-related settings will be ignored if this is set to false # SyncWeather: false # # +# By default, RealTimeWeather will apply the weather sync settings below to all worlds # +# End and nether worlds will not be synced # +# If you ony want to enable weather syncing in some worlds on your server, set this to false # +WeatherSyncAllWorlds: true +# # +# List the worlds that you want RealTimeWeather to sync the weather in # +# End and nether worlds will not be synced # +# This only works if WeatherSyncAllWorlds is set to false # +WeatherSyncWorlds: + - world +# # # Set to false to enable the /weather command (not recommended) # BlockWeatherCommand: true # # From e3e09f24ac108e7541effa17119697f0274731ab Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:53:22 -0400 Subject: [PATCH 10/12] renamed RealTimeWeather.getConfigurator to getConfigManager --- .../java/io/github/jack1424/realtimeweather/EventHandlers.java | 2 +- .../io/github/jack1424/realtimeweather/RealTimeWeather.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/jack1424/realtimeweather/EventHandlers.java b/src/main/java/io/github/jack1424/realtimeweather/EventHandlers.java index dceb20d..910a4c6 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/EventHandlers.java +++ b/src/main/java/io/github/jack1424/realtimeweather/EventHandlers.java @@ -13,7 +13,7 @@ public class EventHandlers implements Listener { private final ConfigManager config; public EventHandlers(RealTimeWeather rtw) { - config = rtw.getConfigurator(); + config = rtw.getConfigManager(); } @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) diff --git a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java index b889fa8..b6f1247 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java +++ b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java @@ -186,7 +186,7 @@ public final class RealTimeWeather extends JavaPlugin { return String.format("RealTimeWeather (v%s) is outdated! v%s is the latest version.", currentVersion, latestVersion); } - public ConfigManager getConfigurator() { + public ConfigManager getConfigManager() { return config; } From dfbd81146ff5d78e108f21f041589f0f92c18f9c Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:53:43 -0400 Subject: [PATCH 11/12] moved some debug statements around --- .../io/github/jack1424/realtimeweather/RealTimeWeather.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java index b6f1247..ce4215f 100644 --- a/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java +++ b/src/main/java/io/github/jack1424/realtimeweather/RealTimeWeather.java @@ -108,9 +108,13 @@ public final class RealTimeWeather extends JavaPlugin { world.setTime(calculateWorldTime(cal, "5:02:27 AM", "6:36:36 PM")); } }, 0L, config.getTimeSyncInterval()); + + debug("Weather sync enabled"); } private void setupWeather() { + debug("Enabling weather sync..."); + try { new WeatherRequestObject(config.getAPIKey(), config.getWeatherLatitude(), config.getWeatherLongitude()); } catch (Exception e) { From d51c5f531fab3632b958acb2645dbf9cc2e41358 Mon Sep 17 00:00:00 2001 From: Jack <55409055+Jack1424@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:54:46 -0400 Subject: [PATCH 12/12] version set to 1.4.0 --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 2012492..d6dbd5d 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'io.github.Jack1424' -version = '1.3.0' +version = '1.4.0' repositories { mavenCentral()