Move weather handling to new wm

This commit is contained in:
Ben Woo 2023-09-04 13:37:29 +08:00
parent 0824108109
commit 0a62081d03
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
2 changed files with 29 additions and 16 deletions

View File

@ -7,10 +7,11 @@
package com.onarandombox.MultiverseCore.listeners;
import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.inject.InjectableListener;
import com.onarandombox.MultiverseCore.worldnew.WorldManager;
import jakarta.inject.Inject;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.weather.ThunderChangeEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
@ -22,10 +23,10 @@ import org.jvnet.hk2.annotations.Service;
@Service
public class MVWeatherListener implements InjectableListener {
private MVWorldManager worldManager;
private final WorldManager worldManager;
@Inject
public MVWeatherListener(MVWorldManager worldManager) {
public MVWeatherListener(WorldManager worldManager) {
this.worldManager = worldManager;
}
@ -35,14 +36,16 @@ public class MVWeatherListener implements InjectableListener {
*/
@EventHandler
public void weatherChange(WeatherChangeEvent event) {
if (event.isCancelled()) {
if (event.isCancelled() || !event.toWeatherState()) {
return;
}
MVWorld world = this.worldManager.getMVWorld(event.getWorld().getName());
if (world != null) {
// If it's going to start raining and we have weather disabled
event.setCancelled((event.toWeatherState() && !world.isWeatherEnabled()));
}
worldManager.getMVWorld(event.getWorld())
.peek((world) -> {
if (!world.getAllowWeather()) {
Logging.fine("Cancelling weather for %s as getAllowWeather is false", world.getName());
event.setCancelled(true);
}
});
}
/**
@ -51,13 +54,15 @@ public class MVWeatherListener implements InjectableListener {
*/
@EventHandler
public void thunderChange(ThunderChangeEvent event) {
if (event.isCancelled()) {
if (event.isCancelled() || !event.toThunderState()) {
return;
}
MVWorld world = this.worldManager.getMVWorld(event.getWorld().getName());
if (world != null) {
// If it's going to start raining and we have weather disabled
event.setCancelled((event.toThunderState() && !world.isWeatherEnabled()));
}
worldManager.getMVWorld(event.getWorld())
.peek((world) -> {
if (!world.getAllowWeather()) {
Logging.fine("Cancelling thunder for %s as getAllowWeather is false", world.getName());
event.setCancelled(true);
}
});
}
}

View File

@ -49,6 +49,14 @@ public class WorldConfigNodes {
public final ConfigNode<Boolean> ALLOW_WEATHER = node(ConfigNode.builder("allow-weather", Boolean.class)
.defaultValue(true)
.name("allow-weather")
.onSetValue((oldValue, newValue) -> {
if (world == null) { return; }
world.getBukkitWorld().peek(world -> {
if (!world.isClearWeather() && !newValue) {
world.setClearWeatherDuration(-1);
}
});
})
.build());
public final ConfigNode<Boolean> AUTO_HEAL = node(ConfigNode.builder("auto-heal", Boolean.class)