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

203 lines
9.1 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;
2016-02-10 19:59:51 +01:00
import java.util.HashMap;
2015-07-30 16:25:16 +02:00
import java.util.Map.Entry;
2016-02-10 19:59:51 +01:00
import java.util.Objects;
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;
2016-02-10 19:59:51 +01:00
import com.intellectualcrafters.configuration.ConfigurationSection;
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;
2016-02-10 19:59:51 +01:00
import com.intellectualcrafters.plot.generator.GeneratorWrapper;
import com.intellectualcrafters.plot.object.PlotArea;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.object.SetupObject;
import com.intellectualcrafters.plot.util.SetupUtils;
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) {
2016-02-10 19:59:51 +01:00
PS.get().removePlotAreas(testWorld);
2015-02-22 14:12:32 +01:00
final String name = plugin.getDescription().getName();
2016-02-10 19:59:51 +01:00
GeneratorWrapper<?> wrapped;
if (generator instanceof GeneratorWrapper<?>) {
wrapped = (GeneratorWrapper<?>) generator;
} else {
wrapped = new BukkitPlotGenerator(testWorld, generator);
}
SetupUtils.generators.put(name, wrapped);
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();
ConfigurationNode[] steps = object.step == null ? new ConfigurationNode[0] : object.step;
2015-02-22 14:12:32 +01:00
final String world = object.world;
int type = object.type;
2016-02-10 19:59:51 +01:00
String worldPath = "worlds." + object.world;
if (!PS.get().config.contains(worldPath)) {
PS.get().config.createSection(worldPath);
2015-02-22 14:12:32 +01:00
}
2016-02-10 19:59:51 +01:00
ConfigurationSection worldSection = PS.get().config.getConfigurationSection(worldPath);
switch (type) {
case 2: {
if (object.id != null) {
String areaname = object.id + "-" + object.min + "-" + object.max;
String areaPath = "areas." + areaname;
if (!worldSection.contains(areaPath)) {
worldSection.createSection(areaPath);
}
ConfigurationSection areaSection = worldSection.getConfigurationSection(areaPath);
HashMap<String, Object> options = new HashMap<>();
for (final ConfigurationNode step : steps) {
options.put(step.getConstant(), step.getValue());
}
options.put("generator.type", object.type);
options.put("generator.terrain", object.terrain);
options.put("generator.plugin", object.plotManager);
if ((object.setupGenerator != null) && !object.setupGenerator.equals(object.plotManager)) {
options.put("generator.init", object.setupGenerator);
}
for (Entry<String, Object> entry : options.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (worldSection.contains(key)) {
Object current = worldSection.get(key);
if (!Objects.equals(value, current)) {
areaSection.set(key, value);
}
} else {
worldSection.set(key, value);
}
}
}
GeneratorWrapper<?> gen = generators.get(object.setupGenerator);
if ((gen != null) && gen.isFull()) {
object.setupGenerator = null;
}
break;
2015-04-21 14:48:18 +02:00
}
2016-02-10 19:59:51 +01:00
case 1: {
for (final ConfigurationNode step : steps) {
worldSection.set(step.getConstant(), step.getValue());
}
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);
if ((object.setupGenerator != null) && !object.setupGenerator.equals(object.plotManager)) {
PS.get().config.set("worlds." + world + "." + "generator.init", object.setupGenerator);
}
GeneratorWrapper<?> gen = generators.get(object.setupGenerator);
if ((gen != null) && gen.isFull()) {
object.setupGenerator = null;
}
break;
}
case 0: {
for (final ConfigurationNode step : steps) {
worldSection.set(step.getConstant(), step.getValue());
}
break;
2015-05-15 18:23:59 +02:00
}
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
2016-02-10 19:59:51 +01:00
public String getGenerator(final PlotArea plotworld) {
2015-09-13 06:04:31 +02:00
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;
}
2016-02-10 19:59:51 +01:00
for (final Entry<String, GeneratorWrapper<?>> entry : generators.entrySet()) {
GeneratorWrapper<?> current = entry.getValue();
if (current.equals(generator)) {
2015-09-13 06:04:31 +02:00
return entry.getKey();
}
2015-04-02 05:52:14 +02:00
}
return null;
}
2015-02-22 14:12:32 +01:00
}