Despawn an entity after time

Discussion in 'Plugin Development' started by HeavyMine13, May 31, 2014.

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

    HeavyMine13

    Hey! I have a kit that spawns a sheep for each player when activated. I want that sheep despawn after 40 ticks after it was spawned. How can i do it?
     
  2. Offline

    TheGamblingMan

  3. Offline

    HeavyMine13

    No worried I tried that. The problem it is removing all of spawned sheep..... I'm not a bukkit noob no worries...
     
  4. Offline

    fireblast709

    If you tried it you wouldn't have asked your question... Unless the actual question differs from the initial post.
     
  5. Offline

    HeavyMine13

    If i do *name* = Bukkit.getWorld(........
    and then another one of those sheep active(Spawn) before the 40 ticks it gets over ridden, so I want a unique id or something for each one.
     
  6. Offline

    fireblast709

    HeavyMine13 Just schedule a task for each one...
    More advanced version (open)
    or one big task with a WeakHashMap<Sheep, Long> which removes Sheep as soon as the long value < System.currentTimeMillis()
    Code:java
    1. @Override
    2. public void run()
    3. {
    4. Iterator<Map.Entry<Sheep, Long>> it = map.entrySet().iterator();
    5. Map.Entry<Sheep, Long> entry;
    6. final long now = System.currentTimeMillis();
    7. while(it.hasNext())
    8. {
    9. entry = it.next();
    10. if(entry.getValue() < now)
    11. {
    12. entry.getKey().remove();
    13. it.remove();
    14. }
    15. }
    16. }
     
Thread Status:
Not open for further replies.

Share This Page