when i write something into MySQL it is easy do it async but how to do this when i want to get something from mysql? e.g. on PreLoginEvent i must check player's rank and if server is full and player hasn't any rank, kick player from server. but how?
SELECT whatever FROM table WHERE name="whatevername" For example. You should tell us your table construction
Threading isn't hard. How to handle threading properly is the thing that requires more cerebrum activity. The server full problem is an example. In the LegionPE-Theta-Base plugin, I store the rank of each user in a database. If the player was not registered, or if the player is a VIP, he can bypass the server full limit. (Look at LoginQuery.php and BaseListener.php in specific) So, how to do this? I must not cancel the event. Otherwise, the player will be kicked all the time. In iMCPE plugin, @iJoshuaHD made it such that player data are cached, so the first time player joins at server full he gets kicked, but the async query is dispatched, and the result is cached, so the second time he rejoins, the plugin knows that he is VIP from the cache, and lets him join. But this is still not the best solution. So, why not just let the player join anyway? Several ticks later, the AsyncTask got a result, and if the plugin realizes that the player is in fact normal and the server player count exceeds maximum, kick the player. Indeed, the player joined for a few ticks. But that is the method of the lowest cost I can think of.
Static = thread-local. A static property cannot be shared among different threads. You should create a MySQLi instance for each thread. Look at HereAuth for example how.
I use that, and due to the tiny delay, It looks to the client as if the event was cancelled. Also because it takes time to join the server, the player would most likely be kicked before there is a chance for it to spawn.
Static methods aren't a problem; they are just like namespace functions. However pthreads understands static class properties as thread local, so it won't work.
These two are not relevant. No matter what your server max player limit is, you still have to run the same amount of queries because the same amount of join attempts will be there. As long as your queries do not hang the async workers for too long (like shorter than the average period between two player joins), it is not really a problem. My suggestion was, just let players join. Only kick them after you have confirmed that they don't have the permission to stay. It is not hard at all if you do things properly.
thx i solved it task sometimes creates an infinite loop so worker can not run other tasks. and why i have only 2 workers if i set worker amount to 4? and i have 4 core CPU