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

pyplugins framework

Hoi Dmytro edited this page May 24, 2020 · 1 revision

The same as Java-like minimum requirements:

  • Main class have to be extended from PythonPlugin class. (You don't have to import it, because it is auto imported on startup of loader plugin).
  • Your main class must have onEnable() and onDisable() methods.

Handlers are available to easily create your Python plugin:

  • PythonCommandExecutor class (CommandAPI)

    You can inherit your own PluginNameCommandExecutor class from PythonCommandExecutor to make handlers for "executeCommand" and "onTabComplete" actions (the command must be declared in plugin.yml). Just create methods for these actions and make the commands attribute of your PluginNameCommandExecutor class with instances of PyCommand class (with command and methods names).

  • PythonListener class (EventAPI)

    Similar to CommandsAPI, but with PythonListener class as parent, the listeners attribute (for save your handlers) of class with instances of PyEventHandler (requires name of method to execute, Bukkit event object and (optional) Bukkit ptiority object).

  • Add Default Configuration Commands (uses CommandAPI)

    NOTE: Secured by permissions - can use only Operators or Console.

    The framework provides to add basic commands for plugin configuration. Just use self.add_configuration() in onEnable() method of your plugin main class and add config.yml to your plugin. Now you can use <PluginPrefix/PluginName>-config reload to reload your plugin configuration.

    You can make options from config be available to set and see (set and params sub-commands of <PluginPrefix/PluginName>-config command). It works in-game or in console. Just add attribute available_options as ['x'] for string option or [('x', int)] for integer option. (DONT FORGET ADD THIS OPTIONS TO DEFAULT CONFIG)

  • Add bStats to your plugin

    Just use self.add_bstats(pluginId) in onEnable() method of your plugin main class.


File: /plugin.yml

name: SamplePlugin
main: SamplePluginMainClass
version: 0.1-dev
commands:
    sample:
        description: send a sample message
        usage: /<command>

NOTE: If you want to customize Configuration Commands please add plugin prefix.


File: /config.yml NOTE: Required for Configuration Commands API, uses as Default config.

# Header (optional)
x: 1
z: 200

File: /plugin.py

# COMMAND API
class SampleCommands(PythonCommandExecutor):
    commands = [
        PyCommand('sample', 'sampleCommand', 'sampleOnTabComplete')
    ]

    def sampleCommand(self, sender, command, label, args):
        # Print to console
        self.plugin.logger.info('aaaa')
        # Print message to command sender
        sender.sendMessage(command.getName())

        # For `/sample test` send "TEST"
        if args and args[0] == "test":
            sender.sendMessage("TEST")
        return True

    def sampleOnTabComplete(self, sender, command, alias, args):
        return ['test']


# EVENT API
from org.bukkit.event import SampleEvent


class SampleListener(PythonListener):
    listeners = [
        PyEventHandler('onSampleEvent', SampleEvent)
    ]

    def onSampleEvent(self, event):
        # Get spawn location
        world = Bukkit.getServer().getWorlds().get(0)
        world_spawn_location = world.getSpawnLocation()

        # Print to console spawn point Y
        self.plugin.logger.info(world_spawn_location.getBlockY())


# MAIN PLUGIN CLASS
class SamplePluginMainClass(PythonPlugin):

    def onEnable(self):
        # Add commands
        self.apply_command_executor(SampleCommands)

        # Add events
        pm = self.getServer().getPluginManager()
        pm.registerEvents(SampleListener(self), self)

        
        # Add configuration
        self.add_configuration(available_options=[
            ('x', int), ('z', int)
        ])
        # Add bStats metrics
        self.add_bstats(0000)
        self.logger.info("plugin enabled!")

    def onDisable(self):
        self.logger.info("plugin disabled!")

Clone this wiki locally