Fix possible memory leak

This commit is contained in:
Daniel Saukel 2020-04-03 19:50:22 +02:00
parent ff1e615d6d
commit cc2004b30b
5 changed files with 18 additions and 9 deletions

View File

@ -184,4 +184,13 @@ public abstract class Windup extends Deactivatable {
}
}
/**
* Use this method to ensure that its world still exists.
*
* @return if the world is already finished
*/
public boolean isWorldFinished() {
return api.getGameWorld(getSign().getWorld()) != null;
}
}

View File

@ -18,7 +18,6 @@ package de.erethon.dungeonsxl.sign.windup;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
@ -50,6 +49,11 @@ public class CommandTask extends BukkitRunnable {
@Override
public void run() {
if (isWorldFinished()) {
sign.deactivate();
return;
}
String command = script.getCommands().get(k++).replace("%player%", sender.getName()).replace("%player_name%", sender.getName());
if (papi) {
Bukkit.getServer().dispatchCommand(sender, PlaceholderAPI.setPlaceholders(player, command));

View File

@ -33,7 +33,7 @@ public class DelayedPowerTask extends BukkitRunnable {
@Override
public void run() {
if (sign.getGameWorld() == null) {
if (isWorldFinished()) {
sign.getEnableTask().cancel();
sign.getDisableTask().cancel();
return;

View File

@ -120,7 +120,7 @@ public class MobSign extends Windup {
initialAmount = n;
provider = attributes.length == 3 ? providers.get(attributes[2]) : null;
setRunnable(new MobSpawnTask(api, this, n));
setRunnable(new MobSpawnTask(this, n));
}
/**

View File

@ -16,8 +16,6 @@
*/
package de.erethon.dungeonsxl.sign.windup;
import de.erethon.dungeonsxl.api.DungeonsAPI;
import de.erethon.dungeonsxl.api.world.GameWorld;
import de.erethon.dungeonsxl.mob.DMob;
import org.bukkit.entity.LivingEntity;
import org.bukkit.scheduler.BukkitRunnable;
@ -29,17 +27,15 @@ public class MobSpawnTask extends BukkitRunnable {
private MobSign sign;
private int k, n;
private GameWorld gameWorld;
public MobSpawnTask(DungeonsAPI api, MobSign sign, int n) {
public MobSpawnTask(MobSign sign, int n) {
this.sign = sign;
gameWorld = sign.getGameWorld();
this.n = n;
}
@Override
public void run() {
if (gameWorld == null) {
if (isWorldFinished()) {
sign.deactivate();
return;
}