summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractState.java
blob: 22fadc0de68a657deff7cb33433bf29e0089379f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.earth2me.essentials.update.states;

import com.earth2me.essentials.update.WorkListener;
import org.bukkit.entity.Player;


public abstract class AbstractState
{
	private transient boolean abortion = false;
	private final transient StateMap stateMap;

	public AbstractState(final StateMap stateMap)
	{
		this.stateMap = stateMap;
	}

	public AbstractState getState(final Class<? extends AbstractState> stateClass)
	{
		return stateMap.get(stateClass);
	}

	public abstract AbstractState getNextState();

	/**
	 * Check if we already know the answer, so the user does not have to answer the question.
	 * 
	 * @return true, if the answer could be guessed.
	 */
	public boolean guessAnswer()
	{
		return false;
	}

	/**
	 * Ask the user the question.
	 * @param sender 
	 */
	public abstract void askQuestion(Player sender);

	/**
	 * React on the answer and set internal variables
	 * @param answer
	 * @return true, if the answer could be recognized as a valid answer
	 */
	public abstract boolean reactOnAnswer(String answer);

	public final AbstractState reactOnAnswer(final Player sender, final String answer)
	{
		final String trimmedAnswer = answer.trim();
		if (trimmedAnswer.equalsIgnoreCase("quit")
			|| trimmedAnswer.equalsIgnoreCase("bye")
			|| trimmedAnswer.equalsIgnoreCase("abort"))
		{
			abort();
			return null;
		}
		final boolean found = reactOnAnswer(trimmedAnswer);
		if (found)
		{
			return getNextState();
		}
		else
		{
			sender.sendMessage("Answer not recognized.");
			return this;
		}
	}

	/**
	 * Do something based on the answer, that the user gave.
	 */
	public abstract void doWork(WorkListener workListener);

	public boolean isAbortion()
	{
		return abortion;
	}

	protected void abort()
	{
		abortion = true;
	}
}