Fixed async unloading task issue

This commit is contained in:
Butzlabben 2019-10-04 13:29:29 +02:00
parent 42efaf4518
commit f5e53bd3e8

View File

@ -11,6 +11,7 @@ import org.apache.commons.io.FileUtils;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -30,6 +31,8 @@ public class SystemWorld {
private boolean unloading = false; private boolean unloading = false;
private boolean creating = false; private boolean creating = false;
private BukkitTask unloadLaterTask;
private SystemWorld(String worldname) { private SystemWorld(String worldname) {
this.worldname = worldname; this.worldname = worldname;
w = Bukkit.getWorld(worldname); w = Bukkit.getWorld(worldname);
@ -177,7 +180,7 @@ public class SystemWorld {
return; return;
} }
Preconditions.checkNotNull(w, "world must not be null"); Preconditions.checkNotNull(w, "world must not be null");
unloading = true; setUnloading(true);
w.save(); w.save();
Chunk[] arrayOfChunk; Chunk[] arrayOfChunk;
int j = (arrayOfChunk = w.getLoadedChunks()).length; int j = (arrayOfChunk = w.getLoadedChunks()).length;
@ -229,7 +232,7 @@ public class SystemWorld {
if (event.isCancelled()) if (event.isCancelled())
return; return;
// Set unloading to true // Set unloading to true
unloading = true; setUnloading(true);
w.save(); w.save();
Chunk[] arrayOfChunk; Chunk[] arrayOfChunk;
int j = (arrayOfChunk = w.getLoadedChunks()).length; int j = (arrayOfChunk = w.getLoadedChunks()).length;
@ -242,7 +245,7 @@ public class SystemWorld {
a.setGameMode(PluginConfig.getSpawnGamemode()); a.setGameMode(PluginConfig.getSpawnGamemode());
} }
Bukkit.getScheduler().runTaskLater(WorldSystem.getInstance(), () -> { unloadLaterTask = Bukkit.getScheduler().runTaskLater(WorldSystem.getInstance(), () -> {
// Still in world unloading process? // Still in world unloading process?
if (unloading && w.getPlayers().size() == 0) { if (unloading && w.getPlayers().size() == 0) {
if (Bukkit.unloadWorld(w, true)) { if (Bukkit.unloadWorld(w, true)) {
@ -251,7 +254,7 @@ public class SystemWorld {
try { try {
FileUtils.moveDirectoryToDirectory(worldinserver, worlddir, false); FileUtils.moveDirectoryToDirectory(worldinserver, worlddir, false);
Bukkit.getWorlds().remove(w); Bukkit.getWorlds().remove(w);
unloading = false; setUnloading(false);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -285,7 +288,7 @@ public class SystemWorld {
if (event.isCancelled()) if (event.isCancelled())
return; return;
unloading = false; setUnloading(false);
p.sendMessage(MessageConfig.getSettingUpWorld()); p.sendMessage(MessageConfig.getSettingUpWorld());
@ -388,7 +391,7 @@ public class SystemWorld {
return; return;
} }
unloading = false; setUnloading(false);
w = Bukkit.getWorld(worldname); w = Bukkit.getWorld(worldname);
if (w == null) if (w == null)
return; return;
@ -429,6 +432,15 @@ public class SystemWorld {
this.creating = creating; this.creating = creating;
} }
public void setUnloading(boolean unloading) {
this.unloading = unloading;
// Cancel unload task if unloading is set to false
if (!unloading && unloadLaterTask != null && !unloadLaterTask.isCancelled()) {
unloadLaterTask.cancel();
}
}
/** /**
* @return the worldname * @return the worldname
*/ */