1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

array_chunk not working??

Discussion in 'Development' started by jasonwynn10, Dec 18, 2016.

  1. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    I am currently working on an updated version of the MyPlot Plugin and when the help command is run, the save commands are output to the player no matter what page i use. Can someone help me understand why? This still works on the old PocketMine
    PHP:
    public function execute(CommandSender $sender, array $args) {
            if (
    count($args) === 0) {
                
    $pageNumber 1;
            } elseif (
    is_numeric($args[0])) {
                
    $pageNumber = (int)$args[0];
                if (
    $pageNumber <= 0) {
                    
    $pageNumber 1;
                }
            } else {
                return 
    false;
            }
            
    $pageHeight 7;
            
    $commands = [];
            foreach (
    $this->cmd->getCommands() as $command) {
                if (
    $command->canUse($sender)) {
                    
    $commands[$command->getName()] = $command;
                }
            }
            
    ksort($commandsSORT_NATURAL SORT_FLAG_CASE);
            
    $commands array_chunk($commands$pageHeight);
            
    /** @var SubCommand[][] $commands */
            
    $pageNumber = (int) min(count($commands), $pageNumber);
            
    $sender->sendMessage($this->translateString("help.header", [$pageNumbercount($commands)]));
            if(
    $sender instanceof Player) {
                foreach (
    $commands[$pageNumber 1] as $command) {
                    
    $sender->sendMessage(TextFormat::DARK_GREEN $command->getName() . ": " TextFormat::WHITE $command->getDescription());
                }
            }else{
                foreach (
    $this->cmd->getCommands() as $subCommand) {
                    
    $sender->sendMessage(TextFormat::DARK_GREEN $subCommand->getName() . ": " TextFormat::WHITE $subCommand->getDescription());
                }
            }
            return 
    true;
        }
     
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I simplified your problem:
    https://3v4l.org/VKnBs
    It is working perfectly (at least for what I expect it to do). Might it be some other problems you have in command parsing?

    Also note that using array_chunk() is not the most efficient way to do it. Might have negligible difference in terms of performance compared to other parts of the code, but using simple arithmetic directlly might be more effective:

    PHP:
    function showPage(int $pageNumberint $pageHeight, array $objects, callable $send){
        
    $pageNumber--; // now starts counting from 0
        
    $cnt count($objects);
        
    printf("Showing page %d of %d"$pageNumber 1ceil(count($objects) / $pageNumber)); // I know this line is not what you asked for, just putting it here for future readers
        
    for($i 0$i $pageHeight and $pageNumber $pageHeight $i $cnt$i++){
            
    $send($objects[$pageNumber $pageHeight $i]);
        }
    }
     
    jasonwynn10 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.