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;
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<World> 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<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() {
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<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() {
return blockWeatherCommand;
}

View File

@@ -77,15 +77,13 @@ 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))
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))
for (World world : config.getTimeSyncWorlds())
if (config.getSunriseSunset().equals("real")) {
SunriseSunsetRequestObject sunriseSunset;
try {
@@ -123,12 +121,9 @@ public final class RealTimeWeather extends JavaPlugin {
return;
}
for (World world : getServer().getWorlds())
if (world.getEnvironment().equals(World.Environment.NORMAL))
for (World world : config.getWeatherSyncWorlds())
world.setGameRuleValue("doWeatherCycle", "false");
debug("Enabling weather sync...");
getServer().getScheduler().scheduleSyncRepeatingTask(this, () -> {
debug("Syncing weather...");
@@ -136,8 +131,7 @@ 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)) {
for (World world : config.getWeatherSyncWorlds()) {
world.setStorm(request.isRaining());
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 #
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
# #