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

This code does not work How to fix it

Discussion in 'Development' started by Brian Medina, Apr 16, 2017.

  1. Brian Medina

    Brian Medina Spider Jockey

    Messages:
    27
    GitHub:
    NintendoCore16
    use google traductor bad
    How to fix it

    PHP:
        ########################NO REAGIN####################
        
    public function onRegainHealth(EntityRegainHealthEvent $event){
            
    $player $event->getEntity();
            if(
    $player instanceof Player){
                
    $w $player->getLevel()->getFolderName();
                
    $regain $this->getConfig()->get("disable-regen");
                if(
    in_array($w$regain)){
                    
    $event->setCancelled();
                }
            }
        }
     
  2. kaleb418

    kaleb418 Spider Jockey

    Messages:
    29
    GitHub:
    kaleb418
    It's hard to tell without you sharing an example of your config, but my guess it that you're searching for $w inside of $regain, when neither of them appear to be an array.
     
  3. Orlando092

    Orlando092 Creeper

    Messages:
    5
    GitHub:
    orlando092
    You could try..

    PHP:
    If(isset($regain[$w])){
         
    $event->setCancelled();
     
  4. kaleb418

    kaleb418 Spider Jockey

    Messages:
    29
    GitHub:
    kaleb418
    Maybe try
    PHP:
    switch($regain){
        case 
    true:
           break;
        case 
    1:
           break;
        case 
    "true":
           break;
        case 
    "1":
           break;
        default:
           
    $event->setCancelled();
    }
    Could you share an example of what $regain might look like (in the config)?
     
    Last edited: Apr 16, 2017
  5. Brian Medina

    Brian Medina Spider Jockey

    Messages:
    27
    GitHub:
    NintendoCore16
    config example
    disable-regen: [world, Lobby, HUB, WORLDS]
     
  6. DaPigGuy

    DaPigGuy Slime

    Messages:
    86
    GitHub:
    DaPigGuy
    Did you register listener?
     
  7. kaleb418

    kaleb418 Spider Jockey

    Messages:
    29
    GitHub:
    kaleb418
    Make sure the items in the array are strings.
    Code:
    ["world", "Lobby", "HUB", "WORLDS"]
    Not sure what else would cause the problem, other than maybe $regain isn't an array?? Try checking the type of the variable, if the above doesn't work.
     
    Last edited: Apr 16, 2017
  8. kaleb418

    kaleb418 Spider Jockey

    Messages:
    29
    GitHub:
    kaleb418
    He would have gotten an error if not, undefined class Plugin\Main\EntityRegainHealthEvent
     
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The code you provided is perfectly OK. The problem may originate from somewhere else, such as not having registered the listener.
    I never knew that someone could read mind through the Internet cables to know whether those variables are arrays. This is already beyond the level of wild guessing.
    Do you even have the foggiest idea what you are saying?
    Why must $w be an array? It is very obvious that it is a level folder name from the OP.
    :facepalm:
     
  10. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Might I recommend adding a default output for $regain to be an empty array to prevent errors?
    $regain = $this->getConfig()->get("disable-regen", []);
     
    SOFe likes this.
  11. Brian Medina

    Brian Medina Spider Jockey

    Messages:
    27
    GitHub:
    NintendoCore16
    gracias tanks (my english is bad)
     
  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.