Creation of spaced worlds works, so do seeds. Now refactor hardcore.

This commit is contained in:
Eric Stokes 2011-06-02 19:59:57 -04:00
parent 3a6d401a72
commit a5a3618439
3 changed files with 96 additions and 24 deletions

View File

@ -37,7 +37,7 @@ public abstract class BaseCommand {
args = new String[0]; args = new String[0];
} }
int l = args.length; int l = args.length;
if (l >= minArgs && l <= maxArgs) { if (l >= minArgs && (maxArgs == -1 ||l <= maxArgs)) {
return args; return args;
} }
} }

View File

@ -67,4 +67,25 @@ public class CommandManager {
public List<BaseCommand> getCommands() { public List<BaseCommand> getCommands() {
return commands; return commands;
} }
private String[] parseAllQuotedStrings(String[] args) {
String[] returnVal = {};
return returnVal;
}
/**
* Takes a string array and returns a combined string, excluding the stop position, including the start
*
* @param args
* @param start
* @param stop
* @return
*/
private String parseQuotedString(String[] args, int start, int stop) {
String returnVal = args[start];
for (int i = start + 1; i < stop; i++) {
returnVal += " " + args[i];
}
return returnVal.replace("\"", "");
}
} }

View File

@ -1,6 +1,8 @@
package com.onarandombox.MultiverseCore.command.commands; package com.onarandombox.MultiverseCore.command.commands;
import java.io.File; import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.World.Environment; import org.bukkit.World.Environment;
@ -10,39 +12,71 @@ import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.command.BaseCommand; import com.onarandombox.MultiverseCore.command.BaseCommand;
public class CreateCommand extends BaseCommand { public class CreateCommand extends BaseCommand {
private final String envNameArray[] = { "NETHER", "NORMAL", "SKYLANDS", "SKYLAND", "HELL" };
private final HashSet<String> envNames = new HashSet<String>(Arrays.asList(envNameArray));
public CreateCommand(MultiverseCore plugin) { public CreateCommand(MultiverseCore plugin) {
super(plugin); super(plugin);
name = "Create World"; name = "Create World";
description = "Creates a new world of the specified type"; description = "Creates a new world of the specified type";
// Syntax is s:SEED so we can see this variable seperately when spaces are allowed in worldnames // Syntax is s:SEED so we can see this variable seperately when spaces are allowed in worldnames
usage = "/mvcoord" + ChatColor.GREEN + "{NAME} {TYPE}" + ChatColor.GOLD + " [s:SEED]"; usage = "/mvcreate" + ChatColor.GREEN + " {NAME} {TYPE}" + ChatColor.GOLD + " [s:SEED]";
minArgs = 2; minArgs = 2;
maxArgs = 3; maxArgs = -1;
identifiers.add("mvcoord"); identifiers.add("mvcreate");
} }
@Override @Override
public void execute(CommandSender sender, String[] args) { public void execute(CommandSender sender, String[] args) {
// TODO: Permissions, check // TODO: Permissions, check
// TODO: Allow spaces in world names, currently this will catch it --FF // TODO: Allow spaces in world names, currently this will catch it --FF
int numOfParams = args.length; int numOfParams = args.length;
// By default the environment will be located at the end, unless there is a seed // We'll search for this in a minute, if it's still -1 after the search, a user did this:
int envPosition = numOfParams - 1; // /mvcreate "My Awesome world NETHER
// ie. they forgot to close the "
int envPosition = -1;
boolean hasSeed = false; boolean hasSeed = false;
String seed = "";
if(args[numOfParams - 1].substring(0, 2).equalsIgnoreCase("s:")) { // Did the user input "s?
if (args[0].substring(0, 1).equals("\"")) {
// Find the magical keyword,
for (int i = 0; i < numOfParams; i++) {
if (args[i].substring(args[i].length() - 1, args[i].length()).equals("\"") && envPosition == -1) {
// We found a word that ended with a quote, the next one MUST be the environment keyword
envPosition = i + 1;
}
}
} else {
// There can only be one word, envPosition must be 1
envPosition = 1;
}
if (envPosition == -1) {
sender.sendMessage("Why are you such a failure...");
return;
}
// If the environment position is not the last in the list, we have a seed after it:
if (envPosition != numOfParams - 1) {
hasSeed = true;
}
String worldName = parseQuotedString(args, 0, envPosition);
String env = args[envPosition];
// If we have a seed, parse the quotes out of it, if not, it's == ""
String seed = hasSeed ? parseQuotedString(args, envPosition + 1, numOfParams) : "";
if (args[numOfParams - 1].substring(0, 2).equalsIgnoreCase("s:")) {
envPosition--; envPosition--;
hasSeed = true; hasSeed = true;
// Pull out the seed name, drop the "s:" // Pull out the seed name, drop the "s:"
seed = args[numOfParams - 1].split(":")[1]; seed = args[numOfParams - 1].split(":")[1];
} }
String env = args[envPosition]; sender.sendMessage("Stuff I found:");
String worldName = args[0]; sender.sendMessage(ChatColor.GREEN + "worldName" + ChatColor.WHITE + worldName);
for(int i = 1; i < envPosition; i++) { sender.sendMessage(ChatColor.GREEN + "env " + ChatColor.WHITE + env);
worldName += " " + args[i]; sender.sendMessage(ChatColor.GREEN + "seed " + ChatColor.WHITE + seed);
}
if (args.length < 2) { if (args.length < 2) {
sender.sendMessage("Not enough parameters to create a new world"); sender.sendMessage("Not enough parameters to create a new world");
@ -52,37 +86,54 @@ public class CreateCommand extends BaseCommand {
sender.sendMessage(ChatColor.RED + "Example - /mvcreate hellworld NETHER"); sender.sendMessage(ChatColor.RED + "Example - /mvcreate hellworld NETHER");
return; return;
} }
if (new File(args[0]).exists() || this.plugin.worlds.containsKey(args[0])) { if (new File(worldName).exists() || this.plugin.worlds.containsKey(worldName)) {
sender.sendMessage(ChatColor.RED + "A Folder/World already exists with this name!"); sender.sendMessage(ChatColor.RED + "A Folder/World already exists with this name!");
sender.sendMessage(ChatColor.RED + "If you are confident it is a world you can import with /mvimport"); sender.sendMessage(ChatColor.RED + "If you are confident it is a world you can import with /mvimport");
return; return;
} }
// String name = args[0].toString();
Environment environment = null; Environment environment = null;
// Don't reference the enum directly as there aren't that many, and we can be more forgiving to users this way // Don't reference the enum directly as there aren't that many, and we can be more forgiving to users this way
if (env.equalsIgnoreCase("NETHER") || env.equalsIgnoreCase("HELL")) if (env.equalsIgnoreCase("NETHER") || env.equalsIgnoreCase("HELL"))
environment = Environment.NETHER; environment = Environment.NETHER;
if (env.equalsIgnoreCase("NORMAL")) if (env.equalsIgnoreCase("NORMAL"))
environment = Environment.NORMAL; environment = Environment.NORMAL;
if (env.equalsIgnoreCase("SKYLANDS") || env.equalsIgnoreCase("SKYLAND")) if (env.equalsIgnoreCase("SKYLANDS") || env.equalsIgnoreCase("SKYLAND"))
environment = Environment.SKYLANDS; environment = Environment.SKYLANDS;
if (environment == null) { if (environment == null) {
sender.sendMessage(ChatColor.RED + "Environment type " + env + " does not exist!"); sender.sendMessage(ChatColor.RED + "Environment type " + env + " does not exist!");
return; return;
} }
if(hasSeed) { if (hasSeed) {
plugin.getServer().createWorld(worldName, environment, Long.parseLong(seed)); // plugin.getServer().createWorld(worldName, environment, Long.parseLong(seed));
plugin.addWorld(worldName, environment, Long.parseLong(seed)); try {
plugin.addWorld(worldName, environment, Long.parseLong(seed));
} catch (NumberFormatException e) {
plugin.addWorld(worldName, environment, (long) seed.hashCode());
}
} else { } else {
plugin.getServer().createWorld(worldName, environment); // plugin.getServer().createWorld(worldName, environment);
plugin.addWorld(worldName, environment); plugin.addWorld(worldName, environment);
} }
return; return;
} }
/**
* Takes a string array and returns a combined string, excluding the stop position, including the start
*
* @param args
* @param start
* @param stop
* @return
*/
private String parseQuotedString(String[] args, int start, int stop) {
String returnVal = args[start];
for (int i = start + 1; i < stop; i++) {
returnVal += " " + args[i];
}
return returnVal.replace("\"", "");
}
} }