luanti-network-api-client/miney/callback.py
2024-12-07 01:17:53 +03:00

32 lines
894 B
Python

import string
from random import choices
import miney
class Callback:
"""Register callbacks inside minetest, to receive events from Lua"""
def __init__(self, mt: miney.Minetest):
self.mt = mt
def activate(self, event: str, callback: callable, parameters: dict = None):
"""
Register a callback for an event.
:param event: Event to register for
:param callback: The function to be called on events
:param parameters: Some events need parameters
:return: None
"""
# Match answer to request
result_id = ''.join(choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k=6))
self.mt.send(
{
'activate_event':
{
'event': event,
},
'id': result_id
}
)