Ensure snowball hit events are properly canceled outside combat phase.

Previously, events where players were hit outside the combat phase or by non-snowball entities were not being canceled appropriately. This commit adds `event.setCancelled(true)` to prevent unintended behaviors in these scenarios.
This commit is contained in:
Teriuihi 2025-02-15 02:48:20 +01:00
parent 07b700bc32
commit f27038f91d

View File

@ -100,13 +100,14 @@ public class SnowballEvent implements Listener {
private void handleSnowballHit(EntityDamageByEntityEvent event, SnowballHitConsumer consumer) {
Optional<GamePhase> optionalGamePhase = gameManager.getGamePhase();
if (optionalGamePhase.isEmpty()) {
log.debug("No game is running but player was hit by snowball");
log.debug("No game is running but player was hit");
return;
}
GamePhase gamePhase = optionalGamePhase.get();
if (!gamePhase.equals(GamePhase.COMBAT)) {
log.debug("Not in combat phase but player was hit by snowball");
log.debug("Not in combat phase but player was hit, cancelling event");
event.setCancelled(true);
return;
}
@ -116,7 +117,8 @@ public class SnowballEvent implements Listener {
}
if (!(event.getDamager() instanceof org.bukkit.entity.Snowball snowball)) {
log.debug("The player was hit by something other than a snowball");
log.debug("The player was hit by something other than a snowball, canceling event");
event.setCancelled(true);
return;
}