Cancel a BukkitRunnable when a event is called

Discussion in 'Spigot Plugin Help' started by raptorf472, Mar 5, 2020.

  1. I'm currently making a plugin that add a type of potion that can help people fly. But i'm having trouble with this:
    When i drink my potion, it gives me 2 minutes to fly. After 1 minutes, i drank another potion but after 1 minute it disabled flight when it supposed to give me 2 more minutes. Can anyone help me? Any help is appreciate :D
  2. If you are not stacking the remaining time. Which means when you drink the second potion after 1 minute, your time would be 2 minutes again. That would be easier.

    Just need a global hashMap to store player's task.
    hashMap<UUID, BukkitTask>.

    When the player drink a new potion, set the task to a new delayed task which disable his flight.

    If you want it to be stackable, it would be a bit harder since I don't know how to get a BukkitTask's remaining time. You could try to figure it out , it would be a lot easier. My suggestion is to make a hashmap to store players potion remaining ticks. Like HashMap<UUID, long>. Then use a Bukkit timer task to reduce it every seconds.

    I hope it helps
  3. I'll try it, tks btw :D
  4. Sure, you are welcome.
  5. if i use a repeating task, will it lag the server?
  6. Good question.
    It would lag the server if too many sync task are running at the same time.
    Do it with runTaskTimerAsynchronously. Then it won't cause tps problem.
  7. Wrong section, please move this thread to here.
  8. After several days, it finally worked :D. Thank you very much
  9. How to move it?
  10. Nah don't worry about it, you can't really move it apart from deleting and re-creating. It's just to get you to be reminded of where to put your threads next time.
    • Agree Agree x 1
  11. Strahan


    I'd set it up with a Map holding the max flight time then when they drink a potion, if the map doesn't contain the player's UUID it inserts the duration for that potion into the map and starts a runnable. If they are in the map, it increases the max flight time by the appropriate amount. Then I'd have the runnable fire every 20 ticks and increment a counter in there every loop. You then have it check if counter >= max flight time and if so, kill flight and remove player from the map then cancel task. I'd also be kind enough to code in a check so when they have 10 seconds of time left it'd send a warning that they are about to plummet from the sky heh.

    PS yes, I know counting seconds based on ticking 20 isn't the most accurate way to check time but for this I wouldn't care, it'd be accurate enough for that.