Skip to content

Commit

Permalink
Move extinguish and clear effects to a behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Oct 27, 2024
1 parent bf8bae3 commit b128d3e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@
import com.lovetropics.minigames.common.core.game.behavior.instances.action.ChestDropAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.ClearAttributeModifierAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.ClearDisguiseAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.ClearEffectsAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.CountdownAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.DamagePlayerAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.EliminatePlayerAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.ExtinguishPlayerFireAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.GiveEffectAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.GiveLootAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.GiveRewardAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.NotificationToastAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.PlaySoundAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.ResetHungerAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.RunCommandsAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.SendMessageAction;
import com.lovetropics.minigames.common.core.game.behavior.instances.action.SetBlockAtPlayerAction;
Expand Down Expand Up @@ -192,6 +195,9 @@ public class GameBehaviorTypes {
public static final GameBehaviorEntry<TeamWinTrigger> TEAM_WIN_TRIGGER = register("team_win_trigger", TeamWinTrigger.CODEC);
public static final GameBehaviorEntry<SyncTeamsBehavior> SYNC_TEAMS = register("sync_teams", SyncTeamsBehavior.CODEC);
public static final GameBehaviorEntry<AddEquipmentAction> ADD_EQUIPMENT = register("add_equipment", AddEquipmentAction.CODEC);
public static final GameBehaviorEntry<ResetHungerAction> RESET_HUNGER = register("reset_hunger", ResetHungerAction.CODEC);
public static final GameBehaviorEntry<ClearEffectsAction> CLEAR_EFFECTS = register("clear_effects", ClearEffectsAction.CODEC);
public static final GameBehaviorEntry<ExtinguishPlayerFireAction> EXTINGUISH_PLAYER_ACTION = register("extinguish_player", ExtinguishPlayerFireAction.CODEC);
public static final GameBehaviorEntry<SetTimeSpeedBehavior> SET_TIME_SPEED = register("set_time_speed", SetTimeSpeedBehavior.CODEC);
public static final GameBehaviorEntry<SetDayTimeBehavior> SET_DAY_TIME = register("set_day_time", SetDayTimeBehavior.CODEC);
public static final GameBehaviorEntry<SetDifficultyBehavior> SET_DIFFICULTY = register("set_difficulty", SetDifficultyBehavior.CODEC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@
import java.util.Map;
import java.util.Optional;

public record ImmediateRespawnBehavior(Optional<PlayerRole> role, Optional<PlayerRole> respawnAsRole, Optional<TemplatedText> deathMessage, boolean dropInventory, GameActionList<ServerPlayer> respawnAction, boolean spectateKiller, boolean extinguishFire, boolean clearEffects) implements IGameBehavior {
public record ImmediateRespawnBehavior(Optional<PlayerRole> role, Optional<PlayerRole> respawnAsRole, Optional<TemplatedText> deathMessage, boolean dropInventory, GameActionList<ServerPlayer> respawnAction, boolean spectateKiller) implements IGameBehavior {
public static final MapCodec<ImmediateRespawnBehavior> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
PlayerRole.CODEC.optionalFieldOf("role").forGetter(c -> c.role),
PlayerRole.CODEC.optionalFieldOf("respawn_as").forGetter(c -> c.respawnAsRole),
TemplatedText.CODEC.optionalFieldOf("death_message").forGetter(c -> c.deathMessage),
Codec.BOOL.optionalFieldOf("drop_inventory", false).forGetter(c -> c.dropInventory),
GameActionList.PLAYER_CODEC.optionalFieldOf("respawn_action", GameActionList.EMPTY).forGetter(c -> c.respawnAction),
Codec.BOOL.optionalFieldOf("spectate_killer", true).forGetter(c -> c.spectateKiller),
Codec.BOOL.optionalFieldOf("extinguish_fire", false).forGetter(c -> c.extinguishFire),
Codec.BOOL.optionalFieldOf("clear_effects", false).forGetter(c -> c.clearEffects)
Codec.BOOL.optionalFieldOf("spectate_killer", true).forGetter(c -> c.spectateKiller)
).apply(i, ImmediateRespawnBehavior::new));

@Override
Expand Down Expand Up @@ -70,12 +68,6 @@ private void respawnPlayer(IGamePhase game, ServerPlayer player, @Nullable Playe
}

player.setHealth(20.0F);
if(extinguishFire) {
player.extinguishFire();
}
if(clearEffects) {
player.removeAllEffects();
}
player.setDeltaMovement(0, 0, 0);

respawnAction.apply(game, GameActionContext.EMPTY, player);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.lovetropics.minigames.common.core.game.behavior.instances.action;

import com.lovetropics.minigames.common.core.game.GameException;
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.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GameActionEvents;
import com.mojang.serialization.MapCodec;

public record ClearEffectsAction() implements IGameBehavior {
public static final MapCodec<ClearEffectsAction> CODEC = MapCodec.unit(ClearEffectsAction::new);
@Override
public void register(IGamePhase game, EventRegistrar events) throws GameException {
events.listen(GameActionEvents.APPLY_TO_PLAYER, (context, target) -> {
if (target.getActiveEffects().isEmpty()) {
return false;
}
target.removeAllEffects();
return true;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.lovetropics.minigames.common.core.game.behavior.instances.action;

import com.lovetropics.minigames.common.core.game.GameException;
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.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GameActionEvents;
import com.mojang.serialization.MapCodec;

public record ExtinguishPlayerFireAction() implements IGameBehavior {
public static final MapCodec<ExtinguishPlayerFireAction> CODEC = MapCodec.unit(ExtinguishPlayerFireAction::new);
@Override
public void register(IGamePhase game, EventRegistrar events) throws GameException {
events.listen(GameActionEvents.APPLY_TO_PLAYER, (context, target) -> {
if (target.isOnFire()) {
target.extinguishFire();
return true;
}
return false;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.lovetropics.minigames.common.core.game.behavior.instances.action;

import com.lovetropics.minigames.common.core.game.GameException;
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.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GameActionEvents;
import com.mojang.serialization.MapCodec;

public record ResetHungerAction() implements IGameBehavior {
public static final MapCodec<ResetHungerAction> CODEC = MapCodec.unit(ResetHungerAction::new);
@Override
public void register(IGamePhase game, EventRegistrar events) throws GameException {
events.listen(GameActionEvents.APPLY_TO_PLAYER, (context, target) -> {
target.getFoodData().setFoodLevel(20);
target.getFoodData().setSaturation(5);
target.getFoodData().setExhaustion(0);
return true;
});
}
}

0 comments on commit b128d3e

Please sign in to comment.