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 466b1dd94..5089de0d4 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 @@ -107,6 +107,7 @@ import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.PlayerTickTrigger; import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.ScheduledActionsTrigger; import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.SetRoleTrigger; +import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.SpawnTrigger; import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.TopPlayerTrigger; import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.WeatherChangeTrigger; import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.WhileInInventoryTrigger; @@ -271,7 +272,7 @@ public class GameBehaviorTypes { public static final GameBehaviorEntry GIVE_REWARD = register("give_reward", GiveRewardAction.CODEC); public static final GameBehaviorEntry TOP_PLAYER_TRIGGER = register("top_player_trigger", TopPlayerTrigger.CODEC); public static final GameBehaviorEntry SET_ROLE = register("set_role", SetRoleTrigger.CODEC); - public static final GameBehaviorEntry ITEM_PICKED_UP = register("item_picked_up", ItemPickedUpTrigger.CODEC); + public static final GameBehaviorEntry SPAWN = register("on_spawn", SpawnTrigger.CODEC); public static final GameBehaviorEntry ITEM_PICKED_UP = register("item_picked_up", ItemPickedUpTrigger.CODEC); public static final GameBehaviorEntry GIVE_ITEMS_TO_KILLER = register("give_items_to_killer", GiveItemsToKillerBehavior.CODEC); public static final GameBehaviorEntry COUNTDOWN_EFFECTS = register("countdown_effects", CountdownEffectsBehavior.CODEC); public static final GameBehaviorEntry TRIGGER_EVERY_PACKAGE = register("trigger_every_package", TriggerEveryPackageBehavior.CODEC); diff --git a/src/main/java/com/lovetropics/minigames/common/core/game/behavior/instances/trigger/SpawnTrigger.java b/src/main/java/com/lovetropics/minigames/common/core/game/behavior/instances/trigger/SpawnTrigger.java new file mode 100644 index 000000000..71e910626 --- /dev/null +++ b/src/main/java/com/lovetropics/minigames/common/core/game/behavior/instances/trigger/SpawnTrigger.java @@ -0,0 +1,39 @@ +package com.lovetropics.minigames.common.core.game.behavior.instances.trigger; + +import com.lovetropics.minigames.common.core.game.GameException; +import com.lovetropics.minigames.common.core.game.IGamePhase; +import com.lovetropics.minigames.common.core.game.behavior.GameBehaviorType; +import com.lovetropics.minigames.common.core.game.behavior.GameBehaviorTypes; +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.GamePlayerEvents; +import com.lovetropics.minigames.common.core.game.player.PlayerRole; +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.server.level.ServerPlayer; + +import java.util.function.Supplier; + +public record SpawnTrigger(PlayerRole role, GameActionList action) implements IGameBehavior { + public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(i -> i.group( + PlayerRole.CODEC.fieldOf("role").forGetter(SpawnTrigger::role), + GameActionList.PLAYER_CODEC.fieldOf("action").forGetter(SpawnTrigger::action) + ).apply(i, SpawnTrigger::new)); + + @Override + public void register(final IGamePhase game, final EventRegistrar events) throws GameException { + action.register(game, events); + events.listen(GamePlayerEvents.SPAWN, (playerId, spawn, role) -> { + if (this.role == role) { + spawn.run(player -> action.apply(game, GameActionContext.EMPTY, player)); + } + }); + } + + @Override + public Supplier> behaviorType() { + return GameBehaviorTypes.SPAWN; + } +}