diff --git a/WorldSystem/bin/config.yml b/WorldSystem/bin/config.yml index 719f1e9..6dbf4c1 100644 --- a/WorldSystem/bin/config.yml +++ b/WorldSystem/bin/config.yml @@ -37,6 +37,10 @@ language: en # Prefix which will be shown before each message prefix: '&8[&3WorldSystem&8] &6' +# Time in days after a not used world will be deleted +# Set to -1 to disable +delete_after: -1 + # Whether WorldSystem should contact the Mojang authserver # If not, some unknown playernames will not be displayed # eg. in the gui or in /ws info diff --git a/WorldSystem/bin/plugin.yml b/WorldSystem/bin/plugin.yml index bfa395f..7801e38 100644 --- a/WorldSystem/bin/plugin.yml +++ b/WorldSystem/bin/plugin.yml @@ -1,5 +1,5 @@ name: WorldSystem -version: 2.2.0.2 +version: 2.2.0.3 author: Butzlabben main: de.butzlabben.world.WorldSystem loadbefore: [FastAsyncWorldEdit, WorldEdit] diff --git a/WorldSystem/src/config.yml b/WorldSystem/src/config.yml index 719f1e9..6dbf4c1 100644 --- a/WorldSystem/src/config.yml +++ b/WorldSystem/src/config.yml @@ -37,6 +37,10 @@ language: en # Prefix which will be shown before each message prefix: '&8[&3WorldSystem&8] &6' +# Time in days after a not used world will be deleted +# Set to -1 to disable +delete_after: -1 + # Whether WorldSystem should contact the Mojang authserver # If not, some unknown playernames will not be displayed # eg. in the gui or in /ws info diff --git a/WorldSystem/src/de/butzlabben/world/WorldSystem.java b/WorldSystem/src/de/butzlabben/world/WorldSystem.java index a468fd6..344c5a4 100644 --- a/WorldSystem/src/de/butzlabben/world/WorldSystem.java +++ b/WorldSystem/src/de/butzlabben/world/WorldSystem.java @@ -75,6 +75,7 @@ public class WorldSystem extends JavaPlugin { Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new WorldCheckerRunnable(), 20 * 5, 20 * PluginConfig.getLagCheckPeriod()); + if (PluginConfig.useGC()) { Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new GCRunnable(), 20 * 5, 20 * PluginConfig.getGCPeriod()); @@ -139,6 +140,12 @@ public class WorldSystem extends JavaPlugin { sw.stopCreating(); }; } + + // Starting for #28 + if (PluginConfig.shouldDelete()) { + Bukkit.getConsoleSender().sendMessage(PluginConfig.getPrefix() + "Searching for old worlds to delete if not loaded for " + PluginConfig.deleteAfter() + " days"); + DependenceConfig.checkWorlds(); + } Bukkit.getConsoleSender().sendMessage(PluginConfig.getPrefix() + "Succesfully enabled WorldSystem v" + version); } @@ -172,6 +179,7 @@ public class WorldSystem extends JavaPlugin { PluginConfig.checkConfig(config); // Done with #6 MessageConfig.checkConfig(new File(languages, "en.yml")); + MessageConfig.checkConfig(new File(languages, "de.yml")); MessageConfig.checkConfig(new File(languages, "hu.yml")); MessageConfig.checkConfig(new File(languages, "nl.yml")); diff --git a/WorldSystem/src/de/butzlabben/world/command/WSDeleteCommand.java b/WorldSystem/src/de/butzlabben/world/command/WSDeleteCommand.java index d848b1e..048e65e 100644 --- a/WorldSystem/src/de/butzlabben/world/command/WSDeleteCommand.java +++ b/WorldSystem/src/de/butzlabben/world/command/WSDeleteCommand.java @@ -1,7 +1,6 @@ package de.butzlabben.world.command; import java.io.File; - import org.apache.commons.io.FileUtils; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @@ -41,10 +40,12 @@ public class WSDeleteCommand implements CommandExecutor { if (sw != null && sw.isLoaded()) sw.directUnload(Bukkit.getWorld(worldname)); Bukkit.getScheduler().runTaskLater(WorldSystem.getInstance(), () -> { - @SuppressWarnings("deprecation") - OfflinePlayer op = Bukkit.getOfflinePlayer(args[1]); + OfflinePlayer op = dc.getOwner(); + String uuid = op.getUniqueId().toString(); File dir = new File(PluginConfig.getWorlddir() + "/" + worldname); + if(!dir.exists()) + dir = new File(Bukkit.getWorldContainer(), worldname); if (dir.exists()) try { FileUtils.deleteDirectory(dir); diff --git a/WorldSystem/src/de/butzlabben/world/config/DependenceConfig.java b/WorldSystem/src/de/butzlabben/world/config/DependenceConfig.java index 1219097..4ceec0f 100644 --- a/WorldSystem/src/de/butzlabben/world/config/DependenceConfig.java +++ b/WorldSystem/src/de/butzlabben/world/config/DependenceConfig.java @@ -2,6 +2,8 @@ package de.butzlabben.world.config; import java.io.File; import java.io.IOException; +import java.util.UUID; + import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.configuration.file.YamlConfiguration; @@ -15,15 +17,16 @@ public class DependenceConfig { setConfig(); } + @SuppressWarnings("deprecation") public DependenceConfig(String s) { - @SuppressWarnings("deprecation") - OfflinePlayer op = Bukkit.getOfflinePlayer(s); - if(op == null) - return; + OfflinePlayer op = Bukkit.getOfflinePlayer(UUID.fromString(s)); + if (op == null) { + op = Bukkit.getOfflinePlayer(s); + if (op == null) + return; + } this.op = op; } - - private void setConfig() { File dconfig = new File("plugins//WorldSystem//dependence.yml"); @@ -40,7 +43,7 @@ public class DependenceConfig { this.op = p; refreshName(); } - + public DependenceConfig(OfflinePlayer p) { this.op = p; refreshName(); @@ -109,6 +112,18 @@ public class DependenceConfig { return name; } + public void setLastLoaded() { + File dconfig = new File("plugins//WorldSystem//dependence.yml"); + YamlConfiguration cfg = YamlConfiguration.loadConfiguration(dconfig); + String uuid = op.getUniqueId().toString(); + cfg.set("Dependences." + uuid + ".last_loaded", System.currentTimeMillis()); + try { + cfg.save(dconfig); + } catch (IOException e) { + e.printStackTrace(); + } + } + public int getHighestID() { File dconfig = new File("plugins//WorldSystem//dependence.yml"); YamlConfiguration dcfg = YamlConfiguration.loadConfiguration(dconfig); @@ -120,5 +135,27 @@ public class DependenceConfig { YamlConfiguration dcfg = YamlConfiguration.loadConfiguration(dconfig); return dcfg.getInt("Dependences." + op.getUniqueId().toString() + ".ID"); } + + public OfflinePlayer getOwner() { + return op; + } + public static void checkWorlds() { + File dconfig = new File("plugins//WorldSystem//dependence.yml"); + YamlConfiguration cfg = YamlConfiguration.loadConfiguration(dconfig); + + long deleteTime = 1000 * 60 * 60 * 24 * PluginConfig.deleteAfter(); + long now = System.currentTimeMillis(); + for (String s : cfg.getConfigurationSection("Dependences").getKeys(false)) { + if (!cfg.isLong("Dependences." + s + ".last_loaded") && !cfg.isInt("Dependences." + s + ".last_loaded")) + continue; + long lastLoaded = cfg.getLong("Dependences." + s + ".last_loaded"); + long diff = now - lastLoaded; + if (diff > deleteTime) { + Bukkit.getConsoleSender().sendMessage( + PluginConfig.getPrefix() + "World of " + s + " was not loaded for too long. Deleting!"); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "ws delete " + s); + } + } + } } diff --git a/WorldSystem/src/de/butzlabben/world/config/GuiConfig.java b/WorldSystem/src/de/butzlabben/world/config/GuiConfig.java index 10fcf14..12b95c0 100644 --- a/WorldSystem/src/de/butzlabben/world/config/GuiConfig.java +++ b/WorldSystem/src/de/butzlabben/world/config/GuiConfig.java @@ -1,8 +1,12 @@ package de.butzlabben.world.config; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.Charset; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; @@ -40,7 +44,12 @@ public class GuiConfig { } public static YamlConfiguration getConfig() { - return YamlConfiguration.loadConfiguration(file); + try { + return YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"))); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return null; } public static int getSlot(String path) { diff --git a/WorldSystem/src/de/butzlabben/world/config/PluginConfig.java b/WorldSystem/src/de/butzlabben/world/config/PluginConfig.java index 7a68395..317adf5 100644 --- a/WorldSystem/src/de/butzlabben/world/config/PluginConfig.java +++ b/WorldSystem/src/de/butzlabben/world/config/PluginConfig.java @@ -16,6 +16,7 @@ import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.WorldCreator; import org.bukkit.World.Environment; import org.bukkit.WorldType; import org.bukkit.configuration.file.YamlConfiguration; @@ -38,7 +39,8 @@ public class PluginConfig { if (false == (cfg.isString("worldfolder") && cfg.isString("worldsource") && cfg.isInt("unloadingtime") && cfg.isBoolean("survival") && cfg.isString("language") && cfg.isString("prefix") && cfg.isInt("request_expires") && cfg.isBoolean("need_confirm") - && cfg.isBoolean("contact_authserver") && cfg.isBoolean("spawn_teleportation") && + && cfg.isBoolean("contact_authserver") && cfg.isBoolean("spawn_teleportation") + && cfg.isInt("delete_after") && cfg.isInt("lagsystem.period_in_seconds") && cfg.isInt("lagsystem.entities_per_world") && cfg.isBoolean("lagsystem.garbagecollector.use") @@ -98,10 +100,6 @@ public class PluginConfig { return null; } - public static String getLicenseKey() { - return getConfig().getString("license"); - } - public static int getGCPeriod() { return getConfig().getInt("lagsystem.garbagecollector.period_in_minutes", 5); } @@ -215,4 +213,27 @@ public class PluginConfig { public static boolean spawnTeleportation() { return getConfig().getBoolean("spawn_teleportation", true); } + + public static boolean shouldDelete() { + return getConfig().getInt("delete_after") != -1; + } + + public static int deleteAfter() { + return getConfig().getInt("delete_after"); + } + + public static WorldCreator getWorldCreator(String worldname) { + WorldCreator creator = new WorldCreator(worldname); + long seed = PluginConfig.getSeed(); + Environment env = PluginConfig.getEnvironment(); + WorldType type = PluginConfig.getWorldType(); + if (seed != 0) + creator.seed(seed); + creator.type(type); + creator.environment(env); + String generator = PluginConfig.getGenerator(); + if (!generator.trim().isEmpty()) + creator.generator(generator); + return creator; + } } diff --git a/WorldSystem/src/de/butzlabben/world/config/SettingsConfig.java b/WorldSystem/src/de/butzlabben/world/config/SettingsConfig.java index 1a1f4b2..70d6c1e 100644 --- a/WorldSystem/src/de/butzlabben/world/config/SettingsConfig.java +++ b/WorldSystem/src/de/butzlabben/world/config/SettingsConfig.java @@ -1,8 +1,12 @@ package de.butzlabben.world.config; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.Charset; import java.nio.file.Files; import java.util.HashMap; import java.util.UUID; @@ -133,7 +137,12 @@ public class SettingsConfig { } private static YamlConfiguration getConfig() { - return YamlConfiguration.loadConfiguration(file); + try { + return YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"))); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return null; } public static void checkConfig() { diff --git a/WorldSystem/src/de/butzlabben/world/wrapper/SystemWorld.java b/WorldSystem/src/de/butzlabben/world/wrapper/SystemWorld.java index 66afc51..35e826b 100644 --- a/WorldSystem/src/de/butzlabben/world/wrapper/SystemWorld.java +++ b/WorldSystem/src/de/butzlabben/world/wrapper/SystemWorld.java @@ -8,10 +8,9 @@ import org.apache.commons.io.FileUtils; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.GameMode; +import org.bukkit.OfflinePlayer; import org.bukkit.World; -import org.bukkit.World.Environment; import org.bukkit.WorldCreator; -import org.bukkit.WorldType; import org.bukkit.entity.Player; import com.google.common.base.Preconditions; @@ -150,7 +149,7 @@ public class SystemWorld { @Override public void run() { // Still in world unloading process? - if (unloading) { + if (unloading && w.getPlayers().size() == 0) { if (Bukkit.unloadWorld(w, true)) { File worldinserver = new File(Bukkit.getWorldContainer(), worldname); File worlddir = new File(PluginConfig.getWorlddir()); @@ -225,7 +224,7 @@ public class SystemWorld { worldname = myName.toString(); } - WorldCreator creator = new WorldCreator(worldname); + WorldCreator creator = PluginConfig.getWorldCreator(worldname); World w = Bukkit.getWorld(worldname); if (w == null) @@ -242,6 +241,10 @@ public class SystemWorld { } else { p.teleport(w.getSpawnLocation()); } + + OfflinePlayer owner = Bukkit.getOfflinePlayer(WorldConfig.getWorldConfig(worldname).getOwner()); + DependenceConfig dc = new DependenceConfig(owner); + dc.setLastLoaded(); } /** @@ -259,17 +262,7 @@ public class SystemWorld { int id = dc.getHighestID() + 1; String worldname = "ID" + id + "-" + uuid; - WorldCreator creator = new WorldCreator(worldname); - long seed = PluginConfig.getSeed(); - Environment env = PluginConfig.getEnvironment(); - WorldType type = PluginConfig.getWorldType(); - if (seed != 0) - creator.seed(seed); - creator.type(type); - creator.environment(env); - String generator = PluginConfig.getGenerator(); - if (!generator.trim().isEmpty()) - creator.generator(generator); + WorldCreator creator = PluginConfig.getWorldCreator(worldname); WorldCreateEvent event = new WorldCreateEvent(p, creator); Bukkit.getPluginManager().callEvent(event); diff --git a/WorldSystem/src/plugin.yml b/WorldSystem/src/plugin.yml index bfa395f..7801e38 100644 --- a/WorldSystem/src/plugin.yml +++ b/WorldSystem/src/plugin.yml @@ -1,5 +1,5 @@ name: WorldSystem -version: 2.2.0.2 +version: 2.2.0.3 author: Butzlabben main: de.butzlabben.world.WorldSystem loadbefore: [FastAsyncWorldEdit, WorldEdit]