From 883ce366d197f287d432ec4d24bf67f4ce6d64dd Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Mon, 3 Jun 2024 16:33:06 +0200 Subject: [PATCH] work it is monday and it means SCHOOL yay finally I don't have time --- dependency-reduced-pom.xml | 10 +++- pom.xml | 58 +++++++++++-------- .../eu/m724/realweather/GlobalConstants.java | 5 ++ ...veTask.java => AsyncWeatherRetriever.java} | 39 ++++++++++--- .../weather/DynamicWeatherApplier.java | 3 +- .../weather/PlayerWeatherDirectory.java | 15 +++++ .../realweather/weather/WeatherMaster.java | 8 ++- 7 files changed, 102 insertions(+), 36 deletions(-) rename src/main/java/eu/m724/realweather/weather/{AsyncWeatherRetrieveTask.java => AsyncWeatherRetriever.java} (70%) create mode 100644 src/main/java/eu/m724/realweather/weather/PlayerWeatherDirectory.java diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 4aaa9e1..b7fd1c3 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -22,13 +22,21 @@ shade - true eu.m724:wtapi org.java-websocket:Java-WebSocket + + + eu.m724:* + + META-INF/MANIFEST.MF + + + + true diff --git a/pom.xml b/pom.xml index 2f0eab6..6c61ef6 100644 --- a/pom.xml +++ b/pom.xml @@ -34,37 +34,45 @@ - + src/main/resources true - - - org.apache.maven.plugins - maven-shade-plugin - 3.6.0 - - - package - - shade - - - true - - - eu.m724:wtapi - org.java-websocket:Java-WebSocket - - - - - - - + + + org.apache.maven.plugins + maven-shade-plugin + 3.6.0 + + + package + + shade + + + + + eu.m724:wtapi + org.java-websocket:Java-WebSocket + + + + + eu.m724:* + + META-INF/MANIFEST.MF + + + + true + + + + + \ No newline at end of file diff --git a/src/main/java/eu/m724/realweather/GlobalConstants.java b/src/main/java/eu/m724/realweather/GlobalConstants.java index bb4a5ba..abbd9fe 100644 --- a/src/main/java/eu/m724/realweather/GlobalConstants.java +++ b/src/main/java/eu/m724/realweather/GlobalConstants.java @@ -5,6 +5,7 @@ import org.bukkit.plugin.Plugin; import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.thunder.ThunderConfig; import eu.m724.realweather.time.TimeConfig; +import eu.m724.realweather.weather.PlayerWeatherDirectory; import eu.m724.realweather.weather.WeatherConfig; public class GlobalConstants { @@ -13,6 +14,7 @@ public class GlobalConstants { static ThunderConfig thunderConfig; static Mapper mapper; static Plugin plugin; + static PlayerWeatherDirectory playerWeatherDirectory; public static WeatherConfig getWeatherConfig() { return weatherConfig; @@ -29,5 +31,8 @@ public class GlobalConstants { public static Plugin getPlugin() { return plugin; } + public static PlayerWeatherDirectory getPlayerWeatherDirectory() { + return playerWeatherDirectory; + } } diff --git a/src/main/java/eu/m724/realweather/weather/AsyncWeatherRetrieveTask.java b/src/main/java/eu/m724/realweather/weather/AsyncWeatherRetriever.java similarity index 70% rename from src/main/java/eu/m724/realweather/weather/AsyncWeatherRetrieveTask.java rename to src/main/java/eu/m724/realweather/weather/AsyncWeatherRetriever.java index 1e16a8a..c47f9b3 100644 --- a/src/main/java/eu/m724/realweather/weather/AsyncWeatherRetrieveTask.java +++ b/src/main/java/eu/m724/realweather/weather/AsyncWeatherRetriever.java @@ -8,6 +8,9 @@ import java.util.stream.Collectors; import org.bukkit.Server; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; @@ -19,21 +22,22 @@ import eu.m724.wtapi.object.Coordinates; import eu.m724.wtapi.object.Weather; import eu.m724.wtapi.provider.WeatherProvider; -public class AsyncWeatherRetrieveTask extends BukkitRunnable { +public class AsyncWeatherRetriever extends BukkitRunnable implements Listener { // TODO split this private WeatherProvider weatherProvider; - private boolean dynamic; // TODO handle config some other way - + + private boolean dynamic = GlobalConstants.getWeatherConfig().dynamic; private Mapper mapper = GlobalConstants.getMapper(); private Plugin plugin = GlobalConstants.getPlugin(); private Server server = plugin.getServer(); + private PlayerWeatherDirectory playerWeatherDirectory = GlobalConstants.getPlayerWeatherDirectory(); - public AsyncWeatherRetrieveTask(WeatherProvider weatherProvider, boolean dynamic) { + public AsyncWeatherRetriever(WeatherProvider weatherProvider) { this.weatherProvider = weatherProvider; - this.dynamic = dynamic; } @Override public void run() { + DebugLogger.info("Weather retrieval", 3); long delay = 6000; if (dynamic) { @@ -62,14 +66,13 @@ public class AsyncWeatherRetrieveTask extends BukkitRunnable { Player player = players.get(i); Weather weather = weathers[i]; + playerWeatherDirectory.weathers.put(player, weather); + AsyncWeatherUpdateEvent event = new AsyncWeatherUpdateEvent(player, weather); server.getPluginManager().callEvent(event); - // the event applies weather } - /*DynamicWeatherApplier applier = new DynamicWeatherApplier(players, ); - applier.runTask(plugin);*/ } catch (CompletionException e) { // TODO handle finer exceptions DebugLogger.info("failed to retrieve weather data", 1); @@ -100,7 +103,27 @@ public class AsyncWeatherRetrieveTask extends BukkitRunnable { } + DebugLogger.info("Scheduling restart with delay %d", 3, delay); runTaskLaterAsynchronously(plugin, delay); } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + Player player = event.getPlayer(); + + Coordinates coordinates = mapper.locationToCoordinates(player.getLocation()); + + CompletableFuture weatherFuture = weatherProvider.getWeather(coordinates); + DebugLogger.info("A player joined so I requested weather", 3); + + weatherFuture.thenAccept(weather -> { + playerWeatherDirectory.weathers.put(player, weather); + + AsyncWeatherUpdateEvent weatherEvent = + new AsyncWeatherUpdateEvent(player, weather); + + server.getPluginManager().callEvent(weatherEvent); + }); // TODO is this really safe? + } } diff --git a/src/main/java/eu/m724/realweather/weather/DynamicWeatherApplier.java b/src/main/java/eu/m724/realweather/weather/DynamicWeatherApplier.java index a88b83e..cffb570 100644 --- a/src/main/java/eu/m724/realweather/weather/DynamicWeatherApplier.java +++ b/src/main/java/eu/m724/realweather/weather/DynamicWeatherApplier.java @@ -12,6 +12,7 @@ import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; +// TODO this class is currently unused and it will probably be removed or replaced public class DynamicWeatherApplier extends BukkitRunnable { private List players; private Weather[] weathers; @@ -43,7 +44,7 @@ public class DynamicWeatherApplier extends BukkitRunnable { player.setPlayerWeather(WeatherType.DOWNFALL); } else { player.setPlayerWeather(WeatherType.CLEAR); - } // TODO + } } } diff --git a/src/main/java/eu/m724/realweather/weather/PlayerWeatherDirectory.java b/src/main/java/eu/m724/realweather/weather/PlayerWeatherDirectory.java new file mode 100644 index 0000000..852cade --- /dev/null +++ b/src/main/java/eu/m724/realweather/weather/PlayerWeatherDirectory.java @@ -0,0 +1,15 @@ +package eu.m724.realweather.weather; + +import java.util.HashMap; + +import org.bukkit.entity.Player; + +import eu.m724.wtapi.object.Weather; + +public class PlayerWeatherDirectory { + HashMap weathers = new HashMap<>(); + + public Weather getWeather(Player player) { + return weathers.get(player); // TODO concurrent exception? + } +} diff --git a/src/main/java/eu/m724/realweather/weather/WeatherMaster.java b/src/main/java/eu/m724/realweather/weather/WeatherMaster.java index 5e83151..6e92a2d 100644 --- a/src/main/java/eu/m724/realweather/weather/WeatherMaster.java +++ b/src/main/java/eu/m724/realweather/weather/WeatherMaster.java @@ -1,5 +1,7 @@ package eu.m724.realweather.weather; +import org.bukkit.plugin.Plugin; + import eu.m724.realweather.DebugLogger; import eu.m724.realweather.GlobalConstants; import eu.m724.realweather.mapper.Mapper; @@ -12,6 +14,7 @@ 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; @@ -33,7 +36,10 @@ public class WeatherMaster { provider.init(); - // TODO start task + AsyncWeatherRetriever retriever = new AsyncWeatherRetriever(provider); + retriever.runTaskAsynchronously(plugin); + plugin.getServer().getPluginManager().registerEvents(retriever, plugin); + DebugLogger.info("weather loaded", 1); }