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

AsyncTask

Comments in 'Plugin Development' started by Samueljh1, Mar 23, 2015.

  1. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    Hi Guys, i tried this:

    PHP:
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new CallbackTask(array($this"rank")), 1200);
    Which works fine, but when the function "rank" is executed in the CallBackTask, the server times out because the thread has paused - because there was too much processing time. I know why, its because of this:

    PHP:
    foreach(Server::getInstance()->getOnlinePlayers() as $donor){
    it works sometimes , but when there are a lot of players , it doesnt.

    Now i know AsyncTask runs a separate thread, so that will prevent it.

    I looked on the docs and i saw this in the description of AsyncTask:

    Code:
    Detailed Description
    Class used to run async tasks in other threads.
    
    WARNING: Do not call PocketMine-MP API methods, or save objects from/on other Threads!!
    
    So how will i do this? Does that matter ^^

    also, could you please include an example of how to do this?

    Like:

    PHP:
    $this->getServer()->getScheduler()->scheduleRepeatingTask(newAsyncTask(/*whatgoeshere*/));
    Thanks :D
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    What do you want the task to do?
    iksaku likes this.
  3. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    i suggest you do some kind of this:
    foreach player as target
    asynctask stuff of target
  4. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    nice idea! Unfortunately what it does involves the PocketMine api... and AsyncTask will only work properly if it doesn't use the api. How would I do this?
  5. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    its my rank system. It uses file_get_contents() . It works by:

    1: On my server app, after the in app purchase is completed:
    2: Http:get is sent (with url vaiables) eg: ?rank=<>&usr=<>
    3: The "usr" variable is then written to a text file, depending what "rank" is. Eg: Vip.txt,vipplus.txt .
    4:Last step: Checking if that player name is in either of the text files.

    what I have tried: OnJoin Check => laggy (lots of players would join very closely together)

    the example above was another attempt.

    Either, I can use a more efficient method (which I cant think of right now) or use an asyncTask. :)

    I was thinking about exploding the text files (to an array) and then foreaching them, but that would eventually stop working when more people donate...
  6. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    if you want to read these text files on AsyncTask, just google the long method that doesnt envolves PocketMine APIs.
    inside onRun(), you do these stuffs here
    inside onCompletion(), you gather the result inside onRun() and do make PocketMine API calls.
    iksaku likes this.
  7. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    Rather than search like that I would create a file "[a].txt" where "a" is the first letter of the user. Then I would write in there the user name and the rank.

    That way instead of having to look at all files to find the rank, you can just jump to the right letter.

    You could in theory write to a file called "username".txt that would just contain the rank of that user. Under Linux that would work fine. Under Windows that would be problematic once you have lots of users.

    You should read a little bit about hash tables. This is a classic problem that is resolved with hash tables.
    iJoshuaHD likes this.
  8. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    I would store the file data in arrays with the keys and values being the usernames (for faster lookup). Then whenever a check was needed, the file modification times would be checked to make sure that the current arrays were up to date.
  9. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    no, its one big text file- each username is separated by "<br>" or "\n". there are two files: vip and vipplus
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Just to remind you, everyone can decompile your app and HTTP-GET that link with their browsers or cURL too. Hope you remembered to prevent it.

    Also, why don't you use a MySQL database instead? It may be faster/more convenient.
    CrazedMiner and iJoshuaHD like this.
  11. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    I have prevented it :) and would mysql really make that much of a difference? Also: I am a complete noob at setting up mysql... The panel is just confusing xD
  12. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    mysql make things easier if things you're doing is done remotely.
    mysql server doesnt have a CP GUI for you to play with. It depends on what MySQL database manager software you are using that makes it "confusing" for you. Try PHPMyAdmin.
  13. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    Thats what I use xD
  14. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    pocketmine is more confusing than using PHPMyAdmin for me :p
    well technically I use Adminer instead of it because its more lightweight and doesnt need crap loads of file. Its only a one file PHP script that you load into your web server :)
    PEMapModder likes this.
  15. Samueljh1
    Offline

    Samueljh1 Active Member Plugin Developer

    Joined:
    Jan 9, 2015
    Posts:
    316
    Plugins:
    5
    Minecraft User:
    Samueljh1_
    Thanks :) I'll check it out
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Command line is the best for me. Anything that requires a mouse or has a GUI (including MCPE) is confusing to me. Only command line is sensible to me xD
    iJoshuaHD likes this.

Share This Page

Advertisement