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

Call Back Tasks

Comments in 'Plugin Development' started by xFlare, Mar 11, 2015.

  1. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    I use a lot of CallBack tasks for things that involve timing. I was wondering, is it a bad practice to do so? Many devs have there own opinions on this.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    All your tasks must extend PluginTask. CallbackTask is not a subclass of PluginTask. You must write your own CallbackPluginTask.
  3. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    @PEMapModder is entirely correct. Scheduling in PocketMine is meant to be wholly associated with the Plugin creating the task, this is not via PluginTask objects. It is incredibly bad practice not to use one. I have written dozens of plugins and have only used one CallbackTask. I am writing a library called Fuzzy (https://github.com/Falkirks/FuzzyAPI) which can run even if the Loader plugin is disabled because it is injected directly into other plugins. In order to do this, I had to employ some CallbackTask trickery. In a final release, I will likely remove this in favour of a task which is connected to the plugin in which the API is injected into.
  4. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Any example plugins that uses a class for this? All plugins I see use call back tasks in it's native code.
  5. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    My logic: if it lags, its a bad practice
    PEMapModder likes this.
  6. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Any way to tell how much resources a plugin takes?
  7. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    do timings to see which takes a lot of resources
    xFlare likes this.
  8. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    My logic: If I cringe when I see it, it's bad practice.
    (Sorry, had to do that)
    PEMapModder and xFlare like this.
  9. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    PluginTask will likely be faster as less methods need to be resolved. Depends on what you are doing with it.
    PEMapModder likes this.
  10. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    I'm using it to broadcast messages and do stuff, when a player does something. For example each time a player chats, I use a call back tasks to set when's the next time a player can chat again.
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Your logic only applies when you only use your plugin on your own server.
    You don't even need a schedule for that. Just store the last chat time with microtime(true), then check the time difference using microtime(true) - $lastChat
    As for broadcasting messages using a timer, after your plugin is disabled, your task should be cancelled, so it should be PluginTask.
    The only thing I don't use PluginTask to do is when I modified something inside the server, and I can't run it immediately, and I have to run it anyway. For example, I have to send a message when handling DataPacketReceiveEvent, but I want the message to be sent after the packet is handled, then I have to send it at the next tick. Since I need to send that message anyway, plugin disabled or not, the least resource-consuming way is not to use PluginTask.
    What about Task vs PluginTask? And if only calculating runtime, (i.e. if the anonymous function is only created in onEnable(), which I don't call it runtime because most of it is compile time actually) Task vs CallbackTask?

    PluginTask should be slower than Task by a few nanoseconds per tick maybe, where most likely less than 0.1% even you have many schedules. (The only difference should be an isEnabled() check)
    As for the callback function thing,
    1. Depends: is it a namespace function? Or a static class function? Or an instance method? Or an anonymous function?
    2. Maybe less memory (possibly one or two kilobytes, i.e. negligible) used because of one less class declared? (I don't count the compiling of the file because I don't count the runtime, and I don't count the larger plugin size with one more file because nobody is having the plugins directory on a floppy disk)
    xFlare likes this.
  12. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    are you sure? i also use this logic on my public plugins too.
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Then you are not entirely correct. Being compatible is more important than being fast.
  14. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    so whats the ressemblance with my statement here:
    with this one?
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    As a public plugin, it better be compatible.
  16. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Code:
    $stack = array("happy", "map");
    
    array_push($stack, "apple", "raspberry");
    
    print_r($stack);
    
    if (in_array("happy", $stack)) {
        echo "- Object in array has been found. Horray!";
    }
    This is sort of out-of-topic but would this be better then saving in a text file?
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You mean a config? You don't need a config if you don't want to save things. Config is just a convenient class for saving data. If you just want in-memory data handling, don't use a config.

    Actually, in all my recent plugins, I never use the Config class except for $this->getConfig(). If you know enough, it is actually not good to use config for data saving.
    iJoshuaHD likes this.
  18. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    just when did my public plugins became incompatible? they all do work besides the third party needed things to edit to make the functionality work like ASR.
    i agree. you better make your own database for that.
    PEMapModder likes this.
  19. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Just an exaggeration.

Share This Page

Advertisement