From 418dab390e877e917e6719b3265af38d2cb049b5 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Thu, 20 Oct 2022 08:03:47 +0200 Subject: add method to load modules externally --- BerryClient/core.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/BerryClient/core.py b/BerryClient/core.py index 1b4e5b5..28e2a3b 100644 --- a/BerryClient/core.py +++ b/BerryClient/core.py @@ -5,6 +5,7 @@ from importlib import import_module, reload from mcpi import minecraft from BerryClient.events import process_events from BerryClient.logging import MinecraftChatHandler +from BerryClient.commands import COMMANDS log = logging.getLogger() @@ -22,7 +23,7 @@ class BerryCore: self.running = False self.__setupLogging() - self.__loadAddons() + self.__loadDefaultAddons() def __setupLogging(self): root_logger = logging.getLogger() @@ -42,12 +43,21 @@ class BerryCore: root_logger.addHandler(stdout_handler) - def __loadAddons(self): + def __loadDefaultAddons(self): for addon in self.ADDONS: + self.__loadAddon(addon) + + + def __loadAddon(self, addon): + try: self.ADDON_MODULES[addon] = import_module(addon) log.info(f"Loaded {addon}") + except ImportError as e: + log.error(f"Failed to load {addon}") + log.exception(e) def __reloadAddons(self): + COMMANDS.clear() for addon in self.ADDONS: try: self.ADDON_MODULES[addon] = reload(self.ADDON_MODULES[addon]) @@ -66,6 +76,9 @@ class BerryCore: def reload(self): self.__reloadAddons() + def load(self, addon): + self.__loadAddon(addon) + def stop(self): self.running = False -- cgit v1.2.3