1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-01 12:20:59 +02:00

Allow states to be automatically added to the state map.

They have to have a Constructor that accept the StateMap as argument, otherwise a RuntimeException is thrown.
This commit is contained in:
snowleo
2011-10-26 17:42:39 +02:00
parent d8a40db32c
commit 91b6a19250

View File

@@ -16,6 +16,23 @@ public abstract class AbstractState
public AbstractState getState(final Class<? extends AbstractState> stateClass)
{
if (!stateMap.containsKey(stateClass))
{
try
{
final AbstractState state = stateClass.getConstructor(StateMap.class).newInstance(stateMap);
stateMap.put(stateClass, state);
}
catch (Exception ex)
{
/*
* This should never happen.
* All states that are added to the map automatically,
* have to have a Constructor that accepts the StateMap.
*/
throw new RuntimeException(ex);
}
}
return stateMap.get(stateClass);
}