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

Solved Add or remove a string from a variable

Comments in 'Plugin Development' started by Andrey Nazarchuk, Jul 15, 2015.

  1. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Hello,
    How do I add/remove a string to/from a variable?
    For example, when a player joins a certain world, I want to be able to add their name to $players
    Like this:
    PHP:
    $player $event->getPlayer()->getName();
    $players $players $player//How do I do this with strings
    Then when the player leaves:
    PHP:
    $player $event->getPlayer()->getName();
    $players $players $player//Again, Strings?
    Thanks.
  2. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    Code:
    public $players = [];
    -------
    //when player joins:
    $this->players[$event->getPlayer()->getName()] = $event->getPlayer()->getName();
    //when player leaves
    if(isset($this->players[$event->getPlayer()->getName()])){
    unset($this->players[$event->getPlayer()->getName()]);
    }
    //get all player names stored
    foreach(array_keys($this->players) as $player){
    echo $player;
    }
    //get specific player
    if(isset($this->players[$event->getPlayer()->getName()])){
    echo "player exists";
    }else{
    echo "player not found";
    }
    
    anyways, why wont you use $this->getServer()->getOnlinePlayers(); ?
  3. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Thank you very much!!! :D
    Because I need the players in the match, the world or the server.
  4. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    well if thats the case, just add strtolower() on names to make sure its equivalent to the player inside the session.
    RekkuzaRage likes this.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Prevent holding player instances to reduce the chance of memory leak.
  6. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    How do I do that?
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I ask you not to do that :p
  8. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Well how do I no hold them? :D
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Then how do you hold them at all?
  10. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    Ok now I am really confused.:oops:
    Legoboy0215 likes this.

Share This Page

Advertisement