UnloadWorld rework
Dieser Commit ist enthalten in:
Ursprung
8efbb5a5b1
Commit
3183482184
@ -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;
|
||||||
|
@ -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) {
|
||||||
|
@ -1,69 +1,64 @@
|
|||||||
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();
|
world.setStorm(false);
|
||||||
return;
|
world.setThundering(false);
|
||||||
}
|
world.setMonsterSpawnLimit(0);
|
||||||
world.setStorm(false);
|
world.setAnimalSpawnLimit(0);
|
||||||
world.setThundering(false);
|
|
||||||
world.setMonsterSpawnLimit(0);
|
|
||||||
world.setAnimalSpawnLimit(0);
|
|
||||||
|
|
||||||
int players = 0;
|
UUID worldOwner;
|
||||||
int other = 0;
|
|
||||||
for (Entity e : world.getEntities()) {
|
|
||||||
if (e instanceof Player)
|
|
||||||
players++;
|
|
||||||
else
|
|
||||||
other++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(players == 0 && timerstart == 0) {
|
try{
|
||||||
timerstart = System.currentTimeMillis();
|
worldOwner = UUID.fromString(world.getName());
|
||||||
}else if(players != 0){
|
}catch(IllegalArgumentException E){
|
||||||
timerstart = 0;
|
continue;
|
||||||
}else{
|
|
||||||
if (System.currentTimeMillis() - timerstart > 60000) {
|
|
||||||
BauWorld.unloadWorld(worldOwner);
|
|
||||||
task.cancel();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren