key0-96/
key0-96/doc/key/
key0-96/doc/key/credits/
key0-96/doc/key/developers/
key0-96/doc/key/developers/resources/
key0-96/setup/caves/
key0-96/setup/help/
key0-96/setup/ruins/
key0-96/src/
key0-96/src/commands/
key0-96/src/events/
key0-96/src/hack/
key0-96/src/sql/
key0-96/src/swing/
key0-96/src/talker/forest/
key0-96/src/talker/objects/
key0-96/src/terminals/
/*
**               j###t  ########## ####   ####
**              j###t   ########## ####   ####
**             j###T               "###L J###"
**          ######P'    ##########  #########
**          ######k,    ##########   T######T
**          ####~###L   ####
**          #### q###L  ##########   .#####
**          ####  \###L ##########   #####"
*/

package key.talker.objects;

import key.*;

import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;
import java.io.*;

/**
  *  Prop.
  *
  *  A simple class that has no behaviour, only a particular appearance,
  *  just like a stage prop.
 */
public class Message extends Prop implements Thing
{
	public static final AtomicElement[] ELEMENTS =
	{
		AtomicElement.construct( Message.class, String.class, "direct",
			AtomicElement.PUBLIC_FIELD,
			"the message sent to the recipient of the use command" ),
		AtomicElement.construct( Message.class, String.class, "feedback",
			AtomicElement.PUBLIC_FIELD,
			"the message sent to the user" ),
		AtomicElement.construct( Message.class, String.class, "room",
			AtomicElement.PUBLIC_FIELD,
			"the message sent to the current room (if set, the target must be in the same room as the originator)" ),
		AtomicElement.construct( Message.class, Boolean.TYPE, "wieldToUse",
			AtomicElement.PUBLIC_FIELD,
			"true if the object must be wielded to be used" ),
	};
	
	public static final char originatorCode = 'o';
	public static final char prefixNameCode = 'p';
	public static final char targetCode = 't';
	
	public static final AtomicStructure STRUCTURE = new AtomicStructure( Atom.STRUCTURE, ELEMENTS );
	
	public String direct = null;
	public String feedback = null;
	public String room = null;
	public boolean wieldToUse = true;
	
	public Message()
	{
	}
	
	public AtomicStructure getDeclaredStructure()
	{
		return( STRUCTURE );
	}
	
	public String getDirect()
		{ return( direct ); }
	
	public String getFeedback()
		{ return( feedback ); }
		
	public String getRoom()
		{ return( room ); }
	
	public boolean getWieldToUse()
		{ return( wieldToUse ); }
	
	public void use( Player p, StringTokenizer args, InteractiveConnection ic, Flags flags, Atom item )
	{
		if( direct != null )
		{
			if( args.hasMoreTokens() )
			{
				String name = args.nextToken();
				
				Player target = (Player) Command.getOnlinePlayer( p, ic, name );
				if( target == null )
					return;
				
				if( room != null )
				{
					if( target.getLocation() != p.getLocation() )
					{
						ic.sendError( target.getName() + " is not in the same room as you." );
					}
				}
				
				p.putCode( originatorCode, p.getName() );
				p.putCode( prefixNameCode, p.getFullName() );
				p.putCode( targetCode, target.getName() );
				
				String main = "";
				String self = "";
				String bcst = "";
				
				if( direct != null )
					main = direct;
				
				if( room != null )
					bcst = Grammar.substitute( room, p.getCodes() );
				
				if( bcst.length() > 0 )
				{
					if( feedback != null )
						self = Grammar.substitute( feedback, p.getCodes() );
					
					new key.effect.DirectedBroadcast( p.getLocation(), p, target, bcst, self, main, targetCode ).cause();
				}
				else
				{
					self = feedback;
					new key.effect.Directed( p, target, self, main, targetCode ).cause();
				}
			}
			else
				ic.sendError( "Usage: use <object> <player>" );
		}
		else
		{
			if( room != null )
			{
				String self = room;
				
				p.putCode( originatorCode, p.getName() );
				p.putCode( prefixNameCode, p.getFullName() );
				
				if( feedback != null )
					self = Grammar.substitute( feedback, p.getCodes() );
				else
					self = Grammar.substitute( room, p.getCodes() );
				
				String main = Grammar.substitute( room, p.getCodes() );
				new key.effect.Broadcast( p.getLocation(), p, main, self ).cause();
			}
			else
				ic.sendFailure( "You cannot use this object." );
		}
	}
}