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

how to add how many times a player can attempt to enter the password

Discussion in 'Development' started by Levi, Nov 1, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    i want 2 make login but i want to kick players if they failed putting password 5 times
    PHP:
    public function onBreakBreakEvent(BlockBreakEvent $event){
            
    $player$event->getPlayer()->getName();
            if(isset(
    $this->login[$player])){
                
    $event->setCancelled(true);
                
    $event->getPlayer()->sendMessage("Please login first!");
            }
        }
     
  2. KnownUnown

    KnownUnown Spider Jockey Poggit Reviewer

    Messages:
    47
    GitHub:
    knownunown
    You've to store some kind of counter per player and increment that counter every time a player attempts to log in. Implementation is left as an exercise to the reader.
     
    jasonwynn10 and Thunder33345 like this.
  3. QuiverlyRivalry

    QuiverlyRivalry Zombie Pigman

    Messages:
    491
    GitHub:
    quiverlyrivalry
    Im pretty sure login inst needed in 1.2
     
  4. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    How you know he want it for 1.2? :D
     
  5. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Try something like this
    PHP:
    /** @var Player $p */
    if(!isset($this->attempt[strotlower($p->getName()])){
        
    $this->attempt[strtolower($p->getName())] = 1;
        return;
    }
    $this->attempt[strtolower($p->getName())] += 1;
    if(
    $this->attempt[strtolower($p->getName())] == 5){
         
    $p->kick('You reached max password attempts');
    }
     
    jasonwynn10 likes this.
  6. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    If you want to use it in PlayerLoginEvent , cancell event instead and set its message.
     
    jasonwynn10 likes this.
  7. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    for these who plan to copy said code:
    please do not hardcode say "magic number"
    using something like a const would be better
    using a config would be prefered
     
    Kyd, jasonwynn10 and Muqsit like this.
  8. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Why? Just stating what you think to be a good idea without reason is pretty sketchy.

    Whilst what you said is a good idea and should be used in these situations, you should provide some evidence as to why it should be done. Giving out advice like this and just assuming people know or understand why it is considered good practice to do so doesn't really help advance the knowledge of those reading and is comparable to just spewing out code on demand.

    For those actually wondering why you should use a config or constant to store the value: It allows you or the users of the plugin to easily modify and view the value instead of having to search for every occurrence in the code. This improves the readability of the code and also makes maintenance easier.
     
    jasonwynn10 and Thunder33345 like this.
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    It looks like the code you posted is completely irrelevant to your question. Did I misunderstand you?
     
  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.