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

help prevent cool down resetting after server restart

Discussion in 'Development' started by Levi, Oct 14, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    hey, I'm making a kit plugin but the cooldown resets when the server restarts. and i dont want that to happen my code:
    oh and its the same thing as when they leave, the cooldown resets but i fixed it onJoin

    PHP:
    public $config;
    public 
    $cooldown = [];

    public function 
    onEnable()
    {
        @
    mkdir($this->getServer()->getDataPath() . "/plugins/Kit");
        
    $this->config = new Config($this->getServer()->getDataPath() . "/plugins/Kit/cooldowns.yml",
            
    Config::YAML, [
                
    "starter" => 86400,
                
    "god" => 86400
            
    ]);
    }
    public function 
    onJoin(PlayerJoinEvent $e)
        {
            
    $player $e->getPlayer();
            if (isset(
    $this->cooldown[$player->getName()])) {
                
    $this->cooldown[$player->getName()] = time();
            }
        }
        public function 
    onQuit(PlayerQuitEvent $e){
        }

        public function 
    onCommand(CommandSender $senderCommand $commandstring $label, array $args): bool
        
    {
            switch (
    $command->getName()) {
                case 
    "kit":
                    if (!isset(
    $args[0])) {
                        
    $sender->sendMessage("/kit <name>");
                        return 
    true;
                    }
                    
    $kit $args[0];
                    if (
    $kit == "starter") {
                        if ((!isset(
    $this->cooldown[$sender->getName()])) || (($this->cooldown[$sender->getName()] +
                                    
    $this->config->get("starter")) - time() <= 0)
                        ) {
                            
    $helmet Item::get(306);
                            
    $chestplate Item::get(311);
     
          
                            foreach([
    $helmet$chestplate] as $items){
                                
    $sender->getInventory()->addItem($items);
                            }
                            
    $sender->sendMessage("You got your kit");
                            
    $this->cooldown[$sender->getName()] = time();
                        } else {
                            
    $sender->sendMessage("kit on cooldown!");
                        }
     
  2. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    Because once the server shuts down all the data in $this->cooldown, to fix this just do

    PHP:
    public function onDisable(){
        foreach(
    $this->cooldown as $player => $time){
            
    $this->config->set($player$time);
            
    $this->config->save();
        }
    }
    Then on in you onEnable add

    PHP:
    foreach($this->config->getAll() as $player => $time){
        
    $this->cooldown[$player] = $time;
    }
     
    OnTheVerge likes this.
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    i don't have config,
     
  4. Mystic30

    Mystic30 Witch

    Messages:
    52
    GitHub:
    Mystic30
    then whats this
    PHP:
    $this->config = new Config($this->getServer()->getDataPath() . "/plugins/Kit/cooldowns.yml",
     
  5. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    If you don't have a config, then why are you creating a new config when the plugin enables

     
  6. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    those are cooldown for each kits, and you mentioned player which is not in the cooldown.yml
     
  7. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    Doesn't need to, it creates on in the config when it disables
     
    Levi likes this.
  8. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    oh thanks
     
  9. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    PHP:
    public function onDisable(){
        foreach(
    $this->cooldown as $player => $time){
            
    $this->config->set($player$time);
            
    $this->config->save();
        }
    }
    Then on in you onEnable add

    PHP:
    foreach($this->config->getAll() as $player => $time){
        
    $this->cooldown[$player] = $time;
    }
    [/QUOTE]
    thank you!! it worked :D
     
  10. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    actually, it made all kits on cool down D:
     
    Last edited: Oct 14, 2017
  11. Scherzhertz

    Scherzhertz Spider

    Messages:
    6
    GitHub:
    Scherzhertz
    Can i get the plugin after you make it?
     
  12. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    i dont really mind but it wont be customizable like other plugins cuz im making it for my server
     
  13. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    ^
     
  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.