Fix worldEnvironment; resolves #805

This commit is contained in:
Daniel Saukel 2020-11-03 01:00:22 +01:00
parent 7ace66d8df
commit f05d7244b6
3 changed files with 29 additions and 27 deletions

View File

@ -25,6 +25,7 @@ import de.erethon.dungeonsxl.api.player.PlayerCache;
import de.erethon.dungeonsxl.api.sign.DungeonSign;
import de.erethon.dungeonsxl.api.world.InstanceWorld;
import de.erethon.dungeonsxl.util.commons.chat.MessageUtil;
import de.erethon.dungeonsxl.util.commons.compatibility.Version;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
@ -45,7 +46,7 @@ public abstract class DInstanceWorld implements InstanceWorld {
protected DungeonsXL plugin;
protected PlayerCache dPlayers;
static int counter;
private static int counter;
protected Map<Block, DungeonSign> signs = new HashMap<>();
private DResourceWorld resourceWorld;
@ -201,10 +202,27 @@ public abstract class DInstanceWorld implements InstanceWorld {
/**
* @return a name for the instance
* @param game whether the instance is a GameWorld
* @param id the id to use
*/
public static String generateName(boolean game, int id) {
return "DXL_" + (game ? "Game" : "Edit") + "_" + id;
public static String generateName(boolean game) {
String name = "DXL_" + (game ? "Game" : "Edit") + "_" + counter;
File instanceFolder = new File(Bukkit.getWorldContainer(), name);
while (instanceFolder.exists()) {
World world = Bukkit.getWorld(name);
boolean removed = false;
if (world != null && world.getPlayers().isEmpty()) {
Bukkit.unloadWorld(name, /* SPIGOT-5225 */ !Version.isAtLeast(Version.MC1_14_4));
}
if (world == null || world.getPlayers().isEmpty()) {
removed = instanceFolder.delete();
}
if (!removed) {
MessageUtil.log(DungeonsXL.getInstance(), "&6Warning: An unrecognized junk instance (&4" + name + "&6) has been found, but could not be deleted.");
counter++;
name = "DXL_" + (game ? "Game" : "Edit") + "_" + counter;
instanceFolder = new File(Bukkit.getWorldContainer(), name);
}
}
return name;
}
@Override

View File

@ -113,7 +113,7 @@ public class DResourceWorld implements ResourceWorld {
public WorldConfig getConfig(boolean generate) {
if (config == null) {
File file = new File(folder, WorldConfig.FILE_NAME);
if (file.exists()) {
if (!file.exists() && generate) {
try {
file.createNewFile();
} catch (IOException exception) {
@ -128,7 +128,7 @@ public class DResourceWorld implements ResourceWorld {
@Override
public Environment getWorldEnvironment() {
return (config != null && config.getWorldEnvironment() != null) ? config.getWorldEnvironment() : Environment.NORMAL;
return (getConfig(false) != null && getConfig(false).getWorldEnvironment() != null) ? getConfig(false).getWorldEnvironment() : Environment.NORMAL;
}
@Override
@ -178,26 +178,8 @@ public class DResourceWorld implements ResourceWorld {
public DInstanceWorld instantiate(boolean game) {
plugin.setLoadingWorld(true);
int id = DInstanceWorld.counter;
String name = DInstanceWorld.generateName(game, id);
String name = DInstanceWorld.generateName(game);
File instanceFolder = new File(Bukkit.getWorldContainer(), name);
while (instanceFolder.exists()) {
World world = Bukkit.getWorld(name);
boolean removed = false;
if (world != null && world.getPlayers().isEmpty()) {
Bukkit.unloadWorld(name, /* SPIGOT-5225 */ !Version.isAtLeast(Version.MC1_14_4));
}
if (world == null || world.getPlayers().isEmpty()) {
removed = instanceFolder.delete();
}
if (!removed) {
MessageUtil.log(plugin, "&6Warning: An unrecognized junk instance (&4" + name + "&6) has been found, but could not be deleted.");
id++;
name = DInstanceWorld.generateName(game, id);
instanceFolder = new File(Bukkit.getWorldContainer(), name);
}
}
DInstanceWorld instance = game ? new DGameWorld(plugin, this, instanceFolder) : new DEditWorld(plugin, this, instanceFolder);
ResourceWorldInstantiateEvent event = new ResourceWorldInstantiateEvent(this, name);
@ -279,8 +261,7 @@ public class DResourceWorld implements ResourceWorld {
* @return the automatically created DEditWorld instance
*/
public DEditWorld generate() {
int id = DInstanceWorld.counter;
String name = DInstanceWorld.generateName(false, id);
String name = DInstanceWorld.generateName(false);
File folder = new File(Bukkit.getWorldContainer(), name);
WorldCreator creator = new WorldCreator(name);
creator.type(WorldType.FLAT);

View File

@ -19,6 +19,7 @@ package de.erethon.dungeonsxl.world;
import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.api.dungeon.GameRule;
import de.erethon.dungeonsxl.api.dungeon.GameRuleContainer;
import de.erethon.dungeonsxl.util.commons.misc.EnumUtil;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@ -67,6 +68,8 @@ public class WorldConfig extends GameRuleContainer {
public void load() {
plugin.getGameRuleRegistry().forEach(this::updateGameRule);
invitedPlayers = config.getStringList("invitedPlayers");
worldEnvironment = EnumUtil.getEnumIgnoreCase(Environment.class, config.getString("worldEnvironment", Environment.NORMAL.name()));
}
public void updateGameRule(GameRule rule) {