From 48885f736a7f641906d247596d37cc54164e9885 Mon Sep 17 00:00:00 2001 From: Gegy Date: Sun, 8 Oct 2023 17:45:41 +0200 Subject: [PATCH] Add event when weather changes --- .../SurviveTheTideWeatherControlBehavior.java | 3 ++- .../core/game/behavior/event/GameWorldEvents.java | 12 ++++++++++++ .../game/behavior/instances/AddWeatherBehavior.java | 4 +++- .../core/game/state/weather/GameWeatherState.java | 11 ++++++++--- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/lovetropics/minigames/common/content/survive_the_tide/behavior/SurviveTheTideWeatherControlBehavior.java b/src/main/java/com/lovetropics/minigames/common/content/survive_the_tide/behavior/SurviveTheTideWeatherControlBehavior.java index e59f61fe6..5b29766f7 100644 --- a/src/main/java/com/lovetropics/minigames/common/content/survive_the_tide/behavior/SurviveTheTideWeatherControlBehavior.java +++ b/src/main/java/com/lovetropics/minigames/common/content/survive_the_tide/behavior/SurviveTheTideWeatherControlBehavior.java @@ -16,6 +16,7 @@ import com.lovetropics.minigames.common.core.network.LoveTropicsNetwork; import com.mojang.serialization.Codec; import net.minecraft.ChatFormatting; +import net.minecraft.SharedConstants; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerLevel; import net.minecraft.sounds.SoundEvents; @@ -101,7 +102,7 @@ private void tick(final IGamePhase game) { } ServerLevel world = game.getWorld(); - if (world.getGameTime() % 20 == 0) { + if (world.getGameTime() % SharedConstants.TICKS_PER_SECOND == 0) { if (weather.getEvent() == null && weather.canStartWeatherEvent()) { if (random.nextFloat() <= config.getRainHeavyChance(progression)) { heavyRainfallStart(progression); diff --git a/src/main/java/com/lovetropics/minigames/common/core/game/behavior/event/GameWorldEvents.java b/src/main/java/com/lovetropics/minigames/common/core/game/behavior/event/GameWorldEvents.java index e05095360..0ab66d345 100644 --- a/src/main/java/com/lovetropics/minigames/common/core/game/behavior/event/GameWorldEvents.java +++ b/src/main/java/com/lovetropics/minigames/common/core/game/behavior/event/GameWorldEvents.java @@ -1,5 +1,6 @@ package com.lovetropics.minigames.common.core.game.behavior.event; +import com.lovetropics.minigames.common.core.game.weather.WeatherEvent; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionResult; import net.minecraft.core.BlockPos; @@ -7,6 +8,7 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.chunk.ChunkAccess; +import javax.annotation.Nullable; import java.util.List; public final class GameWorldEvents { @@ -34,6 +36,12 @@ public final class GameWorldEvents { return InteractionResult.PASS; }); + public static final GameEventType SET_WEATHER = GameEventType.create(SetWeather.class, listeners -> (lastEvent, event) -> { + for (SetWeather listener : listeners) { + listener.onSetWeather(lastEvent, event); + } + }); + private GameWorldEvents() { } @@ -48,4 +56,8 @@ public interface ExplosionDetonate { public interface SaplingGrow { InteractionResult onSaplingGrow(Level world, BlockPos pos); } + + public interface SetWeather { + void onSetWeather(@Nullable WeatherEvent lastEvent, @Nullable WeatherEvent event); + } } diff --git a/src/main/java/com/lovetropics/minigames/common/core/game/behavior/instances/AddWeatherBehavior.java b/src/main/java/com/lovetropics/minigames/common/core/game/behavior/instances/AddWeatherBehavior.java index e976fcaf9..d28af9abc 100644 --- a/src/main/java/com/lovetropics/minigames/common/core/game/behavior/instances/AddWeatherBehavior.java +++ b/src/main/java/com/lovetropics/minigames/common/core/game/behavior/instances/AddWeatherBehavior.java @@ -6,6 +6,7 @@ import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar; import com.lovetropics.minigames.common.core.game.behavior.event.GamePhaseEvents; import com.lovetropics.minigames.common.core.game.behavior.event.GamePlayerEvents; +import com.lovetropics.minigames.common.core.game.behavior.event.GameWorldEvents; import com.lovetropics.minigames.common.core.game.state.GameStateMap; import com.lovetropics.minigames.common.core.game.state.weather.GameWeatherState; import com.lovetropics.minigames.common.core.game.weather.WeatherController; @@ -42,7 +43,8 @@ public AddWeatherBehavior(Map eventEffects) { @Override public void registerState(IGamePhase game, GameStateMap phaseState, GameStateMap instanceState) { WeatherController controller = WeatherControllerManager.forWorld(game.getWorld()); - weather = phaseState.register(GameWeatherState.KEY, new GameWeatherState(controller)); + GameWorldEvents.SetWeather weatherListener = (lastEvent, event) -> game.invoker(GameWorldEvents.SET_WEATHER).onSetWeather(lastEvent, event); + weather = phaseState.register(GameWeatherState.KEY, new GameWeatherState(controller, weatherListener)); } @Override diff --git a/src/main/java/com/lovetropics/minigames/common/core/game/state/weather/GameWeatherState.java b/src/main/java/com/lovetropics/minigames/common/core/game/state/weather/GameWeatherState.java index 31f7432b5..65947cc5f 100644 --- a/src/main/java/com/lovetropics/minigames/common/core/game/state/weather/GameWeatherState.java +++ b/src/main/java/com/lovetropics/minigames/common/core/game/state/weather/GameWeatherState.java @@ -1,10 +1,12 @@ package com.lovetropics.minigames.common.core.game.state.weather; +import com.lovetropics.minigames.common.core.game.behavior.event.GameWorldEvents; import com.lovetropics.minigames.common.core.game.state.GameStateKey; import com.lovetropics.minigames.common.core.game.state.IGameState; import com.lovetropics.minigames.common.core.game.weather.WeatherController; import com.lovetropics.minigames.common.core.game.weather.WeatherEvent; import com.lovetropics.minigames.common.core.game.weather.WeatherEventType; +import net.minecraft.SharedConstants; import javax.annotation.Nullable; @@ -12,14 +14,16 @@ public final class GameWeatherState implements IGameState { public static final GameStateKey KEY = GameStateKey.create("Weather State"); private final WeatherController controller; + private final GameWorldEvents.SetWeather weatherListener; private WeatherEvent event; private int weatherCooldown = 0; - private int weatherCooldownBetweenStates = 220; + private int weatherCooldownBetweenStates = 11 * SharedConstants.TICKS_PER_SECOND; - public GameWeatherState(WeatherController controller) { + public GameWeatherState(WeatherController controller, GameWorldEvents.SetWeather weatherListener) { this.controller = controller; + this.weatherListener = weatherListener; } public void clear() { @@ -47,10 +51,11 @@ public void setEvent(@Nullable WeatherEvent event) { } this.event = event; - if (event != null) { event.apply(controller); } + + weatherListener.onSetWeather(lastEvent, event); } public void clearEvent() {