it is monday and it means SCHOOL yay finally I don't have time
This commit is contained in:
Minecon724 2024-06-03 16:33:06 +02:00
parent af0972f69f
commit 883ce366d1
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
7 changed files with 102 additions and 36 deletions

View file

@ -22,13 +22,21 @@
<goal>shade</goal> <goal>shade</goal>
</goals> </goals>
<configuration> <configuration>
<minimizeJar>true</minimizeJar>
<artifactSet> <artifactSet>
<includes> <includes>
<include>eu.m724:wtapi</include> <include>eu.m724:wtapi</include>
<include>org.java-websocket:Java-WebSocket</include> <include>org.java-websocket:Java-WebSocket</include>
</includes> </includes>
</artifactSet> </artifactSet>
<filters>
<filter>
<artifact>eu.m724:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
</filters>
<minimizeJar>true</minimizeJar>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>

58
pom.xml
View file

@ -34,37 +34,45 @@
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<resources> <resources>
<resource> <resource>
<directory>src/main/resources</directory> <directory>src/main/resources</directory>
<filtering>true</filtering> <filtering>true</filtering>
</resource> </resource>
</resources> </resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version> <version>3.6.0</version>
<executions> <executions>
<execution> <execution>
<phase>package</phase> <phase>package</phase>
<goals> <goals>
<goal>shade</goal> <goal>shade</goal>
</goals> </goals>
<configuration> <configuration>
<minimizeJar>true</minimizeJar> <artifactSet>
<artifactSet> <includes>
<includes> <include>eu.m724:wtapi</include>
<include>eu.m724:wtapi</include> <include>org.java-websocket:Java-WebSocket</include>
<include>org.java-websocket:Java-WebSocket</include> </includes>
</includes> </artifactSet>
</artifactSet> <filters>
</configuration> <filter>
</execution> <artifact>eu.m724:*</artifact>
</executions> <excludes>
</plugin> <exclude>META-INF/MANIFEST.MF</exclude>
</plugins> </excludes>
</filter>
</filters>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build> </build>
</project> </project>

View file

@ -5,6 +5,7 @@ import org.bukkit.plugin.Plugin;
import eu.m724.realweather.mapper.Mapper; import eu.m724.realweather.mapper.Mapper;
import eu.m724.realweather.thunder.ThunderConfig; import eu.m724.realweather.thunder.ThunderConfig;
import eu.m724.realweather.time.TimeConfig; import eu.m724.realweather.time.TimeConfig;
import eu.m724.realweather.weather.PlayerWeatherDirectory;
import eu.m724.realweather.weather.WeatherConfig; import eu.m724.realweather.weather.WeatherConfig;
public class GlobalConstants { public class GlobalConstants {
@ -13,6 +14,7 @@ public class GlobalConstants {
static ThunderConfig thunderConfig; static ThunderConfig thunderConfig;
static Mapper mapper; static Mapper mapper;
static Plugin plugin; static Plugin plugin;
static PlayerWeatherDirectory playerWeatherDirectory;
public static WeatherConfig getWeatherConfig() { public static WeatherConfig getWeatherConfig() {
return weatherConfig; return weatherConfig;
@ -29,5 +31,8 @@ public class GlobalConstants {
public static Plugin getPlugin() { public static Plugin getPlugin() {
return plugin; return plugin;
} }
public static PlayerWeatherDirectory getPlayerWeatherDirectory() {
return playerWeatherDirectory;
}
} }

View file

@ -8,6 +8,9 @@ import java.util.stream.Collectors;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.entity.Player; 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.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; 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.object.Weather;
import eu.m724.wtapi.provider.WeatherProvider; 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 WeatherProvider weatherProvider;
private boolean dynamic; // TODO handle config some other way
private boolean dynamic = GlobalConstants.getWeatherConfig().dynamic;
private Mapper mapper = GlobalConstants.getMapper(); private Mapper mapper = GlobalConstants.getMapper();
private Plugin plugin = GlobalConstants.getPlugin(); private Plugin plugin = GlobalConstants.getPlugin();
private Server server = plugin.getServer(); private Server server = plugin.getServer();
private PlayerWeatherDirectory playerWeatherDirectory = GlobalConstants.getPlayerWeatherDirectory();
public AsyncWeatherRetrieveTask(WeatherProvider weatherProvider, boolean dynamic) { public AsyncWeatherRetriever(WeatherProvider weatherProvider) {
this.weatherProvider = weatherProvider; this.weatherProvider = weatherProvider;
this.dynamic = dynamic;
} }
@Override @Override
public void run() { public void run() {
DebugLogger.info("Weather retrieval", 3);
long delay = 6000; long delay = 6000;
if (dynamic) { if (dynamic) {
@ -62,14 +66,13 @@ public class AsyncWeatherRetrieveTask extends BukkitRunnable {
Player player = players.get(i); Player player = players.get(i);
Weather weather = weathers[i]; Weather weather = weathers[i];
playerWeatherDirectory.weathers.put(player, weather);
AsyncWeatherUpdateEvent event = AsyncWeatherUpdateEvent event =
new AsyncWeatherUpdateEvent(player, weather); new AsyncWeatherUpdateEvent(player, weather);
server.getPluginManager().callEvent(event); server.getPluginManager().callEvent(event);
// the event applies weather
} }
/*DynamicWeatherApplier applier = new DynamicWeatherApplier(players, );
applier.runTask(plugin);*/
} catch (CompletionException e) { // TODO handle finer exceptions } catch (CompletionException e) { // TODO handle finer exceptions
DebugLogger.info("failed to retrieve weather data", 1); 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); runTaskLaterAsynchronously(plugin, delay);
} }
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
Coordinates coordinates = mapper.locationToCoordinates(player.getLocation());
CompletableFuture<Weather> 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?
}
} }

View file

@ -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.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent; 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 { public class DynamicWeatherApplier extends BukkitRunnable {
private List<Player> players; private List<Player> players;
private Weather[] weathers; private Weather[] weathers;
@ -43,7 +44,7 @@ public class DynamicWeatherApplier extends BukkitRunnable {
player.setPlayerWeather(WeatherType.DOWNFALL); player.setPlayerWeather(WeatherType.DOWNFALL);
} else { } else {
player.setPlayerWeather(WeatherType.CLEAR); player.setPlayerWeather(WeatherType.CLEAR);
} // TODO }
} }
} }

View file

@ -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<Player, Weather> weathers = new HashMap<>();
public Weather getWeather(Player player) {
return weathers.get(player); // TODO concurrent exception?
}
}

View file

@ -1,5 +1,7 @@
package eu.m724.realweather.weather; package eu.m724.realweather.weather;
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;
@ -12,6 +14,7 @@ public class WeatherMaster {
private WeatherConfig config; private WeatherConfig config;
private WeatherProvider provider; private WeatherProvider provider;
private Mapper mapper = GlobalConstants.getMapper(); private Mapper mapper = GlobalConstants.getMapper();
private Plugin plugin = GlobalConstants.getPlugin();
public WeatherMaster(WeatherConfig config) { public WeatherMaster(WeatherConfig config) {
this.config = config; this.config = config;
@ -33,7 +36,10 @@ public class WeatherMaster {
provider.init(); provider.init();
// TODO start task AsyncWeatherRetriever retriever = new AsyncWeatherRetriever(provider);
retriever.runTaskAsynchronously(plugin);
plugin.getServer().getPluginManager().registerEvents(retriever, plugin);
DebugLogger.info("weather loaded", 1); DebugLogger.info("weather loaded", 1);
} }