/**
marcinko
 */
package pl.edu.agh.megamud.base;

/**
 * Abstraction of a "action" that a creature can do. Behaviur should last
 * short(time) beacouse it takes time of the eventmanager which is on another
 * thread but all beahaviours are running on this eventmanager thread
 **/
public abstract class Behaviour {
	/**
	 * An owner of this behaviour. Behaviour will work on this creature.
	 */
	protected BehaviourHolderInterface owner;
	/**
	 * Delay after which behaviour will run.
	 */
	protected long delay;
	/**
	 * Time this behaviour will be run.
	 */
	protected long nextTime;

	public long getDelay() {
		return delay;
	}

	public void setDelay(long delay) {
		this.delay = delay;
	}

	public long getNextTime() {
		return nextTime;
	}

	public Behaviour(BehaviourHolderInterface o, long delay) {
		this.owner = o;
		this.delay = delay;
		installBehaviourInHoleder();
	}

	public void installBehaviourInHoleder() {
		owner.addBehaviour(this);
	}

	public Object getOwner() {
		return owner;
	}

	/**
	 * To be implemented by subclasses - what actually Behavior does.
	 */
	protected abstract void action();

	/**
	 * Initialize a behaviour.
	 */
	public Behaviour init() {
		put();

		return this;
	}

	/**
	 * Use this to call an action on a behaviour.
	 */
	public void makeAction() {
		action();
		// owner.removeBehaviour(this);
	}

	/**
	 * Use this in lower classes to (re-)initialize a behaviour.
	 */
	protected final void put() {
		nextTime = EventManager.getInstance().put(new Long(delay), this);
	}

}

@@ -1359,16 +1359,25 @@
 on() %7B%0D%0A
+%09%09try%7B%0D%0A%09
 %09%09action
@@ -1387,39 +1387,58 @@
 %0D%0A%09%09
-// owner.removeBehaviour(this);
+%7Dcatch(Exception e)%7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D
 %0D%0A%09%7D

Fix on Behaviour for possible action failing.