Skip to content

Commit

Permalink
Add on_spawn trigger behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Nov 14, 2024
1 parent faae345 commit f70d468
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -271,7 +272,7 @@ public class GameBehaviorTypes {
public static final GameBehaviorEntry<GiveRewardAction> GIVE_REWARD = register("give_reward", GiveRewardAction.CODEC);
public static final GameBehaviorEntry<TopPlayerTrigger> TOP_PLAYER_TRIGGER = register("top_player_trigger", TopPlayerTrigger.CODEC);
public static final GameBehaviorEntry<SetRoleTrigger> SET_ROLE = register("set_role", SetRoleTrigger.CODEC);
public static final GameBehaviorEntry<ItemPickedUpTrigger> ITEM_PICKED_UP = register("item_picked_up", ItemPickedUpTrigger.CODEC);
public static final GameBehaviorEntry<SpawnTrigger> SPAWN = register("on_spawn", SpawnTrigger.CODEC); public static final GameBehaviorEntry<ItemPickedUpTrigger> ITEM_PICKED_UP = register("item_picked_up", ItemPickedUpTrigger.CODEC);
public static final GameBehaviorEntry<GiveItemsToKillerBehavior> GIVE_ITEMS_TO_KILLER = register("give_items_to_killer", GiveItemsToKillerBehavior.CODEC);
public static final GameBehaviorEntry<CountdownEffectsBehavior> COUNTDOWN_EFFECTS = register("countdown_effects", CountdownEffectsBehavior.CODEC);
public static final GameBehaviorEntry<TriggerEveryPackageBehavior> TRIGGER_EVERY_PACKAGE = register("trigger_every_package", TriggerEveryPackageBehavior.CODEC);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ServerPlayer> action) implements IGameBehavior {
public static final MapCodec<SpawnTrigger> 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<? extends GameBehaviorType<?>> behaviorType() {
return GameBehaviorTypes.SPAWN;
}
}

0 comments on commit f70d468

Please sign in to comment.