Skip to content
This repository was archived by the owner on May 15, 2024. It is now read-only.

Plugins

Guillaume R edited this page Jun 21, 2018 · 7 revisions

TuubesCore provides a foundation for game servers, it does not implement the game components. Those are provided and extended by plugins.

Creating a Scala plugin

We might support more languages in the future, but for now only Scala is officialy supported (theoretically it's possible to write plugins in Java or another compatible JVM language).

Creating a Scala plugin is extremely simple: all you have to do is to create a class that extends our Plugin trait.

import org.tuubes.core.engine.World
import org.tuubes.core.plugins.{Plugin, PluginDescription}

/**
 * An example of plugin class.
 * Note: the override modifiers aren't mandatory here, but I've let them
 * to show that the methods and values come from the parent trait.
 */
class PluginExample extends Plugin {
  override val description = PluginExample

  override def onLoad() = {
    logger.info("Plugin loaded!")
  }

  override def onUnload() = {
    logger.info("Plugin unloaded!")
  }

  override def onEnable(world: World) = {
    logger.info(s"Plugin enabled in world $world")
  }

  override def onDisable(world: World) = {
    logger.info(s"Plugin disabled in world $world")
  }
}

/**
 * The companion object extends PluginDescription.
 * The PluginDescription is needed to load the plugin.
 */
object PluginExample extends PluginDescription {
  override val name = "ElectronWill's Example"
  override val version = "1.0.0"
  override val optionalDeps = Seq("PluginA", "PluginB") // 2 optional dependencies
  override val requiredDeps = Nil // No required dependencies
}

Notice how we've used the companion object to provide the description of the plugin. You have to use the companion object, otherwise Tuubes cannot initialize the description before the plugin, and the plugin won't load.

A plugin's life

  1. Tuubes inspects the plugin file and finds its description and main class.
  2. Tuubes loads the plugin and its dependencies (if any), that is, it creates an instance of the plugin class. The plugin's state is INIT.
  3. Tuubes calls onLoad(). The plugin's state is now LOADED.
  4. Then, it enables the plugin in some worlds (possibly none), calling onEnable(World).
  5. The plugin happily lives in the server, reacting to events and doing stuff.
  6. Sometimes, the plugin is enabled in more worlds and disabled in others. onEnable(World) and onDisable(World) are called as needed.
  7. All good things come to an end. Tuubes disables the plugin, calling onDisable(World) for each world where the plugin is enabled, and then onUnload(). The plugin's state is now TERMINATED, forever. Goodbye, little plugin!

TL;DR:

  1. Plugin file inspection
  2. Plugin instance created, state = INIT
  3. onLoad(), state = LOADED
  4. onEnable(World) with zero or more worlds
  5. doing stuff
  6. onDisable(World) for each world
  7. onUnload(), state = TERMINATED definitively

Clone this wiki locally