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

The player error when executing the Function

Discussion in 'Development' started by Dyntes, Oct 29, 2020.

  1. Dyntes

    Dyntes Spider Jockey

    Messages:
    48
    GitHub:
    Dyntes
    Hey, I have a poblem that the player crash when I execute all of the plugin function... The player just like lagging even if have a good ping... The player couldn't send chat.. Couldn't use any command and just like lagging, the player like destroying block but everyone doesn't see he is destroying the block... Below is the code >>>

    Main.php >>
    PHP:
    <?php

    declare(strict_types=1);

    namespace 
    Dyntes\StopTime;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\entity\ProjectileLaunchEvent;
    Use 
    pocketmine\event\Listener;
    use 
    pocketmine\utils\TextFormat;

    class 
    Main extends PluginBase implements Listener{

        private 
    $stack 0;
        private 
    $stopper = [];
        private 
    $name;

        public function 
    getStopperById(int $id){
            return 
    $this->stopper[$this->stack];
        }
        public function 
    getStopperByName(string $Sname){
            foreach(
    $this->stopper as $player){
                if(
    $player->getName() == $Sname)
                return 
    $player->getStopper();
            }   
        }
        public function 
    newStopper(Player $player){      //register the stop time ability to joining player
            
    $this->getLogger()->info("Checking if the player is already a StopTime user...");
            
    $this->getLogger()->info("Name: ".$player->getName());
            if(
    $this->getStopperByName($player->getName()) != null){
                
    $this->getLogger()->info("The player named ".$player->getName()." is already a StopTime user...");
                
    $this->getStopperByName($player->getName())->setPlayer($player);
            }
            else{
                
    $this->getLogger()->info("The player is not a StopTime user yet...");
                
    $this->getLogger()->info("Registering a StopTime user...");
                
    $this->stopper[$this->stack] = new Stopper($this,$player,$this->stack,5,3);
                
    $this->getLogger()->info("Done making a StopTime user...");
                
    $this->getLogger()->info("Player Name: ".$player->getName());
                
    $this->getLogger()->info("User ID: ".$this->stack);
                
    $this->stack += 1;
            }
        }
        public function 
    onLoad(){
            
    $this->getLogger()->info(TextFormat::YELLOW."The Stop Time plugin is now Loading...");
        }
        public function 
    onEnable(){
            
    $this->stack 0;
            
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
            
    $this->getLogger()->info(TextFormat::GREEN."The Stop Time plugin is Enabled...");
        }
        public function 
    onDisable(){
            
    $this->getLogger()->info(TextFormat::RED."The Stop Time plugin is Disabled...");
        }
        public function 
    onJoinEvent(PlayerJoinEvent $event){
            
    $player $event->getPlayer();
            
    $this->newstopper($player);
        }
        public function 
    onCommand(CommandSender $senderCommand $cmdstring $lbl, array $args):bool{
            switch(
    $cmd->getName()){
                case 
    "stoptime":{
                    if(
    $sender instanceof Player){
                        
    $stopperSe $this->getStopperByName($sender->getName());
                        
    $stopperSe->stopTime();
                    }
                    else
                    
    $sender->sendMessage(TextFormat::RED."This command must be execute by a Player!");
                    break;
                }
            }
            return 
    true;
        }
        public function 
    onPlayerMove(PlayerMoveEvent $event){
            
    $player $event->getPlayer();
            if(
    Saver::isPlayerStopped($player)){
                foreach(
    $player->getLevel()->getPlayers() as $playe){
                    
    $player->hidePlayer($playe);
                }
                
    $event->setCancelled(true);
            }
            else
            foreach(
    $player->getLevel()->getPlayers() as $playe){
                
    $player->showPlayer($playe);
            }
        }

        public function 
    onProjectileLaunch(ProjectileLaunchEvent $event){
            
    $projectile $event->getEntity();
            if(
    Saver::isLevelStopped($projectile->getLevel())){
                
    Saver::addStoppedProjectile($projectile);
            }
        }
    }

    Saver.php >>>
    PHP:
    <?php

    namespace Dyntes\StopTime;

    use 
    pocketmine\entity\projectile\Projectile;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\Player;

    class 
    Saver{

        private static 
    $stoppers = [];
        private static 
    $stoppedProjectile = [];

        public static function 
    getStoppers(){
            return 
    self::$stoppers;
        }

        public static function 
    getStopperByName(string $name){
            foreach(
    self::$stoppers as $stopper){
                if(
    $stopper->getPlayer()->getName() == $name)
                return 
    $stopper;
            }
        }

        public static function 
    isLevelStopped(Level $level):bool{
            
    $yes false;
            foreach(
    self::$stoppers as $stopper){
                if(
    $stopper->getStoppedLevel() == $level)
                
    $yes true;
            }
            return 
    $yes;
        }
        public static function 
    getStopperByPlayer(Player $player){
            foreach(
    self::$stoppers as $stopper){
                if(
    $stopper->getPlayer() == $player)
                return 
    $stopper;
            }
        }

        public static function 
    isStopperStopped($stopp):bool{
            
    $yes false;
            foreach(
    self::$stoppers as $stopper){
                if(
    $stopper == $stopp){
                    if(
    self::isLevelStopped($stopp->getPlayer()->getLevel()) == true){
                        if(
    $stopp->isStopping() == true){
                            
    $yes false;
                        }
                        else
                        
    $yes true;
                    }
                    else
                    
    $yes false;
                }
            }
            return 
    $yes;
        }

        public static function 
    isPlayerStopped(Player $player):bool{
            
    $stopp self::getStopperByPlayer($player);
            
    $yes self::isStopperStopped($stopp);
            return 
    $yes;
        }

        public static function 
    addStoppedProjectile(Projectile $entity){
            
    $sp self::$stoppedProjectile;
            
    array_push($sp, new StoppedProjectile($entity));
            
    self::$stoppedProjectile $sp;
            unset(
    $sp);
        }

        public static function 
    removeStoppedProjectile(){
            
    self::$stoppedProjectile = [];
        }

    }

    Stopper.php >>
    PHP:
    <?php

    namespace Dyntes\StopTime;

    use 
    pocketmine\Player;
    use 
    Dyntes\StopTime\StoppedLevel;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\level\Level;

    class 
    Stopper{

        private 
    $plugin;
        private 
    $player;
        private 
    $ID;
        private 
    $stopping;
        private 
    $remainD;
        private 
    $cd 5;
        private 
    $remainCD;
        private 
    $d 5;
        private 
    $stoppedLevel;

        public function 
    getId(){
            return 
    $this->ID;
        }

        public function 
    getPlugin(){
            return 
    $this->plugin;
        }

        public function 
    getPlayer(){
            return 
    $this->player;
        }

        public function 
    getStopper(){
            return 
    $this;
        }

        public function 
    getName(){
            return 
    $this->player->getName();
        }

        public function 
    getCooldown(){
            return 
    $this->cd;
        }

        public function 
    getDuration(){
            return 
    $this->d;
        }

        public function 
    getStoppedLevel(){
            return 
    $this->stoppedLevel;
        }

        public function 
    isStopping(){
            
    $yes false;
            if(
    $this->stoppedLevel != null)
            
    $yes true;
            return 
    $yes;
        }

        public function 
    stopTime(){
            if(
    $this->remainCD 1){
                
    $this->stoppedLevel $this->getPlayer()->getLevel();
                
    $this->remainCD $this->cd;
                
    $this->remainD $this->d;
                while(
    $this->remainD > -1){
                    if(
    $this->remainD 0){
                        
    sleep(1);
                        
    $this->getPlayer()->sendMessage($this->remainD." seconds left...");
                    }
                    else{
                        
    sleep(1);
                        
    $this->getPlayer()->sendMessage("The time will move again...");
                        
    $this->stoppedLevel null;
                        while(
    $this->remainCD 0){
                            if(
    $this->remainD 0){
                                
    sleep(1);
                                
    $this->remainCD -= 1;
                            }
                        }
                    }
                    
    $this->remainD -= 1;
                }
            }
            else{
                
    $this->getPlayer()->sendMessage(TextFormat::YELLOW."This ability is now cooldown...");
                
    $this->getPlayer()->sendMessage("Cooldown Remaining: ".$this->remainCD." seconds");
            }
            
        }

        public function 
    __construct(Main $main,Player $stopper,int $id,int $duration,int $cooldown){
            
    $this->plugin $main;
            
    $this->ID $id;
            
    $this->player $stopper;
            
    $this->cd $cooldown;
            
    $this->$duration;
        }
    }


    StoppedProjectile.php >>
    PHP:
    <?php

    namespace Dyntes\StopTime;

    use 
    pocketmine\entity\projectile\Projectile;
    use 
    pocketmine\math\Vector;

    class 
    StoppedProjectile{

        private 
    $entity;
        private 
    $default;

        public function 
    __construct(Projectile $entity){
            
    $this->entity $entity;
            
    $this->default $entity->getMotion();
            
    $entity->setMotion(new Vector(000));
            while(
    Saver::isLevelStopped($entity->getLevel()) == true){

            }
            
    $this->resume();
        }

        private function 
    resume(){
            
    Saver::removeStoppedProjectile();
            
    $this->entity->setMotion($this->default);
            unset(
    $this);
        }
    }


    Soooo.... The player sooooo lagging when executing the /stoptime command... I just want to make the stop time plugin... Even when the player left, the player still has to be in the server... Th e console don't detect if the player leave...
    I hope anyone could help me with this problem...

    Everyone could ask me to give this plugin source code if you want...
     
  2. Dyntes

    Dyntes Spider Jockey

    Messages:
    48
    GitHub:
    Dyntes
    Also... The console doesn't appear to be have an error... Just the player issue...
     
  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.