/
area/
classes/net/sourceforge/pain/logic/
classes/net/sourceforge/pain/logic/event/
classes/net/sourceforge/pain/logic/fn/util/
classes/net/sourceforge/pain/network/console/
classes/net/sourceforge/pain/plugin/
classes/net/sourceforge/pain/plugin/reset/
classes/net/sourceforge/pain/plugin/shutdown/
classes/net/sourceforge/pain/plugin/social/
classest/net/sourceforge/pain/db/data/
doc/
doc/paindb/resources/
src/net/sourceforge/pain/logic/
src/net/sourceforge/pain/logic/event/
src/net/sourceforge/pain/logic/fn/util/
src/net/sourceforge/pain/network/console/
src/net/sourceforge/pain/network/console/telnet/
src/net/sourceforge/pain/plugin/
src/net/sourceforge/pain/plugin/command/
src/net/sourceforge/pain/plugin/reset/
src/net/sourceforge/pain/plugin/shutdown/
src/net/sourceforge/pain/plugin/social/
src/net/sourceforge/pain/util/
tests/
tests/net/sourceforge/pain/db/data/
package net.sourceforge.pain.logic.event.console.command.builder;

import net.sourceforge.pain.network.console.*;
import net.sourceforge.pain.data.type.*;
import net.sourceforge.pain.logic.fn.*;
import net.sourceforge.pain.logic.fn.util.*;

import java.util.*;

/**
 * PAiN  Date: 05.06.2003  Time: 1:40:02
 */
public final class BC_Unlink extends BuilderCommand {

	public void processBuilderCommand(BuilderShell p, String args) throws Exception {
		boolean failed = true;
		final Space space = p.player.asLocated().getLocation();
		LinkedSpace link = (LinkedSpace) space.getRole(LinkedSpace.class);
		if (link == null) {
			MessageOutFn.outln(p.console, "ERROR:No exists found");
			return;
		}
		if (args != null) {
			StringTokenizer st = new StringTokenizer(args, " ");
			final String directionStr = st.nextToken();
			if (directionStr.length() != 1) {
				MessageOutFn.outln(p.console, "Illegal direction:" + directionStr);
				return;
			}
			final int dir;
			try {
				dir = Utils.exitCharToDir(directionStr.charAt(0));
			} catch (Exception e) {
				MessageOutFn.outln(p.console, "Illegal direction:" + directionStr);
				return;
			}
			Exit e = link.getExit(dir);
			if (e == null) {
				MessageOutFn.outln(p.console, "ERROR:No exist found");
				return;
			}
			boolean single = false;
			if (!st.hasMoreTokens() || (single = "SINGLE".equals(st.nextToken()))) {
				failed = false;
				Space targetSpace = e.getTargetSpace();
				if (!single) {
					LinkedSpace targetSpaceLink = (LinkedSpace) targetSpace.getRole(LinkedSpace.class);
					if (targetSpaceLink != null) {
						int reverseDir = LinkedSpace.reverseDir[dir];
						Exit reverseExit = targetSpaceLink.getExit(reverseDir);
						if (reverseExit != null) {
							unlink(p.console, targetSpaceLink, reverseDir, space);
						}
					}
				}
				unlink(p.console, link, dir, targetSpace);
			}
		}
		if (failed) {
			showUsage(p.console);
		}
	}

	void unlink(Console console, LinkedSpace link, int dir, Space targetSpace) throws Exception {
		Space space = link.asSpace();
		MessageOutFn.outln(console, "Destroying path from {Y" + space.getName() + "{x direction:{C" + LangUtil.exitName[dir] + "{x to {Y" + targetSpace.getName() + "{x");
		Exit e = link.getExit(dir);
		e.delete();
		boolean noExistsLeft = true;
		for (int i = LinkedSpace.FIRST_DIR; i <= LinkedSpace.LAST_DIR; i++) {
			if (link.getExit(dir) != null) {
				noExistsLeft = false;
				break;
			}
		}
		if (noExistsLeft) {
			space.removeRole(LinkedSpace.class);
		}
	}

	public void showUsage(Console console) {
		MessageOutFn.outln(console, "UNLINK:Command syntax <DIRECTION> [SINGLE]\n");
	}

	public void showHelp(Console console) {
		MessageOutFn.outln(console, "Builder command UNPATH destroys exit from current space");
		MessageOutFn.outln(console, "Usage: unlink <DIRECTION> [SINGLE]");
		MessageOutFn.outln(console, "where {wDIRECTION{x is a letter from (N,E,S,W,U,D)");
		MessageOutFn.outln(console, "if {wSINGLE{x option is specified only single way link will be destroyed");
		MessageOutFn.outln(console, "NOTE: LinkedSpace role will be removed from current or target space ");
		MessageOutFn.outln(console, "      if no other exits will found from space");
	}

}