/
codebase/
codebase/area/
codebase/doc/
codebase/etc/
codebase/src/net/sourceforge/pain/data/trigger/
codebase/src/net/sourceforge/pain/logic/
codebase/src/net/sourceforge/pain/logic/affect/
codebase/src/net/sourceforge/pain/logic/event/
codebase/src/net/sourceforge/pain/logic/event/deploy/
codebase/src/net/sourceforge/pain/logic/event/guitool/
codebase/src/net/sourceforge/pain/logic/event/guitool/event/
codebase/src/net/sourceforge/pain/logic/fn/util/
codebase/src/net/sourceforge/pain/logic/trigger/
codebase/src/net/sourceforge/pain/logic/trigger/impl/
codebase/src/net/sourceforge/pain/network/console/
codebase/src/net/sourceforge/pain/network/console/telnet/
codebase/src/net/sourceforge/pain/network/guitool/
codebase/src/net/sourceforge/pain/plugin/
codebase/src/net/sourceforge/pain/plugin/command/
codebase/src/net/sourceforge/pain/plugin/reset/
codebase/src/net/sourceforge/pain/plugin/shutdown/
codebase/src/net/sourceforge/pain/plugin/social/
codebase/src/net/sourceforge/pain/util/
db/doc/javadoc/resources/
db/src/net/sourceforge/pain/util/
gui/
gui/lib/
gui/src/net/sourceforge/pain/tools/guitool/dbbrowse/
gui/src/net/sourceforge/pain/tools/guitool/dialog/
gui/src/net/sourceforge/pain/tools/guitool/menu/
gui/src/net/sourceforge/pain/tools/guitool/resources/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/explorer/
tests/
tests/src/
tests/src/net/sourceforge/pain/db/data/
package net.sourceforge.pain.logic.event.console.command;

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

public final class Give extends CommandHandler {

	public boolean isAccessible() {
		return super.isAccessible() && player.is(Creature.class);
	}


	public void processCommand() throws Exception {
		if (commandParams == null || commandParams.length() == 0) {
			MessageOutFn.outln(console, "Give what to whom?");
			return;
		}
		String[] tokens = Utils.WHITESPACE_SPLIT_PATTERN.split(commandParams.toLowerCase());
		if (tokens.length == 1 || (tokens.length == 2 && "to".equals(tokens[1]))) {
			MessageOutFn.outln(console, "Give to whom?");
			return;
		}
		String objName = tokens[0];
		String targetName = tokens.length > 2 && "to".equals(tokens[1]) ? tokens[2] : tokens[1];

		// look for obj
		Creature creature = (Creature) player.getRole(Creature.class);
		final Located plocated = player.asLocated();
		Interactive iObj = SpaceFindFn.findByPrefix(plocated, creature.getInventory(), objName);
		Physical physObj = iObj != null ? (Physical) iObj.getRole(Physical.class) : null;
		if (physObj == null) {
			if (iObj != null) {
				MessageOutFn.outln(console, "You failed.");
			} else {
				MessageOutFn.outln(console, "You do not have that item.");
			}
			return;
		}

		// look for target
		Interactive iTarget = SpaceFindFn.findByPrefix(plocated, targetName);
		Creature cTarget = iTarget != null ? (Creature) iTarget.getRole(Creature.class) : null;
		if (cTarget == null) {
			if (iTarget != null) {
				MessageOutFn.outln(console, "You failed.");
			} else {
				MessageOutFn.outln(console, "They aren't here.");
			}
			return;
		}
		if (cTarget.sameObjectAs(player)) {
			MessageOutFn.outln(console, "You failed.");
			return;
		}
		GiveFn.give(creature, physObj, cTarget);
	}

	public void showHelp() {
		MessageOutFn.outln(console, command.name + ": use this command to give items to other persons");
	}

}