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_
    How could you detect player movement in a repeated class? Also wouldn't that be laggy as well as you are constantly checking for the same thing
  2. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    Uh, just copy the code in your onMove(PlayerMoveEvent $event) method and paste them in your task? For the second question, whether you listen to events or schedule a repeating task, you are "constantly checking for the same thing", only that the interval is different.
  3. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    but then there would be no way of getting the specific player and most of the functions used like $ev->getFrom() would be unused
  4. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    Well, think this in a different way. What does PlayerMoveEvent->getFrom() return? The player location. So, simply use a foreach loop to loop through Server->getOnlinePlayers(), and in the loop, get player location by Player->getLocation(), and do the remaining condition checks just as original.
  5. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    your method would be a lot laggier, because you would be having to constantly loop through each player, check if their coordinates have changed and then do the code. This would use a lot more processing power. Also, getFrom() is the players LAST location.
  6. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    You don't need to "check if their coordinates have changed", you don't even need to know about the last location of players as well. From what I can see from your previous code, you are simply going to have players join a match when they step on the emerald block in an area specified.

    PHP:
    // Assuming you are using my Task class, if not, just move the function to onRun() method of a PluginTask
    (new Task(function(Plugin $plugin) {
        foreach (
    $targetLevel->getPlayers() as $player) {
            if (
    $player-><= -130) continue;
            
    // Do other checks here
            
    if ($targetLevel->getBlock($player)->getSide(Vector3::SIDE_DOWN)->getId() === Block::EMERALD_BLOCK) {
                
    $match->join($player);
            }
        }
    })->
    repeat(20)->schedule();
    I am 100% sure this way cost far less processing power; feel free to do a benchmark or something. Well, actually you just ignore about the detail implementation, just think about that, how many times will our code be run? If using your event method, as MovePlayerPacket is intercepted quite frequently, I would say around 15 for each player each second. Assuming there are 30 players on the server, your code will be run 15*30 = 450 times. Meanwhile, my code is executed only once per second, though I have to do a for loop, so let's say its 1*30 = 30 times. Unless your code is 15x faster than mine, I would argue that using the repeating task method will be far efficient.
  7. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    seems legit xD ill try it out :p
  8. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    Huh?
    [18:22:05] [Server thread/CRITICAL]: Could not execute task Samueljh1\Samcraft\Minigames\Skywars\SWTask: Class Samueljh1\Samcraft\Minigames\Skywars\Task not found

    [18:22:05] [Server thread/CRITICAL]: ClassNotFoundException: "Class Samueljh1\Samcraft\Minigames\Skywars\Task not found" (EXCEPTION) in "/src/spl/BaseClassLoader" at line 144

    EDIT: nvm :)
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Check how SpecialBlock listens to playet movement.
  10. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    how would a block do that
  11. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    It is a plugin… *facepalm*
    TigerHix likes this.
  12. Samueljh1H8sLag
    Offline

    Samueljh1H8sLag Active Member

    Joined:
    Jun 20, 2015
    Posts:
    168
    Minecraft User:
    Samueljh1_
    o xD
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PlayerMoveEvent = PocketMine firing one event upon every player every tick
    Check like every 10 ticks = checking every player per 10 ticks

    Keep in mind that PocketMine is also PHP, and getting things done by PocketMine is in no ways faster than getting them done in the plugin.

Share This Page

Advertisement