Esempio n. 1
0
    def trigger_listener(self, listener: EventListener, args: Tuple[Any, ...]):
        """
		Event listener triggering implementation
		The server_interface parameter will be automatically added as the 1st parameter
		"""
        try:
            with self.with_plugin_context(listener.plugin):
                listener.execute(listener.plugin.server_interface, *args)
        except:
            self.logger.exception(
                'Error invoking listener {}'.format(listener))
Esempio n. 2
0
    def trigger_listener(self, listener: EventListener, args: Tuple[Any, ...]):
        """
		The terminated entry for triggering a listener
		The server_interface parameter will be automatically added as the 1st parameter
		"""
        # self.thread_pool.add_task(lambda: listener.execute(*args), listener.plugin)
        with self.with_plugin_context(listener.plugin):
            try:
                listener.execute(self.mcdr_server.server_interface, *args)
            except:
                self.logger.exception(
                    'Error invoking listener {}'.format(listener))
Esempio n. 3
0
 def __register_default_listeners(self):
     for event in MCDRPluginEvents.get_event_list():
         if isinstance(event.default_method_name, str):
             func = getattr(self.module_instance, event.default_method_name,
                            None)
             if callable(func):
                 self.register_event_listener(
                     event,
                     EventListener(self, func, DEFAULT_LISTENER_PRIORITY))
Esempio n. 4
0
    def register_event_listener(self,
                                event: Union[PluginEvent, str],
                                callback: Callable,
                                priority: Optional[int] = None) -> None:
        """
		Register an event listener for the current plugin
		:param event: The id of the event, or a PluginEvent instance. It indicates the target event for the plugin to listen
		:param callback: The callback listener method for the event
		:param priority: The priority of the listener. It will be set to the default value 1000 if it's not specified
		"""
        if priority is None:
            priority = DEFAULT_LISTENER_PRIORITY
        if isinstance(event, str):
            event = LiteralEvent(event_id=event)
        self.__plugin.register_event_listener(
            event, EventListener(self.__plugin, callback, priority))
    def register_event_listener(
            self,
            event: Union[PluginEvent, str],
            callback: Callable,
            priority: int = DEFAULT_LISTENER_PRIORITY) -> None:
        """
		Register an event listener for the current plugin
		:param event: The id of the event, or a PluginEvent instance. It indicates the target event for the plugin to listen
		:param callback: The callback listener method for the event
		:param priority: The priority of the listener. It will be set to the default value 1000 if it's not specified
		:raise: IllegalCallError if it's not invoked in the task executor thread
		"""
        plugin = self.__get_current_plugin()
        if isinstance(event, str):
            event = LiteralEvent(event_id=event)
        plugin.register_event_listener(
            event, EventListener(plugin, callback, priority))
Esempio n. 6
0
    def add_event_listener(self,
                           event: Union[PluginEvent, str],
                           callback: Callable,
                           priority: int = DEFAULT_LISTENER_PRIORITY):
        """
		Add an event listener for the current plugin

		:param event: The id of the event to listen, or the PluginEvent instance if it's a built-in
		MCDR event
		:param callback: The callback listener method for the event
		:param priority: The priority of the listener
		:raise: IllegalCallError if it's not called in a MCDR provided thread
		"""
        plugin = self.__get_current_plugin()
        if isinstance(event, str):
            event = LiteralEvent(event_id=event)
        plugin.add_event_listener(event,
                                  EventListener(plugin, callback, priority))
 def __register_event_listeners(self):
     self.register_event_listener(MCDRPluginEvents.GENERAL_INFO,
                                  EventListener(self, self.on_info, 10))