-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...a/com/lovetropics/minigames/common/core/game/behavior/instances/trigger/SpawnTrigger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |