added support for enabling/disabling time and weather sync in specific worlds (closes #7)

This commit is contained in:
Jack
2024-07-23 17:52:43 -04:00
parent 549541c74a
commit 1ffa9d81bb
3 changed files with 130 additions and 36 deletions

View File

@@ -1,6 +1,7 @@
package io.github.jack1424.realtimeweather; package io.github.jack1424.realtimeweather;
import io.github.jack1424.realtimeweather.requests.WeatherRequestObject; import io.github.jack1424.realtimeweather.requests.WeatherRequestObject;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
@@ -11,6 +12,7 @@ import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.time.zone.ZoneRulesException; import java.time.zone.ZoneRulesException;
import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.TimeZone; import java.util.TimeZone;
@@ -18,9 +20,10 @@ public class ConfigManager {
private final RealTimeWeather rtw; private final RealTimeWeather rtw;
private final FileConfiguration configFile; private final FileConfiguration configFile;
private TimeZone timeZone; private TimeZone timeZone;
private boolean debug, timeEnabled, weatherEnabled, blockTimeSetCommand, blockWeatherCommand, disableBedsAtNight, disableBedsDuringThunder; private boolean debug, timeEnabled, weatherEnabled, timeSyncAllWorlds, weatherSyncAllWorlds, blockTimeSetCommand, blockWeatherCommand, disableBedsAtNight, disableBedsDuringThunder;
private long timeSyncInterval, weatherSyncInterval, updateCheckInterval; private long updateCheckInterval, timeSyncInterval, weatherSyncInterval;
private String sunriseSunset, sunriseSunsetLatitude, sunriseSunsetLongitude, apiKey, weatherLatitude, weatherLongitude, disableBedsAtNightMessage, disableBedsDuringThunderMessage, sunriseCustomTime, sunsetCustomTime; private String sunriseSunset, sunriseSunsetLatitude, sunriseSunsetLongitude, apiKey, weatherLatitude, weatherLongitude, disableBedsAtNightMessage, disableBedsDuringThunderMessage, sunriseCustomTime, sunsetCustomTime;
private HashSet<World> timeSyncWorlds, weatherSyncWorlds;
public ConfigManager(RealTimeWeather rtw) { public ConfigManager(RealTimeWeather rtw) {
this.rtw = rtw; this.rtw = rtw;
@@ -33,6 +36,16 @@ public class ConfigManager {
setTimeEnabled(configFile.getBoolean("SyncTime")); setTimeEnabled(configFile.getBoolean("SyncTime"));
if (isTimeEnabled()) if (isTimeEnabled())
try { 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")); setBlockTimeSetCommand(configFile.getBoolean("BlockTimeSetCommand"));
setDisableBedsAtNight(configFile.getBoolean("DisableBedsAtNight")); setDisableBedsAtNight(configFile.getBoolean("DisableBedsAtNight"));
setDisableBedsAtNightMessage(configFile.getString("DisableBedsAtNightMessage")); setDisableBedsAtNightMessage(configFile.getString("DisableBedsAtNightMessage"));
@@ -57,6 +70,16 @@ public class ConfigManager {
setWeatherEnabled(configFile.getBoolean("SyncWeather")); setWeatherEnabled(configFile.getBoolean("SyncWeather"));
if (isWeatherEnabled()) if (isWeatherEnabled())
try { 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")); setBlockWeatherCommand(configFile.getBoolean("BlockWeatherCommand"));
setDisableBedsDuringThunder(configFile.getBoolean("DisableBedsDuringThunder")); setDisableBedsDuringThunder(configFile.getBoolean("DisableBedsDuringThunder"));
setDisableBedsDuringThunderMessage(configFile.getString("DisableBedsDuringThunderMessage")); setDisableBedsDuringThunderMessage(configFile.getString("DisableBedsDuringThunderMessage"));
@@ -75,6 +98,15 @@ public class ConfigManager {
setUpdateCheckInterval(configFile.getLong("UpdateCheckInterval")); 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() { public boolean debugEnabled() {
return debug; return debug;
} }
@@ -93,6 +125,29 @@ public class ConfigManager {
rtw.debug("SyncTime set to " + value); 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<World> 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() { public boolean getBlockTimeSetCommand() {
return blockTimeSetCommand; return blockTimeSetCommand;
} }
@@ -215,6 +270,29 @@ public class ConfigManager {
rtw.debug("SyncWeather set to " + value); 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<World> 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() { public boolean getBlockWeatherCommand() {
return blockWeatherCommand; return blockWeatherCommand;
} }

View File

@@ -77,15 +77,13 @@ public final class RealTimeWeather extends JavaPlugin {
if (config.getSunriseSunset().equals("custom")) if (config.getSunriseSunset().equals("custom"))
debug("Using custom sunrise/sunset times. Sunrise: " + config.getSunriseCustomTime() + ", Sunset: " + config.getSunsetCustomTime()); debug("Using custom sunrise/sunset times. Sunrise: " + config.getSunriseCustomTime() + ", Sunset: " + config.getSunsetCustomTime());
for (World world : getServer().getWorlds()) for (World world : config.getTimeSyncWorlds())
if (world.getEnvironment().equals(World.Environment.NORMAL))
world.setGameRuleValue("doDaylightCycle", "false"); world.setGameRuleValue("doDaylightCycle", "false");
getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> { getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> {
if (config.isTimeEnabled()) { if (config.isTimeEnabled()) {
Calendar cal = Calendar.getInstance(config.getTimeZone()); Calendar cal = Calendar.getInstance(config.getTimeZone());
for (World world : getServer().getWorlds()) for (World world : config.getTimeSyncWorlds())
if (world.getEnvironment().equals(World.Environment.NORMAL))
if (config.getSunriseSunset().equals("real")) { if (config.getSunriseSunset().equals("real")) {
SunriseSunsetRequestObject sunriseSunset; SunriseSunsetRequestObject sunriseSunset;
try { try {
@@ -123,12 +121,9 @@ public final class RealTimeWeather extends JavaPlugin {
return; return;
} }
for (World world : getServer().getWorlds()) for (World world : config.getWeatherSyncWorlds())
if (world.getEnvironment().equals(World.Environment.NORMAL))
world.setGameRuleValue("doWeatherCycle", "false"); world.setGameRuleValue("doWeatherCycle", "false");
debug("Enabling weather sync...");
getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> { getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> {
debug("Syncing weather..."); debug("Syncing weather...");
@@ -136,8 +131,7 @@ public final class RealTimeWeather extends JavaPlugin {
WeatherRequestObject request = new WeatherRequestObject(config.getAPIKey(), config.getWeatherLatitude(), config.getWeatherLongitude()); WeatherRequestObject request = new WeatherRequestObject(config.getAPIKey(), config.getWeatherLatitude(), config.getWeatherLongitude());
debug("Setting weather (Rain: " + request.isRaining() + ", Thunder: " + request.isThundering() + ")..."); debug("Setting weather (Rain: " + request.isRaining() + ", Thunder: " + request.isThundering() + ")...");
for (World world : getServer().getWorlds()) for (World world : config.getWeatherSyncWorlds()) {
if (world.getEnvironment().equals(World.Environment.NORMAL)) {
world.setStorm(request.isRaining()); world.setStorm(request.isRaining());
world.setThundering(request.isThundering()); world.setThundering(request.isThundering());
} }

View File

@@ -21,6 +21,17 @@ Debug: false
# All time-related settings will be ignored if this is set to false # # All time-related settings will be ignored if this is set to false #
SyncTime: 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) # # Set to false to enable the /time set command (not recommended) #
BlockTimeSetCommand: true BlockTimeSetCommand: true
# # # #
@@ -60,6 +71,17 @@ SunsetCustomTime: '6:36:36 PM'
# All weather-related settings will be ignored if this is set to false # # All weather-related settings will be ignored if this is set to false #
SyncWeather: 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) # # Set to false to enable the /weather command (not recommended) #
BlockWeatherCommand: true BlockWeatherCommand: true
# # # #