/*
 * This file is part of Politics.
 *
 * Copyright (c) 2012-2012, VolumetricPixels <http://volumetricpixels.com/>
 * Politics is licensed under the Affero General Public License Version 3.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.volumetricpixels.politics.command.universe;

import org.spout.api.Spout;
import org.spout.api.command.Command;
import org.spout.api.command.CommandContext;
import org.spout.api.command.CommandSource;
import org.spout.api.exception.CommandException;
import org.spout.api.geo.World;

import com.volumetricpixels.politics.MsgStyle;
import com.volumetricpixels.politics.Politics;
import com.volumetricpixels.politics.event.PoliticsEventFactory;
import com.volumetricpixels.politics.plot.PoliticsWorld;
import com.volumetricpixels.politics.universe.Universe;
import com.volumetricpixels.politics.universe.UniverseRules;
import java.util.ArrayList;
import java.util.List;

/**
 * Used to create universes.
 */
public class UniverseCreateCommand extends UniverseCommand {
	/**
	 * C'tor
	 */
	public UniverseCreateCommand() {
		super("create");
	}

	@Override
	public void processCommand(CommandSource source, Command command, CommandContext args) throws CommandException {
		String name = args.getString(0).toLowerCase();

		if (name.contains(" ")) {
			source.sendMessage(MsgStyle.ERROR, "Spaces are not allowed in universe names.");
			return;
		}

		if (name.contains("/")) {
			source.sendMessage(MsgStyle.ERROR, "Slashes are not allowed in universe names.");
			return;
		}

		String rules = args.getString(1).toLowerCase();
		UniverseRules theRules = Politics.getUniverseManager().getRules(rules);
		if (theRules == null) {
			source.sendMessage(MsgStyle.ERROR, "There is no set of rules named ", MsgStyle.ERROR_HIGHLIGHT, rules, MsgStyle.ERROR, ". To see the available rules, use ", MsgStyle.ERROR_HIGHLIGHT, "universe rules", MsgStyle.ERROR, ".");
			return;
		}

		String worldsStr = args.getString(2);
		List<PoliticsWorld> worlds = new ArrayList<PoliticsWorld>();
		if (worldsStr == null) {
			for (World world : Spout.getEngine().getWorlds()) {
				worlds.add(Politics.getWorld(world));
			}
		} else {
			String[] worldNames = worldsStr.split(",");
			for (String worldName : worldNames) {
				String trimmed = worldName.trim();
				World world = Spout.getEngine().getWorld(trimmed);
				if (world == null) {
					continue;
				}
				PoliticsWorld pw = Politics.getWorld(world);
				worlds.add(pw);
			}
		}

		if (worlds.size() <= 0) {
			source.sendMessage(MsgStyle.ERROR, "There were no valid worlds specified.");
			return;
		}

		Universe universe = Politics.getUniverseManager().createUniverse(name, theRules);
		PoliticsEventFactory.callUniverseCreateEvent(universe);
		source.sendMessage(MsgStyle.SUCCESS, "A new universe has been created named '" + name + "' with the rules '" + rules + "'.");
	}

	@Override
	protected String[] getAliases() {
		return new String[]{"new", "c", "n"};
	}

	@Override
	public void setupCommand(Command cmd) {
		cmd.setArgBounds(2, -1);
		cmd.setHelp("Creates a new Universe with the given rules.");
		cmd.setUsage("<name> <rules> [worlds...]");
		cmd.setPermissions(true, "politics.admin.universe.create");
	}
}

@@ -2136,32 +2136,405 @@
 %09%09%09return;%0A%09%09%7D%0A%0A
+%09%09boolean force = args.hasFlag('f');%0A%09%09Universe existing = Politics.getUniverse(name);%0A%09%09if (existing != null) %7B%0A%09%09%09source.sendMessage(MsgStyle.ERROR, %22A universe named '%22 + name + %22' already exists. Please destroy that universe via the %60%22, MsgStyle.ERROR_HIGHLIGHT, %22universe destroy%22, MsgStyle.ERROR, %22' command if you wish to overwrite that universe.%22);%0A%09%09%09return;%0A%09%09%7D%0A%0A
 %09%09String rules =

Remove the ability to create duplicate universes. Signed-off-by: Ian Macalinao <ianmacalinao@gmail.com>