Adding a delayed task to a plugin
We will now add a warp delay to our Warper
plugin. This will require players to stand still after running the warp or home commands. If they move too much then the warp task will be canceled and they will not be teleported. This will prevent players from teleporting when someone is attacking them or they are falling to their death.
If you haven't already, add a variable of warpDelay
within your main class. This is given in the following line of code:
static int warpDelay = 5;
This time will be in seconds. We will multiply it by 20 to calculate the number of ticks that we wish to delay the task.
We will also need to keep track of who is in the process of warping so that we can check if they move. Add another variable of current warpers. This will be a
HashMap
so that we can keep track of which players are warping and the tasks that will be run to teleport them. That way, if a specific player moves, we can get their task and cancel it. This is shown in the following...