Okay so let me show you two snippets of code: Code: public void sendMessageToArena(String string) { for(String name : this.playerCache) Bukkit.getPlayerExact(name).sendMessage(string); } Code: sendMessageToArena("blah"); this.playerCache.add(player); The player is not supposed to receive the message, "blah." However, sometimes the player receives it. Now I'm guessing it's because chat runs on a separate thread. How do I stop this from happening in a safe manner?
The reason for this is it's calculating every playerName in playerCache which will take a really really short amount of time (in Milliseconds). So the "this.playerCache.add(player);" will be run before all of the players have been sent the message. You could add a delay using the scheduleDelayedTask(JavaPlugin plugin, Runnable runnable, long delayTime); So you could have: Code: {plugin instance}.getServer().getScheduler().scheduleDelayedTask({plugin instance}, new Runnable() { public void run() { playerCache.add(player); } }, 30L); What that'll do is add the player to playerCache after 1.5 seconds.
KingFaris10 I'm not saying this is wrong, as it makes sense. But would it be better to just delay the chat on its own thread?
Not really, it's just a normal programmers way + Bukkit's prefered method of coding but you should never make new threads/delay events like that.