Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Illegal Offset type in isset

Comments in 'Plugin Development' started by Legoboy0215, Mar 18, 2016.

  1. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    PHP:
    public function onJoin(PlayerJoinEvent $event){
            
    $player $event->getPlayer();
                if(
    $this->base->bah->exists($player->getName())){ //Error happens when this is called
                    //Blah
                
    }else{
                    
    $this->base->bah->set($player->getName(), 0);
                    
    $this->base->bah->save();
                }
        }
    PHP:
    WarningIllegal offset type in isset or empty in phar:///root/pm/PocketMine-MP.phar/src/pocketmine/utils/Config.php on line 351
    No debug trace at ALL. What did I do wrong?
  2. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    $this->base->bah is Config, right?

    It's not your fault, PocketMine.
    Legoboy0215 likes this.
  3. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Yes.
  4. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Try this:

    PHP:
    $name $player->getName();
    $cfa $this->base->bah->getAll();
    switch(isset(
    $cfa[$name])){
    case 
    0:
    //false
    //do something
    break;
    case 
    1:
    //true
    //do something
    break;
    default:
    //unknown error
    break;
    }
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    K. Thank!
  6. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Nope. Same issue.
  7. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Maybe because the index $player->getName() does not exist.
    Maybe something like this is better:
    $cfa = $this->base->bah->getAll();
    $name = $player->getName();
    $cfa[$name] = 0;
    $this->base->bah->setAll($cfa);
    $this->base->bah->save();
  8. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    That would return an UndefinedIndex error I believe. Now, I am not even sure is it caused by that code. How do I fix it?!
  9. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    You won't lose nothing if you try. It works for me.

    Code:
    $cfa = $this->base->bah->getAll();
    $name = $player->getName();
    $this->base->bah->setNested($name, 0);//set nested might work
    $this->base->bah->setAll($cfa);
    $this->base->bah->save();
  10. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    I did try. As I said, I am not even sure what is causing this. testing at the moment.
  11. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Try setNested ??
  12. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    I am calling exists, I believe :)
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Show the stack trace. Set debug.level to 2, then show us the stack trace.
    I suspect you called exists() with a non-string parameter. Try var_dump()ing your parameter?
    Legoboy0215 likes this.
  14. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Sorry, solved XD. As I said, there was no stack trace. Before the code I posted (That had no problem :oops:), I ran a function that ran a function that ran another function in a other plugin. The function called exists on a Player parameter. Sorry for disturbing all of you :(
  15. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    isset()
    Doesn't do the same as
    PHP:
    array_key_exists();
    ?
  16. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Hell no, example:
    PHP:
    $a = array('key1' => 'hello''key2' => null);

    isset(
    $a['key1']);             // true
    array_key_exists('key1'$a);  // true

    isset($a['key2']);             // false
    array_key_exists('key2'$a);  // false
    Array_key_exists shows if the key ACTUALLY exists, while isset does the same but the value can't be null.

    Half-ref: http://stackoverflow.com/questions/3210935/difference-between-isset-and-array-key-exists
    PEMapModder likes this.

Share This Page

Advertisement