2015-07-30 19:24:01 +02:00
|
|
|
package com.plotsquared.bukkit.util;
|
2015-05-19 18:09:22 +02:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
import com.intellectualcrafters.plot.PS;
|
2016-02-10 19:59:51 +01:00
|
|
|
import com.intellectualcrafters.plot.generator.GeneratorWrapper;
|
2015-07-30 16:25:16 +02:00
|
|
|
import com.intellectualcrafters.plot.util.SetupUtils;
|
2016-02-10 19:59:51 +01:00
|
|
|
import com.plotsquared.bukkit.generator.BukkitAugmentedGenerator;
|
2016-02-14 02:01:18 +01:00
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.generator.BlockPopulator;
|
|
|
|
import org.bukkit.generator.ChunkGenerator;
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
2015-07-05 17:44:10 +02:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public class SetGenCB {
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public static void setGenerator(final World world) throws Exception {
|
2015-05-19 18:09:22 +02:00
|
|
|
SetupUtils.manager.updateGenerators();
|
2016-02-10 19:59:51 +01:00
|
|
|
PS.get().removePlotAreas(world.getName());
|
2015-09-11 12:09:22 +02:00
|
|
|
final ChunkGenerator gen = world.getGenerator();
|
2015-09-13 06:04:31 +02:00
|
|
|
if (gen == null) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-11 12:09:22 +02:00
|
|
|
final String name = gen.getClass().getCanonicalName();
|
2015-05-19 18:09:22 +02:00
|
|
|
boolean set = false;
|
2016-02-10 19:59:51 +01:00
|
|
|
for (final GeneratorWrapper<?> wrapper : SetupUtils.generators.values()) {
|
|
|
|
ChunkGenerator newGen = (ChunkGenerator) wrapper.getPlatformGenerator();
|
|
|
|
if (newGen == null) {
|
|
|
|
newGen = (ChunkGenerator) wrapper;
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
if (newGen.getClass().getCanonicalName().equals(name)) {
|
2015-05-19 18:09:22 +02:00
|
|
|
// set generator
|
2015-09-11 12:09:22 +02:00
|
|
|
final Field generator = world.getClass().getDeclaredField("generator");
|
|
|
|
final Field populators = world.getClass().getDeclaredField("populators");
|
2015-05-19 18:09:22 +02:00
|
|
|
generator.setAccessible(true);
|
|
|
|
populators.setAccessible(true);
|
|
|
|
// Set populators (just in case)
|
|
|
|
populators.set(world, new ArrayList<>());
|
|
|
|
// Set generator
|
2016-02-10 19:59:51 +01:00
|
|
|
generator.set(world, newGen);
|
|
|
|
populators.set(world, newGen.getDefaultPopulators(world));
|
2015-05-19 18:09:22 +02:00
|
|
|
// end
|
|
|
|
set = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
if (!set) {
|
2015-09-11 12:09:22 +02:00
|
|
|
final Iterator<BlockPopulator> iter = world.getPopulators().iterator();
|
2015-09-13 06:04:31 +02:00
|
|
|
while (iter.hasNext()) {
|
2016-02-10 19:59:51 +01:00
|
|
|
if (iter.next() instanceof BukkitAugmentedGenerator) {
|
2015-05-19 18:09:22 +02:00
|
|
|
iter.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-27 19:28:39 +02:00
|
|
|
PS.get().loadWorld(world.getName(), PS.get().IMP.getGenerator(world.getName(), null));
|
2015-05-19 18:09:22 +02:00
|
|
|
}
|
|
|
|
}
|