示例#1
0
    def execute_command(self, command: str, source: CommandSource):
        first_literal_element = utils.get_element(command)
        plugin_root_nodes = self.root_nodes[first_literal_element]

        if isinstance(source, InfoCommandSource):
            if len(plugin_root_nodes) > 0 and source.is_console:
                # If this is a command, don't send it towards the server if it's from console input
                source.get_info().cancel_send_to_server()

        for plugin_root_node in plugin_root_nodes:
            plugin = plugin_root_node.plugin
            node = plugin_root_node.node
            with self.mcdr_server.plugin_manager.with_plugin_context(plugin):
                try:
                    node.execute(source, command)
                except CommandError as error:
                    if not error.is_handled():
                        translation_key = 'command_exception.{}'.format(
                            string_util.hump_to_underline(
                                type(error).__name__))
                        try:
                            error.set_message(
                                self.__translate_command_error_header(
                                    translation_key, error))
                        except KeyError:
                            self.logger.debug(
                                'Fail to translated command error with key {}'.
                                format(translation_key),
                                option=DebugOption.COMMAND)
                        source.reply(error.to_mc_color_text())
                except:
                    self.logger.exception(
                        'Error when executing command "{}" with command source "{}" on {} registered by {}'
                        .format(command, source, node, plugin))
示例#2
0
    def execute_command(self, source: CommandSource, command: str):
        first_literal_element = utils.get_element(command)
        root_nodes = self.root_nodes.get(first_literal_element, [])
        if len(root_nodes
               ) > 0 and source.source_type == CommandSourceType.CONSOLE:
            # If this is a command, don't send it towards the server if it's from console input
            source.get_info().cancel_send_to_server()

        command_errors = []
        general_exc_info = []
        for node in root_nodes:
            try:
                node.execute(source, command)
            except CommandError as e:
                command_errors.append(e)
            except:
                general_exc_info.append(sys.exc_info())
        for exc_info in general_exc_info:
            self.logger.error(
                'Error when executing command "{}" with command source "{}"'.
                format(command, source),
                exc_info=exc_info)
        for error in command_errors:
            if not error.is_handled():
                translation_key = 'command_exception.{}'.format(
                    string_util.hump_to_underline(type(error).__name__))
                try:
                    error.set_translated_message(
                        translation_key, lambda key, args: self.mcdr_server.tr(
                            key, *args, allow_failure=False))
                except KeyError:
                    self.logger.debug(
                        'Fail to translated command error with key {}'.format(
                            translation_key),
                        option=DebugOption.COMMAND)
                source.reply(error.to_mc_color_text())
示例#3
0
	def _traverse(self, command: str, source: CommandSource, purpose: TraversePurpose) -> None or List[CommandSuggestion]:
		first_literal_element = utils.get_element(command)
		plugin_root_nodes = self.root_nodes.get(first_literal_element, [])
		suggestions = CommandSuggestions()

		if purpose == TraversePurpose.EXECUTE and isinstance(source, InfoCommandSource):
			if len(plugin_root_nodes) > 0 and source.is_console:
				# If this is a command, don't send it towards the server if it's from console input
				source.get_info().cancel_send_to_server()

		if purpose == TraversePurpose.SUGGEST and len(plugin_root_nodes) == 0:
			return CommandSuggestions([CommandSuggestion('', literal) for literal in self.root_nodes.keys()])

		for plugin_root_node in plugin_root_nodes:
			plugin = plugin_root_node.plugin
			node = plugin_root_node.node
			try:
				with self.mcdr_server.plugin_manager.with_plugin_context(plugin):
					if purpose == TraversePurpose.EXECUTE:
						node.execute(source, command)
					elif purpose == TraversePurpose.SUGGEST:
						suggestions.extend(node.generate_suggestions(source, command))

			except CommandError as error:
				if not error.is_handled():
					translation_key = 'command_exception.{}'.format(string_util.hump_to_underline(type(error).__name__))
					try:
						error.set_message(self.__translate_command_error_header(translation_key, error))
					except KeyError:
						self.logger.debug('Fail to translated command error with key {}'.format(translation_key), option=DebugOption.COMMAND)
					source.reply(error.to_mc_color_text())
			except:
				self.logger.exception('Error when executing command "{}" with command source "{}" on {} registered by {}'.format(command, source, node, plugin))

		if purpose == TraversePurpose.SUGGEST:
			return suggestions