diff --git a/src/de/warking/bausystem/world/BauWorld.java b/src/de/warking/bausystem/world/BauWorld.java index 7c8bec5..8ff3498 100644 --- a/src/de/warking/bausystem/world/BauWorld.java +++ b/src/de/warking/bausystem/world/BauWorld.java @@ -5,11 +5,9 @@ import de.warking.hunjy.MySQL.Bauwelt; import de.warking.hunjy.MySQL.BauweltMember; import de.warking.hunjy.MySQL.WarkingUser; import org.apache.commons.io.FileUtils; -import org.bukkit.Bukkit; -import org.bukkit.Chunk; -import org.bukkit.World; -import org.bukkit.WorldCreator; +import org.bukkit.*; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; import java.io.File; import java.io.IOException; @@ -25,17 +23,15 @@ public class BauWorld { File w = new File("plugins/BauSystem/worlds/" + owner); File region = new File("plugins/WorldGuard/worlds/" + owner); - if (!region.exists() || !region.isDirectory()) { + if (!region.exists() || !region.isDirectory()) FileUtils.copyDirectory(new File(BauSystem.getInstance().config.regionDir), new File("plugins/WorldGuard/worlds/" + owner)); - } - - if (w.exists() && w.isDirectory()) { - world = Bukkit.createWorld(new WorldCreator(owner.toString())); - } else { + if (!w.exists() || !w.isDirectory()) FileUtils.copyDirectory(new File(BauSystem.getInstance().config.backupDir), new File("plugins/BauSystem/worlds/" + owner.toString())); - world = Bukkit.createWorld(new WorldCreator(owner.toString())); - } - Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), new LagDetectorRunnable(world, owner), 20, 20); + + world = Bukkit.createWorld(new WorldCreator(owner.toString())); + LagDetectorRunnable detector = new LagDetectorRunnable(world, owner); + BukkitTask task = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), detector, 20, 20); + detector.setTask(task); } public static void unloadWorld(UUID owner) { @@ -44,15 +40,15 @@ public class BauWorld { if (world != null) { for (Player p : world.getPlayers()) { p.teleport(BauSystem.getInstance().config.spawn); + p.setGameMode(GameMode.ADVENTURE); } - world.save(); - for (Chunk c : world.getLoadedChunks()) { c.unload(); } - Bukkit.unloadWorld(world, true); + if(!Bukkit.unloadWorld(world, true)) + System.out.println("Unloading world failed!"); } } diff --git a/src/de/warking/bausystem/world/LagDetectorRunnable.java b/src/de/warking/bausystem/world/LagDetectorRunnable.java index d982e02..a60d2e2 100644 --- a/src/de/warking/bausystem/world/LagDetectorRunnable.java +++ b/src/de/warking/bausystem/world/LagDetectorRunnable.java @@ -4,6 +4,7 @@ import de.warking.bausystem.BauSystem; import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; import java.util.UUID; @@ -12,6 +13,7 @@ class LagDetectorRunnable implements Runnable { private final World world; private final UUID worldOwner; private long timerstart = 0; + private BukkitTask task; public LagDetectorRunnable(World w, UUID owner) { world = w; @@ -20,43 +22,48 @@ class LagDetectorRunnable implements Runnable { @Override public void run() { - if (world == null) { - return; - } - world.setStorm(false); - world.setThundering(false); - world.setMonsterSpawnLimit(0); - world.setAnimalSpawnLimit(0); - - int players = 0; - int other = 0; - for (Entity e : world.getEntities()) { - if (e instanceof Player) - players++; - else - other++; - } - - if(players == 0 && timerstart == 0) { - timerstart = System.currentTimeMillis(); - }else if(players != 0){ - timerstart = 0; - }else{ - if (System.currentTimeMillis() - timerstart > 60000) { - BauWorld.unloadWorld(worldOwner); + if (world == null) { + task.cancel(); return; } - } + world.setStorm(false); + world.setThundering(false); + world.setMonsterSpawnLimit(0); + world.setAnimalSpawnLimit(0); - - if (other > 1000) { - for (Entity e : world.getEntities()) { + int players = 0; + int other = 0; + for (Entity e : world.getEntities()) { if (e instanceof Player) - continue; - e.remove(); - } - BauWorld.broadcast(worldOwner, BauSystem.PREFIX +"§cLagverursachung erkannt. Es wurden §6"+other+" Entities§c entfernt."); - } + players++; + else + other++; + } + + if(players == 0 && timerstart == 0) { + timerstart = System.currentTimeMillis(); + }else if(players != 0){ + timerstart = 0; + }else{ + if (System.currentTimeMillis() - timerstart > 60000) { + BauWorld.unloadWorld(worldOwner); + task.cancel(); + return; + } + } + + + if (other > 1000) { + for (Entity e : world.getEntities()) { + if (e instanceof Player) + continue; + e.remove(); + } + BauWorld.broadcast(worldOwner, BauSystem.PREFIX +"§cLagverursachung erkannt. Es wurden §6"+other+" Entities§c entfernt."); + } } + public void setTask(BukkitTask task) { + this.task = task; + } }