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

Dynamic Variable for every Player

Discussion in 'Development' started by SkyZone, Dec 10, 2016.

  1. SkyZone

    SkyZone Slime

    Messages:
    95
    Im developing a GunGame Plugin and i need to make a global variable for every player to store the level of the player, but i dont know how to do this. This is my Code:
    PHP:
    public function onJoin(PlayerJoinEvent $event){
            
    $var $event->getPlayer()->getNameTag();
            
    $event->setJoinMessage(TextFormat::RED.$event->getPlayer()->getName().TextFormat::BLUE." spielt nun GunGame.");
            
    $this->getServer()->broadcastPopup(TextFormat::RED.$event->getPlayer()->getName().TextFormat::BLUE." spielt nun GunGame.");
            
    $event->getPlayer()->getInventory()->clearAll();
            
    $event->getPlayer()->getInventory()->setItem(0Item::get(271));
            
    $event->getPlayer()->setGamemode(0);
            
    $event->getPlayer()->setMaxHealth(20);
            
        }
        

        
        public function 
    onDeath(PlayerDeathEvent $event){
            global 
    $level;
            
    $ldc $event->getPlayer()->getLastDamageCause()->getEntity();
            if(
    $ldc instanceof \pocketmine\Player){
                
    $dinv $ldc->getPlayer();
                
    $sinv $event->getPlayer();
                
    $damager $ldc->getPlayer()->getNameTag();
                
    $iskilled $event->getPlayer()->getNameTag();
                
    $event->setDeathMessage(TextFormat::RED."$iskilled ".TextFormat::BLUE."wurde von ".TextFormat::GOLD."$damager".TextFormat::BLUE." getötet");
                
    //Change Level of Killer:
                
                
                //Player-Level-Changer of $damager:
                
                
                
                //Player-Level-Changer of $iskilled:
            
    }
            
        }
    Later i will make this:
    Code:
    if($... == 1){
    ...
    }elseif($...) == 2{
    
    } ...
    Someone knows how to do this? (Sorry that some parts of the code are in german)
     
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    We don't use global variables to store data. PocketMine uses CLI PHP and it is not like web server PHP. All players are handled in the same runtime, so storing any global data will be saved for the whole server. Moreover, all plugins share the same runtime, so always use class-local properties rather than global variables which clashes with other plugins.
    I'm going to write a thread in the Resources forum to explain about the management of storing per-player data effectively tonight or tomorrow.
    Bonus: I'll use your scenario as an example, so it would be best if you have more types of data that I can use as exmaple ;)

    By the way, this is not correct. The entity in the last damage cause is still the player who is dead. You should use getDamager(), but this is off-topic. Go to your former thread to discuss about this.
     
    Last edited by a moderator: Dec 10, 2016
    SkyZone and Primus like this.
  3. SkyZone

    SkyZone Slime

    Messages:
    95
    Okay, thanks for your help :D You could also show how to save the time of the last login of every Player in a Variable. This is OffTopic but: Will there be any playce on this forums to public plugins?
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Use global variables?
    PHP:
    public $lastlogin = [];

    /*
    * When the plugin gets enabled,
    * it will auto load the saved lastlogin data
    * from config and put it in the global
    * variable.
    */
    public function onEnable() {
        
    $config $this->getConfig();
        
    $restock $config->get("lastlogin", []);
        if (
    count($restock) > 0) {
            foreach (
    $restock as $playername => $logintime) {
                
    $this->lastlogin[$playername] = $logintime;
            }
        }
    }

    /*
    * When the player joins, it will save the
    * lastlogin data in the global variable.
    */
    public function onJoin(PlayerJoinEvent $e) {
        
    $p $e->getPlayer();
        
    $name strtolower($p->getName());
        
    $this->lastlogin[$name] = date('d/m/Y h:i:s');
    }

    /*
    * When disabled, it will save the lastlogin
    * data (from the global variable) to the
    * config. (config.yml)
    */
    public function onDisable() {
        
    $config $this->getConfig();
        
    $config->setNested("lastlogin"$this->lastlogin);
        
    $config->save();
    }

    /*
    * Returns a formatted string of the last
    * time the player joined the server.
    * $player->sendMessage($this->getPlayerLastLogin($player);
    * Returns "Last joined at 31/12/2016 22:59:00".
    * Day/Month/Year Hour:Minute:Second
    */
    public function getPlayerLastLogin(Player $player) {
        
    $name strtolower($player->getName());
        return 
    "Last joined at ".$this->lastlogin[$name];
    }
    I'm not jobless, just bored.
     
    Skullex and Primus like this.
  5. Primus

    Primus Zombie Pigman

    Messages:
    749
    Those are called properties.
     
    Muqsit likes this.
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    :eek: didn't know that!
     
    Primus likes this.
  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.