1+ """Example plugin demonstrating commands, configuration, and lifecycle methods."""
2+
13from endstone import Player
24from endstone .command import Command , CommandSender , ConsoleCommandSender
35from endstone .plugin import Plugin
68
79
810class ExamplePlugin (Plugin ):
11+ # The prefix shown in log messages, e.g. [ExamplePlugin] Hello!
912 prefix = "ExamplePlugin"
13+
14+ # Must match the major.minor version of the Endstone API you are targeting.
1015 api_version = "0.11"
1116
17+ # Commands are declared as a dict. Each key is the command name, and the value
18+ # configures how it appears in-game. The "permissions" list controls who can use it.
1219 commands = {
1320 "hello" : {
1421 "description" : "Send a greeting" ,
@@ -17,6 +24,9 @@ class ExamplePlugin(Plugin):
1724 },
1825 }
1926
27+ # Permissions are declared separately from commands. The "default" field controls
28+ # who gets the permission automatically: True = everyone, "op" = operators only,
29+ # False = no one (must be explicitly granted).
2030 permissions = {
2131 "example.command.hello" : {
2232 "description" : "Allow users to use the /hello command." ,
@@ -25,19 +35,42 @@ class ExamplePlugin(Plugin):
2535 }
2636
2737 def on_enable (self ) -> None :
38+ """Called when the plugin is enabled. Use this for setup."""
39+
40+ # Copies config.toml to the plugin's data folder on first run.
41+ # On subsequent runs it does nothing, preserving the user's edits.
2842 self .save_default_config ()
43+
44+ # Register event listeners. Endstone scans the object for @event_handler
45+ # methods and hooks them into the event system.
2946 self .register_events (ExampleListener (self ))
47+
3048 self .logger .info ("ExamplePlugin enabled!" )
3149
3250 def on_disable (self ) -> None :
51+ """Called when the plugin is disabled. Use this for cleanup."""
3352 self .logger .info ("ExamplePlugin disabled!" )
3453
3554 def on_command (self , sender : CommandSender , command : Command , args : list [str ]) -> bool :
55+ """Called when a player or the console runs one of this plugin's commands.
56+
57+ Args:
58+ sender: Who ran the command (Player, ConsoleCommandSender, etc.).
59+ command: The command that was executed.
60+ args: The arguments passed after the command name.
61+
62+ Returns:
63+ True if the command was handled successfully.
64+ """
3665 match command .name :
3766 case "hello" :
38- greeting = self .config ["greeting" ] # from config.toml
67+ # Read the greeting from config.toml (editable by server admins)
68+ greeting = self .config ["greeting" ]
3969
40- # Use isinstance to check the sender type
70+ # Use isinstance to tailor behavior to the sender type.
71+ # This is a common pattern since players and the console have
72+ # different capabilities (e.g. players can receive chat messages,
73+ # but the console can only write to the log).
4174 if isinstance (sender , Player ):
4275 sender .send_message (f"{ greeting } , { sender .name } !" )
4376 elif isinstance (sender , ConsoleCommandSender ):
0 commit comments