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:
Minecon724 2024-06-22 09:31:21 +02:00
parent 96c8777ac2
commit aae479740a
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
10 changed files with 35 additions and 65 deletions

View file

@ -49,7 +49,7 @@
<dependency> <dependency>
<groupId>eu.m724</groupId> <groupId>eu.m724</groupId>
<artifactId>wtapi</artifactId> <artifactId>wtapi</artifactId>
<version>0.5</version> <version>0.6</version>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -14,9 +14,9 @@ import com.google.common.base.Charsets;
import eu.m724.realweather.commands.AdminCommand; import eu.m724.realweather.commands.AdminCommand;
import eu.m724.realweather.commands.GeoCommand; import eu.m724.realweather.commands.GeoCommand;
import eu.m724.realweather.commands.LocalTimeCommand; import eu.m724.realweather.commands.LocalTimeCommand;
import eu.m724.realweather.exception.UserError;
import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.mapper.MapperConfig; import eu.m724.realweather.mapper.MapperConfig;
import eu.m724.realweather.object.UserException;
import eu.m724.realweather.thunder.ThunderConfig; import eu.m724.realweather.thunder.ThunderConfig;
import eu.m724.realweather.thunder.ThunderMaster; import eu.m724.realweather.thunder.ThunderMaster;
import eu.m724.realweather.time.TimeConfig; 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.updater.UpdaterConfig;
import eu.m724.realweather.weather.WeatherConfig; import eu.m724.realweather.weather.WeatherConfig;
import eu.m724.realweather.weather.WeatherMaster; import eu.m724.realweather.weather.WeatherMaster;
import eu.m724.wtapi.provider.exception.NoSuchProviderException;
import eu.m724.wtapi.provider.exception.ProviderException; import eu.m724.wtapi.provider.exception.ProviderException;
public class RealWeatherPlugin extends JavaPlugin { public class RealWeatherPlugin extends JavaPlugin {
@ -118,7 +119,7 @@ public class RealWeatherPlugin extends JavaPlugin {
GlobalConstants.updaterConfig = UpdaterConfig.fromConfiguration(configuration.getConfigurationSection("updater")); GlobalConstants.updaterConfig = UpdaterConfig.fromConfiguration(configuration.getConfigurationSection("updater"));
updater = new Updater(GlobalConstants.updaterConfig); updater = new Updater(GlobalConstants.updaterConfig);
updater.init(); updater.init();
} catch (UserException e) { } catch (UserError | NoSuchProviderException e) {
logger.severe("There are errors in your config:"); logger.severe("There are errors in your config:");
logger.severe(e.getMessage()); logger.severe(e.getMessage());

View 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);
}
}

View file

@ -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);
}
}

View file

