realweather/src/main/java/eu/m724/realweather/weather/WeatherMaster.java
Minecon724 aae479740a
some refactoring I don't remember it was yesterday
commiting just in case because I'm about to do something risky
2024-06-22 09:31:21 +02:00

45 lines
1.5 KiB
Java

package eu.m724.realweather.weather;
import org.bukkit.GameRule;
import org.bukkit.plugin.Plugin;
import eu.m724.realweather.DebugLogger;
import eu.m724.realweather.GlobalConstants;
import eu.m724.realweather.mapper.Mapper;
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.weather.WeatherProvider;
public class WeatherMaster {
private WeatherConfig config;
private WeatherProvider provider;
private Mapper mapper = GlobalConstants.getMapper();
private Plugin plugin = GlobalConstants.getPlugin();
public WeatherMaster(WeatherConfig config) {
this.config = config;
}
/**
* initializes, tests and starts
* @throws ProviderException if provider initialization failed
* @throws NoSuchProviderException config issue
*/
public void init() throws ProviderException, NoSuchProviderException {
if (!config.enabled)
return;
provider = Providers.getWeatherProvider(config.provider, config.apiKey);
provider.init();
AsyncWeatherRetriever retriever = new AsyncWeatherRetriever(provider);
retriever.runTaskAsynchronously(plugin);
plugin.getServer().getPluginManager().registerEvents(retriever, plugin);
mapper.registerWorldLoadConsumer(world -> world.setGameRule(GameRule.DO_WEATHER_CYCLE, false));
mapper.registerWorldUnloadConsumer(world -> world.setGameRule(GameRule.DO_WEATHER_CYCLE, true));
DebugLogger.info("weather loaded", 1);
}
}