1.15.2 Delayed Task, but only if the chunks loaded

Discussion in 'Spigot Plugin Development' started by hidden1nin, Mar 29, 2020.

  1. I want to despawn an armorstand after 5 minutes, but if the chunk its in isnt loaded then the timer doesn't count down, how can i do this?
  2. You could just check if the chunk is loaded once the 5minutes have passed and if not, just ignore it. Otherwise you could listen on the chunk unload event and cancel the timer that way.
  3. @kumpelblase2 I think the idea that is to be accomplished here, is to only decrease the "left-over" time while the chunk is loaded, not check after 5 minutes if it's loaded at that time.

    This takes a few extra steps, but shouldn't be too much of a problem if you don't require persistence:
    1) Create two maps that can hold armor stands as keys and their tasks as values (you could name the maps something like active... / idle...)
    2) Whenever one of your "special" armor stands is created, add it to the active map together with their created task that updates it
    3) Listen to chunk unloading and iterate through your active map and see if one of the armor stands is inside of it, if so remove it from the active map and add it to the idle map (eventually stopping the task, depending on your implementation).
    4) Listen to chunk loading and iterate through your idle map and see if one of the armor stands is indie of it, if so remove it from the idle map and add it back to the active map (eventually creating a new task, depending on your implementation).

    Of course the logic inside your tasks is important on how exactly you would go onto this, but you should get the idea :)
  4. Notice that if using the first way, the only method to check if a chunk is loaded without loading the chunk (afaik) is World#isChunkLoaded