Callback
Manages event and chat command registrations with the server.
The Callback object is accessed through the callbacks property of the Luanti object.
- class miney.callback.Callback(luanti: Luanti)[source]
Manages event and chat command registrations with the server.
This class provides a way to handle server-side events using asynchronous callbacks. Use decorators like
on()andcommand()to register functions that are automatically called when events occur. This is handled in a background thread.You can access this class via the
callbacksproperty.- command(name: str, params: str = '', description: str = '', privileges: Dict[str, bool] | None = None) Callable[source]
Decorator to register a chat command.
This is the recommended way to register chat commands.
from miney.events import ChatCommandEvent @lt.callbacks.command("greet", "<name>", description="Greets a player.") def greet_command(event: ChatCommandEvent): lt.chat.send_to_all(f"Hello, {event.param}!")
- Parameters:
name – The name of the command.
params – The command’s parameter string (for /help).
description – The command’s description (for /help).
privileges – Privileges required to execute the command.
- Returns:
The decorator function.
- on(event: str, parameters: Dict[str, Any] | None = None) Callable[source]
Decorator to register a callback for an event.
This is the recommended way to register event handlers. The decorated function will be called with a specific subclass of
Eventwhen the corresponding event occurs on the server.from miney.events import PlayerJoinsEvent @lt.callbacks.on("player_joins") def on_player_join(event: PlayerJoinsEvent): print(f"Player {event.player_name} joined the game.")
- Parameters:
event – The name of the event to subscribe to.
parameters – Optional filters for the event subscription.
- Returns:
The decorator function.
- register(event: str, callback: Callable[[Event], None], parameters: Dict[str, Any] | None = None) str[source]
Register a callback for an event procedurally.
This is an alternative to using the
on()decorator. It returns a unique token for the registration.from miney.events import PlayerLeavesEvent def on_player_leave(event: PlayerLeavesEvent): print(f"Player {event.player_name} left.") lt.callbacks.register("player_leaves", on_player_leave)
- Parameters:
event – The name of the event to subscribe to.
callback – The function to execute when the event occurs.
parameters – Optional filters for the event subscription.
- Returns:
A unique token for this registration.
- register_command(name: str, callback: Callable[[Event], None], params: str = '', description: str = '', privileges: Dict[str, bool] | None = None) str[source]
Register a chat command procedurally.
This is an alternative to using the
command()decorator.from miney.events import ChatCommandEvent def teleport_command(event: ChatCommandEvent): # Placeholder for teleport logic print(f"Teleporting {event.issuer} to {event.param}") lt.callbacks.register_command( "teleport", teleport_command, params="<x,y,z>", description="Teleport to coordinates." )
- Parameters:
name – The name of the command.
callback – The function to call when the command is executed.
params – The command’s parameter string (for /help).
description – The command’s description (for /help).
privileges – Privileges required to execute the command.
- Returns:
The command name.
- shutdown() None[source]
Best-effort cleanup: unregister all known subscriptions and commands, then stop dispatcher.
- unregister(event: str, token_or_callback: Any) None[source]
Deactivate an event subscription by token or callback function.
It is generally easier to unregister by providing the original callback function reference.
# Assuming 'on_player_leave' was registered before lt.callbacks.unregister("player_leaves", on_player_leave)
- Parameters:
event – The name of the event (e.g., “player_leaves”).
token_or_callback – The handler function or the token from register().