Class BasicStateMachine<T extends IStateMachineTransition<S>,S extends IState>
java.lang.Object
com.minecolonies.api.entity.ai.statemachine.basestatemachine.BasicStateMachine<T,S>
- All Implemented Interfaces:
IStateMachine<T,S>
- Direct Known Subclasses:
TickRateStateMachine
public class BasicStateMachine<T extends IStateMachineTransition<S>,S extends IState>
extends Object
implements IStateMachine<T,S>
Basic statemachine class, can be used for any Transition typed which extends the transition interface. It contains the current state and a hashmap for events and transitions,
which are the minimal requirements to have a working statemachine.
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe current states list of transitionsprotected final @NotNull Map<IStateEventType, List<T>> The lists of transitions and events -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBasicStateMachine(S initialState, @NotNull Consumer<RuntimeException> exceptionHandler) Construct a new StateMachine -
Method Summary
Modifier and TypeMethodDescriptionvoidaddTransition(T transition) Add one transitionvoidaddTransitionGroup(List<S> stateGroup, T transition) Add a transition to a group of states has higher priority than normal transitionsbooleancheckTransition(T transition) Check the condition for a transitionnet.minecraft.network.chat.ComponentGet the state transition history for displayfinal SgetState()Get the current state of the statemachineprotected voidHandle an exception higher up.voidremoveTransition(T transition) Unregisters a transitionvoidreset()Resets the statemachinevoidsetHistoryEnabled(boolean enabled, int memorySize) Set whether the state history is trackedvoidtick()Updates the statemachine.booleantransitionToNext(T transition) Change the state to the next
-
Field Details
-
transitionMap
@NotNull protected final @NotNull Map<S extends IState,List<T extends IStateMachineTransition<S>>> transitionMapThe lists of transitions and events -
eventTransitionMap
@NotNull protected final @NotNull Map<IStateEventType,List<T extends IStateMachineTransition<S>>> eventTransitionMap -
currentStateTransitions
The current states list of transitions
-
-
Constructor Details
-
BasicStateMachine
protected BasicStateMachine(@NotNull S initialState, @NotNull @NotNull Consumer<RuntimeException> exceptionHandler) Construct a new StateMachine- Parameters:
initialState- the initial state.exceptionHandler- the exception handler.
-
-
Method Details
-
addTransition
Add one transition- Specified by:
addTransitionin interfaceIStateMachine<T extends IStateMachineTransition<S>,S extends IState> - Parameters:
transition- the transition to add
-
addTransitionGroup
Description copied from interface:IStateMachineAdd a transition to a group of states has higher priority than normal transitions- Specified by:
addTransitionGroupin interfaceIStateMachine<T extends IStateMachineTransition<S>,S extends IState> - Parameters:
transition- the transition to add
-
removeTransition
Unregisters a transition- Specified by:
removeTransitionin interfaceIStateMachine<T extends IStateMachineTransition<S>,S extends IState> - Parameters:
transition- the transition to remove.
-
tick
public void tick()Updates the statemachine.- Specified by:
tickin interfaceIStateMachine<T extends IStateMachineTransition<S>,S extends IState>
-
checkTransition
Check the condition for a transition- Specified by:
checkTransitionin interfaceIStateMachine<T extends IStateMachineTransition<S>,S extends IState> - Parameters:
transition- the target to check- Returns:
- true if this target worked and we should stop executing this tick
-
transitionToNext
Description copied from interface:IStateMachineChange the state to the next- Specified by:
transitionToNextin interfaceIStateMachine<T extends IStateMachineTransition<S>,S extends IState> - Parameters:
transition- the transition providing the state change- Returns:
- true if the transition provided a state to change to, false on null
-
onException
Handle an exception higher up.- Parameters:
e- The exception to be handled.
-
getState
Get the current state of the statemachine- Specified by:
getStatein interfaceIStateMachine<T extends IStateMachineTransition<S>,S extends IState> - Returns:
- The current IAIState.
-
reset
public void reset()Resets the statemachine- Specified by:
resetin interfaceIStateMachine<T extends IStateMachineTransition<S>,S extends IState>
-
setHistoryEnabled
public void setHistoryEnabled(boolean enabled, int memorySize) Description copied from interface:IStateMachineSet whether the state history is tracked- Specified by:
setHistoryEnabledin interfaceIStateMachine<T extends IStateMachineTransition<S>,S extends IState> - Parameters:
enabled-memorySize- amount of entries kept
-
getHistory
public net.minecraft.network.chat.Component getHistory()Description copied from interface:IStateMachineGet the state transition history for display- Specified by:
getHistoryin interfaceIStateMachine<T extends IStateMachineTransition<S>,S extends IState> - Returns:
-