diff --git a/include/endstone/detail/command/command_map.h b/include/endstone/detail/command/command_map.h index fd9a94fbc..f674cb7d0 100644 --- a/include/endstone/detail/command/command_map.h +++ b/include/endstone/detail/command/command_map.h @@ -36,8 +36,11 @@ class EndstoneCommandMap : public CommandMap { void setMinecraftCommands(); void setPluginCommands(); + void saveCommandRegistryState() const; + void restoreCommandRegistryState() const; + EndstoneServer &server_; - std::mutex mutex_; + std::recursive_mutex mutex_; std::unordered_map> known_commands_; }; diff --git a/python/src/endstone/_internal/plugin_loader.py b/python/src/endstone/_internal/plugin_loader.py index a7c71e1a6..15f16a119 100644 --- a/python/src/endstone/_internal/plugin_loader.py +++ b/python/src/endstone/_internal/plugin_loader.py @@ -60,6 +60,9 @@ def _build_permissions(permissions: dict) -> list[Permission]: def load_plugins(self, directory) -> List[Plugin]: importlib.invalidate_caches() + for module in list(sys.modules.keys()): + if module.startswith("endstone_"): + del sys.modules[module] env = os.environ.copy() env.pop("LD_PRELOAD", "") @@ -114,8 +117,6 @@ def load_plugins(self, directory) -> List[Plugin]: # get distribution metadata try: plugin_metadata = metadata(ep.dist.name).json - module = importlib.import_module(ep.module) - importlib.reload(module) cls = ep.load() except Exception as e: self.server.logger.error(f"Error occurred when trying to load plugin from entry point '{ep.name}': {e}") diff --git a/src/endstone_core/command/command_map.cpp b/src/endstone_core/command/command_map.cpp index 3964f3e10..ef1d539cc 100644 --- a/src/endstone_core/command/command_map.cpp +++ b/src/endstone_core/command/command_map.cpp @@ -37,6 +37,7 @@ namespace endstone::detail { EndstoneCommandMap::EndstoneCommandMap(EndstoneServer &server) : server_(server) { + saveCommandRegistryState(); setMinecraftCommands(); setDefaultCommands(); } @@ -48,6 +49,7 @@ void EndstoneCommandMap::clearCommands() command->unregisterFrom(*this); } known_commands_.clear(); + restoreCommandRegistryState(); setMinecraftCommands(); setDefaultCommands(); } @@ -268,4 +270,31 @@ bool EndstoneCommandMap::registerCommand(std::shared_ptr command) return true; } +namespace { +struct { + std::vector enums; + std::map enum_lookup; + std::map signatures; + std::map aliases; +} gCommandRegistryState; +} // namespace + +void EndstoneCommandMap::saveCommandRegistryState() const +{ + auto ®istry = server_.getMinecraftCommands().getRegistry(); + gCommandRegistryState.enums = registry.enums; + gCommandRegistryState.enum_lookup = registry.enum_lookup; + gCommandRegistryState.signatures = registry.signatures; + gCommandRegistryState.aliases = registry.aliases; +} + +void EndstoneCommandMap::restoreCommandRegistryState() const +{ + auto ®istry = server_.getMinecraftCommands().getRegistry(); + registry.enums = gCommandRegistryState.enums; + registry.enum_lookup = gCommandRegistryState.enum_lookup; + registry.signatures = gCommandRegistryState.signatures; + registry.aliases = gCommandRegistryState.aliases; +} + } // namespace endstone::detail