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

Datei anzeigen

@ -7,7 +7,6 @@ import de.warking.hunjy.MySQL.WarkingUser;
import org.apache.commons.io.FileUtils; 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.BukkitTask;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -28,10 +27,7 @@ public class BauWorld {
if (!w.exists() || !w.isDirectory()) if (!w.exists() || !w.isDirectory())
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())); 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) { public static void unloadWorld(UUID owner) {

Datei anzeigen

@ -1,36 +1,34 @@
package de.warking.bausystem.world; package de.warking.bausystem.world;
import de.warking.bausystem.BauSystem; import de.warking.bausystem.BauSystem;
import org.bukkit.Bukkit;
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.HashMap;
import java.util.UUID; import java.util.UUID;
class LagDetectorRunnable implements Runnable { public class LagDetectorRunnable implements Runnable {
private final World world; private HashMap<UUID, Long> timerstart = new HashMap<>();
private final UUID worldOwner;
private long timerstart = 0;
private BukkitTask task;
public LagDetectorRunnable(World w, UUID owner) {
world = w;
worldOwner = owner;
}
@Override @Override
public void run() { public void run() {
if (world == null || task.isCancelled()) { for(World world : Bukkit.getWorlds()){
task.cancel();
return;
}
world.setStorm(false); world.setStorm(false);
world.setThundering(false); world.setThundering(false);
world.setMonsterSpawnLimit(0); world.setMonsterSpawnLimit(0);
world.setAnimalSpawnLimit(0); world.setAnimalSpawnLimit(0);
UUID worldOwner;
try{
worldOwner = UUID.fromString(world.getName());
}catch(IllegalArgumentException E){
continue;
}
int players = 0; int players = 0;
int other = 0; int other = 0;
for (Entity e : world.getEntities()) { for (Entity e : world.getEntities()) {
@ -40,18 +38,18 @@ class LagDetectorRunnable implements Runnable {
other++; other++;
} }
if(players == 0 && timerstart == 0) { if(timerstart.containsKey(worldOwner)) {
timerstart = System.currentTimeMillis(); if(players == 0){
}else if(players != 0){ if (System.currentTimeMillis() - timerstart.get(worldOwner) > 60000) {
timerstart = 0;
}else{
if (System.currentTimeMillis() - timerstart > 60000) {
BauWorld.unloadWorld(worldOwner); BauWorld.unloadWorld(worldOwner);
task.cancel(); continue;
return;
} }
}else{
timerstart.remove(worldOwner);
}
}else if(players == 0){
timerstart.put(worldOwner, System.currentTimeMillis());
} }
if (other > 1000) { if (other > 1000) {
for (Entity e : world.getEntities()) { for (Entity e : world.getEntities()) {
@ -62,8 +60,5 @@ class LagDetectorRunnable implements Runnable {
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;
} }
} }