From e7733e749d44ee43e4ccfb4ce6d6498bcd1720c1 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 20 May 2015 02:09:22 +1000 Subject: [PATCH] Set the generators on loaded worlds - uses reflection to access private fields of CBS internals - sets the generator correctly (verified) - sets the populators correctly (verified) - should also work for augmented world types (needs verification) --- PlotSquared/pom.xml | 2 +- .../intellectualcrafters/plot/BukkitMain.java | 9 ++- .../plot/PlotSquared.java | 1 + .../plot/util/bukkit/SetGenCB.java | 55 +++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/SetGenCB.java diff --git a/PlotSquared/pom.xml b/PlotSquared/pom.xml index 435e49c14..abc5838b2 100644 --- a/PlotSquared/pom.xml +++ b/PlotSquared/pom.xml @@ -8,7 +8,7 @@ UTF-8 PlotSquared - 2.11.7 + 2.11.8 PlotSquared jar diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java index 0f7b4fb93..7a3c059c5 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java @@ -2,6 +2,7 @@ package com.intellectualcrafters.plot; import java.io.File; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import net.milkbowl.vault.economy.Economy; @@ -123,6 +124,7 @@ import com.intellectualcrafters.plot.util.bukkit.SendChunk; import com.intellectualcrafters.plot.util.bukkit.SetBlockFast; import com.intellectualcrafters.plot.util.bukkit.SetBlockFast_1_8; import com.intellectualcrafters.plot.util.bukkit.SetBlockSlow; +import com.intellectualcrafters.plot.util.bukkit.SetGenCB; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; import com.intellectualcrafters.plot.uuid.DefaultUUIDWrapper; import com.intellectualcrafters.plot.uuid.LowerOfflineUUIDWrapper; @@ -172,7 +174,12 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { if (worlds.size() > 0) { UUIDHandler.cacheAll(worlds.get(0).getName()); for (World world : worlds) { - Bukkit.getServer().unloadWorld(world, false); + try { + SetGenCB.setGenerator(world); + } catch (Exception e) { + log("Failed to reload world: " + world.getName()); + Bukkit.getServer().unloadWorld(world, false); + } } } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java index c16828815..dfdea3af4 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java @@ -498,6 +498,7 @@ public class PlotSquared { } public PlotSquared(final IPlotMain imp_class) { + SetupUtils.generators = new HashMap<>(); THIS = this; IMP = imp_class; try { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/SetGenCB.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/SetGenCB.java new file mode 100644 index 000000000..c8612be3f --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/SetGenCB.java @@ -0,0 +1,55 @@ +package com.intellectualcrafters.plot.util.bukkit; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Iterator; + +import org.bukkit.World; +import org.bukkit.generator.BlockPopulator; +import org.bukkit.generator.ChunkGenerator; + +import com.intellectualcrafters.plot.PlotSquared; +import com.intellectualcrafters.plot.generator.AugmentedPopulator; +import com.intellectualcrafters.plot.util.SetupUtils; + +public class SetGenCB { + public static void setGenerator(World world) throws Exception { + SetupUtils.manager.updateGenerators(); + PlotSquared.removePlotWorld(world.getName()); + ChunkGenerator gen = world.getGenerator(); + if (gen == null) { + return; + } + String name = gen.getClass().getCanonicalName(); + boolean set = false; + for (ChunkGenerator newGen : SetupUtils.generators.values()) { + if (newGen.getClass().getCanonicalName().equals(name)) { + // set generator + Field generator = world.getClass().getDeclaredField("generator"); + Field populators = world.getClass().getDeclaredField("populators"); + generator.setAccessible(true); + populators.setAccessible(true); + // Set populators (just in case) + populators.set(world, new ArrayList<>()); + // Set generator + Constructor constructor = newGen.getClass().getConstructor(String.class); + ChunkGenerator newNewGen = constructor.newInstance(world.getName()); + generator.set(world, newNewGen); + populators.set(world, newNewGen.getDefaultPopulators(world)); + // end + set = true; + break; + } + } + if (!set) { + Iterator iter = world.getPopulators().iterator(); + while (iter.hasNext()) { + if (iter.next() instanceof AugmentedPopulator) { + iter.remove(); + } + } + } + PlotSquared.loadWorld(world.getName(), null); + } +}