Split into two events
This commit is contained in:
parent
505d8df3d6
commit
a33022380a
7 changed files with 115 additions and 84 deletions
|
@ -1,6 +1,6 @@
|
|||
# realweather
|
||||
|
||||
For MC 1.19.4+ and Java 21+
|
||||
For MC 1.16.5+ and Java 21+
|
||||
|
||||
### Building
|
||||
To compile, clone this repo and `mvn clean package`. \
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package eu.m724.realweather.api.weather;
|
||||
|
||||
import eu.m724.wtapi.object.Weather;
|
||||
|
||||
/**
|
||||
* Called when weather is <em>updated</em> for the <strong>server</strong>
|
||||
* <br>
|
||||
* This is only used on <em>static</em> mode. For the dynamic mode counterpart, see {@link AsyncPlayerWeatherUpdateEvent}
|
||||
*/
|
||||
public class AsyncGlobalWeatherUpdateEvent extends AsyncWeatherUpdateEvent {
|
||||
public AsyncGlobalWeatherUpdateEvent(Weather weather) {
|
||||
super(weather);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package eu.m724.realweather.api.weather;
|
||||
|
||||
import eu.m724.wtapi.object.Weather;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
/**
|
||||
* Called when weather is <em>updated</em> for a player
|
||||
* <br>
|
||||
* This is only used on <em>dynamic</em> mode. For the static mode counterpart, see {@link AsyncGlobalWeatherUpdateEvent}
|
||||
*/
|
||||
public class AsyncPlayerWeatherUpdateEvent extends AsyncWeatherUpdateEvent {
|
||||
private static final HandlerList HANDLERS = new HandlerList();
|
||||
|
||||
private final Player player;
|
||||
|
||||
public AsyncPlayerWeatherUpdateEvent(Player player, Weather weather) {
|
||||
super(weather);
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The player to update weather for
|
||||
*/
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
}
|
|
@ -1,65 +1,50 @@
|
|||
package eu.m724.realweather.api.weather;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import eu.m724.wtapi.object.Weather;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
import eu.m724.wtapi.object.Weather;
|
||||
class AsyncWeatherUpdateEvent extends Event implements Cancellable {
|
||||
private static final HandlerList HANDLERS = new HandlerList();
|
||||
|
||||
/**
|
||||
* Fired when a weather state is retrieved<br>
|
||||
* It doesn't mean the weather has changed, just that we retrieved the state<br>
|
||||
*/
|
||||
public class AsyncWeatherUpdateEvent extends Event implements Cancellable {
|
||||
private static final HandlerList HANDLERS = new HandlerList();
|
||||
|
||||
private final Player player;
|
||||
private final Weather weather;
|
||||
private final Weather weather;
|
||||
|
||||
private boolean cancelled;
|
||||
private boolean cancelled;
|
||||
|
||||
public AsyncWeatherUpdateEvent(Player player, Weather weather) {
|
||||
super(true);
|
||||
this.player = player;
|
||||
this.weather = weather;
|
||||
}
|
||||
public AsyncWeatherUpdateEvent(Weather weather) {
|
||||
super(true);
|
||||
this.weather = weather;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a player that the weather is for, null if worldwide (static mode)
|
||||
*/
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
/**
|
||||
* @return The new weather
|
||||
*/
|
||||
public Weather getWeather() {
|
||||
return weather;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the weather state that was just changed
|
||||
*/
|
||||
public Weather getWeather() {
|
||||
return weather;
|
||||
}
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS;
|
||||
}
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return HANDLERS;
|
||||
}
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel weather change<br>
|
||||
* It will only cancel changing the actual weather by the plugin, not retrieving and caching it
|
||||
* @param cancelled to cancel or not
|
||||
*/
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
/**
|
||||
* Cancel the weather change
|
||||
*
|
||||
* @param cancelled Whether to cancel
|
||||
*/
|
||||
@Override
|
||||
public void setCancelled(boolean cancelled) {
|
||||
this.cancelled = cancelled;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.time.ZoneOffset;
|
|||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import eu.m724.realweather.api.weather.AsyncPlayerWeatherUpdateEvent;
|
||||
import eu.m724.wtapi.provider.weather.WeatherQueryResult;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
|
@ -16,7 +17,6 @@ import org.bukkit.scheduler.BukkitRunnable;
|
|||
import eu.m724.realweather.DebugLogger;
|
||||
import eu.m724.realweather.GlobalConstants;
|
||||
import eu.m724.realweather.mapper.Mapper;
|
||||
import eu.m724.realweather.api.weather.AsyncWeatherUpdateEvent;
|
||||
import eu.m724.wtapi.object.Coordinates;
|
||||
import eu.m724.wtapi.object.Weather;
|
||||
import eu.m724.wtapi.provider.weather.WeatherProvider;
|
||||
|
@ -129,8 +129,8 @@ public class DynamicWeatherRetriever extends BukkitRunnable implements Listener
|
|||
for (Player player : coordinates.coordinatesPlayersMap().get(coordinatesArray[i])) {
|
||||
playerWeatherCache.put(player, weather, weather.timestamp().toEpochSecond(ZoneOffset.UTC));
|
||||
|
||||
AsyncWeatherUpdateEvent event =
|
||||
new AsyncWeatherUpdateEvent(player, weather);
|
||||
AsyncPlayerWeatherUpdateEvent event =
|
||||
new AsyncPlayerWeatherUpdateEvent(player, weather);
|
||||
|
||||
server.getPluginManager().callEvent(event);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package eu.m724.realweather.weather;
|
|||
|
||||
import eu.m724.realweather.DebugLogger;
|
||||
import eu.m724.realweather.GlobalConstants;
|
||||
import eu.m724.realweather.api.weather.AsyncWeatherUpdateEvent;
|
||||
import eu.m724.realweather.api.weather.AsyncGlobalWeatherUpdateEvent;
|
||||
import eu.m724.realweather.mapper.Mapper;
|
||||
import eu.m724.wtapi.object.Coordinates;
|
||||
import eu.m724.wtapi.object.Weather;
|
||||
|
@ -38,8 +38,8 @@ public class StaticWeatherRetriever extends BukkitRunnable {
|
|||
|
||||
Weather weather = result.weathers()[0];
|
||||
|
||||
AsyncWeatherUpdateEvent event =
|
||||
new AsyncWeatherUpdateEvent(null, weather);
|
||||
AsyncGlobalWeatherUpdateEvent event =
|
||||
new AsyncGlobalWeatherUpdateEvent(weather);
|
||||
|
||||
plugin.getServer().getPluginManager().callEvent(event);
|
||||
|
||||
|
|
|
@ -2,7 +2,8 @@ package eu.m724.realweather.weather;
|
|||
|
||||
import eu.m724.realweather.DebugLogger;
|
||||
import eu.m724.realweather.GlobalConstants;
|
||||
import eu.m724.realweather.api.weather.AsyncWeatherUpdateEvent;
|
||||
import eu.m724.realweather.api.weather.AsyncGlobalWeatherUpdateEvent;
|
||||
import eu.m724.realweather.api.weather.AsyncPlayerWeatherUpdateEvent;
|
||||
import eu.m724.realweather.mapper.Mapper;
|
||||
import eu.m724.wtapi.object.Weather;
|
||||
import org.bukkit.WeatherType;
|
||||
|
@ -16,36 +17,39 @@ public class WeatherChanger implements Listener {
|
|||
private final Mapper mapper = GlobalConstants.getMapper();
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onWeatherUpdate(AsyncWeatherUpdateEvent event) {
|
||||
public void onGlobalWeatherUpdate(AsyncGlobalWeatherUpdateEvent event) {
|
||||
Weather weather = event.getWeather();
|
||||
|
||||
DebugLogger.info("Changing weather static", 3);
|
||||
mapper.getWorlds().forEach(w -> {
|
||||
DebugLogger.info("Changing weather static in world %s", 2, w.getName());
|
||||
if (weather.thundering()) {
|
||||
w.setClearWeatherDuration(0);
|
||||
w.setWeatherDuration(120000);
|
||||
w.setThunderDuration(120000);
|
||||
} else if (weather.raining() || weather.snowing()) {
|
||||
w.setClearWeatherDuration(0);
|
||||
w.setWeatherDuration(120000);
|
||||
w.setThunderDuration(0);
|
||||
} else {
|
||||
w.setClearWeatherDuration(120000);
|
||||
w.setWeatherDuration(0);
|
||||
w.setThunderDuration(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerWeatherUpdate(AsyncPlayerWeatherUpdateEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Weather weather = event.getWeather();
|
||||
|
||||
if (player != null) { // dynamic mode
|
||||
DebugLogger.info("Changing weather for player %s", 2, player.getName());
|
||||
DebugLogger.info("Changing weather for player %s", 2, player.getName());
|
||||
|
||||
if (weather.thundering() || weather.snowing() || weather.raining()) {
|
||||
player.setPlayerWeather(WeatherType.DOWNFALL);
|
||||
} else {
|
||||
player.setPlayerWeather(WeatherType.CLEAR);
|
||||
}
|
||||
} else { // static mode
|
||||
DebugLogger.info("Changing weather static", 3);
|
||||
mapper.getWorlds().forEach(w -> {
|
||||
DebugLogger.info("Changing weather static in world %s", 2, w.getName());
|
||||
if (weather.thundering()) {
|
||||
w.setClearWeatherDuration(0);
|
||||
w.setWeatherDuration(120000);
|
||||
w.setThunderDuration(120000);
|
||||
} else if (weather.raining() || weather.snowing()) {
|
||||
w.setClearWeatherDuration(0);
|
||||
w.setWeatherDuration(120000);
|
||||
w.setThunderDuration(0);
|
||||
} else {
|
||||
w.setClearWeatherDuration(120000);
|
||||
w.setWeatherDuration(0);
|
||||
w.setThunderDuration(0);
|
||||
}
|
||||
});
|
||||
if (weather.thundering() || weather.snowing() || weather.raining()) {
|
||||
player.setPlayerWeather(WeatherType.DOWNFALL);
|
||||
} else {
|
||||
player.setPlayerWeather(WeatherType.CLEAR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue