From 6074fc8033aac3464bfc30e67a5e1314cbd27a1f Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Fri, 30 Sep 2016 14:26:20 +1000 Subject: [PATCH] Nukkit generator fixes --- .../database/plotme/LikePlotMeConverter.java | 2 - .../configuration/file/FileConfiguration.java | 5 ++- .../com/plotsquared/nukkit/NukkitMain.java | 42 ++++++++++++++++++- .../nukkit/generator/NukkitPlotGenerator.java | 5 +-- .../nukkit/util/NukkitSetupUtils.java | 16 ++++++- 5 files changed, 60 insertions(+), 10 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/database/plotme/LikePlotMeConverter.java b/Bukkit/src/main/java/com/plotsquared/bukkit/database/plotme/LikePlotMeConverter.java index ffee994ad..364868a35 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/database/plotme/LikePlotMeConverter.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/database/plotme/LikePlotMeConverter.java @@ -213,8 +213,6 @@ public class LikePlotMeConverter { } catch (IOException ignored) { ignored.printStackTrace(); } - } else { - System.out.println("FILE NOT EXIST " + plotmeDgFile.getAbsolutePath()); } for (Entry> entry : plots.entrySet()) { String world = entry.getKey(); diff --git a/Core/src/main/java/com/intellectualcrafters/configuration/file/FileConfiguration.java b/Core/src/main/java/com/intellectualcrafters/configuration/file/FileConfiguration.java index 4989f0084..2c4f61991 100644 --- a/Core/src/main/java/com/intellectualcrafters/configuration/file/FileConfiguration.java +++ b/Core/src/main/java/com/intellectualcrafters/configuration/file/FileConfiguration.java @@ -52,7 +52,10 @@ public abstract class FileConfiguration extends MemoryConfiguration { * any reason. */ public void save(File file) throws IOException { - file.getParentFile().mkdirs(); + File parent = file.getParentFile(); + if (parent != null) { + parent.mkdirs(); + } String data = saveToString(); diff --git a/Nukkit/src/main/java/com/plotsquared/nukkit/NukkitMain.java b/Nukkit/src/main/java/com/plotsquared/nukkit/NukkitMain.java index d5a1c6649..0daaa8ca2 100644 --- a/Nukkit/src/main/java/com/plotsquared/nukkit/NukkitMain.java +++ b/Nukkit/src/main/java/com/plotsquared/nukkit/NukkitMain.java @@ -10,9 +10,11 @@ import cn.nukkit.level.generator.Generator; import cn.nukkit.metadata.MetadataValue; import cn.nukkit.plugin.Plugin; import cn.nukkit.plugin.PluginBase; +import com.intellectualcrafters.configuration.ConfigurationSection; import com.intellectualcrafters.plot.IPlotMain; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.config.ConfigurationNode; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.generator.GeneratorWrapper; import com.intellectualcrafters.plot.generator.HybridGen; @@ -24,6 +26,7 @@ import com.intellectualcrafters.plot.object.PlotArea; import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.RunnableVal; +import com.intellectualcrafters.plot.object.SetupObject; import com.intellectualcrafters.plot.object.chat.PlainChatManager; import com.intellectualcrafters.plot.util.AbstractTitle; import com.intellectualcrafters.plot.util.ChatManager; @@ -278,7 +281,7 @@ public final class NukkitMain extends PluginBase implements Listener, IPlotMain Class gen = Generator.getGenerator(name); if (gen != null) { Generator instance = gen.getConstructor(Map.class).newInstance(map); - if (GeneratorWrapper.class.isAssignableFrom(gen.getClass())) { + if (instance instanceof GeneratorWrapper) { return (GeneratorWrapper) instance; } map.put("generator", instance); @@ -346,7 +349,42 @@ public final class NukkitMain extends PluginBase implements Listener, IPlotMain @Override public void setGenerator(String worldName) { - throw new UnsupportedOperationException("Not implemented: setGenerator"); + Level world = getServer().getLevelByName(worldName); + if (world == null) { + // create world + ConfigurationSection worldConfig = PS.get().worlds.getConfigurationSection("worlds." + worldName); + String manager = worldConfig.getString("generator.plugin", getPluginName()); + SetupObject setup = new SetupObject(); + setup.plotManager = manager; + setup.setupGenerator = worldConfig.getString("generator.init", manager); + setup.type = worldConfig.getInt("generator.type"); + setup.terrain = worldConfig.getInt("generator.terrain"); + setup.step = new ConfigurationNode[0]; + setup.world = worldName; + SetupUtils.manager.setupWorld(setup); + world = getServer().getLevelByName(worldName); + } else { + HashMap map = new HashMap<>(); + map.put("world", world.getName()); + map.put("plot-generator", PS.get().IMP.getDefaultGenerator()); + setGenerator(world, new NukkitPlotGenerator(map)); + } + if (world != null) { + try { + Field fieldInstance = Level.class.getDeclaredField("generatorInstance"); + fieldInstance.setAccessible(true); + Generator gen = (Generator) fieldInstance.get(world); + if (gen instanceof NukkitPlotGenerator) { + PS.get().loadWorld(worldName, (NukkitPlotGenerator) gen); + } else if (gen instanceof GeneratorWrapper) { + PS.get().loadWorld(worldName, (GeneratorWrapper) gen); + } else if (PS.get().worlds.contains("worlds." + worldName)) { + PS.get().loadWorld(worldName, null); + } + } catch (Throwable e) { + e.printStackTrace(); + } + } } private void setGenerator(Level level, Generator generator) { diff --git a/Nukkit/src/main/java/com/plotsquared/nukkit/generator/NukkitPlotGenerator.java b/Nukkit/src/main/java/com/plotsquared/nukkit/generator/NukkitPlotGenerator.java index 57ad4936d..931b568d5 100644 --- a/Nukkit/src/main/java/com/plotsquared/nukkit/generator/NukkitPlotGenerator.java +++ b/Nukkit/src/main/java/com/plotsquared/nukkit/generator/NukkitPlotGenerator.java @@ -6,7 +6,6 @@ import cn.nukkit.math.NukkitRandom; import cn.nukkit.math.Vector3; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.generator.GeneratorWrapper; -import com.intellectualcrafters.plot.generator.HybridGen; import com.intellectualcrafters.plot.generator.IndependentPlotGenerator; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.PlotArea; @@ -43,10 +42,10 @@ public class NukkitPlotGenerator extends Generator implements GeneratorWrapper map = new HashMap<>(); map.put("world", object.world); map.put("plot-generator", PS.get().IMP.getDefaultGenerator()); - plugin.getServer().generateLevel(object.world, object.world.hashCode(), NukkitPlotGenerator.class, map); + if (!plugin.getServer().generateLevel(object.world, object.world.hashCode(), NukkitPlotGenerator.class, map)) { + plugin.getServer().loadLevel(object.world); + } + try { + File nukkitFile = new File("nukkit.yml"); + YamlConfiguration nukkitYml = YamlConfiguration.loadConfiguration(nukkitFile); + nukkitYml.set("worlds." + object.world + ".generator", object.setupGenerator); + nukkitYml.save(nukkitFile); + } catch (Throwable e) { + e.printStackTrace(); + } } else { - plugin.getServer().generateLevel(object.world, object.world.hashCode()); + if (!plugin.getServer().generateLevel(object.world, object.world.hashCode())) { + plugin.getServer().loadLevel(object.world); + } } return object.world; }