diff options
author | snowleo <schneeleo@gmail.com> | 2011-10-23 23:08:18 +0200 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2011-10-23 23:08:42 +0200 |
commit | 8854b3f56585d480cd0c8151e7bbbf12d00cd984 (patch) | |
tree | 1a137b3595e60e0f2aac6f70184f782fb1a37ea7 /EssentialsUpdate | |
parent | 6974abea28233fd6118567841dad4e3af0a43d4b (diff) | |
download | Essentials-8854b3f56585d480cd0c8151e7bbbf12d00cd984.tar Essentials-8854b3f56585d480cd0c8151e7bbbf12d00cd984.tar.gz Essentials-8854b3f56585d480cd0c8151e7bbbf12d00cd984.tar.lz Essentials-8854b3f56585d480cd0c8151e7bbbf12d00cd984.tar.xz Essentials-8854b3f56585d480cd0c8151e7bbbf12d00cd984.zip |
Moving the link between the states from the constructor of the state machine to the states classes.
Diffstat (limited to 'EssentialsUpdate')
7 files changed, 94 insertions, 27 deletions
diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractState.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractState.java index b217ebd4b..22fadc0de 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractState.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractState.java @@ -7,6 +7,17 @@ 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(); @@ -40,7 +51,7 @@ public abstract class AbstractState || trimmedAnswer.equalsIgnoreCase("bye") || trimmedAnswer.equalsIgnoreCase("abort")) { - abortion = true; + abort(); return null; } final boolean found = reactOnAnswer(trimmedAnswer); @@ -64,4 +75,9 @@ public abstract class AbstractState { return abortion; } + + protected void abort() + { + abortion = true; + } } diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractYesNoState.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractYesNoState.java index 67701c8db..67baf6bb6 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractYesNoState.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/AbstractYesNoState.java @@ -4,11 +4,17 @@ package com.earth2me.essentials.update.states; public abstract class AbstractYesNoState extends AbstractState { private boolean answer = false; - private final transient AbstractState yesState; - private final transient AbstractState noState; + private final transient Class<? extends AbstractState> yesState; + private final transient Class<? extends AbstractState> noState; - public AbstractYesNoState(final AbstractState yesState, final AbstractState noState) + public AbstractYesNoState(final StateMap states, final Class<? extends AbstractState> nextState) { + this(states, nextState, nextState); + } + + public AbstractYesNoState(final StateMap states, final Class<? extends AbstractState> yesState, final Class<? extends AbstractState> noState) + { + super(states); this.yesState = yesState; this.noState = noState; } @@ -16,7 +22,9 @@ public abstract class AbstractYesNoState extends AbstractState @Override public AbstractState getNextState() { - return answer ? yesState : noState; + return answer + ? (yesState == null ? null : getState(yesState)) + : (noState == null ? null : getState(noState)); } @Override diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsChat.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsChat.java index 6669161d4..516ec35b0 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsChat.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsChat.java @@ -1,7 +1,7 @@ package com.earth2me.essentials.update.states; import com.earth2me.essentials.update.WorkListener; -import com.earth2me.essentials.update.tasks.InstallChat; +import com.earth2me.essentials.update.tasks.InstallModule; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -9,9 +9,9 @@ import org.bukkit.plugin.Plugin; public class EssentialsChat extends AbstractYesNoState { - public EssentialsChat(final AbstractState next) + public EssentialsChat(final StateMap states) { - super(next, next); + super(states, EssentialsProtect.class); } @Override @@ -38,7 +38,7 @@ public class EssentialsChat extends AbstractYesNoState { if (getAnswer()) { - new InstallChat(listener).start(); + new InstallModule(listener, "EssentialsChat").start(); return; } listener.onWorkDone(); diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsProtect.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsProtect.java new file mode 100644 index 000000000..5300ca543 --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/EssentialsProtect.java @@ -0,0 +1,46 @@ +package com.earth2me.essentials.update.states; + +import com.earth2me.essentials.update.WorkListener; +import com.earth2me.essentials.update.tasks.InstallModule; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + + +public class EssentialsProtect extends AbstractYesNoState +{ + public EssentialsProtect(final StateMap states) + { + super(states, null); + } + + @Override + public boolean guessAnswer() + { + final Plugin plugin = Bukkit.getPluginManager().getPlugin("EssentialsProtect"); + if (plugin != null) + { + setAnswer(true); + return true; + } + return false; + } + + @Override + public void askQuestion(final Player sender) + { + sender.sendMessage("Do you want to install EssentialsProtect? (yes/no)"); + sender.sendMessage("Short descriptive text about what EssentialsProtect does."); + } + + @Override + public void doWork(final WorkListener listener) + { + if (getAnswer()) + { + new InstallModule(listener, "EssentialsProtect").start(); + return; + } + listener.onWorkDone(); + } +}
\ No newline at end of file diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java index b69ee14a9..53612715e 100644 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMachine.java @@ -2,8 +2,6 @@ package com.earth2me.essentials.update.states; import com.earth2me.essentials.update.WorkListener; import com.earth2me.essentials.update.VersionInfo; -import java.util.ArrayList; -import java.util.List; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -15,7 +13,7 @@ public class StateMachine extends WorkListener { ABORT, WAIT, DONE } - private final transient List<AbstractState> states = new ArrayList<AbstractState>(); + private final transient StateMap states = new StateMap(); private transient AbstractState current; private final transient Player player; @@ -24,9 +22,8 @@ public class StateMachine extends WorkListener super(plugin, newVersionInfo); this.player = player; states.clear(); - final AbstractState state = new EssentialsChat(null); - states.add(state); - current = state; + states.put(EssentialsChat.class, new EssentialsChat(states)); + current = states.get(0); } public MachineResult askQuestion() diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMap.java b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMap.java new file mode 100644 index 000000000..02d6cc129 --- /dev/null +++ b/EssentialsUpdate/src/com/earth2me/essentials/update/states/StateMap.java @@ -0,0 +1,12 @@ +package com.earth2me.essentials.update.states; + +import java.util.LinkedHashMap; + + +public class StateMap extends LinkedHashMap<Class<? extends AbstractState>, AbstractState> +{ + public StateMap() + { + super(); + } +} diff --git a/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/InstallChat.java b/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/InstallChat.java deleted file mode 100644 index 44b4e245d..000000000 --- a/EssentialsUpdate/src/com/earth2me/essentials/update/tasks/InstallChat.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.earth2me.essentials.update.tasks; - -import com.earth2me.essentials.update.WorkListener; - - -public class InstallChat extends InstallModule -{ - public InstallChat(final WorkListener listener) - { - super(listener, "EssentialsChat"); - } -} |