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; package com.onarandombox.MultiverseCore.listeners;
import com.onarandombox.MultiverseCore.api.MVWorld; import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.inject.InjectableListener; import com.onarandombox.MultiverseCore.inject.InjectableListener;
import com.onarandombox.MultiverseCore.worldnew.WorldManager;
import jakarta.inject.Inject; import jakarta.inject.Inject;
import org.bukkit.Location;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.weather.ThunderChangeEvent; import org.bukkit.event.weather.ThunderChangeEvent;
import org.bukkit.event.weather.WeatherChangeEvent; import org.bukkit.event.weather.WeatherChangeEvent;
@ -22,10 +23,10 @@ import org.jvnet.hk2.annotations.Service;
@Service @Service
public class MVWeatherListener implements InjectableListener { public class MVWeatherListener implements InjectableListener {
private MVWorldManager worldManager; private final WorldManager worldManager;
@Inject @Inject
public MVWeatherListener(MVWorldManager worldManager) { public MVWeatherListener(WorldManager worldManager) {
this.worldManager = worldManager; this.worldManager = worldManager;
} }
@ -35,14 +36,16 @@ public class MVWeatherListener implements InjectableListener {
*/ */
@EventHandler @EventHandler
public void weatherChange(WeatherChangeEvent event) { public void weatherChange(WeatherChangeEvent event) {
if (event.isCancelled()) { if (event.isCancelled() || !event.toWeatherState()) {
return; return;
} }
MVWorld world = this.worldManager.getMVWorld(event.getWorld().getName()); worldManager.getMVWorld(event.getWorld())
if (world != null) { .peek((world) -> {
// If it's going to start raining and we have weather disabled if (!world.getAllowWeather()) {
event.setCancelled((event.toWeatherState() && !world.isWeatherEnabled())); 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 @EventHandler
public void thunderChange(ThunderChangeEvent event) { public void thunderChange(ThunderChangeEvent event) {
if (event.isCancelled()) { if (event.isCancelled() || !event.toThunderState()) {
return; return;
} }
MVWorld world = this.worldManager.getMVWorld(event.getWorld().getName()); worldManager.getMVWorld(event.getWorld())
if (world != null) { .peek((world) -> {
// If it's going to start raining and we have weather disabled if (!world.getAllowWeather()) {
event.setCancelled((event.toThunderState() && !world.isWeatherEnabled())); 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) public final ConfigNode<Boolean> ALLOW_WEATHER = node(ConfigNode.builder("allow-weather", Boolean.class)
.defaultValue(true) .defaultValue(true)
.name("allow-weather") .name("allow-weather")
.onSetValue((oldValue, newValue) -> {
if (world == null) { return; }
world.getBukkitWorld().peek(world -> {
if (!world.isClearWeather() && !newValue) {
world.setClearWeatherDuration(-1);
}
});
})
.build()); .build());
public final ConfigNode<Boolean> AUTO_HEAL = node(ConfigNode.builder("auto-heal", Boolean.class) public final ConfigNode<Boolean> AUTO_HEAL = node(ConfigNode.builder("auto-heal", Boolean.class)