some refactoring I don't remember it was yesterday
commiting just in case because I'm about to do something risky
This commit is contained in:
		
					parent
					
						
							
								96c8777ac2
							
						
					
				
			
			
				commit
				
					
						aae479740a
					
				
			
		
					 10 changed files with 35 additions and 65 deletions
				
			
		
							
								
								
									
										2
									
								
								pom.xml
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								pom.xml
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -49,7 +49,7 @@
 | 
			
		|||
		<dependency>
 | 
			
		||||
			<groupId>eu.m724</groupId>
 | 
			
		||||
		    <artifactId>wtapi</artifactId>
 | 
			
		||||
		    <version>0.5</version>
 | 
			
		||||
		    <version>0.6</version>
 | 
			
		||||
		</dependency>
 | 
			
		||||
	</dependencies>
 | 
			
		||||
	
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,9 +14,9 @@ import com.google.common.base.Charsets;
 | 
			
		|||
import eu.m724.realweather.commands.AdminCommand;
 | 
			
		||||
import eu.m724.realweather.commands.GeoCommand;
 | 
			
		||||
import eu.m724.realweather.commands.LocalTimeCommand;
 | 
			
		||||
import eu.m724.realweather.exception.UserError;
 | 
			
		||||
import eu.m724.realweather.mapper.Mapper;
 | 
			
		||||
import eu.m724.realweather.mapper.MapperConfig;
 | 
			
		||||
import eu.m724.realweather.object.UserException;
 | 
			
		||||
import eu.m724.realweather.thunder.ThunderConfig;
 | 
			
		||||
import eu.m724.realweather.thunder.ThunderMaster;
 | 
			
		||||
import eu.m724.realweather.time.TimeConfig;
 | 
			
		||||
| 
						 | 
				
			
			@ -26,6 +26,7 @@ import eu.m724.realweather.updater.Updater;
 | 
			
		|||
import eu.m724.realweather.updater.UpdaterConfig;
 | 
			
		||||
import eu.m724.realweather.weather.WeatherConfig;
 | 
			
		||||
import eu.m724.realweather.weather.WeatherMaster;
 | 
			
		||||
import eu.m724.wtapi.provider.exception.NoSuchProviderException;
 | 
			
		||||
import eu.m724.wtapi.provider.exception.ProviderException;
 | 
			
		||||
 | 
			
		||||
public class RealWeatherPlugin extends JavaPlugin {
 | 
			
