Executing a code after the player interacted with a block after x seconds

Discussion in 'Plugin Development' started by Debels, Jun 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Debels

    I'm trying to execute a piece of code after the player interacted with a block after a defined amount of seconds, my problem isn't executing the code after the time passed, but to check i the player was interacting with that block all the x seconds.

    My current code is basically a hashmap that stores the player name, endtime. and on playerinteract event make it check for right_click_block, then add it to the hashmap and each time it triggers the event check if the time passed, but the problem with this is that lets say the wait is 5 seconds, the player can simply interact with the block walk away do something else then interact with the block again and it works as if you where interacting with it the whole time.

    The solution I have in mind isn't the best since if the server lags it will make the user interact with the block again to achieve it. Which is if the time in the hashmap is lets say 0.25 lower than the current one take it as he stopped interacting, but that could also be "exploited" per say.

    What would you do in this case?,
    Thanks for reading,
    Looking forward to your reply.
     
  2. Offline

    Os_Reboot

    It depends on what action you are checking for.
    Unfortunately, Action.LEFT_CLICK_BLOCK only triggers when a player starts mining a block, so if you are checking for that you would have to do something like set a timer that checks if their target block remains the same for a period of time. There would be no way to be sure the player is still attempting to mine the block after a period of time.
    If you are testing for Action.RIGHT_CLICK_BLOCK, things get a little more complicated. What I have done in one of my plugins is make a sort of 'cooldown' period to account for things like lag. I made a hashmap that would keep track of a player and an integer. When the player right clicked the block it would add a value of 2 to the hashmap. Then, in a separate repeating timer, I would subtract 1 as long as the value is greater than 0, regardless if they interacted with the block in that tick. This way, if the player suddenly stopped interacting for a single tick it would only increase the time they would have to interact by a tick as opposed to reset it.
    Not too sure if this is the solution you are after, but I thought I'd contribute [cake]
     
Thread Status:
Not open for further replies.

Share This Page