From 0a62081d03d594d1d4b8b742eed2033a7b46b101 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 4 Sep 2023 13:37:29 +0800 Subject: [PATCH] Move weather handling to new wm --- .../listeners/MVWeatherListener.java | 37 +++++++++++-------- .../worldnew/config/WorldConfigNodes.java | 8 ++++ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVWeatherListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVWeatherListener.java index 7e08fa4e..68b8a516 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVWeatherListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVWeatherListener.java @@ -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); + } + }); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/WorldConfigNodes.java b/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/WorldConfigNodes.java index 0aee9fbd..e935643d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/WorldConfigNodes.java +++ b/src/main/java/com/onarandombox/MultiverseCore/worldnew/config/WorldConfigNodes.java @@ -49,6 +49,14 @@ public class WorldConfigNodes { public final ConfigNode 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 AUTO_HEAL = node(ConfigNode.builder("auto-heal", Boolean.class)