Make SheepBouncer logic cancellable.

Following the changes in 96c1f18517, this commit makes SheepBouncer cancellable for the same reasons as outlined in the MASpawnThread commit.
This commit is contained in:
Andreas Troelsen
2018-08-19 20:48:04 +02:00
parent 96c1f18517
commit 754eb156d1
2 changed files with 44 additions and 9 deletions
@@ -586,6 +586,7 @@ public class ArenaImpl implements Arena
// Stop spawning. // Stop spawning.
stopSpawner(); stopSpawner();
stopBouncingSheep();
// Announce and clean arena floor, etc. // Announce and clean arena floor, etc.
if (settings.getBoolean("global-end-announce", false)) { if (settings.getBoolean("global-end-announce", false)) {
@@ -1030,15 +1031,24 @@ public class ArenaImpl implements Arena
world.setSpawnFlags(allowMonsters, allowAnimals); world.setSpawnFlags(allowMonsters, allowAnimals);
} }
private void startBouncingSheep() private void startBouncingSheep() {
{ if (sheepBouncer != null) {
// Create a new bouncer if necessary. sheepBouncer.stop();
if (sheepBouncer == null) { sheepBouncer = null;
sheepBouncer = new SheepBouncer(this);
} }
// Start bouncing! sheepBouncer = new SheepBouncer(this);
scheduleTask(sheepBouncer, settings.getInt("first-wave-delay", 5) * 20); sheepBouncer.start();
}
private void stopBouncingSheep() {
if (sheepBouncer == null) {
plugin.getLogger().warning("Can't stop non-existent sheep bouncer in arena " + configName() + ". This should never happen.");
return;
}
sheepBouncer.stop();
sheepBouncer = null;
} }
@Override @Override
@@ -1,9 +1,11 @@
package com.garbagemule.MobArena.waves; package com.garbagemule.MobArena.waves;
import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.Arena;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@@ -13,11 +15,34 @@ public class SheepBouncer implements Runnable
public static final int BOUNCE_INTERVAL = 20; public static final int BOUNCE_INTERVAL = 20;
private Arena arena; private Arena arena;
private Set<LivingEntity> sheep; private Set<LivingEntity> sheep;
private BukkitTask task;
public SheepBouncer(Arena arena) { public SheepBouncer(Arena arena) {
this.arena = arena; this.arena = arena;
} }
public void start() {
if (task != null) {
arena.getPlugin().getLogger().warning("Starting sheep bouncer in arena " + arena.configName() + " with existing bouncer still running. This should never happen.");
task.cancel();
task = null;
}
int delay = arena.getSettings().getInt("first-wave-delay", 5) * 20;
task = Bukkit.getScheduler().runTaskLater(arena.getPlugin(), this, delay);
}
public void stop() {
if (task == null) {
arena.getPlugin().getLogger().warning("Can't stop non-existent sheep bouncer in arena " + arena.configName() + ". This should never happen.");
return;
}
task.cancel();
task = null;
}
@Override @Override
public void run() { public void run() {
// If the arena isn't running or has no players, bail out // If the arena isn't running or has no players, bail out
@@ -63,7 +88,7 @@ public class SheepBouncer implements Runnable
} }
// Reschedule for more bouncy madness! // Reschedule for more bouncy madness!
arena.scheduleTask(this, BOUNCE_INTERVAL); task = Bukkit.getScheduler().runTaskLater(arena.getPlugin(), this, BOUNCE_INTERVAL);
} }
/*private boolean isTargetNearby(Creature c, LivingEntity t) { /*private boolean isTargetNearby(Creature c, LivingEntity t) {