Skip to content

Commit

Permalink
Fix: don't track kills from yourself, add exclude_self flag to on_dea…
Browse files Browse the repository at this point in the history
…th trigger
  • Loading branch information
Gegy committed Nov 18, 2024
1 parent 48a1602 commit 74f2a7d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public void register(IGamePhase game, EventRegistrar events) {

final ServerPlayer killerPlayer = Util.getKillerPlayer(player, damageSource);
if (killerPlayer != null) {
statistics.forPlayer(killerPlayer).incrementInt(StatisticKey.KILLS, 1);
if (killerPlayer != player) {
statistics.forPlayer(killerPlayer).incrementInt(StatisticKey.KILLS, 1);

GameTeamKey team = teams != null ? teams.getTeamForPlayer(killerPlayer) : null;
if (team != null) {
statistics.forTeam(team).incrementInt(StatisticKey.KILLS, 1);
GameTeamKey team = teams != null ? teams.getTeamForPlayer(killerPlayer) : null;
if (team != null) {
statistics.forTeam(team).incrementInt(StatisticKey.KILLS, 1);
}
}

playerStatistics.set(StatisticKey.KILLED_BY, PlayerKey.from(killerPlayer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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.util.Util;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.advancements.critereon.EntityPredicate;
Expand All @@ -19,12 +20,13 @@
import java.util.Optional;
import java.util.function.Supplier;

public record OnDeathTrigger(GameActionList<ServerPlayer> killedAction, GameActionList<ServerPlayer> killerAction, Optional<EntityPredicate> killedPredicate, Optional<EntityPredicate> killerPredicate) implements IGameBehavior {
public record OnDeathTrigger(GameActionList<ServerPlayer> killedAction, GameActionList<ServerPlayer> killerAction, Optional<EntityPredicate> killedPredicate, Optional<EntityPredicate> killerPredicate, boolean excludeSelf) implements IGameBehavior {
public static final MapCodec<OnDeathTrigger> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
GameActionList.PLAYER_CODEC.optionalFieldOf("killed_action", GameActionList.EMPTY).forGetter(OnDeathTrigger::killedAction),
GameActionList.PLAYER_CODEC.optionalFieldOf("killer_action", GameActionList.EMPTY).forGetter(OnDeathTrigger::killerAction),
EntityPredicate.CODEC.optionalFieldOf("killed_predicate").forGetter(OnDeathTrigger::killedPredicate),
EntityPredicate.CODEC.optionalFieldOf("killer_predicate").forGetter(OnDeathTrigger::killerPredicate)
EntityPredicate.CODEC.optionalFieldOf("killer_predicate").forGetter(OnDeathTrigger::killerPredicate),
Codec.BOOL.optionalFieldOf("exclude_self", false).forGetter(OnDeathTrigger::excludeSelf)
).apply(i, OnDeathTrigger::new));

@Override
Expand All @@ -34,6 +36,9 @@ public void register(IGamePhase game, EventRegistrar events) {

events.listen(GamePlayerEvents.DEATH, (player, damageSource) -> {
final ServerPlayer killer = Util.getKillerPlayer(player, damageSource);
if (excludeSelf && killer == player) {
return InteractionResult.PASS;
}
if (killerPredicate.isPresent() && !killerPredicate.get().matches(player, killer)) {
return InteractionResult.PASS;
}
Expand Down

0 comments on commit 74f2a7d

Please sign in to comment.