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 5b29766f7..06b67554c 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 @@ -1,9 +1,5 @@ package com.lovetropics.minigames.common.content.survive_the_tide.behavior; -import com.lovetropics.minigames.client.toast.NotificationIcon; -import com.lovetropics.minigames.client.toast.NotificationStyle; -import com.lovetropics.minigames.client.toast.ShowNotificationToastMessage; -import com.lovetropics.minigames.common.content.survive_the_tide.SurviveTheTide; import com.lovetropics.minigames.common.content.survive_the_tide.SurviveTheTideWeatherConfig; import com.lovetropics.minigames.common.core.game.IGamePhase; import com.lovetropics.minigames.common.core.game.behavior.IGameBehavior; @@ -13,20 +9,10 @@ import com.lovetropics.minigames.common.core.game.state.weather.GameWeatherState; import com.lovetropics.minigames.common.core.game.weather.WeatherEvent; import com.lovetropics.minigames.common.core.game.weather.WeatherEventType; -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; -import net.minecraft.sounds.SoundSource; import net.minecraft.util.RandomSource; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.ItemLike; -import net.minecraft.world.level.block.Blocks; - -import java.util.function.Supplier; public class SurviveTheTideWeatherControlBehavior implements IGameBehavior { public static final Codec CODEC = SurviveTheTideWeatherConfig.CODEC.xmap(SurviveTheTideWeatherControlBehavior::new, b -> b.config); @@ -77,8 +63,6 @@ public class SurviveTheTideWeatherControlBehavior implements IGameBehavior { * - consider design to factor in worn items to negate player effects */ - private static final Component TITLE = Component.literal("WEATHER REPORT: \n").withStyle(ChatFormatting.BOLD); - protected GameProgressionState progression; protected GameWeatherState weather; @@ -107,15 +91,15 @@ private void tick(final IGamePhase game) { if (random.nextFloat() <= config.getRainHeavyChance(progression)) { heavyRainfallStart(progression); } else if (random.nextFloat() <= config.getRainAcidChance(progression)) { - acidRainStart(game, progression); + acidRainStart(progression); } else if (random.nextFloat() <= config.getHailChance(progression)) { - hailStart(game, progression); + hailStart(progression); } else if (random.nextFloat() <= config.getHeatwaveChance(progression)) { - heatwaveStart(game, progression); + heatwaveStart(progression); } else if (random.nextFloat() <= config.getSandstormChance(progression)) { - sandstormStart(game, progression); + sandstormStart(progression); } else if (random.nextFloat() <= config.getSnowstormChance(progression)) { - snowstormStart(game, progression); + snowstormStart(progression); } } @@ -137,101 +121,45 @@ private void heavyRainfallStart(GameProgressionState progression) { weather.setEvent(WeatherEvent.heavyRain(time)); } - private void acidRainStart(IGamePhase game, GameProgressionState progression) { + private void acidRainStart(GameProgressionState progression) { int time = config.getRainAcidMinTime() + random.nextInt(config.getRainAcidExtraRandTime()); if (config.halveEventTime(progression)) { time /= 2; } weather.setEvent(WeatherEvent.acidRain(time)); - - broadcastNotification(game, - Component.literal("Acid Rain is falling!\n") - .append("Find shelter, or make sure to carry an ") - .append(umbrellaName()), - createNotificationStyle(SurviveTheTide.ACID_REPELLENT_UMBRELLA) - ); } - private void hailStart(IGamePhase game, GameProgressionState progression) { + private void hailStart(GameProgressionState progression) { int time = config.getRainHeavyMinTime() + random.nextInt(config.getRainHeavyExtraRandTime()); if (config.halveEventTime(progression)) { time /= 2; } weather.setEvent(WeatherEvent.hail(time)); - - broadcastNotification(game, - Component.literal("Hail is falling!\n") - .append("Find shelter, or make sure to carry an ") - .append(umbrellaName()), - createNotificationStyle(SurviveTheTide.ACID_REPELLENT_UMBRELLA) - ); } - private void heatwaveStart(IGamePhase game, GameProgressionState progression) { + private void heatwaveStart(GameProgressionState progression) { int time = config.getHeatwaveMinTime() + random.nextInt(config.getHeatwaveExtraRandTime()); if (config.halveEventTime(progression)) { time /= 2; } weather.setEvent(WeatherEvent.heatwave(time)); - - broadcastNotification(game, - Component.literal("A Heat Wave is passing!\n") - .append("Stay inside, or make sure to equip ") - .append(sunscreenName()), - createNotificationStyle(SurviveTheTide.SUPER_SUNSCREEN) - ); } - private void sandstormStart(IGamePhase game, GameProgressionState progression) { + private void sandstormStart(GameProgressionState progression) { //TODO: more config int time = config.getHeatwaveMinTime() + random.nextInt(config.getHeatwaveExtraRandTime()); if (config.halveEventTime(progression)) { time /= 2; } weather.setEvent(WeatherEvent.sandstorm(time, config.getSandstormBuildupTickRate(), config.getSandstormMaxStackable())); - - broadcastNotification(game, - Component.literal("A Sandstorm is passing!\n") - .append("Find shelter!"), - createNotificationStyle(() -> Blocks.SAND) - ); } - private void snowstormStart(IGamePhase game, GameProgressionState progression) { + private void snowstormStart(GameProgressionState progression) { //TODO: more config int time = config.getHeatwaveMinTime() + random.nextInt(config.getHeatwaveExtraRandTime()); if (config.halveEventTime(progression)) { time /= 2; } weather.setEvent(WeatherEvent.snowstorm(time, config.getSnowstormBuildupTickRate(), config.getSnowstormMaxStackable())); - - broadcastNotification(game, - Component.literal("A Snowstorm is passing!\n") - .append("Find shelter!"), - createNotificationStyle(() -> Blocks.SNOW_BLOCK) - ); - } - - private static void broadcastNotification(IGamePhase game, Component message, NotificationStyle style) { - ShowNotificationToastMessage packet = new ShowNotificationToastMessage(Component.literal("").append(TITLE).append(message), style); - game.getAllPlayers().sendPacket(LoveTropicsNetwork.CHANNEL, packet); - game.getParticipants().playSound(SoundEvents.VILLAGER_NO, SoundSource.MASTER, 1.0f, 1.0f); - } - - private static Component umbrellaName() { - return Component.translatable(SurviveTheTide.ACID_REPELLENT_UMBRELLA.get().getDescriptionId()); - } - - private static Component sunscreenName() { - return Component.translatable(SurviveTheTide.SUPER_SUNSCREEN.get().getDescriptionId()); - } - - private static NotificationStyle createNotificationStyle(final Supplier item) { - return new NotificationStyle( - NotificationIcon.item(new ItemStack(item.get())), - NotificationStyle.Sentiment.NEGATIVE, - NotificationStyle.Color.DARK, - 5 * 1000 - ); } } diff --git a/src/main/java/com/lovetropics/minigames/common/core/game/behavior/GameBehaviorTypes.java b/src/main/java/com/lovetropics/minigames/common/core/game/behavior/GameBehaviorTypes.java index bd04f4523..6c725c16c 100644 --- a/src/main/java/com/lovetropics/minigames/common/core/game/behavior/GameBehaviorTypes.java +++ b/src/main/java/com/lovetropics/minigames/common/core/game/behavior/GameBehaviorTypes.java @@ -75,6 +75,7 @@ import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.OnKillTrigger; import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.PhaseChangeTrigger; import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.ScheduledActionsTrigger; +import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.WeatherChangeTrigger; import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.WhileInRegionTrigger; import com.lovetropics.minigames.common.core.game.behavior.instances.tweak.CancelPlayerDamageBehavior; import com.lovetropics.minigames.common.core.game.behavior.instances.tweak.DisableHungerBehavior; @@ -160,6 +161,7 @@ public class GameBehaviorTypes { public static final GameBehaviorEntry GAME_END_EFFECTS = register("game_end_effects", GameEndEffectsBehavior.CODEC); public static final GameBehaviorEntry TIPS_AND_TRICKS = register("tips_and_tricks", TipsAndTricksBehavior.CODEC); public static final GameBehaviorEntry PHASE_PROGRESS_BAR = register("phase_progress_bar", ProgressBarBehavior.CODEC); + public static final GameBehaviorEntry WEATHER_CHANGE_TRIGGER = register("weather_change_trigger", WeatherChangeTrigger.CODEC); public static final GameBehaviorEntry BIND_OBJECTIVE_TO_STATISTIC = register("bind_objective_to_statistic", BindObjectiveToStatisticBehavior.CODEC); public static final GameBehaviorEntry PLACE_BY_STATISTIC = register("place_by_statistic", PlaceByStatisticBehavior.CODEC); diff --git a/src/main/java/com/lovetropics/minigames/common/core/game/behavior/instances/trigger/WeatherChangeTrigger.java b/src/main/java/com/lovetropics/minigames/common/core/game/behavior/instances/trigger/WeatherChangeTrigger.java new file mode 100644 index 000000000..f81f7a528 --- /dev/null +++ b/src/main/java/com/lovetropics/minigames/common/core/game/behavior/instances/trigger/WeatherChangeTrigger.java @@ -0,0 +1,41 @@ +package com.lovetropics.minigames.common.core.game.behavior.instances.trigger; + +import com.lovetropics.minigames.common.core.game.IGamePhase; +import com.lovetropics.minigames.common.core.game.behavior.IGameBehavior; +import com.lovetropics.minigames.common.core.game.behavior.action.GameActionContext; +import com.lovetropics.minigames.common.core.game.behavior.action.GameActionList; +import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar; +import com.lovetropics.minigames.common.core.game.behavior.event.GameWorldEvents; +import com.lovetropics.minigames.common.core.game.weather.WeatherEventType; +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; + +import java.util.Map; + +public class WeatherChangeTrigger implements IGameBehavior { + public static final Codec CODEC = RecordCodecBuilder.create(i -> i.group( + Codec.unboundedMap(WeatherEventType.CODEC, GameActionList.CODEC).fieldOf("events").forGetter(c -> c.eventActions) + ).apply(i, WeatherChangeTrigger::new)); + + private final Map eventActions; + + public WeatherChangeTrigger(Map eventActions) { + this.eventActions = eventActions; + } + + @Override + public void register(IGamePhase game, EventRegistrar events) { + for (GameActionList actions : eventActions.values()) { + actions.register(game, events); + } + + events.listen(GameWorldEvents.SET_WEATHER, (lastEvent, event) -> { + if (event != null) { + GameActionList actions = eventActions.get(event.getType()); + if (actions != null) { + actions.apply(game, GameActionContext.EMPTY); + } + } + }); + } +}