-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Allowing custom command to handle raw command line inputs #6708
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like the SimpleCommandMap changes are more than what's needed. The diff shouldn't need to be so many lines.
$cmd = "\xC3"; // invalid UTF-8 byte
var_dump(preg_split('/\s+/u', $cmd, 2)); // false
Also, wala - PHP magic $parts = preg_split('/\s+/u', $commandLine, 2) ?: [""]; // ensure the $parts will always be an array of string
$sentCommandLabel = $parts[0];
if(!empty($sentCommandLabel) && ($target = $this->getCommand($sentCommandLabel)) !== null){
$timings = Timings::getCommandDispatchTimings($target->getLabel());
$timings->startTiming();
try{
if($target->testPermission($sender)){
$target->executeRaw($sender, $sentCommandLabel, $parts[1] ?? "");
}
}catch(InvalidCommandSyntaxException $e){
$sender->sendMessage($sender->getLanguage()->translate(KnownTranslationFactory::commands_generic_usage($target->getUsage())));
}finally{
$timings->stopTiming();
}
return true;
}
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_notFound($sentCommandLabel, "/help")->prefix(TextFormat::RED));
return false; |
|
You're overthinking it. A simple |
The orginal code
Kinda unrelated to this PR, but if only ascii whitespace |
|
This is really overcomplicating it. All that's needed is to explode by spaces into 2 parts. Trim the parts and be done with it. No one is putting a tab after the command name. |
- set explode limit to 2 - add `use function explode` - remove `use function preg_split`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly OK now
|
To be noted, if we changed to that, we probably need to switch execute to protected. If its public, This suggests that one or other could be called whereas the plugin can implement either. |
Well, eh... Ain't those devs decided to code it that way? Maybe adding some docs?
I don't think |
|
I share your point of you in the handling of raw args. But, we can't rely on only docs where a creepy dev can create a plugin calling execute by its way skipping all rawArgs calls |
As I mentioned before, that creepy dev already coded it like that. If you want to stop such practices, you can only stop them from being released into productions or advise them using
Previously in issue #6704, And I still don't think it's a major problem, and at its worst only causing some stupid behaviors for those practicing it. |
|
Well, the other option that was originally proposed is having the command expose a function that parses the args to an array. But I don't really like that approach since it still falls down to Doing it with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks OK in principle, how I envisioned it. However I don't have a good answer to the concern raised by @ShockedPlot7560 around how execute() should be treated following this change. It probably should become protected but that will break BC, so I'm not sure how it should be handled.
|
With our internal discussions against this PR, we don't really have better design than this for the moment. As it's planned to rework the command design, maybe, it should be OK to merge as it is. But I'm not a really big fan of this solution by my concern raised in the past and not a big fan to merge as it is too. |
|
Perhaps we should save it for PM6? |
|
Worth noting this is a bit of a pain for stuff like FormattedCommandAlias, since we may want to be able to pass pre-parsed arguments to commands as well. Not clear how all these pieces should fit together. See also #4193 and PocketMine-MP/src/command/FormattedCommandAlias.php Lines 88 to 92 in 335fcc8
|
|
Superseded by #6837. Since this would require BC break anyway (otherwise the whole Thanks for making an effort to contribute, though! |
Current PMMP handles the parsing of command lines in a hardcoded way without any non-hacky method for commands to handle the raw inputs. This PR tweaks the API a little so it will allow custom commands to do such things without harming the performance.
Related issues & PRs
Changes
API changes
Command->executeRaw()to Command class. This allowing custom command to handle the raw command inputs by override the parent method.Backwards compatibility
Yes