		||||
| 
						 | 
				
			
			@ -118,7 +119,7 @@ public class RealWeatherPlugin extends JavaPlugin {
 | 
			
		|||
			GlobalConstants.updaterConfig = UpdaterConfig.fromConfiguration(configuration.getConfigurationSection("updater"));
 | 
			
		||||
			updater = new Updater(GlobalConstants.updaterConfig);
 | 
			
		||||
			updater.init();
 | 
			
		||||
		} catch (UserException e) {
 | 
			
		||||
		} catch (UserError | NoSuchProviderException e) {
 | 
			
		||||
			logger.severe("There are errors in your config:");
 | 
			
		||||
			logger.severe(e.getMessage());
 | 
			
		||||
			
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										11
									
								
								src/main/java/eu/m724/realweather/exception/UserError.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/main/java/eu/m724/realweather/exception/UserError.java
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
package eu.m724.realweather.exception;
 | 
			
		||||
 | 
			
		||||
public class UserError extends Error {
 | 
			
		||||
 | 
			
		||||
	private static final long serialVersionUID = 7152429719832602384L;
 | 
			
		||||
	
 | 
			
		||||
	public UserError(String message) {
 | 
			
		||||
		super(message);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,11 +0,0 @@
 | 
			
		|||
package eu.m724.realweather.object;
 | 
			
		||||
 | 
			
		||||
public class UserException extends Exception {
 | 
			
		||||
 | 
			
		||||
	private static final long serialVersionUID = 6850666306511891275L;
 | 
			
		||||
	
 | 
			
		||||
	public UserException(String message) {
 | 
			
		||||
		super(message);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -4,10 +4,10 @@ import org.bukkit.plugin.Plugin;
 | 
			
		|||
 | 
			
		||||
import eu.m724.realweather.DebugLogger;
 | 
			
		||||
import eu.m724.realweather.GlobalConstants;
 | 
			
		||||
import eu.m724.realweather.object.UserException;
 | 
			
		||||
import eu.m724.wtapi.provider.Providers;
 | 
			
		||||
import eu.m724.wtapi.provider.exception.NoSuchProviderException;
 | 
			
		||||
import eu.m724.wtapi.provider.exception.ProviderException;
 | 
			
		||||
import eu.m724.wtapi.thunder.ThunderProvider;
 | 
			
		||||
import eu.m724.wtapi.thunder.impl.blitzortung.BlitzortungProvider;
 | 
			
		||||
import eu.m724.wtapi.provider.thunder.ThunderProvider;
 | 
			
		||||
 | 
			
		||||
public class ThunderMaster {
 | 
			
		||||
	private ThunderConfig config;
 | 
			
		||||
| 
						 | 
				
			
			@ -22,18 +22,14 @@ public class ThunderMaster {
 | 
			
		|||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * initializes, tests and starts
 | 
			
		||||
	 * @throws UserException config issue
 | 
			
		||||
	 * @throws ProviderException if provider initialization failed
 | 
			
		||||
	 * @throws NoSuchProviderException config issue
 | 
			
		||||
	 */
 | 
			
		||||
	public void init() throws UserException, ProviderException {
 | 
			
		||||
	public void init() throws ProviderException, NoSuchProviderException {
 | 
			
		||||
		if (!config.enabled)
 | 
			
		||||
			return;
 | 
			
		||||
		
 | 
			
		||||
		provider = createProvider();
 | 
			
		||||
		
 | 
			
		||||
		if (provider == null)
 | 
			
		||||
			throw new UserException("Invalid provider: " + config.provider);
 | 
			
		||||
		
 | 
			
		||||
		provider = Providers.getThunderProvider(config.provider, null);
 | 
			
		||||
		provider.init();
 | 
			
		||||
		
 | 
			
		||||
		thunderTask = new ThunderTask(provider);
 | 
			
		||||
| 
						 | 
				
			
			@ -43,15 +39,6 @@ public class ThunderMaster {
 | 
			
		|||
		DebugLogger.info("thunder loaded", 1);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private ThunderProvider createProvider() {
 | 
			
		||||
		switch (config.provider) {
 | 
			
		||||
		case "blitzortung":
 | 
			
		||||
			return new BlitzortungProvider();
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public long getLatency() {
 | 
			
		||||
		return provider.getLatency();
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,8 +8,8 @@ import org.bukkit.scheduler.BukkitRunnable;
 | 
			
		|||
import eu.m724.realweather.DebugLogger;
 | 
			
		||||
import eu.m724.realweather.GlobalConstants;
 | 
			
		||||
import eu.m724.realweather.mapper.Mapper;
 | 
			
		||||
import eu.m724.wtapi.thunder.ThunderProvider;
 | 
			
		||||
import eu.m724.wtapi.thunder.impl.blitzortung.TimedStrike;
 | 
			
		||||
import eu.m724.wtapi.provider.thunder.ThunderProvider;
 | 
			
		||||
import eu.m724.wtapi.provider.thunder.impl.blitzortung.TimedStrike;
 | 
			
		||||
 | 
			
		||||
class ThunderTask extends BukkitRunnable {
 | 
			
		||||
	private ThunderProvider thunderProvider;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,7 +6,6 @@ import org.bukkit.plugin.Plugin;
 | 
			
		|||
import eu.m724.realweather.DebugLogger;
 | 
			
		||||
import eu.m724.realweather.GlobalConstants;
 | 
			
		||||
import eu.m724.realweather.mapper.Mapper;
 | 
			
		||||
import eu.m724.realweather.object.UserException;
 | 
			
		||||
 | 
			
		||||
public class TimeMaster {
 | 
			
		||||
	private TimeConfig timeConfig;
 | 
			
		||||
| 
						 | 
				
			
			@ -17,15 +16,11 @@ public class TimeMaster {
 | 
			
		|||
		this.timeConfig = timeConfig;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * initializes, tests and starts
 | 
			
		||||
	 * @throws UserException config issue
 | 
			
		||||
	 */
 | 
			
		||||
	public void init() throws UserException {
 | 
			
		||||
	
 | 
			
		||||
	public void init() {
 | 
			
		||||
		if (!timeConfig.enabled)
 | 
			
		||||
			return;
 | 
			
		||||
		
 | 
			
		||||
		
 | 
			
		||||
		long period = (long) (72 / timeConfig.scale);
 | 
			
		||||
		
 | 
			
		||||
		if (timeConfig.scale * Math.floor(period) != 72.0) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,7 +17,7 @@ import org.bukkit.plugin.Plugin;
 | 
			
		|||
import org.bukkit.scheduler.BukkitRunnable;
 | 
			
		||||
import eu.m724.realweather.DebugLogger;
 | 
			
		||||
import eu.m724.realweather.GlobalConstants;
 | 
			
		||||
import eu.m724.realweather.object.UserException;
 | 
			
		||||
import eu.m724.realweather.exception.UserError;
 | 
			
		||||
import eu.m724.realweather.updater.metadata.MetadataRetriever;
 | 
			
		||||
import eu.m724.realweather.updater.metadata.MetadataServerException;
 | 
			
		||||
import eu.m724.realweather.updater.metadata.VersionMetadata;
 | 
			
		||||
| 
						 | 
				
			
			@ -50,13 +50,13 @@ public class Updater extends BukkitRunnable implements Listener {
 | 
			
		|||
		this.metadataRetriever = new MetadataRetriever(plugin, updaterConfig.channel);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void init() throws UserException {
 | 
			
		||||
	public void init() {
 | 
			
		||||
		try {
 | 
			
		||||
			DebugLogger.info("probing for channels...", 1);
 | 
			
		||||
			
 | 
			
		||||
			List<String> channels = metadataRetriever.getChannels().join();
 | 
			
		||||
			if (!channels.contains(updaterConfig.channel)) {
 | 
			
		||||
				throw new UserException(
 | 
			
		||||
				throw new UserError( // TODO replace this with its own error / exception?
 | 
			
		||||
						"Invalid channel: %s. Valid ones are: %s"
 | 
			
		||||
						.formatted(updaterConfig.channel, String.join(", ", channels))); // WHY DID NOBODY TELL ME ABOUT .formatted
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,7 +19,7 @@ import eu.m724.realweather.mapper.Mapper;
 | 
			
		|||
import eu.m724.realweather.weather.event.AsyncWeatherUpdateEvent;
 | 
			
		||||
import eu.m724.wtapi.object.Coordinates;
 | 
			
		||||
import eu.m724.wtapi.object.Weather;
 | 
			
		||||
import eu.m724.wtapi.provider.WeatherProvider;
 | 
			
		||||
import eu.m724.wtapi.provider.weather.WeatherProvider;
 | 
			
		||||
 | 
			
		||||
public class AsyncWeatherRetriever extends BukkitRunnable implements Listener { // TODO split this
 | 
			
		||||
	private WeatherProvider weatherProvider;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,10 +6,10 @@ import org.bukkit.plugin.Plugin;
 | 
			
		|||
import eu.m724.realweather.DebugLogger;
 | 
			
		||||
import eu.m724.realweather.GlobalConstants;
 | 
			
		||||
import eu.m724.realweather.mapper.Mapper;
 | 
			
		||||
import eu.m724.realweather.object.UserException;
 | 
			
		||||
import eu.m724.wtapi.provider.WeatherProvider;
 | 
			
		||||
import eu.m724.wtapi.provider.Providers;
 | 
			
		||||
import eu.m724.wtapi.provider.exception.NoSuchProviderException;
 | 
			
		||||
import eu.m724.wtapi.provider.exception.ProviderException;
 | 
			
		||||
import eu.m724.wtapi.provider.impl.openweathermap.OpenWeatherMapProvider;
 | 
			
		||||
import eu.m724.wtapi.provider.weather.WeatherProvider;
 | 
			
		||||
 | 
			
		||||
public class WeatherMaster {
 | 
			
		||||
	private WeatherConfig config;
 | 
			
		||||
| 
						 | 
				
			
			@ -23,18 +23,14 @@ public class WeatherMaster {
 | 
			
		|||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * initializes, tests and starts
 | 
			
		||||
	 * @throws UserException config issue
 | 
			
		||||
	 * @throws ProviderException if provider initialization failed
 | 
			
		||||
	 * @throws NoSuchProviderException config issue
 | 
			
		||||
	 */
 | 
			
		||||
	public void init() throws UserException, ProviderException {
 | 
			
		||||
	public void init() throws ProviderException, NoSuchProviderException {
 | 
			
		||||
		if (!config.enabled)
 | 
			
		||||
			return;
 | 
			
		||||
		
 | 
			
		||||
		provider = createProvider();
 | 
			
		||||
		
 | 
			
		||||
		if (provider == null)
 | 
			
		||||
			throw new UserException("Invalid provider: " + config.provider);
 | 
			
		||||
		
 | 
			
		||||
		provider = Providers.getWeatherProvider(config.provider, config.apiKey);
 | 
			
		||||
		provider.init();
 | 
			
		||||
		
 | 
			
		||||
		AsyncWeatherRetriever retriever = new AsyncWeatherRetriever(provider);
 | 
			
		||||
| 
						 | 
				
			
			@ -46,13 +42,4 @@ public class WeatherMaster {
 | 
			
		|||
		
 | 
			
		||||
		DebugLogger.info("weather loaded", 1);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private WeatherProvider createProvider() {
 | 
			
		||||
		switch (config.provider) {
 | 
			
		||||
		case "openweathermap":
 | 
			
		||||
			return new OpenWeatherMapProvider(config.apiKey);
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue