TF-PlotSquared/PlotSquared/src/com/intellectualcrafters/plot/commands/Setup.java

207 lines
5.9 KiB
Java
Raw Normal View History

package com.intellectualcrafters.plot.commands;
import java.io.IOException;
2014-10-11 12:09:14 -07:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
2014-10-02 18:21:34 +10:00
import org.apache.commons.lang.StringUtils;
2014-10-11 12:09:14 -07:00
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
2014-10-11 12:09:14 -07:00
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.Plugin;
2014-10-02 18:21:34 +10:00
import com.intellectualcrafters.plot.C;
import com.intellectualcrafters.plot.ConfigurationNode;
import com.intellectualcrafters.plot.PlayerFunctions;
2014-10-11 12:09:14 -07:00
import com.intellectualcrafters.plot.PlotGenerator;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.PlotWorld;
2014-10-11 12:09:14 -07:00
import com.intellectualcrafters.plot.generator.DefaultPlotWorld;
/**
* Created by Citymonstret on 2014-09-26.
*/
public class Setup extends SubCommand implements Listener {
public static Map<String, SetupObject> setupMap = new HashMap<>();
private class SetupObject {
String world;
2014-10-11 12:09:14 -07:00
String plugin;
int current = 0;
ConfigurationNode[] step;
2014-10-11 12:09:14 -07:00
public SetupObject(String world, PlotWorld plotworld, String plugin) {
this.world = world;
this.step = plotworld.getSettingNodes();
2014-10-11 12:09:14 -07:00
this.plugin = plugin;
}
public String getPlugin() {
return this.plugin;
}
public int getCurrent() {
return this.current;
}
public int getMax() {
return this.step.length;
}
}
public Setup() {
super("setup", "plots.admin", "Setup a PlotWorld", "setup {world}",
"setup", CommandCategory.ACTIONS);
}
@Override
public boolean execute(Player plr, String... args) {
boolean finished = false;
if (setupMap.containsKey(plr.getName())) {
SetupObject object = setupMap.get(plr.getName());
if (object.getCurrent() == object.getMax()) {
ConfigurationNode[] steps = object.step;
String world = object.world;
for (ConfigurationNode step : steps) {
PlotMain.config.set(
"worlds." + world + "." + step.getConstant(),
step.getValue());
}
try {
PlotMain.config.save(PlotMain.configFile);
} catch (IOException e) {
e.printStackTrace();
}
2014-10-11 12:09:14 -07:00
// Creating the worlds
if (object.getPlugin().equals("Multiverse-Core")) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mv create "+world+" normal -g "+object.plugin);
}
else if (object.getPlugin().equals("MultiWorld")) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mw create "+world+" plugin:"+object.plugin);
}
sendMessage(plr, C.SETUP_FINISHED, object.world);
setupMap.remove(plr.getName());
return true;
}
ConfigurationNode step = object.step[object.current];
if (args.length < 1) {
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "",
2014-10-11 19:05:50 +11:00
step.getDescription(), step.getType().getType(),
step.getDefaultValue() + "");
return true;
} else {
if (args[0].equalsIgnoreCase("cancel")) {
setupMap.remove(plr.getName());
PlayerFunctions.sendMessage(plr, "&cCancelled setup.");
return true;
}
if (args[0].equalsIgnoreCase("back")) {
if (object.current > 0) {
object.current--;
step = object.step[object.current];
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "",
2014-10-11 19:05:50 +11:00
step.getDescription(), step.getType().getType(),
step.getDefaultValue() + "");
return true;
} else {
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "",
2014-10-11 19:05:50 +11:00
step.getDescription(), step.getType().getType(),
step.getDefaultValue() + "");
return true;
}
}
boolean valid = step.isValid(args[0]);
if (valid) {
sendMessage(plr, C.SETUP_VALID_ARG, step.getConstant(),
args[0]);
step.setValue(args[0]);
object.current++;
if (object.getCurrent() == object.getMax()) {
execute(plr, args);
return true;
}
step = object.step[object.current];
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "",
2014-10-11 19:05:50 +11:00
step.getDescription(), step.getType().getType(),
step.getDefaultValue() + "");
return true;
} else {
sendMessage(plr, C.SETUP_INVALID_ARG, args[0],
step.getConstant());
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "",
2014-10-11 19:05:50 +11:00
step.getDescription(), step.getType().getType(),
step.getDefaultValue() + "");
return true;
}
}
} else {
if (args.length < 1) {
sendMessage(plr, C.SETUP_MISSING_WORLD);
return true;
}
2014-10-11 12:09:14 -07:00
if (args.length < 2) {
sendMessage(plr, C.SETUP_MISSING_GENERATOR);
return true;
}
String world = args[0];
if (StringUtils.isNumeric(args[0])) {
sendMessage(plr, C.SETUP_WORLD_TAKEN, world);
return true;
}
2014-10-11 12:09:14 -07:00
if (PlotMain.getWorldSettings(world) != null) {
sendMessage(plr, C.SETUP_WORLD_TAKEN, world);
return true;
}
2014-10-11 12:09:14 -07:00
ArrayList<String> generators = new ArrayList<String>();
ChunkGenerator generator = null;
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (plugin.isEnabled()) {
ChunkGenerator currentGen = plugin.getDefaultWorldGenerator("world", "");
if (currentGen != null) {
String name = plugin.getDescription().getName();
generators.add(name);
if (args[1].equals(name)) {
generator = currentGen;
break;
}
}
}
}
if (generator == null) {
sendMessage(plr, C.SETUP_INVALID_GENERATOR, StringUtils.join(generators,C.BLOCK_LIST_SEPARATER.s()));
return true;
}
PlotWorld plotworld;
if (generator instanceof PlotGenerator) {
2014-10-11 19:05:50 +11:00
plotworld = ((PlotGenerator) generator).getNewPlotWorld(world);
2014-10-11 12:09:14 -07:00
}
else {
plotworld = new DefaultPlotWorld(world);
}
2014-10-11 12:09:14 -07:00
setupMap.put(plr.getName(), new SetupObject(world, plotworld, args[1]));
sendMessage(plr, C.SETUP_INIT);
SetupObject object = setupMap.get(plr.getName());
ConfigurationNode step = object.step[object.current];
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "",
2014-10-11 19:05:50 +11:00
step.getDescription(), step.getType().getType(),
step.getDefaultValue() + "");
return true;
}
}
}