Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ClosureCommand #6063

Open
wants to merge 2 commits into
base: minor-next
Choose a base branch
from
Open

Added ClosureCommand #6063

wants to merge 2 commits into from

Conversation

dktapps
Copy link
Member

@dktapps dktapps commented Sep 21, 2023

this is intended to replace PluginCommand and CommandExecutor, both of which are overengineered and unfit for purpose. Allowing a closure allows much greater flexibility.

We can't use this within the core yet, as plugins will expect PluginBase->getCommand() to return PluginCommand (with its associated setExecutor() and similar APIs). However, I think this is useful enough to add by itself.

Changes

API changes

  • Added a new ClosureCommand class. The callback accepted is similar to (and compatible with) the signatures required by CommandExecutor in the past, allowing easy migration.

Behavioural changes

None. This class is not yet used in the core.

While I'd like to migrate directly to this, various plugins expect that PluginCommand will be returned by PluginBase->getCommand().

In addition, there are questions about identifying plugin commands if this is used. This is no different a problem than we already see with regular Command users, but perhaps worth mentioning anyway.

Backwards compatibility

No BC issues.

Follow-up

Tests

Not directly tested, but this is trivially PHPStan-verifiable.

this is intended to replace PluginCommand and CommandExecutor, both of which are overengineered and unfit for purpose.
Allowing a closure allows much greater flexibility.

We can't use this within the core yet, as plugins will expect PluginBase->getCommand() to return PluginCommand (with its associated setExecutor() and similar APIs).
However, I think this is useful enough to add by itself.
@dktapps dktapps added Category: API Related to the plugin API Type: Enhancement Contributes features or other improvements to PocketMine-MP labels Sep 21, 2023
ShockedPlot7560
ShockedPlot7560 previously approved these changes Oct 3, 2023
src/command/ClosureCommand.php Outdated Show resolved Hide resolved
@dktapps
Copy link
Member Author

dktapps commented Nov 9, 2023

I'm not sure whether we should allow replacing the closure handler a la CommandExecutor->setExecutor(). setExecutor() is used by some plugins to provide custom handlers for commands declared via pocketmine.yml. The command can just be re-registered with the new handler, so I'm not sure whether this is needed or not.

@SOF3
Copy link
Member

SOF3 commented Nov 10, 2023

Let's not overcomplicate the API. Mutable executor may as well be implemented within the closure as a delegation where it matters.

$this->cmd = new ClosureCommand(xxx);
$this->cmd->setClosure(xxx);
register($this->cmd);

is barely different from

$this->closure = xxx;
register(new ClosureCommand(fn(...$args) => ($this->closure)(...$args)));

@dktapps
Copy link
Member Author

dktapps commented Nov 10, 2023

Delegate won't be available if the command was registered by the server from plugin.yml. That's basically the whole use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: API Related to the plugin API Type: Enhancement Contributes features or other improvements to PocketMine-MP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants