States
A game will consist of multiple states, like "Waiting for more players", "Starting", "Running", "Ending", etc.
States will always be their own classes that extend VitalMinigameInstanceState<T extends VitalMinigameInstance>.
public class MyInstanceState extends VitalMinigameInstanceState<MyInstance> {
public MyInstanceState(MyInstance myInstance) {
super(myInstance);
}
}
Note that we are directly defining to which instance this state belongs to (
<MyInstance>).
This way, we can always access our instance directly from our state to read or write game data, etc.
Event handling
Every state will by default always be a listener and can therefore directly receive events.
You can use the default Spigot event handlers in your states.
public class MyInstanceState extends VitalMinigameInstanceState<MyInstance> {
public MyInstanceState(MyInstance myInstance) {
super(myInstance);
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
// ...
}
}
The event handlers that you define in your states will only receive events if the event that occurred actually came from your instance world.
Also, all of your event handlers will only be active when this state is currently active. This is what makes Vital minigames so powerful!
Switching states
You can freely switch between your states using the setState(VitalMinigameInstanceState) method in your instance class.
This allows you to, for example, switch a state directly from another state.
public class MyInstanceState extends VitalMinigameInstanceState<MyInstance> {
public MyInstanceState(MyInstance myInstance) {
super(myInstance);
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
getInstance().setState(getInstance().getSomeOtherStateThatYouDefinedInYourInstance());
}
}
Lifecycles
| Lifecycle | Usage |
|---|---|
onEnable() | Perform an action when this state is enabled. |
onDisable() | Perform an action when this state is disabled. |