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

Timer

Discussion in 'Development' started by Trxgically, Apr 19, 2019.

  1. Trxgically

    Trxgically Spider Jockey

    Messages:
    26
    GitHub:
    trxgically
    I was wondering how to implement a timer into my plugin. So basically like after they do /command claim to claim an item they have to wait another x amount hours to claim that item again. I want the timer to save where it stopped, so basically if I /stop the server or if the server restarts, it starts back at where the countdown previously stopped.

    Also please if you have the time, please do break down the code so I can understand it instead of just throwing code at me, thanks!
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Use php's time() function to get the current timestamp.
    Use your preferred way to save that together with a player's name or uuid.
    Compare times whenever the command gets run.
     
  3. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    you can use a task to decrement the time every 20 ticks
     
  4. Trxgically

    Trxgically Spider Jockey

    Messages:
    26
    GitHub:
    trxgically
    This isn't reall helping since I'm pretty much new to PMMP plugin development.
     
  5. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    This is stupid.

    Before doing nothing, learn how time() works.
    You must create a variable for each player to save the previous time where he had ran the command, this can be inside the .yml file of the player or whatever thing you've made (sqlite, mysql)
    PHP:
    class CustomPlayer extends Player {

        private 
    $countdown null;

        public function 
    getCountdown() {
            return 
    $this->countdown;
        }

        public function 
    setCountdown(?int $countdown) {
            
    $this->countdown $countdown;
        }

    }

    class 
    Countdown {

        private const 
    SECONDS_TO_WAIT 60 5;

        private static function 
    getPlayerCountdown(CustomPlayer $player) {
            return 
    time() - $player->getCountdown();
        }

        public static function 
    setPlayerCountdown(CustomPlayer $player) {
            
    $player->setCountdown(time());
        }

        public static function 
    hasPlayerCountdown(CustomPlayer $player) {
           
    $countdown self::getPlayerCountdown($player);

            if(
    $countdown self::SECONDS_TO_WAIT) {
                
    $player->setCountdown(null);
                return 
    false;
            } elseif(
    $player->getCountdown() != null) {
                
    $player->sendMessage("You can't use this command for " self::SECONDS_TO_WAIT $countdown " seconds.");
                return 
    true;
            }

            return 
    false;
        }

    }

    class 
    YourCommand extends Command {

        
    // Just an example
        
    public function execute(CustomPlayer $player) {
            if(
    Countdown::hasPlayerCountdown($player)) {
                
    // Dont execute the command
            
    } else {
                
    // Execute the command
                
    Countdown::startPlayerCountdown($player));            
            }
        }

    }
    Hope you understood this well.
     
    HimbeersaftLP likes this.
  6. Trxgically

    Trxgically Spider Jockey

    Messages:
    26
    GitHub:
    trxgically
    Thanks I understood!
     
  7. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    he ask for the timer to count down when the server is running
     
  8. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    I'm not replying to this
     
    KielKing 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.