Skip to content

Commit

Permalink
Add event when weather changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Oct 8, 2023
1 parent 28da2ea commit 48885f7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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;
import net.minecraft.world.level.Explosion;
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 {
Expand Down Expand Up @@ -34,6 +36,12 @@ public final class GameWorldEvents {
return InteractionResult.PASS;
});

public static final GameEventType<SetWeather> SET_WEATHER = GameEventType.create(SetWeather.class, listeners -> (lastEvent, event) -> {
for (SetWeather listener : listeners) {
listener.onSetWeather(lastEvent, event);
}
});

private GameWorldEvents() {
}

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,7 +43,8 @@ public AddWeatherBehavior(Map<WeatherEventType, EventEffects> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
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;

public final class GameWeatherState implements IGameState {
public static final GameStateKey<GameWeatherState> 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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 48885f7

Please sign in to comment.