Clean up PvP activation logic.

The idea behind the previous implementation worked, but it was a tad bit
confusing. This commit refactors the activation logic by simply removing
it entirely. The "activation" part of the logic is now derived from the
wave number (if 0, it means we haven't _really_ started yet), instead of
relying on the spawn thread to toggle the flag on and off. This kind of
dependency inversion (spawn thread -> listener, listener -> "phase") is
a pretty decent (albeit super tiny) step towards cleaning up the whole
session system, so I call that a victory in and of itself!
This commit is contained in:
Andreas Troelsen 2023-11-13 16:19:01 +01:00
parent a662157cbf
commit 457bf2ffff
2 changed files with 12 additions and 24 deletions

View File

@ -116,7 +116,6 @@ public class ArenaListener
private boolean protect;
private boolean monsterExp;
private boolean monsterInfight;
private boolean pvpOn;
private boolean pvpEnabled;
private boolean foodRegen;
private boolean lockFoodLevel;
@ -142,7 +141,7 @@ public class ArenaListener
this.protect = s.getBoolean("protect", true);
this.monsterExp = s.getBoolean("monster-exp", false);
this.monsterInfight = s.getBoolean("monster-infight", false);
this.pvpOn = s.getBoolean("pvp-enabled", false);
this.pvpEnabled = s.getBoolean("pvp-enabled", false);
this.foodRegen = s.getBoolean("food-regen", false);
this.lockFoodLevel = s.getBoolean("lock-food-level", true);
this.allowTeleport = s.getBoolean("allow-teleporting", false);
@ -161,16 +160,6 @@ public class ArenaListener
);
}
void pvpActivate() {
if (arena.isRunning() && !arena.getPlayersInArena().isEmpty()) {
pvpEnabled = pvpOn;
}
}
void pvpDeactivate() {
if (pvpOn) pvpEnabled = false;
}
public void onBlockBreak(BlockBreakEvent event) {
// Check if the block is a sign, it might be a leaderboard
if (event.getBlock() instanceof Sign) {
@ -722,11 +711,15 @@ public class ArenaListener
return;
}
// Cancel PvP damage if disabled
if (!pvpEnabled && damager instanceof Player && !damager.equals(player)) {
event.setCancelled(true);
return;
// If this is player damage (and not self-inflicted), handle PvP
if (damager instanceof Player && !damager.equals(player)) {
// PvP must be enabled, and the first wave must have spawned
if (!pvpEnabled || arena.getWaveManager().getWaveNumber() == 0) {
event.setCancelled(true);
return;
}
}
event.setCancelled(false);
arena.getArenaPlayer(player).getStats().add("dmgTaken", event.getDamage());
@ -802,7 +795,7 @@ public class ArenaListener
return;
}
if (!pvpEnabled) {
if (!pvpEnabled || arena.getWaveManager().getWaveNumber() == 0) {
event.setCancelled(true);
}
}
@ -913,7 +906,7 @@ public class ArenaListener
if (potion.getShooter() instanceof Player) {
// Check for PvP stuff if the shooter is a player
if (!pvpEnabled) {
if (!pvpEnabled || arena.getWaveManager().getWaveNumber() == 0) {
// If a potion has harmful effects, remove all players.
for (PotionEffect effect : potion.getEffects()) {
PotionEffectType type = effect.getType();

View File

@ -91,10 +91,7 @@ public class MASpawnThread implements Runnable
}
int delay = arena.getSettings().getInt("first-wave-delay", 5) * 20;
task = Bukkit.getScheduler().runTaskLater(plugin, () -> {
arena.getEventListener().pvpActivate();
this.run();
}, delay);
task = Bukkit.getScheduler().runTaskLater(plugin, this, delay);
}
public void stop() {
@ -103,8 +100,6 @@ public class MASpawnThread implements Runnable
return;
}
arena.getEventListener().pvpDeactivate();
task.cancel();
task = null;
}