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)
This commit is contained in:
boy0001 2015-05-20 02:09:22 +10:00
parent bb4f8fceec
commit e7733e749d
4 changed files with 65 additions and 2 deletions

View File

@ -8,7 +8,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<artifactId>PlotSquared</artifactId>
<version>2.11.7</version>
<version>2.11.8</version>
<name>PlotSquared</name>
<packaging>jar</packaging>
<build>

View File

@ -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);
}
}
}
}

View File

@ -498,6 +498,7 @@ public class PlotSquared {
}
public PlotSquared(final IPlotMain imp_class) {
SetupUtils.generators = new HashMap<>();
THIS = this;
IMP = imp_class;
try {

View File

@ -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<? extends ChunkGenerator> 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<BlockPopulator> iter = world.getPopulators().iterator();
while (iter.hasNext()) {
if (iter.next() instanceof AugmentedPopulator) {
iter.remove();
}
}
}
PlotSquared.loadWorld(world.getName(), null);
}
}