Merge pull request #1000 from ludovic111/fix/pvp-friendly-fire

🤖 Agent: merged after verify passed. Teammates with friendly fire off no longer hurt each other on a server (lan_e2e pvp); part of #484.
This commit is contained in:
Ludovic Marie
2026-10-19 11:52:26 +02:00
committed by GitHub
2 changed files with 26 additions and 2 deletions
+14
View File
@@ -20,6 +20,8 @@ pub(super) struct Strike {
crit: bool,
from: [f64; 3],
sweep: Option<[f64; 3]>,
/// The attacker's team, for the victim's `Player.canHarmPlayer`.
team: Option<horizon_client::scoreboard::Team>,
}
impl Game<'_> {
@@ -88,12 +90,24 @@ impl Game<'_> {
crit,
from,
sweep,
team: self.player_team().cloned(),
})
}
/// The hit landing on the victim, then the attacker's attack sound.
pub(super) fn pvp_land(&mut self, host: &mut LanHost, victim: Victim, s: &Strike) {
// ServerPlayer.canHarmPlayer: the pvp game rule, then the victim's
// team (Player.canHarmPlayer: allies only with friendly fire on).
let team = match victim {
Victim::Host => self.player_team(),
Victim::Guest(conn) => host
.hub
.guest(conn)
.and_then(|g| self.score.board.players_team(&g.name)),
};
let harm = horizon_client::scoreboard::teams_allow_damage(team, s.team.as_ref());
let hurt = self.ui.rules.bool("pvp")
&& harm
&& match victim {
Victim::Host => self.mob_hits_player(s.amount, s.from, "minecraft:player_attack"),
Victim::Guest(conn) => self
+12 -2
View File
@@ -18,7 +18,8 @@ address. The script then checks, from both sides, what a guest's game used to ge
- mobs: with the host in creative, a zombie and a skeleton go for the guest and hurt them;
- hazards: a guest in survival gets hungry sprint-jumping and is hurt by lava, a block at the eyes,
drowning and the void (then respawns);
- pvp: the guest and the host hit each other with a sword; with the pvp game rule off, nothing;
- pvp: the guest and the host hit each other with a sword; with the pvp game rule off, or on one
team without friendly fire, nothing;
- respawn: a player killed is drawn dead by the other side, and alive again once it respawns (the
respawned player is a new entity to the others); the host dies, and on the server thread
the guest too;
@@ -582,8 +583,14 @@ def check_pvp(host, guest, out):
hits = {}
try:
for name, rule, attacker in (('guest hits host', 'true', guest), ('host hits guest', 'true', host),
('pvp off', 'false', guest)):
('pvp off', 'false', guest), ('guest hits teammate', 'true', guest),
('host hits teammate', 'true', host)):
host('command', command=f'gamerule pvp {rule}')
if name == 'guest hits teammate':
# Player.canHarmPlayer: the victim's team without friendly fire spares its allies.
host('command', command='team add parity_allies')
host('command', command='team modify parity_allies friendlyFire false')
host('command', command='team join parity_allies @a')
face_each_other(host, guest)
attacker.wait(30) # a full attack charge
victim = (lambda: host('state')['player']['health']) if attacker is guest else (lambda: remote(guest)['health'])
@@ -594,9 +601,12 @@ def check_pvp(host, guest, out):
attacker.shot(out, 'pvp-' + name.replace(' ', '-'))
finally:
host('command', command='gamerule pvp true')
host('command', command='team remove parity_allies')
assert hits['guest hits host'][1] < hits['guest hits host'][0], f'the guest did not hurt the host: {hits}'
assert hits['host hits guest'][1] < hits['host hits guest'][0], f'the host did not hurt the guest: {hits}'
assert hits['pvp off'][1] == hits['pvp off'][0], f'pvp off still hurts: {hits}'
for name in ('guest hits teammate', 'host hits teammate'):
assert hits[name][1] == hits[name][0], f'friendly fire off still hurts ({name}): {hits}'
return hits