SteamWar/BauSystem
Archiviert
13
0

UnloadWorld rework

Dieser Commit ist enthalten in:
Lixfel 2019-03-31 18:18:23 +02:00
Ursprung 8efbb5a5b1
Commit 3183482184
3 geänderte Dateien mit 44 neuen und 52 gelöschten Zeilen

Datei anzeigen

@ -10,6 +10,7 @@ import de.warking.bausystem.commands.CommandGamemode;
import de.warking.bausystem.commands.CommandGui;
import de.warking.bausystem.commands.CommandTeleport;
import de.warking.bausystem.config.Config;
import de.warking.bausystem.world.LagDetectorRunnable;
import de.warking.bausystem.world.RegionListener;
import de.warking.hunjy.MySQL.WarkingUser;
import org.bukkit.Bukkit;
@ -148,7 +149,7 @@ public class BauSystem extends JavaPlugin implements Listener {
}
}, 20L, 20L);
plugin = this;
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), new LagDetectorRunnable(), 20, 20);
}
public static BauSystem getPlugin() {
return plugin;

Datei anzeigen

@ -7,7 +7,6 @@ import de.warking.hunjy.MySQL.WarkingUser;
import org.apache.commons.io.FileUtils;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import java.io.File;
import java.io.IOException;
@ -28,10 +27,7 @@ public class BauWorld {
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()));
LagDetectorRunnable detector = new LagDetectorRunnable(world, owner);
BukkitTask task = Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), detector, 20, 20);
detector.setTask(task);
Bukkit.createWorld(new WorldCreator(owner.toString()));
}
public static void unloadWorld(UUID owner) {

Datei anzeigen

@ -1,69 +1,64 @@
package de.warking.bausystem.world;
import de.warking.bausystem.BauSystem;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashMap;
import java.util.UUID;
class LagDetectorRunnable implements Runnable {
public 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;
worldOwner = owner;
}
private HashMap<UUID, Long> timerstart = new HashMap<>();
@Override
public void run() {
if (world == null || task.isCancelled()) {
task.cancel();
return;
}
world.setStorm(false);
world.setThundering(false);
world.setMonsterSpawnLimit(0);
world.setAnimalSpawnLimit(0);
for(World world : Bukkit.getWorlds()){
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++;
}
UUID worldOwner;
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;
try{
worldOwner = UUID.fromString(world.getName());
}catch(IllegalArgumentException E){
continue;
}
}
if (other > 1000) {
int players = 0;
int other = 0;
for (Entity e : world.getEntities()) {
if (e instanceof Player)
continue;
e.remove();
players++;
else
other++;
}
if(timerstart.containsKey(worldOwner)) {
if(players == 0){
if (System.currentTimeMillis() - timerstart.get(worldOwner) > 60000) {
BauWorld.unloadWorld(worldOwner);
continue;
}
}else{
timerstart.remove(worldOwner);
}
}else if(players == 0){
timerstart.put(worldOwner, System.currentTimeMillis());
}
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.");
}
BauWorld.broadcast(worldOwner, BauSystem.PREFIX +"§cLagverursachung erkannt. Es wurden §6"+other+" Entities§c entfernt.");
}
}
public void setTask(BukkitTask task) {
this.task = task;
}
}