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

Another dump question!

Comments in 'Plugin Development' started by JackboyPlay, Mar 29, 2016.

  1. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    PHP:
    public $kitpvp = array();

    public function 
    onBlaBla(BlaBla $event){
    unset(
    $this->kitpvp[$event->getPlayer()->getName()]);
    }
    Why does this not work?
  2. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    PS: The player is settet in the array!
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It works.
    Try var_dump($kitpvp) before and after the unset line? Did anything change?
  4. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay



    array(1) {
    [0]=>
    string(11) "JackboyPlay"
    }
    Last edited: Mar 29, 2016
  5. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    And so I remove the player:
    PHP:
    public function onLeave(PlayerQuitEvent $event){
    unset(
    $this->kitpvp[$event->getPlayer()->getName()]);
    }
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Don't you see it? Your array is 0 => "JackboyPlay". The key is 0, and the value is "JackboyPlay". $array[$key] means to refer to the entry associated to $key in $array. $key is the key. In this case, you should unset($array[0]).

    It seems like you defined the array property using $this->kitpvp[] = $playerName, right? In that case, you are adding $playerName to the array as a value, with an automatically incrementing key. In this case, since this value is the first one, the key is int(0).

    The most efficient way to fix this is to use `$this->kitpvp[$playerName] = true;` instead. `true` is just a dummy value. However, I am pretty sure that you will need to store more data than just whether a player is in the array, so you may want to store some actual value instead, for example, the cached data of the player's statistics.
    And to check whether the player is in the array, use isset($this->kitpvp[$playerName]).
  7. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    @PEMapModder I'll input the player so:
    PHP:
    if(!isset($this->kitpvp[$event->getPlayer()->getName()])){
    array_push($this->kitpvp$event->getPlayer()->getName());
    }
  8. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    @PEMapModder and I store only the playernames in the array :D
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Excuse me, what did I just say?

    http://php.net/manual/en/function.array-push.php#refsect1-function.array-push-description
    arrpush.PNG

    array_push() will push $var as the value, not the key. The key will auto-increment. You must set the player name as the key, and then just stuff anything non-null as the value. I have already explained that in the post above -- if you want to associate nothing to the value, just store true.
  10. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
  12. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    @PEMapModder
    PHP:
    $this->kitpvp[$event->getPlayer()->getName()] = $this->kitpvp[$event->getPlayer()->getName()];
    Is that right?
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Possible. But you are wasting memory twice. Why not just stuff a boolean true into the value?
  14. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    @PEMapModder did you mean
    PHP:
     $this->kitpvp[$event->getPlayer()->getName()] = true;
    ?
  15. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    100 POINTS YEAH

Share This Page

Advertisement