PlotSquared/src/main/java/com/plotsquared/bukkit/util/BukkitSetupUtils.java

149 lines
6.6 KiB
Java
Raw Normal View History

2015-07-30 19:24:01 +02:00
package com.plotsquared.bukkit.util;
2015-02-22 14:12:32 +01:00
2015-08-09 11:58:29 +02:00
import java.io.File;
2015-07-30 16:25:16 +02:00
import java.io.IOException;
import java.util.Map.Entry;
2015-02-22 14:12:32 +01:00
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.Plugin;
2015-08-09 11:58:29 +02:00
import com.intellectualcrafters.configuration.file.YamlConfiguration;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.ConfigurationNode;
import com.intellectualcrafters.plot.generator.PlotGenerator;
2015-08-02 21:25:41 +02:00
import com.intellectualcrafters.plot.object.PlotCluster;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.SetupObject;
import com.intellectualcrafters.plot.util.SetupUtils;
2015-08-02 21:25:41 +02:00
import com.plotsquared.bukkit.generator.AugmentedPopulator;
2015-07-30 16:25:16 +02:00
import com.plotsquared.bukkit.generator.BukkitGeneratorWrapper;
import com.plotsquared.bukkit.generator.BukkitPlotGenerator;
2015-02-22 14:12:32 +01:00
2015-09-13 06:04:31 +02:00
public class BukkitSetupUtils extends SetupUtils {
2015-02-22 14:12:32 +01:00
@Override
2015-09-13 06:04:31 +02:00
public void updateGenerators() {
if (SetupUtils.generators.size() > 0) {
return;
}
2015-02-22 14:12:32 +01:00
final String testWorld = "CheckingPlotSquaredGenerator";
2015-09-13 06:04:31 +02:00
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (plugin.isEnabled()) {
2015-02-22 14:12:32 +01:00
final ChunkGenerator generator = plugin.getDefaultWorldGenerator(testWorld, "");
2015-09-13 06:04:31 +02:00
if (generator != null) {
PS.get().removePlotWorld(testWorld);
2015-02-22 14:12:32 +01:00
final String name = plugin.getDescription().getName();
2015-09-11 12:09:22 +02:00
SetupUtils.generators.put(name, new BukkitGeneratorWrapper("CheckingPlotSquaredGenerator", generator));
2015-02-22 14:12:32 +01:00
}
}
}
}
2015-09-13 06:04:31 +02:00
2015-02-22 14:12:32 +01:00
@Override
2015-09-13 06:04:31 +02:00
public String setupWorld(final SetupObject object) {
2015-04-21 16:03:27 +02:00
SetupUtils.manager.updateGenerators();
2015-02-22 14:12:32 +01:00
final ConfigurationNode[] steps = object.step;
final String world = object.world;
2015-09-13 06:04:31 +02:00
for (final ConfigurationNode step : steps) {
PS.get().config.set("worlds." + world + "." + step.getConstant(), step.getValue());
2015-02-22 14:12:32 +01:00
}
2015-09-13 06:04:31 +02:00
if (object.type != 0) {
PS.get().config.set("worlds." + world + "." + "generator.type", object.type);
PS.get().config.set("worlds." + world + "." + "generator.terrain", object.terrain);
PS.get().config.set("worlds." + world + "." + "generator.plugin", object.plotManager);
2015-09-13 06:04:31 +02:00
if ((object.setupGenerator != null) && !object.setupGenerator.equals(object.plotManager)) {
PS.get().config.set("worlds." + world + "." + "generator.init", object.setupGenerator);
2015-04-21 14:48:18 +02:00
}
2015-09-11 12:09:22 +02:00
final PlotGenerator<ChunkGenerator> gen = (PlotGenerator<ChunkGenerator>) generators.get(object.setupGenerator);
2015-09-13 06:04:31 +02:00
if ((gen != null) && (gen.generator instanceof BukkitPlotGenerator)) {
2015-05-15 18:23:59 +02:00
object.setupGenerator = null;
}
2015-02-22 14:12:32 +01:00
}
2015-09-13 06:04:31 +02:00
try {
PS.get().config.save(PS.get().configFile);
2015-09-13 06:04:31 +02:00
} catch (final IOException e) {
2015-02-22 14:12:32 +01:00
e.printStackTrace();
}
2015-09-13 06:04:31 +02:00
if (object.setupGenerator != null) {
if ((Bukkit.getPluginManager().getPlugin("Multiverse-Core") != null) && Bukkit.getPluginManager().getPlugin("Multiverse-Core").isEnabled()) {
2015-04-21 14:48:18 +02:00
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mv create " + world + " normal -g " + object.setupGenerator);
2015-08-09 11:58:29 +02:00
setGenerator(world, object.setupGenerator);
2015-09-13 06:04:31 +02:00
if (Bukkit.getWorld(world) != null) {
return world;
}
2015-02-22 14:12:32 +01:00
}
2015-09-13 06:04:31 +02:00
if ((Bukkit.getPluginManager().getPlugin("MultiWorld") != null) && Bukkit.getPluginManager().getPlugin("MultiWorld").isEnabled()) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mw create " + world + " plugin:" + object.setupGenerator);
setGenerator(world, object.setupGenerator);
2015-09-13 06:04:31 +02:00
if (Bukkit.getWorld(world) != null) {
return world;
}
}
final WorldCreator wc = new WorldCreator(object.world);
wc.generator(object.setupGenerator);
wc.environment(Environment.NORMAL);
Bukkit.createWorld(wc);
setGenerator(world, object.setupGenerator);
2015-09-13 06:04:31 +02:00
} else {
if ((Bukkit.getPluginManager().getPlugin("Multiverse-Core") != null) && Bukkit.getPluginManager().getPlugin("Multiverse-Core").isEnabled()) {
2015-02-22 14:12:32 +01:00
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mv create " + world + " normal");
2015-09-13 06:04:31 +02:00
if (Bukkit.getWorld(world) != null) {
return world;
}
}
2015-09-13 06:04:31 +02:00
if ((Bukkit.getPluginManager().getPlugin("MultiWorld") != null) && Bukkit.getPluginManager().getPlugin("MultiWorld").isEnabled()) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mw create " + world);
2015-09-13 06:04:31 +02:00
if (Bukkit.getWorld(world) != null) {
return world;
}
2015-02-22 14:12:32 +01:00
}
Bukkit.createWorld(new WorldCreator(object.world).environment(World.Environment.NORMAL));
2015-02-22 14:12:32 +01:00
}
return object.world;
}
2015-09-13 06:04:31 +02:00
public void setGenerator(final String world, final String generator) {
if ((Bukkit.getWorlds().size() == 0) || !Bukkit.getWorlds().get(0).getName().equals(world)) {
return;
}
2015-09-11 12:09:22 +02:00
final File file = new File("bukkit.yml").getAbsoluteFile();
final YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
2015-08-09 11:58:29 +02:00
yml.set("worlds." + world + ".generator", generator);
2015-09-13 06:04:31 +02:00
try {
2015-08-09 11:58:29 +02:00
yml.save(file);
2015-09-13 06:04:31 +02:00
} catch (final IOException e) {
2015-08-09 11:58:29 +02:00
e.printStackTrace();
}
}
2015-09-13 06:04:31 +02:00
2015-04-02 05:52:14 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String getGenerator(final PlotWorld plotworld) {
if (SetupUtils.generators.size() == 0) {
2015-04-02 05:52:14 +02:00
updateGenerators();
}
2015-09-11 12:09:22 +02:00
final World world = Bukkit.getWorld(plotworld.worldname);
2015-09-13 06:04:31 +02:00
if (world == null) {
return null;
}
2015-09-11 12:09:22 +02:00
final ChunkGenerator generator = world.getGenerator();
2015-09-13 06:04:31 +02:00
if (!(generator instanceof BukkitPlotGenerator)) {
return null;
}
for (final Entry<String, PlotGenerator<?>> entry : generators.entrySet()) {
if (entry.getValue().generator.getClass().getName().equals(generator.getClass().getName())) {
return entry.getKey();
}
2015-04-02 05:52:14 +02:00
}
return null;
}
2015-09-13 06:04:31 +02:00
2015-08-02 21:25:41 +02:00
@Override
2015-09-13 06:04:31 +02:00
public void removePopulator(final String world, final PlotCluster cluster) {
2015-08-02 21:25:41 +02:00
AugmentedPopulator.removePopulator(world, cluster);
}
2015-02-22 14:12:32 +01:00
}