@ -4,10 +4,10 @@ import org.bukkit.plugin.Plugin;
import eu.m724.realweather.DebugLogger; import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants; 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.provider.exception.ProviderException;
import eu.m724.wtapi.thunder.ThunderProvider; import eu.m724.wtapi.provider.thunder.ThunderProvider;
import eu.m724.wtapi.thunder.impl.blitzortung.BlitzortungProvider;
public class ThunderMaster { public class ThunderMaster {
private ThunderConfig config; private ThunderConfig config;
@ -22,18 +22,14 @@ public class ThunderMaster {
/** /**
* initializes, tests and starts * initializes, tests and starts
* @throws UserException config issue
* @throws ProviderException if provider initialization failed * @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) if (!config.enabled)
return; return;
provider = createProvider(); provider = Providers.getThunderProvider(config.provider, null);
if (provider == null)
throw new UserException("Invalid provider: " + config.provider);
provider.init(); provider.init();
thunderTask = new ThunderTask(provider); thunderTask = new ThunderTask(provider);
@ -43,15 +39,6 @@ public class ThunderMaster {
DebugLogger.info("thunder loaded", 1); DebugLogger.info("thunder loaded", 1);
} }
private ThunderProvider createProvider() {
switch (config.provider) {
case "blitzortung":
return new BlitzortungProvider();
}
return null;
}
public long getLatency() { public long getLatency() {
return provider.getLatency(); return provider.getLatency();
} }

View file

@ -8,8 +8,8 @@ import org.bukkit.scheduler.BukkitRunnable;
import eu.m724.realweather.DebugLogger; import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants; import eu.m724.realweather.GlobalConstants;
import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.Mapper;
import eu.m724.wtapi.thunder.ThunderProvider; import eu.m724.wtapi.provider.thunder.ThunderProvider;
import eu.m724.wtapi.thunder.impl.blitzortung.TimedStrike; import eu.m724.wtapi.provider.thunder.impl.blitzortung.TimedStrike;
class ThunderTask extends BukkitRunnable { class ThunderTask extends BukkitRunnable {
private ThunderProvider thunderProvider; private ThunderProvider thunderProvider;

View file

@ -6,7 +6,6 @@ import org.bukkit.plugin.Plugin;
import eu.m724.realweather.DebugLogger; import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants; import eu.m724.realweather.GlobalConstants;
import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.object.UserException;
public class TimeMaster { public class TimeMaster {
private TimeConfig timeConfig; private TimeConfig timeConfig;
@ -17,15 +16,11 @@ public class TimeMaster {
this.timeConfig = timeConfig; this.timeConfig = timeConfig;
} }
/**
* initializes, tests and starts public void init() {
* @throws UserException config issue
*/
public void init() throws UserException {
if (!timeConfig.enabled) if (!timeConfig.enabled)
return; return;
long period = (long) (72 / timeConfig.scale); long period = (long) (72 / timeConfig.scale);
if (timeConfig.scale * Math.floor(period) != 72.0) { if (timeConfig.scale * Math.floor(period) != 72.0) {

View file

@ -17,7 +17,7 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import eu.m724.realweather.DebugLogger; import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants; 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.MetadataRetriever;
import eu.m724.realweather.updater.metadata.MetadataServerException; import eu.m724.realweather.updater.metadata.MetadataServerException;
import eu.m724.realweather.updater.metadata.VersionMetadata; 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); this.metadataRetriever = new MetadataRetriever(plugin, updaterConfig.channel);
} }
public void init() throws UserException { public void init() {
try { try {
DebugLogger.info("probing for channels...", 1); DebugLogger.info("probing for channels...", 1);
List<String> channels = metadataRetriever.getChannels().join(); List<String> channels = metadataRetriever.getChannels().join();
if (!channels.contains(updaterConfig.channel)) { 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" "Invalid channel: %s. Valid ones are: %s"
.formatted(updaterConfig.channel, String.join(", ", channels))); // WHY DID NOBODY TELL ME ABOUT .formatted .formatted(updaterConfig.channel, String.join(", ", channels))); // WHY DID NOBODY TELL ME ABOUT .formatted
} }

View file

@ -19,7 +19,7 @@ import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.weather.event.AsyncWeatherUpdateEvent; import eu.m724.realweather.weather.event.AsyncWeatherUpdateEvent;
import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Coordinates;
import eu.m724.wtapi.object.Weather; 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 public class AsyncWeatherRetriever extends BukkitRunnable implements Listener { // TODO split this
private WeatherProvider weatherProvider; private WeatherProvider weatherProvider;

View file

@ -6,10 +6,10 @@ import org.bukkit.plugin.Plugin;
import eu.m724.realweather.DebugLogger; import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants; import eu.m724.realweather.GlobalConstants;
import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.object.UserException; import eu.m724.wtapi.provider.Providers;
import eu.m724.wtapi.provider.WeatherProvider; import eu.m724.wtapi.provider.exception.NoSuchProviderException;
import eu.m724.wtapi.provider.exception.ProviderException; 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 { public class WeatherMaster {
private WeatherConfig config; private WeatherConfig config;
@ -23,18 +23,14 @@ public class WeatherMaster {
/** /**
* initializes, tests and starts * initializes, tests and starts
* @throws UserException config issue
* @throws ProviderException if provider initialization failed * @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) if (!config.enabled)
return; return;
provider = createProvider(); provider = Providers.getWeatherProvider(config.provider, config.apiKey);
if (provider == null)
throw new UserException("Invalid provider: " + config.provider);
provider.init(); provider.init();
AsyncWeatherRetriever retriever = new AsyncWeatherRetriever(provider); AsyncWeatherRetriever retriever = new AsyncWeatherRetriever(provider);
@ -46,13 +42,4 @@ public class WeatherMaster {
DebugLogger.info("weather loaded", 1); DebugLogger.info("weather loaded", 1);
} }
private WeatherProvider createProvider() {
switch (config.provider) {
case "openweathermap":
return new OpenWeatherMapProvider(config.apiKey);
}
return null;
}
} }