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

Most efficient way to handle events?

Comments in 'Plugin Development' started by Samueljh1H8sLag, Aug 7, 2015.

  1. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    Hey, I was wondering what the most efficient way to handle events is. You know- to create less lag.

    At the moment, i have a bunch events all in one file- in one plugin, and some events repeated in other plugins for different purposes.

    Its a bit like this:

    Main minigame plugin
    PHP:
    function onMove(PlayerMoveEvent $pme){

        
    //Stuff

    }

    function 
    onDmg(EntityDamageEvent $ede){

        
    //Stuff

    }
    ~8 events

    then in my leaderb0ard plugin

    PHP:
    function onMove(PlayerMoveEvent $pme){

        
    //Stuff

    }
    etc etc. These plugins cannot be merged.

    So, what is the fastest and less laggy way to register a bunch of events?

    Tnx :p
  2. BalAnce
    Offline

    BalAnce Active Member

    Joined:
    Mar 10, 2015
    Posts:
    125
    Minecraft User:
    BalAnce
    Wait does the leaderboard plugin like a extension to the minigame? If so why not make it all one plugin?
  3. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    cuz its a lot of work xD but i will soon.

    also @PEMapModder said it isn't good to have loads of events in one plugin
  4. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Wait you want those Events in other file?
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Yes. I find it hard to. I think create a file called PlayerMoveEvent and register it?
  6. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    Sep files create less lag?
  7. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    IDK. But looks more neat. Check out how @luca28pet does it with EventListener files.
  8. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    No not for each event
    ~\AuthorName\PluginName\EventListener.php -
    PHP:
    <?php

    namespace AuthorName\PluginName;

    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\event\Listener;

    use 
    pocketmine\plugin\PluginBase;

    class 
    EventListener extends PluginBase implements Listener{

    private 
    $plugin;

    public function 
    __construct(Main $plugin){ // Or whatever you main class is named
    $this->plugin $plugin;
    }

    public function 
    onMove(PlayerMoveEvent $event){
    #Some code
    }

    }
    In Main class:
    PHP:
    public function onEnable(){
    $this->getServer()->getPluginManager()->registerEvents(EventListener($this), $this);
    }
    Xenturio likes this.
  9. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    that's less laggy?
  10. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Dunno but more efficient then doing in Main class, It's easier to manage your code. less lagy = php7
  11. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Yeah. Or sperate files are good also.
  12. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    ok done, i prefer separate files also cuz its easier to read
  13. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Yay!
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Registering events in main class or in other classes have negligible effect (yes, a player moving a bit can create like 1000 times effect on performance, so neglect that effect) on performance. Moreover, difference is only on performance.

    What I said about events is that unless for special reasons like plugin compatibility and you are forced to do so, you shouldn't register the same event for multiple times.
    TigerHix likes this.
  15. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    so registering onMove will create a lot of lag?

    here is my event:

    PHP:
    function onMove(PlayerMoveEvent $me){

         
    $p $me->getPlayer();

         if(
    $p->getLevel() === $this->plugin->lobby){

              if(!
    $p->isOP()){

                   
    $p->getInventory()->clearAll();
                   
    $p->getInventory()->sendArmorContents([$p]);

                   if(
    $p->getY() < 64){

                         
    $p->teleport($this->lobby->getSafeSpawn());

                   }

              }

              if(
    $p->getZ() > -130 && ($p->getLevel()->getBlock($this->Vec3Round($p->getPosition()))->getId() === Block::EMERALD_BLOCK)){

                   if(
    $p->getLevel()->getBlock($this->Vec3Round($me->getFrom()))->getId() !== Block::EMERALD_BLOCK){

                        
    $p->sendMessage("§a§lFinding §r§aa §lMatch §r§ato §lJoin§r§a...");

                        foreach(
    $this->plugin->games as $game){

                             if(
    $game->isJoinable() && ($game->getHostName() === "") || in_array(strtolower($p->getName()), $this->plugin->invitedPlayers[$game->getHostName()])){

                                  
    $this->plugin->getServer()->getScheduler()->scheduleDelayedTask(new CallbackTask(array($this->plugin"joinGame"), array($game$p)), 3);

                                  return 
    true;

                              }

                        }

                        
    $p->sendMessage("§4§lSorry, All §r§4of the §lMatches §r§4are §lFull. Try Again Later!");

                   }

              }

         }

         foreach(
    $this->plugin->games as $game){

              
    $game->onMove($me);

         }

    }
    Should i use Packets Instead?
    Last edited: Aug 12, 2015
  16. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Yes. Onmove is laggy.
  17. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    should i use packets or would that b pointless?
  18. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    I do not play with packets so do not ask me :)
    jojoe77777 likes this.
  19. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    k.
    Legoboy0215 likes this.
  20. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    Packets don't apply to everything. From what I can see from the code, you are trying to have players join a match when they step on an emerald block. For this one, an efficient way would be using a repeated task instead. You schedule a repeating task, and then in that task you check for player movement, which will be far better than handling PlayerMoveEvent.

    Don't get me wrong, it doesn't mean that you have to avoid using events or avoid using PlayerMoveEvent. Actually, unless you have 100+ event handlers fit into one plugin, you won't be slowing down PocketMine much, by simply listening to them. The lag comes from your code, where you do a lot of "if" conditions to check if the player is stepping on an emerald block. Your code involves so many things to execute; if it is a one-time method, acceptable. But hey, you are forcing PocketMine to run that code on every occurrence of PlayerMoveEvent, and PlayerMoveEvent fires extremely frequently. For your case, I think running the code per second is enough, so use a task that is scheduled for every second.

    If you don't mind, you can have a a try on my custom Task class:



    It allows you to schedule a task anonymously (without to extend the PluginTask class), has a working cancel() method, and supports repeating or be automatically cancelled after running a specified amount of times. The usage examples are also included, so feel free to take a look.
    Samueljh1H8sLag likes this.

Share This Page

Advertisement