Resource [Cheat Sheet] Delays after certain events

Discussion in 'Spigot Plugin Development' started by sbinder93, May 26, 2016.

  1. Hey there,
    I'd like to create a small "cheat-sheet" about delays for applying methods on a Player, because I think it happens way too much in Spigot plugin development that you need to add a delay in certain situations.

    I selected a couple of events that should specify the different situations and added some methods for the Player. I already added some delays, that I've been using and I think they are safe. But what can you add to this list? What do you think is a "safe" delay (maybe my delays are too high) or where do you think you do not need a delay at all? Any experience with that in your plugin? Just tell the others here on this thread and I'm going to update the table below as often as possible. If you have any other methods you think that should be delayed, then I'll add them as well.

    Cheers!


    Code (Text):

    ################# PlayerChangedWorldEvent ## PlayerJoinEvent        ## PlayerJoinEvent        ## PlayerRespawnEvent     ## PlayerTeleportEvent    ##
    #################                         ## hasPlayedBefore()      ## !hasPlayedBefore()     ##                        ##                        ##
    ####################################################################################################################################################
    setGameMode()  ##                         ## no delay needed        ## 3 ticks                ## no delay needed        ##                        ##
    teleport()     ##                         ## no delay needed        ## 3 ticks                ## setRespawnLocation()   ##                        ##
    hide()/show()  ##                         ## no delay needed        ## 3 ticks                ## no delay needed        ##                        ##
                   ##                         ##                        ##                        ##                        ##                        ##
                   ##                         ##                        ##                        ##                        ##                        ##
                   ##                         ##                        ##                        ##                        ##                        ##
                   ##                         ##                        ##                        ##                        ##                        ##
     
     
    #1 sbinder93, May 26, 2016
    Last edited: May 27, 2016
    • Useful Useful x 3
  2. Sorry I don't really understand what your trying to do here
     
  3. There are some actions, that when perfomred on a player, will not work, because they are happing too close to some event. Here he tells for example, that in a PlayerJoinEvent, you would need to cerate a runnable, with runTaskLater of 5 ticks, to perform actions like: setGameMode
     
  4. Looks great, took me so long to find out why .hide(); didn't work back then, delaying it 20 ticks indeed helped. But now I see 5 is enough, thanks :)

    About the teleportation, I have never had problems teleporting someone with PlayerJoinEvent without delay?

    EDIT
    Same for setGameMode()
     
  5. If they are joining for the first time, you will need a delay. Otherwise you dont.
     
    • Like Like x 2
  6. 1 tick should be enougth, shouldn't it? Because Minecraft is a tick-based game, adding/teleporting/world-switching a player should all happen within one tick.
    I think that there should be pre/post events, so that you can apply your stuff without waiting.
    Maybe you could use the EventType.MONITOR for this.
     
    • Agree Agree x 1
  7. Serializator

    Supporter

    This is what I thought, but not sure if it's true tho.
     
  8. I think it also has to do something with the state of the client. Not sure though.

    That won't work.
     
  9. AFAIK 1 tick should be fine, but in production environments this often doesn't work for my plugins :/ That's why I opened this thread. Just a little bit of sharing experience to each other and then completing the table in my first post to make sure everyone knows about "best-practice" delays.
     
  10. 1-2 ticks should be all you need for delays.
     
  11. Is there a good way of finding out, if a player joins the first time?
     
  12. Serializator

    Supporter

    I think the method Player#hasPlayedBefore() or something exists.
     
    • Agree Agree x 2
    • Like Like x 1
    • Winner Winner x 1
  13. Tip: you could put the "resource" prefix on this thread :p

    I recommend a 3 ticks seconds delay using teleport() on PlayerRespawnEvent; Because sometimes don't work!
     
  14. Okay. So right know it seems like that a delay needs to be added to all methods if Player#hasPlayedBefore() is false. If it's true, you can instantly apply the methods on the player. Is this right? Can anyone approve this? I might check on this this weekend myself.

    I'd use PlayerRespawnEvent#setRespawnLocation() there…



    What about those PlayerChangedWorld and PlayerTeleport events? As you know, the client needs to load the new world and therefore that loading screen is shown for a short period of time. Any experiences? Is it safe to apply methods immediately or do you need to wait until the new world has loaded?
     
    #14 sbinder93, May 27, 2016
    Last edited: May 27, 2016
  15. I usually wait 1 tick for these events, but the most of time the methods works on these events... Can happen the client experience some lag... But the method will work