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

Help_-_get the right player

Comments in 'Plugin Development' started by Marcelo234, Feb 13, 2016.

  1. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    Hey Guys..Here i have a code:: so after a player join he should gets every few seconds his coins and levels...But If you can see in the code the server will send all coins and levels of the joined players to all Players who are online! But How to send the Player datas only to the right player?
    Main:
    PHP:
     public function onJoin(PlayerJoinEvent $event) {
          
    $player $event->getPlayer();
    $this->player = new Config($this->getDataFolder() . "PlayersInfo/" strtolower($player->getName()) . ".yml" Config::YAML, array());
            
    $level $this->player->get("Level");
            
    $coins $this->player->get("coins");
            
    $playerworld $player->getLevel()->getName();
            
    $this->config = new config($this->getDataFolder() . "config.yml"Config::YAML, array());
            
    $time1 intval($this->config->get("time")) * 2;
           
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new SendInfoCoinsLevelTask($this$player$coins$level$playerworld), $time1);
           
    }
    SendInfoCoinsLevelTask:
    PHP:
    <?php

    namespace McKaff_Settings;

    use 
    pocketmine\Player;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    McKaff_Settings\Main;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\utils\TextFormat as MT;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\Listener;

    class 
    SendInfoCoinsLevelTask extends PluginTask{
       
         public function 
    __construct(Main $pluginPlayer $player$coins$level){
            
    $this->plugin $plugin;
            
    $this->player $player;
            
    $this->coins $coins;
            
    $this->level $level;
            
    parent::__construct($plugin$player$coins$level);
        }
        public function 
    getPlugin(){  
            return 
    $this->plugin;
        }

        public function 
    onRun($currentTick){
            foreach(
    $this->getPlugin()->getServer()->getOnlinePlayers() as $player){
                
    $this->player = new Config($this->getPlugin()->getDataFolder() . "PlayersInfo/" strtolower($player->getName()) . ".yml" Config::YAML);
                
    $this->config = new config($this->getPlugin()->getDataFolder() . "config.yml"Config::YAML, array());
                
    $language $this->player->get("Language");
                
    $vip =  $this->player->get("V.I.P.");
                
    $limitedviptime $this->player->get("limited_V.I.P._time");
                
    $playerworld $player->getLevel()->getName();
                
    $servertime $this->config->get("servertime");
          if(
    $playerworld == $this->config->get("lobby_spawn_world")){
            if(
    $vip == "true"){
                if(
    $language == "DE") {
                    
    $player->sendPopup(MT::BOLD.MT::GOLD."Deine Coins: ".MT::GRAY.$this->coins.MT::GOLD." Dein Level: ".MT::GRAY.$this->level,MT::BOLD.MT::AQUA."[V.I.P.]");
                }
                if(
    $language == "EN") {
                    
    $player->sendPopup(MT::BOLD.MT::GOLD."Your Coins: ".MT::GRAY.$this->coins.MT::GOLD." Your Level: ".MT::GRAY.$this->level,MT::BOLD.MT::AQUA."[V.I.P.]");
                }
            }
            }
            if(
    $vip == "false"){
                if(
    $language == "DE") {
                    
    $player->sendPopup(MT::BOLD.MT::GOLD."Deine Coins: ".MT::GRAY.$this->coins.MT::GOLD." Dein Level: ".MT::GRAY.$this->level);
                }
                if(
    $language == "EN") {
                    
    $player->sendPopup(MT::BOLD.MT::GOLD."Your Coins: ".MT::GRAY.$this->coins.MT::GOLD." Your Level: ".MT::GRAY.$this->level);
                }
            
    $this->player->set("V.I.P.""false");
            
    $this->player->set("limited_V.I.P.""false");
            }
          }
        }
    }
    Thanks Marcelo234
  2. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    why using global variables (ex. $this->player;) when not needed? why creating different schedulers for each player ? why passing $player $coins and $level to the PluginTask construct ? why using $this->getPlugin() instead of using $this->getOwner() ? do you know $playerworld is not used (in Main) ? why looping into online players if you need to send the popup only to 1 player? to get only one player you can do:
    PHP:
    Server->getPlayer('playerName');
    //or if you want to get the player by the exact name:
    Server->getPlayerExact('playerName'); //if i remember correctly
    btw , very bad coded plugin
    seems like you just wrote random things copying other plugins and without understanding nothing
    Last edited: Feb 13, 2016
  3. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    1. Create the config in onJoin() because at onRun() it will be made every second
    2. Why == "true" its called true without ""
    3. The problem why everyone gets the stats of the same player:
    PHP:
    if($vip == "false"){
     if(
    $language == "DE") {$player->sendPopup(MT::BOLD.MT::GOLD."Deine Coins: ".MT::GRAY.$this->coins.MT::GOLD." Dein Level: ".MT::GRAY.$this->level);
     }
     if(
    $language == "EN") {$player->sendPopup(MT::BOLD.MT::GOLD."Your Coins: ".MT::GRAY.$this->coins.MT::GOLD." Your Level: ".MT::GRAY.$this->level);
     }
    $this->player->set("V.I.P.""false");$this->player->set("limited_V.I.P.""false");
     }
    Why do you use a public variable? Just do $playerconfig->get("coins");
    Btw I recommend you to decompile your code a bit
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You don't need to call parent constructor with so many parameters. PluginTask::__construct() only requires one parameter.
    PocketKiller, Marcelo234 and Vaivez66 like this.
  5. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    Ok thanks ....
    I created the Global variable...And I changed the code...But there is an error.
    Main:
    PHP:
    class Main extends PluginBase implements Listener {
       
        public 
    $playerconfig;   

    public function 
    onJoin(PlayerJoinEvent $event) {
    $player $event->getPlayer();
            
    $this->playerconfig = new Config($this->getDataFolder() . "PlayersInfo/" strtolower($player->getName()) . ".yml" Config::YAML);
            
    $this->config = new config($this->getDataFolder() . "config.yml"Config::YAML, array());
            
    $time1 intval($this->config->get("time")) * 2;
           
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new SendInfoCoinsLevelTask($this$player), $time1);
    }

    }
    SendInfoCoinsLevelTask:
    PHP:
    <?php

    namespace McKaff_Settings;

    use 
    pocketmine\Player;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    McKaff_Settings\Main;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\utils\TextFormat as MT;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\Listener;

    class 
    SendInfoCoinsLevelTask extends PluginTask{
       
         public function 
    __construct(Main $pluginPlayer $player){
            
    $this->plugin $plugin;
            
    $this->player $player;
            
    parent::__construct($plugin$player);
        }
        public function 
    getPlugin(){  
            return 
    $this->plugin;
        }

        public function 
    onRun($currentTick){
                
    $this->config = new config($this->getPlugin()->getDataFolder() . "config.yml"Config::YAML, array());
                
    $language $this->playerconfig->get("Language");
                
    $vip =  $this->playerconfig->get("V.I.P.");
                
    $limitedviptime $this->playerconfig->get("limited_V.I.P._time");
                
    $playerworld $player->getLevel()->getName();
                
    $servertime $this->config->get("servertime");
          if(
    $playerworld == $this->config->get("lobby_spawn_world")){
            if(
    $vip == "true"){
                if(
    $language == "DE") {
                    
    $player->sendPopup(MT::BOLD.MT::GOLD."Deine Coins: ".MT::GRAY.$this->playerconfig->get("coins").MT::GOLD." Dein Level: ".MT::GRAY.$this->playerconfig->get("Level"),MT::BOLD.MT::AQUA."[V.I.P.]");
                }
                if(
    $language == "EN") {
                    
    $player->sendPopup(MT::BOLD.MT::GOLD."Your Coins: ".MT::GRAY.$this->playerconfig->get("coins").MT::GOLD." Your Level: ".MT::GRAY.$this->playerconfig->get("Level"),MT::BOLD.MT::AQUA."[V.I.P.]");
                }
            }
            }
            if(
    $vip == "false"){
                if(
    $language == "DE") {
                    
    $player->sendPopup(MT::BOLD.MT::GOLD."Deine Coins: ".MT::GRAY.$this->playerconfig->get("coins").MT::GOLD." Dein Level: ".MT::GRAY.$this->playerconfig->get("Level"));
                }
                if(
    $language == "EN") {
                    
    $player->sendPopup(MT::BOLD.MT::GOLD."Your Coins: ".MT::GRAY.$this->playerconfig->get("coins").MT::GOLD." Your Level: ".MT::GRAY.$this->playerconfig->get("Level"));
                }
            
    $this->player->set("V.I.P.""false");
            
    $this->player->set("limited_V.I.P.""false");
            }
        }
    }
    Thanks Marcelo234
  6. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    Error :
    Undefinied variable playerconfig
  7. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    No, the problem is that you create everytime onRun gets triggered a new config for each player will be created
    Marcelo234 likes this.
  8. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    No, because i wrote the create file thing into onJoin()
  9. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
  10. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    Sorry I don't ckeck why it won't work
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is called a class property, not a global variable.
    PocketKiller likes this.
  12. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    *Undefined property SendInfoCoinsTask::$playerconfig*
    you defined the property for the Main class, you did not define it for the SendInfoCoins class.
    so you would have to passing the Main::$playerconfig to the SendInfoCoinsTask and adding an argument for it and setting SendInfoCoinsTask::$playerconfig to that given argument.
    Last edited: Feb 14, 2016

Share This Page

Advertisement