Skip to content

Commit

Permalink
feat: use shared_ptr as the holder of CommandExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Mar 7, 2024
1 parent 8376e0b commit 0f26a54
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions python/src/endstone/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
class PluginCommand(_PluginCommand):
def __init__(self, command: Command, owner: _Plugin):
_PluginCommand.__init__(self, command, owner)
self._executor: CommandExecutor = owner # keep it alive
self._executor: CommandExecutor | None = None

def _get_executor(self):
return self._executor
if self._executor:
return self._executor
else:
return self.plugin

def _set_executor(self, executor):
self._executor = executor
Expand Down
2 changes: 1 addition & 1 deletion src/endstone_python/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void init_command(py::module &m)
.def_property_readonly("registered", &endstone::Command::isRegistered,
"Returns the current registered state of this command");

py_class<CommandExecutor, PyCommandExecutor>(m, "CommandExecutor")
py_class<CommandExecutor, PyCommandExecutor, std::shared_ptr<CommandExecutor>>(m, "CommandExecutor")
.def(py::init<>())
.def("on_command", &endstone::CommandExecutor::onCommand, py::arg("sender"), py::arg("command"),
py::arg("args"), "Executes the given command, returning its success.");
Expand Down
2 changes: 1 addition & 1 deletion src/endstone_python/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void init_plugin(py::module &m)
py_class<PluginLoader, PyPluginLoader>(m, "PluginLoader");
py_class<PluginCommand, Command, PyPluginCommand, std::shared_ptr<PluginCommand>>(m, "PluginCommand");

py_class<Plugin, CommandExecutor, PyPlugin>(m, "Plugin")
py_class<Plugin, CommandExecutor, PyPlugin, std::shared_ptr<Plugin>>(m, "Plugin")
.def(py::init<>())
.def("on_load", &Plugin::onLoad, "Called after a plugin is loaded but before it has been enabled.")
.def("on_enable", &Plugin::onEnable, "Called when this plugin is enabled")
Expand Down

0 comments on commit 0f26a54

Please sign in to comment.