mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-01-26 18:11:52 +01:00
Moving the link between the states from the constructor of the state machine to the states classes.
This commit is contained in:
parent
6974abea28
commit
8854b3f565
@ -7,6 +7,17 @@ import org.bukkit.entity.Player;
|
|||||||
public abstract class AbstractState
|
public abstract class AbstractState
|
||||||
{
|
{
|
||||||
private transient boolean abortion = false;
|
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();
|
public abstract AbstractState getNextState();
|
||||||
|
|
||||||
@ -40,7 +51,7 @@ public abstract class AbstractState
|
|||||||
|| trimmedAnswer.equalsIgnoreCase("bye")
|
|| trimmedAnswer.equalsIgnoreCase("bye")
|
||||||
|| trimmedAnswer.equalsIgnoreCase("abort"))
|
|| trimmedAnswer.equalsIgnoreCase("abort"))
|
||||||
{
|
{
|
||||||
abortion = true;
|
abort();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final boolean found = reactOnAnswer(trimmedAnswer);
|
final boolean found = reactOnAnswer(trimmedAnswer);
|
||||||
@ -64,4 +75,9 @@ public abstract class AbstractState
|
|||||||
{
|
{
|
||||||
return abortion;
|
return abortion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void abort()
|
||||||
|
{
|
||||||
|
abortion = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,17 @@ package com.earth2me.essentials.update.states;
|
|||||||
public abstract class AbstractYesNoState extends AbstractState
|
public abstract class AbstractYesNoState extends AbstractState
|
||||||
{
|
{
|
||||||
private boolean answer = false;
|
private boolean answer = false;
|
||||||
private final transient AbstractState yesState;
|
private final transient Class<? extends AbstractState> yesState;
|
||||||
private final transient AbstractState noState;
|
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.yesState = yesState;
|
||||||
this.noState = noState;
|
this.noState = noState;
|
||||||
}
|
}
|
||||||
@ -16,7 +22,9 @@ public abstract class AbstractYesNoState extends AbstractState
|
|||||||
@Override
|
@Override
|
||||||
public AbstractState getNextState()
|
public AbstractState getNextState()
|
||||||
{
|
{
|
||||||
return answer ? yesState : noState;
|
return answer
|
||||||
|
? (yesState == null ? null : getState(yesState))
|
||||||
|
: (noState == null ? null : getState(noState));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.earth2me.essentials.update.states;
|
package com.earth2me.essentials.update.states;
|
||||||
|
|
||||||
import com.earth2me.essentials.update.WorkListener;
|
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.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
@ -9,9 +9,9 @@ import org.bukkit.plugin.Plugin;
|
|||||||
|
|
||||||
public class EssentialsChat extends AbstractYesNoState
|
public class EssentialsChat extends AbstractYesNoState
|
||||||
{
|
{
|
||||||
public EssentialsChat(final AbstractState next)
|
public EssentialsChat(final StateMap states)
|
||||||
{
|
{
|
||||||
super(next, next);
|
super(states, EssentialsProtect.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -38,7 +38,7 @@ public class EssentialsChat extends AbstractYesNoState
|
|||||||
{
|
{
|
||||||
if (getAnswer())
|
if (getAnswer())
|
||||||
{
|
{
|
||||||
new InstallChat(listener).start();
|
new InstallModule(listener, "EssentialsChat").start();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
listener.onWorkDone();
|
listener.onWorkDone();
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -2,8 +2,6 @@ package com.earth2me.essentials.update.states;
|
|||||||
|
|
||||||
import com.earth2me.essentials.update.WorkListener;
|
import com.earth2me.essentials.update.WorkListener;
|
||||||
import com.earth2me.essentials.update.VersionInfo;
|
import com.earth2me.essentials.update.VersionInfo;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
@ -15,7 +13,7 @@ public class StateMachine extends WorkListener
|
|||||||
{
|
{
|
||||||
ABORT, WAIT, DONE
|
ABORT, WAIT, DONE
|
||||||
}
|
}
|
||||||
private final transient List<AbstractState> states = new ArrayList<AbstractState>();
|
private final transient StateMap states = new StateMap();
|
||||||
private transient AbstractState current;
|
private transient AbstractState current;
|
||||||
private final transient Player player;
|
private final transient Player player;
|
||||||
|
|
||||||
@ -24,9 +22,8 @@ public class StateMachine extends WorkListener
|
|||||||
super(plugin, newVersionInfo);
|
super(plugin, newVersionInfo);
|
||||||
this.player = player;
|
this.player = player;
|
||||||
states.clear();
|
states.clear();
|
||||||
final AbstractState state = new EssentialsChat(null);
|
states.put(EssentialsChat.class, new EssentialsChat(states));
|
||||||
states.add(state);
|
current = states.get(0);
|
||||||
current = state;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public MachineResult askQuestion()
|
public MachineResult askQuestion()
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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");
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user