Skip to content

Lifecycle

Lars Bärtschi edited this page Jan 8, 2018 · 11 revisions

Plugin Lifecycle

Each Twasi Plugin consist of at least two classes: One class extends TwasiPluginPlugin, the other TwasiUserPlugin.

TL;DR

  • The plugin specifies a class that extends TwasiPlugin
  • One TwasiPlugin instance will be created per runtime (single instance)
  • One TwasiUserPlugin instance will be created per User. Data that is stored per user should go there.

How to implement

Why is this? Doesn't that makes writing plugins more complicated?

The problem is the relationship between plugins, twasi-core and users.

1 Twasi-Core has several Users 1 Twasi-Core loads several Plugins

Now a Twasi plugin wants to perform actions on the twasi-core level (e.g. on startup of Twasi-Core), and on user level (e.g. if a message is written in the chat of the user). The main class specified in plugin.yml should hereby link to the class that extends TwasiPlugin (TwasiCore-Extension). Then from there, link to your TwasiUserPlugin using

@Override
public Class<? extends TwasiUserPlugin> getUserPluginClass() {
  return myUserPluginName.class;
}

Differences between TwasiCorePlugin and TwasiUserPlugin:

  • TwasiCorePlugin will only be instantiated one single time (once per application). TwasiUserPlugin will be instantiated once per user.

What you should do where

TwasiCorePlugin:

  • Read / Load config
  • Register web handlers
  • Listen to CLI events (CLI TODO)

TwasiUserPlugin:

  • Handle/answer commands
  • Handle/answer incoming messages from twitch

What is the advantage of this?

You will need to store variables per user. Let's imagine a simple counter. Everytime a certain command is executed, it should increase a number and then print it out. Now the number should be stored per user. User1 should be able to have his counter at 1337, while User2 has his at 13. TwasiUserPlugin enables us to do this using simple class variables. They won't interfere each other, since there is one instance per user.

What is important?

It is important that your TwasiUserPlugin is as light as possible. Because there will be created one TwasiUserPlugin per Plugin per User. If 500 users have 20 plugins installed, this makes a total of 10'000 instances. This is a lot! Please avoid to store anything in the user instance that could be stored globally (e.g. in the TwasiCorePlugin).

Other things to avoid in TwasiUserPlugin:

  • Accessing global config files
  • Storing global config files
  • Have big data cached (saved in class variables), access the database directly.

Twasi-Core extension (extends TwasiCorePlugin)

onActivate

onActivate is called after:

  • The application is started up
  • The application is reloaded (using the reload command)

Recommended tasks:

  • Read/Create (if not exist) config files
  • Open up connections to needed resources
@Override
public void onActivate(TwasiActivateEvent e) {
  // The plugin was activated
}

onDeactivate

onDeactivate is called before:

  • The application is shut down
  • The application is reloaded (using the reload command)

Recommended tasks:

  • Close and free up all resources
  • Persist all data. The application want to shut down.
@Override
public void onDeactivate(TwasiDeactivateEvent e) {
  // The plugin was deactivated
}

Twasi-User extension (extends TwasiUserPlugin)

onEnable

onEnable is called

  • after an instance is started (initial instances will be started at the application start)
  • after an instance is restarted
  • after the plugin is installed onto an instance

Recommended tasks:

  • Prepare for receiving onMessage/onCommand events. You can receive them from now on.
  • Open some resources for the particular instance
@Override
public void onEnable(TwasiEnableEvent e) {
  // The plugin was enabled
}

onDisable

onDisable is called

  • before an instance is shut down (all instances will be shut down before the application does)
  • before an instance is restarted
  • before the plugin is uninstalled onto an instance

Recommended tasks:

  • Persist data per user (=instance). Close all resources for this instance.
@Override
public void onDisable(TwasiDisableEvent e) {
  // The plugin was disabled
}

onInstall

onInstall is called

  • After a new user signed up for all default plugins
  • When a plugin gets installed to an instance

Recommended tasks:

  • Prepare the database for the new instance
  • Create default permissions

This event is only called once per installation. The application might restart without calling this event. You should only access the database or anything other that is persistent here.

@Override
public void onInstall(TwasiInstallEvent e) {
  // The plugin was installed
}

onUninstall

onUninstall is called

  • After a user selects to uninstall a plugin

Recommended tasks:

  • Clean up the database (Maybe the user wants to reinstall the plugin later, so keep important data!)
  • Remove default permissions (if they still exist)
@Override
public void onUninstall(TwasiUninstallEvent e) {
  // The plugin was uninstalled
}

onMessage

onMessage is called

  • After a message was written in any chat where the plugin is installed
@Override
public void onMessage(TwasiMessageEvent message) {
  // A message was dispatched
}

onCommand

onCommand is called

  • After a command was written in any chat where the plugin is installed
@Override
public void onCommand(TwasiCommandEvent command) {
  // A command was executed
}