added support for enabling/disabling time and weather sync in specific worlds (closes #7)
This commit is contained in:
		@@ -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;
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -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());
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
#                                                                                                                    #
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user