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

Solved Need help with my Scoreboard

Discussion in 'Development' started by Deeonix, Aug 24, 2017.

  1. Deeonix

    Deeonix Spider Jockey

    Messages:
    29
    Hey. I think I'm stopping my Scoreboard/Task wrong.

    Here is the Code:

    Main.php:
    PHP:
    <?php

    namespace Deeonix\DCore;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\player\PlayerQuitEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\utils\TextFormat;

    class 
    Main extends PluginBase implements Listener{
        
        public 
    $tasks = [];

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info(TextFormat::GREEN."Enabled!");
        }
        
        public function 
    onDisable() {
            
    $this->getLogger()->info(TextFormat::RED."Disabled!");
        }
        
        public function 
    onLoad() {
            
    $this->getLogger()->info(TextFormat::YELLOW."Loaded!");
        }

        public function 
    onJoin(PlayerJoinEvent $event) {
            
    $player $event->getPlayer();
            
    $name $player->getName();
            
    $event->setJoinMessage(TextFormat::GREEN.TextFormat::BOLD."» ".TextFormat::RESET.TextFormat::GREEN.$name);
            
    $this->enableSB($player);
            
    //Server::getInstance()->broadcastMessage(TextFormat::GREEN.TextFormat::BOLD."» ".TextFormat::RESET.TextFormat::GREEN.$name);
        
    }
        
        public function 
    onQuit(PlayerQuitEvent $event) {
            
    $player $event->getPlayer();
            
    $name $player->getName();
            
    $event->setQuitMessage(TextFormat::RED.TextFormat::BOLD."« ".TextFormat::RESET.TextFormat::RED.$name);
            
    //Server::getInstance()->broadcastMessage(TextFormat::RED.TextFormat::BOLD."« ".TextFormat::RESET.TextFormat::GREEN.$name);
        
    }
        
        public function 
    enableSB($player) {
            
    $task = new SB($this$player);
            
    $taskid $task->getTaskId();
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask($task20);
            
    $this->tasks[$taskid] = $taskid;
        }

        public function 
    disableSB($id) {
            unset(
    $this->tasks[$id]);
            
    $this->getServer()->getScheduler()->cancelTask($id);
        }
        
    }
    SB.php:

    PHP:
    <?php

    namespace Deeonix\DCore;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\Player;
    use 
    Deeonix\DCore\Main;

    class 
    SB extends PluginTask {
        
        public 
    $plugin;
        private 
    $player;
        private 
    $name;

        public function 
    __construct(Main $pluginPlayer $player) {
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
            
    $this->player $player;
            
    $this->name $this->player->getName();
        }

        public function 
    getPlugin() {
            return 
    $this->plugin;
        }
        
        public function 
    onRun($tick) {
            while(
    $this->plugin->getServer()->getPlayer($this->name)->isOnline()) {
                
    $this->plugin->getServer()->getPlayer($this->name)->sendTip("                                                             Name: ".$this->name."\n                                                             Rang: Master");
            }
            
    $this->plugin->disableSB($this->getTaskId());
        }
        
    }
    When a player leava the server i get this message:
     
  2. AshBull

    AshBull Spider Jockey

    Messages:
    31
    This causes an infinite loop.
     
    Marabou, XdmingXD, SOFe and 2 others like this.
  3. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    explanation:
    you cant do that because server will now keep spamming tip packet to the FIRST client that joins, and ignore everything else
    not even a second client can join nor anything because you used while(remember that blocks other background under the hood stuff)
    acceptable mistake for people who havent know PHP, but you should use a repeating task
    and use a IF player is online
    send tip
    else cancel task
     
    Jack Noordhuis and SOFe like this.
  4. Deeonix

    Deeonix Spider Jockey

    Messages:
    29
    Fixed:
    PHP:
    <?php

    namespace Deeonix\DCore;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\Player;
    use 
    Deeonix\DCore\Main;

    class 
    SB extends PluginTask {
        
        public 
    $plugin;
        private 
    $player;
        private 
    $name;

        public function 
    __construct(Main $pluginPlayer $player) {
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
            
    $this->player $player;
            
    $this->name $this->player->getName();
        }

        public function 
    getPlugin() {
            return 
    $this->plugin;
        }
        
        public function 
    onRun($tick) {
            if(
    $this->plugin->getServer()->getPlayer($this->name) === null) {
                
    $this->plugin->disableSB($this->getTaskId());
            } else {
                
    $this->plugin->getServer()->getPlayer($this->name)->sendTip("                                                             Name: ".$this->name."\n                                                             Rang: Master");
            }
        }
        
    }
    Thanks for the hint :D
     
  5. Deeonix

    Deeonix Spider Jockey

    Messages:
    29
    The hint of @AshBull was enough to solve the problem ;D
     
    AshBull likes this.
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    yes but just figure i might as well explain it anyways /shrug/
     
    Deeonix 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.