Hi I need to retrieve data from an API. Sometimes this takes up to 10 sek. Now i wanna inform the player, that this could take some time. Like so. Any idea how i can make this. Greez Edocsyl
Create a class that does your background work. Code: class someClass implements Runnable{ private yourplugin plugin; private Player player; private somedatatype somedata=null; public yourplugin(yourplugin p, String playername, somedatatype somedata){ plugin=p; // Don't pass an instance of player just pass the name and // get an instance inside this runnable thread player=plugin.getServer().getPlayerExact(playername); // if somedata is type pointer of string, array or another class // then you will have to create a new pointer to this data // if it is simple data types like int or long or... just use it this.somedata=somedata; } //Entry point public void run(){ //put your code that will take awhile here // Then when done just If(player!=null){ player.sendMessage("some message. Done!"); } } } As long as you don't need to call any non-ThreadSafe API calls this is waht you do Put this somewhare in you code that test if you need to do this background stuff Code: if(something==somthing){ Runnable yp=new someClass(plugin, player.getName(), somedata); Thread thread = new Thread(yp); // Start the thread thread.start(); // this runs on a separet thread, so it is indedentent // of the main thread and wont cause a hold up or lag player.sendMessage("This may take some time"); } If you do need some non-ThreadSafe API calls then that is another discussion of how to que them to the main thread. The above code will create a separet Thread to run your background stuff and then ends when done.
you giving him n non thread safe example, plugin.getServer().getPlayerExact(playername); is not thread safe
Yes it is! I use it in this way to an extream amount and never had a problem yet. "Some" of the Player Memeber funtions are not thread safe, such as player.getNearbyEntities(x,y,z); but plugin.getServer().getPlayerExact(playername); in the constructor is safe.
but player.sendMessage("some message. Done!"); is not threadsafe, and thats also is inside the run block EDIT: no problems dont mean its thread safe, thread bugs only occur someetimes on havy load, and not on normal load
the implementation of craftbukkit have an field called "entity" inside CraftPlayer, thats is not thread safe implemented, Player.sendMessage() depends on that not thread safe field, so it may throw an NPE when you try to acces that methode
I dont really think you understand how threadsafe code works... I can create a class which has loads of collections which are accessed by a separate thread, but i can still call a method within that class without worrying about threadsafety so long as it doesnt interact with any of those collections. And infact your incorrect: Code: public void sendRawMessage(String message) { if (getHandle().netServerHandler == null) return; getHandle().netServerHandler.sendPacket(new Packet3Chat(message)); } public void sendMessage(String message) { if (!conversationTracker.isConversingModaly()) { this.sendRawMessage(message); } } Thats all it does.