Skip to content

Commit

Permalink
Close #89 Player can hit a Mob during SAFE_AREA phase
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoRutigliano committed Jul 4, 2024
1 parent 44107db commit 494445a
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/main/java/lar/minecraft/hg/managers/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
Expand Down Expand Up @@ -101,14 +103,31 @@ public void run() {
* Player damage event
*/
@EventHandler
public void onPlayerDamage(EntityDamageEvent event) {
if (!SpigotPlugin.isPlaying()) {
public void onEntityDamage(EntityDamageEvent event) {
// In SAFE_AREA phase Mobs can be hit by Players
if (SpigotPlugin.isSafeArea()) {
if (event instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) event;
if (entityDamageByEntityEvent.getDamager() instanceof Player && entityDamageByEntityEvent.getEntity() instanceof Mob) {
event.setCancelled(false);
} else {
event.setCancelled(true);
}
} else {
event.setCancelled(true);
}
}

// The hit can be done only in PLAYING phase
if (SpigotPlugin.isWaitingForStart() || SpigotPlugin.isLobby() || SpigotPlugin.isWinning() || SpigotPlugin.isEnded()) {
event.setCancelled(true);
}

}

@EventHandler
public void onFoodLevelChange(FoodLevelChangeEvent event) {
// Avoid to lose food level in all phases except for PLAYING and SAFE_AREA
if (SpigotPlugin.isWaitingForStart() || SpigotPlugin.isLobby() || SpigotPlugin.isWinning() || SpigotPlugin.isEnded()) {
event.setCancelled(true);
}
Expand Down

0 comments on commit 494445a

Please sign in to comment.