Skip to content

Commit

Permalink
feat: /reload commands now works
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Aug 7, 2024
1 parent 913dfd3 commit eac008b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
5 changes: 4 additions & 1 deletion include/endstone/detail/command/command_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::shared_ptr<Command>> known_commands_;
};

Expand Down
5 changes: 3 additions & 2 deletions python/src/endstone/_internal/plugin_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand Down Expand Up @@ -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}")
Expand Down
29 changes: 29 additions & 0 deletions src/endstone_core/command/command_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace endstone::detail {

EndstoneCommandMap::EndstoneCommandMap(EndstoneServer &server) : server_(server)
{
saveCommandRegistryState();
setMinecraftCommands();
setDefaultCommands();
}
Expand All @@ -48,6 +49,7 @@ void EndstoneCommandMap::clearCommands()
command->unregisterFrom(*this);
}
known_commands_.clear();
restoreCommandRegistryState();
setMinecraftCommands();
setDefaultCommands();
}
Expand Down Expand Up @@ -268,4 +270,31 @@ bool EndstoneCommandMap::registerCommand(std::shared_ptr<Command> command)
return true;
}

namespace {
struct {
std::vector<CommandRegistry::Enum> enums;
std::map<std::string, std::uint32_t> enum_lookup;
std::map<std::string, CommandRegistry::Signature> signatures;
std::map<std::string, std::string> aliases;
} gCommandRegistryState;
} // namespace

void EndstoneCommandMap::saveCommandRegistryState() const
{
auto &registry = 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 &registry = server_.getMinecraftCommands().getRegistry();
registry.enums = gCommandRegistryState.enums;
registry.enum_lookup = gCommandRegistryState.enum_lookup;
registry.signatures = gCommandRegistryState.signatures;
registry.aliases = gCommandRegistryState.aliases;
}

} // namespace endstone::detail

0 comments on commit eac008b

Please sign in to comment.