new setup command

This commit is contained in:
boy0001 2015-02-10 17:14:55 +11:00
parent ba35d8b774
commit 76ed708c79
4 changed files with 259 additions and 182 deletions

View File

@ -8,7 +8,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<artifactId>PlotSquared</artifactId>
<version>2.6.4</version>
<version>2.7.0</version>
<name>PlotSquared</name>
<packaging>jar</packaging>
<build>

View File

@ -71,7 +71,6 @@ public enum Command {
LIST("list", "l"),
SET("set", "s"),
PURGE("purge"),
SETUP("setup"),
OP("op", "add"),
DEOP("deop", "deadmin"),
BAN("ban", "block"),

View File

@ -49,7 +49,7 @@ public class MainCommand implements CommandExecutor, TabCompleter {
*/
public static final String MAIN_PERMISSION = "plots.use";
private final static SubCommand[] _subCommands = new SubCommand[]{new DebugSetup(), new DebugSaveTest(), new DebugLoadTest(), new CreateRoadSchematic(), new RegenAllRoads(), new DebugClear(), new Ban(), new Unban(), new OP(), new DEOP(), new Claim(), new Paste(), new Copy(), new Clipboard(), new Auto(), new Home(), new Visit(), new TP(), new Set(), new Clear(), new Delete(), new SetOwner(), new Denied(), new Helpers(), new Trusted(), new Info(), new list(), new Help(), new Debug(), new Schematic(), new plugin(), new Inventory(), new Purge(), new Reload(), new Merge(), new Unlink(), new Kick(), new Setup(), new Rate(), new DebugClaimTest(), new Inbox(), new Comment(), new Database(), new Unclaim(), new Swap(), new MusicSubcommand(), new DebugRoadRegen(), new Trim(), new DebugExec(), new FlagCmd(), new Target(), new DebugFixFlags() };
private final static SubCommand[] _subCommands = new SubCommand[]{new Setup(), new DebugSaveTest(), new DebugLoadTest(), new CreateRoadSchematic(), new RegenAllRoads(), new DebugClear(), new Ban(), new Unban(), new OP(), new DEOP(), new Claim(), new Paste(), new Copy(), new Clipboard(), new Auto(), new Home(), new Visit(), new TP(), new Set(), new Clear(), new Delete(), new SetOwner(), new Denied(), new Helpers(), new Trusted(), new Info(), new list(), new Help(), new Debug(), new Schematic(), new plugin(), new Inventory(), new Purge(), new Reload(), new Merge(), new Unlink(), new Kick(), new Rate(), new DebugClaimTest(), new Inbox(), new Comment(), new Database(), new Unclaim(), new Swap(), new MusicSubcommand(), new DebugRoadRegen(), new Trim(), new DebugExec(), new FlagCmd(), new Target(), new DebugFixFlags() };
public final static ArrayList<SubCommand> subCommands = new ArrayList<SubCommand>() {
{

View File

@ -22,229 +22,307 @@
package com.intellectualcrafters.plot.commands;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.Plugin;
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.ConfigurationNode;
import com.intellectualcrafters.plot.generator.HybridPlotWorld;
import com.intellectualcrafters.plot.object.PlotGenerator;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.util.PlayerFunctions;
/**
* Created 2014-09-26 for PlotSquared
*
* @author Citymonstret, Empire92
*/
public class Setup extends SubCommand implements Listener {
public class Setup extends SubCommand {
public final static Map<String, SetupObject> setupMap = new HashMap<>();
public HashMap<String, PlotGenerator> generators = new HashMap<>();
public Setup() {
super("setup", "plots.admin.command.setup", "Setup a PlotWorld", "setup {world} {generator}", "setup", CommandCategory.ACTIONS, false);
super("setup", "plots.admin.command.setup", "Plotworld setup command", "setup", "create", CommandCategory.ACTIONS, true);
}
private class SetupObject {
int current = 0;
int setup_index = 0;
String world = null;
String generator = null;
int type = 0;
int terrain = 0;
ConfigurationNode[] step = null;
}
public void updateGenerators() {
if (generators.size() > 0) {
return;
}
String testWorld = "CheckingPlotSquaredGenerator";
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (plugin.isEnabled()) {
ChunkGenerator generator = plugin.getDefaultWorldGenerator(testWorld, "");
if (generator != null) {
PlotMain.removePlotWorld(testWorld);
final String name = plugin.getDescription().getName();
if (generator instanceof PlotGenerator) {
generators.put(name, (PlotGenerator) generator);
}
}
}
}
}
@Override
public boolean execute(final Player plr, final String... args) {
String plrname;
if (plr == null) {
plrname = "";
} else {
plrname = plr.getName();
// going through setup
String name = plr.getName();
if (!setupMap.containsKey(name)) {
SetupObject object = new SetupObject();
setupMap.put(name, object);
updateGenerators();
String prefix = "\n&8 - &7";
sendMessage(plr, C.SETUP_INIT);
PlayerFunctions.sendMessage(plr, "&6What generator do you want?" + prefix + StringUtils.join(generators.keySet(), prefix).replaceAll("PlotSquared", "&2PlotSquared"));
return false;
}
if (setupMap.containsKey(plrname)) {
final SetupObject object = setupMap.get(plrname);
if (object.getCurrent() == object.getMax()) {
final ConfigurationNode[] steps = object.step;
final String world = object.world;
for (final ConfigurationNode step : steps) {
PlotMain.config.set("worlds." + world + "." + step.getConstant(), step.getValue());
}
try {
PlotMain.config.save(PlotMain.configFile);
} catch (final IOException e) {
e.printStackTrace();
}
// Creating the worlds
if ((Bukkit.getPluginManager().getPlugin("Multiverse-Core") != null) && Bukkit.getPluginManager().getPlugin("Multiverse-Core").isEnabled()) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mv create " + world + " normal -g " + object.plugin);
} else {
if ((Bukkit.getPluginManager().getPlugin("MultiWorld") != null) && Bukkit.getPluginManager().getPlugin("MultiWorld").isEnabled()) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mw create " + world + " plugin:" + object.plugin);
} else {
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (plugin.isEnabled()) {
if (plugin.getDefaultWorldGenerator("world", "") != null) {
final String name = plugin.getDescription().getName();
if (object.plugin.equals(name)) {
final ChunkGenerator generator = plugin.getDefaultWorldGenerator(world, "");
final World myworld = WorldCreator.name(world).generator(generator).createWorld();
PlayerFunctions.sendMessage(plr, "&aLoaded world.");
if (plr != null) {
plr.teleport(myworld.getSpawnLocation());
}
break;
}
}
}
}
}
}
try {
plr.teleport(Bukkit.getWorld(world).getSpawnLocation());
}
catch (Exception e) {}
sendMessage(plr, C.SETUP_FINISHED, object.world);
setupMap.remove(plrname);
return true;
if (args.length == 1) {
if (args[0].equalsIgnoreCase("cancel")) {
setupMap.remove(plr.getName());
PlayerFunctions.sendMessage(plr, "&aCancelled setup");
return false;
}
ConfigurationNode step = object.step[object.current];
if (args.length < 1) {
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
return true;
} else {
if (args[0].equalsIgnoreCase("cancel")) {
final String world = object.world;
PlotMain.config.set("worlds." + world, null);
try {
PlotMain.config.save(PlotMain.configFile);
} catch (final IOException e) {
e.printStackTrace();
if (args[0].equalsIgnoreCase("back")) {
SetupObject object = setupMap.get(plr.getName());
if (object.setup_index > 0) {
object.setup_index--;
ConfigurationNode node = object.step[object.current];
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "", node.getDescription(), node.getType().getType(), node.getDefaultValue() + "");
return false;
}
else if (object.current > 0 ){
object.current--;
}
}
}
SetupObject object = setupMap.get(name);
int index = object.current;
switch(index) {
case 0: { // choose generator
if (args.length != 1 || !generators.containsKey(args[0])) {
String prefix = "\n&8 - &7";
PlayerFunctions.sendMessage(plr, "&cYou must choose a generator!" + prefix + StringUtils.join(generators.keySet(), prefix).replaceAll("PlotSquared", "&2PlotSquared"));
sendMessage(plr, C.SETUP_INIT);
return false;
}
object.generator = args[0];
object.current++;
PlayerFunctions.sendMessage(plr, "&6What world type do you want?"
+ "\n&8 - &2DEFAULT&8 - &7Standard plot generation"
+ "\n&8 - &7AUGMENTED&8 - &7Plot generation with terrain"
+ "\n&8 - &7PARTIAL&8 - &7Vanilla with clusters of plots");
break;
}
case 1: { // choose world type
List<String> types = Arrays.asList(new String[] {"default", "augmented", "partial"});
if (args.length != 1 || !types.contains(args[0].toLowerCase())) {
PlayerFunctions.sendMessage(plr, "&cYou must choose a world type!"
+ "\n&8 - &2DEFAULT&8 - &7Standard plot generation"
+ "\n&8 - &7AUGMENTED&8 - &7Plot generation with terrain"
+ "\n&8 - &7PARTIAL&8 - &7Vanilla with clusters of plots");
return false;
}
object.type = types.indexOf(args[0].toLowerCase());
if (object.type == 0) {
object.current++;
if (object.step == null) {
object.step = generators.get(object.generator).getNewPlotWorld(null).getSettingNodes();
}
setupMap.remove(plrname);
PlayerFunctions.sendMessage(plr, "&cCancelled setup.");
ConfigurationNode step = object.step[object.setup_index];
sendMessage(plr, C.SETUP_STEP, object.setup_index + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
}
else {
PlayerFunctions.sendMessage(plr, "&6What terrain would you like in plots?"
+ "\n&8 - &2NONE&8 - &7No terrain at all"
+ "\n&8 - &7ORE&8 - &7Just some ore veins and trees"
+ "\n&8 - &7ALL&8 - &7Entirely vanilla generation");
}
object.current++;
break;
}
case 2: { // Choose terrain
List<String> terrain = Arrays.asList(new String[] {"none", "ore", "all"});
if (args.length != 1 || !terrain.contains(args[0].toLowerCase())) {
PlayerFunctions.sendMessage(plr, "&cYou must choose the terrain!"
+ "\n&8 - &2NONE&8 - &7No terrain at all"
+ "\n&8 - &7ORE&8 - &7Just some ore veins and trees"
+ "\n&8 - &7ALL&8 - &7Entirely vanilla generation");
return false;
}
object.terrain = terrain.indexOf(args[0].toLowerCase());
object.current++;
if (object.step == null) {
object.step = generators.get(object.generator).getNewPlotWorld(null).getSettingNodes();
}
ConfigurationNode step = object.step[object.setup_index];
sendMessage(plr, C.SETUP_STEP, object.setup_index + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
break;
}
case 3: { // world setup
if (object.setup_index == object.step.length) {
PlayerFunctions.sendMessage(plr, "&6What do you want your world to be called?");
object.setup_index = 0;
object.current++;
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 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
return true;
} else {
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
return true;
}
ConfigurationNode step = object.step[object.setup_index];
if (args.length < 1) {
sendMessage(plr, C.SETUP_STEP, object.setup_index + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
return false;
}
final 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()) {
object.setup_index++;
if (object.setup_index == object.step.length) {
execute(plr, args);
return true;
return false;
}
step = object.step[object.current];
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
return true;
step = object.step[object.setup_index];
sendMessage(plr, C.SETUP_STEP, object.setup_index + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
return false;
} else {
sendMessage(plr, C.SETUP_INVALID_ARG, args[0], step.getConstant());
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
return true;
sendMessage(plr, C.SETUP_STEP, object.setup_index + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
return false;
}
}
} else {
if (args.length < 1) {
sendMessage(plr, C.SETUP_MISSING_WORLD);
return true;
}
if (args.length < 2) {
sendMessage(plr, C.SETUP_MISSING_GENERATOR);
return true;
}
final String world = args[0];
if (StringUtils.isNumeric(args[0])) {
sendMessage(plr, C.SETUP_WORLD_TAKEN, world);
return true;
}
if (PlotMain.getWorldSettings(world) != null) {
sendMessage(plr, C.SETUP_WORLD_TAKEN, world);
return true;
}
final ArrayList<String> generators = new ArrayList<>();
ChunkGenerator generator = null;
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (plugin.isEnabled()) {
if (plugin.getDefaultWorldGenerator("CheckingPlotSquaredGenerator", "") != null) {
PlotMain.removePlotWorld(world);
final String name = plugin.getDescription().getName();
generators.add(name);
if (args[1].equals(name)) {
generator = plugin.getDefaultWorldGenerator(world, "");
break;
}
}
case 4: {
if (args.length != 1) {
PlayerFunctions.sendMessage(plr, "&cYou need to choose a world name!");
return false;
}
if (Bukkit.getWorld(args[0]) != null) {
PlayerFunctions.sendMessage(plr, "&cThat world name is already taken!");
}
object.world = args[0];
setupMap.remove(plr.getName());
World world = setupWorld(object);
try {
plr.teleport(world.getSpawnLocation());
}
catch (Exception e) {
plr.sendMessage("&cAn error occured. See console for more information");
e.printStackTrace();
}
sendMessage(plr, C.SETUP_FINISHED, object.world);
setupMap.remove(plr.getName());
}
PlotMain.config.set("worlds.CheckingPlotSquaredGenerator", null);
try {
PlotMain.config.save(PlotMain.configFile);
} catch (final IOException e) {
e.printStackTrace();
}
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) {
plotworld = ((PlotGenerator) generator).getNewPlotWorld(world);
}
/*
* 0.0 normal hybrid no clusters
* 0.1 normal hybrid with clusters
* 0.2 normal hybrid require clusters
* 1.0 augmented whole world
* 1.1 augmented whole world with ore
* 1.2 augmented whole world with terrain
* 2.1 augmented partial world
* 2.2 augmented partial world with ore
* 2.3 augmented partial world with terrain
* 3.0 no generation + normal manager
*
* generator.TYPE: PlotSquared, augmented, partial
* generator.TERRAIN
*
* WORLD.TYPE: hybrid, augmented, partial
* if (augmented/partial)
* WORLD.TERRAIN:
*
*
* types (0, 1, 2, 3)
* 0: no options
* 1:
*
*
* - return null
* - schedule task to create world later
* - externalize multiverse/world hooks to separate class
* - create vanilla world
* - add augmented populator
* - add config option type
* - Work on heirarchy for setting nodes so you don't need to provide irrelevent info (world setup)
* - use code from setup command for world arguments (above) so that it persists
* - work on plot clearing for augmented plot worlds (terrain) (heads, banners, paintings, animals, inventoryhandler)
* - make a generic clear function for any generator
* - clean up plotmanager class (remove unnecessary methods)
* - make simple plot manager which can be used by external generators (don't make abstract)
* - plugins will override any of it's methods
* - make heirarchy of generators of increasing abstraction:
* = totally abstract (circle plots, moving plots, no tesselation)
* = tessellating generator
* = grid generator
* = square generator
* = square plot generator (must have plot section and road section) (plot height, road height)
* = hybrid generator
*
* - All will support whole world augmentation
* - Only grid will support partial plot worlds
*
*/
return false;
}
public World setupWorld(SetupObject object) {
// Configuration
final ConfigurationNode[] steps = object.step;
final String world = object.world;
for (final ConfigurationNode step : steps) {
PlotMain.config.set("worlds." + world + "." + step.getConstant(), step.getValue());
}
if (object.type != 0) {
PlotMain.config.set("worlds." + world + "." + "generator.type", object.type);
PlotMain.config.set("worlds." + world + "." + "generator.terrain", object.terrain);
PlotMain.config.set("worlds." + world + "." + "generator.plugin", object.generator);
}
try {
PlotMain.config.save(PlotMain.configFile);
} catch (final IOException e) {
e.printStackTrace();
}
if (object.type == 0) {
if ((Bukkit.getPluginManager().getPlugin("Multiverse-Core") != null) && Bukkit.getPluginManager().getPlugin("Multiverse-Core").isEnabled()) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mv create " + world + " normal -g " + object.generator);
} else {
plotworld = new HybridPlotWorld(world);
if ((Bukkit.getPluginManager().getPlugin("MultiWorld") != null) && Bukkit.getPluginManager().getPlugin("MultiWorld").isEnabled()) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mw create " + world + " plugin:" + object.generator);
} else {
WorldCreator wc = new WorldCreator(object.world);
wc.generator(object.generator);
wc.environment(Environment.NORMAL);
World newWorld = Bukkit.createWorld(wc);
}
}
PlotMain.removePlotWorld(world);
setupMap.put(plrname, new SetupObject(world, plotworld, args[1]));
sendMessage(plr, C.SETUP_INIT);
final SetupObject object = setupMap.get(plrname);
final ConfigurationNode step = object.step[object.current];
sendMessage(plr, C.SETUP_STEP, object.current + 1 + "", step.getDescription(), step.getType().getType(), step.getDefaultValue() + "");
return true;
}
else {
if ((Bukkit.getPluginManager().getPlugin("Multiverse-Core") != null) && Bukkit.getPluginManager().getPlugin("Multiverse-Core").isEnabled()) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mv create " + world + " normal");
} else {
if ((Bukkit.getPluginManager().getPlugin("MultiWorld") != null) && Bukkit.getPluginManager().getPlugin("MultiWorld").isEnabled()) {
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "mw create " + world);
} else {
Bukkit.createWorld(new WorldCreator(object.world).environment(World.Environment.NORMAL));
}
}
}
return Bukkit.getWorld(object.world);
}
private class SetupObject {
final String world;
final String plugin;
final ConfigurationNode[] step;
int current = 0;
public SetupObject(final String world, final PlotWorld plotworld, final String plugin) {
this.world = world;
this.step = plotworld.getSettingNodes();
this.plugin = plugin;
}
public int getCurrent() {
return this.current;
}
public int getMax() {
return this.step.length;
}
}
}