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

How do i create an array of ALL players and loop though them all

Discussion in 'Development' started by Atomization, May 31, 2018.

  1. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    do the same again but another entry for cooldown ends
     
  2. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    You've quoted both. Ahem, anyway...
    I won't go as deep as providing you with the full code to copy&paste, so here's a couple of examples for you to understand what I (we all?) mean and, hopefully, use that knowledge right:
    PHP:
    $config = new Config($dataFolder "players.json");

    //So here's your rank duration and command cooldown time. Both of these values are in seconds.
    //You may set these values in the code, in a config or even in your rank command...
    $rankDuration 86400//24 hours
    $commandCooldown 43200//12 hours

    //As far as I understand, In your command you should check for the cooldown time and if it's been surpassed, the player can use the command again.
    //The expiration time of player's rank can add up (if cooldown time is less than rank's duration).
    public function execute(CommandSender $senderstring $commandLabel, array $args) {
        
    $currentTime time(); //Here's a current timestamp. Saving it to not use time() too often.

        
    $playerConfig $config->get($sender->getName(), [
            
    "expires" => $currentTime//Fill the default expiration date with a current timestamp.
            
    "cooldown" => 0
        
    ]);

        if (
    $playerConfig["cooldown"] > $currentTime) { //Here's your check...
            
    $sender->sendMessage("HEY! Don't use this too often!");
            return;
        }

        
    //Now we should check if player's rank has expired, just in case it has. We don't want to add 24 hours to a timestamp of two days ago, if player has waited two days before getting his rank again.
        
    if ($playerConfig["expires"] < $currentTime) {
            
    $playerConfig["expires"] = $currentTime//If it did expire, fill it with the default value - current timestamp.
        
    }

        
    $playerConfig["expires"] += $rankDuration;
        
    $playerConfig["cooldown"] = $currentTime $commandCooldown;

        
    $config->set($sender->getName(), $playerConfig);
        
    $config->save();
    }

    //When a player joins you should just check if their rank has expired.
    public function onPlayerJoin(PlayerJoinEvent $event) {
        if (
    $config->exists($event->getPlayer()->getName())) {
            if (
    $config->get($event->getPlayer()->getName())["expires"] < $time) {
                
    //Remove the rank.
                
    $config->remove($event->getPlayer()->getName());
                
    $config->save();
            } else {
                
    //If the rank's yet to expire, set your timer here or add the player to a list for your timer.
                //...just in case if it expires when the player is online.
            
    }
        }
    }
     
  3. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Yes, I want to get good performance out of my server, but I don't understand how to use time()
     
  4. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    PHP:
    time(); //Somewhat like 1527855650
    $timeThen time();
    sleep(3); //Wait 3 secs
    $timeNow time();
    $timeNow $timeThen//true
    $timeThen $timeNow//true
    $timeNow $timeThen//3
     
  5. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    That's somewhat helpful, but doesn't sleep() freeze the whole server?
     
  6. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    It does, I wrote that as an example, it would work fine in a standalone script.
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    It is just an example. You should call time() from two separate scopes, e.g. during two events.
     
  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.