Plugin Base

Defines the abstract base class (ABC) for all plugins.

This module provides the PluginBase class, which serves as the foundational template for all plugins within the Bedrock Server Manager ecosystem. Plugins must inherit from this class to be recognized and loaded by the PluginManager.

By using the @app_event("event_name") decorator on their methods, plugins can subscribe to and react to specific events triggered by the core application or other parts of the server manager.

class bedrock_server_manager.plugins.plugin_base.PluginBase(plugin_name: str, api: AppAPI, logger: Logger)

Bases: ABC

The abstract base class (ABC) from which all plugins must inherit.

Plugins should subclass PluginBase and must define a class attribute named version (e.g., version = "1.0.0"). This version string is used by the PluginManager for metadata and potential compatibility checks.

Instances of concrete plugin subclasses are provided with the following attributes by the PluginManager during initialization:

name

The plugin’s name, typically derived from its Python module filename (e.g., “my_plugin” for my_plugin.py).

Type:

str

api

An instance of the API bridge, providing safe access to core application functions.

Type:

AppAPI

logger

A pre-configured logger instance, specific to this plugin. Log messages will automatically include the plugin’s name.

Type:

logging.Logger

version

The plugin’s own version string, copied from its class attribute.

Type:

str

Plugins implement their functionality by using the @app_event("event_name") decorator on their methods to listen for specific application events. These methods are called by the PluginManager when corresponding application events occur.

author: str = 'N/A'
description: str = ''
dependencies: List[str] = []
optional_dependencies: List[str] = []
name: str = 'N/A'
get_plugin_setting(key: str, default: Any = None) Any

Retrieves a setting specific to this plugin.

Parameters:
  • key (str) – The setting key.

  • default (Any) – The default value to return if the setting is not found.

Returns:

The setting value or the default.

Return type:

Any

set_plugin_setting(key: str, value: Any) Dict[str, Any]

Saves a setting specific to this plugin.

Parameters:
  • key (str) – The setting key.

  • value (Any) – The value to save.

Returns:

The result of the save operation.

Return type:

Dict[str, Any]

get_fastapi_routers() List[Any]

Called by the PluginManager after the plugin is loaded to retrieve any custom FastAPI routers (fastapi.APIRouter instances) the plugin wishes to register with the main web application.

Plugins should override this method to return a list of APIRouter objects.

Returns:

A list of fastapi.APIRouter objects. Defaults to an empty list.

Return type:

List[Any]

get_static_mounts() List[tuple[str, Path, str]]

Called by the PluginManager after the plugin is loaded to retrieve configurations for mounting static file directories for this plugin.

Each configuration should be a tuple: (mount_path, directory_path, name), suitable for FastAPI.mount(mount_path, StaticFiles(directory=directory_path), name=name).

  • mount_path (str): The URL path prefix for these static files (e.g., “/static/myplugin”).

    This should be unique among plugins.

  • directory_path (Path): A pathlib.Path object pointing to the directory

    containing the static files for this plugin.

  • name (str): A unique name for this static mount (e.g., “myplugin_static”).

Example

from pathlib import Path # Assuming static files are in a ‘static’ subdir relative to the plugin file static_dir = Path(__file__).parent / “static” return [(“/static/myplugin”, static_dir, “myplugin_static”)]

Returns:

A list of tuples, each for a static directory mount.

Defaults to an empty list.

Return type:

List[tuple[str, Path, str]]