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