2016-02-22 07:20:22 +01:00
|
|
|
package com.plotsquared.sponge.util;
|
|
|
|
|
2016-02-25 10:13:07 +01:00
|
|
|
import com.intellectualcrafters.configuration.ConfigurationSection;
|
|
|
|
import com.intellectualcrafters.plot.PS;
|
|
|
|
import com.intellectualcrafters.plot.config.ConfigurationNode;
|
|
|
|
import com.intellectualcrafters.plot.generator.GeneratorWrapper;
|
|
|
|
import com.intellectualcrafters.plot.generator.HybridGen;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotArea;
|
|
|
|
import com.intellectualcrafters.plot.object.SetupObject;
|
|
|
|
import com.intellectualcrafters.plot.util.SetupUtils;
|
|
|
|
import com.plotsquared.sponge.generator.SpongePlotGenerator;
|
2016-02-22 07:20:22 +01:00
|
|
|
import org.spongepowered.api.Sponge;
|
|
|
|
import org.spongepowered.api.world.DimensionTypes;
|
|
|
|
import org.spongepowered.api.world.GeneratorTypes;
|
|
|
|
import org.spongepowered.api.world.World;
|
|
|
|
import org.spongepowered.api.world.WorldCreationSettings;
|
|
|
|
import org.spongepowered.api.world.WorldCreationSettings.Builder;
|
|
|
|
import org.spongepowered.api.world.gen.WorldGenerator;
|
|
|
|
import org.spongepowered.api.world.gen.WorldGeneratorModifier;
|
|
|
|
import org.spongepowered.api.world.storage.WorldProperties;
|
|
|
|
|
2016-02-25 10:13:07 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.Objects;
|
2016-02-22 07:20:22 +01:00
|
|
|
|
|
|
|
public class SpongeSetupUtils extends SetupUtils {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateGenerators() {
|
|
|
|
if (!SetupUtils.generators.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetupUtils.generators.put("PlotSquared", new SpongePlotGenerator(new HybridGen()));
|
|
|
|
// TODO get external world generators
|
|
|
|
Collection<WorldGeneratorModifier> wgms = Sponge.getRegistry().getAllOf(WorldGeneratorModifier.class);
|
|
|
|
for (WorldGeneratorModifier wgm : wgms) {
|
|
|
|
String id = wgm.getId();
|
|
|
|
String name = wgm.getName();
|
|
|
|
if (wgm instanceof GeneratorWrapper<?>) {
|
2016-03-29 23:00:07 +02:00
|
|
|
SetupUtils.generators.put(id, (GeneratorWrapper<?>) wgm);
|
|
|
|
SetupUtils.generators.put(name, (GeneratorWrapper<?>) wgm);
|
2016-02-22 07:20:22 +01:00
|
|
|
} else {
|
|
|
|
SpongePlotGenerator wrap = new SpongePlotGenerator(wgm);
|
2016-03-29 23:00:07 +02:00
|
|
|
SetupUtils.generators.put(id, wrap);
|
|
|
|
SetupUtils.generators.put(name, wrap);
|
2016-02-22 07:20:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-29 21:47:59 +02:00
|
|
|
public String getGenerator(PlotArea plotworld) {
|
2016-02-22 07:20:22 +01:00
|
|
|
if (SetupUtils.generators.isEmpty()) {
|
|
|
|
updateGenerators();
|
|
|
|
}
|
2016-03-29 21:47:59 +02:00
|
|
|
World world = SpongeUtil.getWorld(plotworld.worldname);
|
2016-02-22 07:20:22 +01:00
|
|
|
if (world == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-03-29 21:47:59 +02:00
|
|
|
WorldGenerator generator = world.getWorldGenerator();
|
2016-02-22 07:20:22 +01:00
|
|
|
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-29 21:47:59 +02:00
|
|
|
public String setupWorld(SetupObject object) {
|
2016-02-22 07:20:22 +01:00
|
|
|
SetupUtils.manager.updateGenerators();
|
|
|
|
ConfigurationNode[] steps = object.step == null ? new ConfigurationNode[0] : object.step;
|
2016-03-29 21:47:59 +02:00
|
|
|
String world = object.world;
|
2016-02-22 07:20:22 +01:00
|
|
|
int type = object.type;
|
|
|
|
String worldPath = "worlds." + object.world;
|
|
|
|
if (!PS.get().config.contains(worldPath)) {
|
|
|
|
PS.get().config.createSection(worldPath);
|
|
|
|
}
|
|
|
|
ConfigurationSection worldSection = PS.get().config.getConfigurationSection(worldPath);
|
|
|
|
switch (type) {
|
|
|
|
case 2: {
|
|
|
|
if (object.id != null) {
|
2016-03-29 21:47:59 +02:00
|
|
|
String areaName = object.id + "-" + object.min + "-" + object.max;
|
|
|
|
String areaPath = "areas." + areaName;
|
2016-02-22 07:20:22 +01:00
|
|
|
if (!worldSection.contains(areaPath)) {
|
|
|
|
worldSection.createSection(areaPath);
|
|
|
|
}
|
|
|
|
ConfigurationSection areaSection = worldSection.getConfigurationSection(areaPath);
|
|
|
|
HashMap<String, Object> options = new HashMap<>();
|
2016-03-29 21:47:59 +02:00
|
|
|
for (ConfigurationNode step : steps) {
|
2016-02-22 07:20:22 +01:00
|
|
|
options.put(step.getConstant(), step.getValue());
|
|
|
|
}
|
|
|
|
options.put("generator.type", object.type);
|
|
|
|
options.put("generator.terrain", object.terrain);
|
|
|
|
options.put("generator.plugin", object.plotManager);
|
2016-03-29 23:00:07 +02:00
|
|
|
if (object.setupGenerator != null && !object.setupGenerator.equals(object.plotManager)) {
|
2016-02-22 07:20:22 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 23:00:07 +02:00
|
|
|
GeneratorWrapper<?> gen = SetupUtils.generators.get(object.setupGenerator);
|
|
|
|
if (gen != null && gen.isFull()) {
|
2016-02-22 07:20:22 +01:00
|
|
|
object.setupGenerator = null;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-03-29 23:00:07 +02:00
|
|
|
case 1:
|
2016-03-29 21:47:59 +02:00
|
|
|
for (ConfigurationNode step : steps) {
|
2016-02-22 07:20:22 +01:00
|
|
|
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);
|
2016-03-29 23:00:07 +02:00
|
|
|
if (object.setupGenerator != null && !object.setupGenerator.equals(object.plotManager)) {
|
2016-02-22 07:20:22 +01:00
|
|
|
PS.get().config.set("worlds." + world + "." + "generator.init", object.setupGenerator);
|
|
|
|
}
|
2016-03-29 23:00:07 +02:00
|
|
|
GeneratorWrapper<?> gen = SetupUtils.generators.get(object.setupGenerator);
|
|
|
|
if (gen != null && gen.isFull()) {
|
2016-02-22 07:20:22 +01:00
|
|
|
object.setupGenerator = null;
|
|
|
|
}
|
|
|
|
break;
|
2016-03-29 23:00:07 +02:00
|
|
|
case 0:
|
2016-03-29 21:47:59 +02:00
|
|
|
for (ConfigurationNode step : steps) {
|
2016-02-22 07:20:22 +01:00
|
|
|
worldSection.set(step.getConstant(), step.getValue());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
PS.get().config.save(PS.get().configFile);
|
2016-03-29 21:47:59 +02:00
|
|
|
} catch (IOException e) {
|
2016-02-22 07:20:22 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
if (object.setupGenerator != null) {
|
|
|
|
// create world with generator
|
2016-03-29 23:00:07 +02:00
|
|
|
GeneratorWrapper<?> gw = SetupUtils.generators.get(object.setupGenerator);
|
2016-02-22 07:20:22 +01:00
|
|
|
WorldGeneratorModifier wgm = (WorldGeneratorModifier) gw.getPlatformGenerator();
|
|
|
|
|
|
|
|
WorldCreationSettings settings = Sponge.getRegistry().createBuilder(Builder.class)
|
|
|
|
.name(object.world)
|
|
|
|
.loadsOnStartup(true)
|
2016-02-25 10:13:07 +01:00
|
|
|
.keepsSpawnLoaded(true)
|
2016-02-22 07:20:22 +01:00
|
|
|
.dimension(DimensionTypes.OVERWORLD)
|
2016-02-25 10:13:07 +01:00
|
|
|
.generator(GeneratorTypes.OVERWORLD)
|
2016-02-22 07:20:22 +01:00
|
|
|
.usesMapFeatures(false)
|
|
|
|
.enabled(true)
|
|
|
|
.generatorModifiers(wgm)
|
|
|
|
.build();
|
|
|
|
WorldProperties properties = Sponge.getServer().createWorldProperties(settings).get();
|
|
|
|
World worldObj = Sponge.getServer().loadWorld(properties).get();
|
|
|
|
} else {
|
|
|
|
// create vanilla world
|
|
|
|
WorldCreationSettings settings = Sponge.getRegistry().createBuilder(Builder.class)
|
|
|
|
.name(object.world)
|
|
|
|
.loadsOnStartup(true)
|
2016-02-25 10:13:07 +01:00
|
|
|
.keepsSpawnLoaded(true)
|
2016-02-22 07:20:22 +01:00
|
|
|
.dimension(DimensionTypes.OVERWORLD)
|
|
|
|
.generator(GeneratorTypes.OVERWORLD)
|
|
|
|
.usesMapFeatures(true)
|
|
|
|
.enabled(true)
|
|
|
|
.build();
|
|
|
|
WorldProperties properties = Sponge.getServer().createWorldProperties(settings).get();
|
|
|
|
World worldObj = Sponge.getServer().loadWorld(properties).get();
|
|
|
|
}
|
|
|
|
return object.world;
|
|
|
|
}
|
|
|
|
}
|