TrackedEntity.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package com.gmail.nossr50.skills.archery;
  2. import org.bukkit.Bukkit;
  3. import org.bukkit.entity.LivingEntity;
  4. import com.gmail.nossr50.mcMMO;
  5. public class TrackedEntity implements Runnable {
  6. private LivingEntity livingEntity;
  7. private int arrowCount;
  8. private int previousTicksLived;
  9. private int taskId;
  10. protected TrackedEntity(LivingEntity livingEntity) {
  11. this.livingEntity = livingEntity;
  12. taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(mcMMO.p, this, 12000, 12000);
  13. }
  14. //LivingEntity.isDead() isn't a reliable way to know if an entity is still active
  15. //This method must not be called more than once per server tick
  16. private boolean isActive() {
  17. int currentTicksLived = livingEntity.getTicksLived();
  18. if (currentTicksLived == previousTicksLived) {
  19. return false;
  20. }
  21. previousTicksLived = currentTicksLived;
  22. return true;
  23. }
  24. protected LivingEntity getLivingEntity() {
  25. return livingEntity;
  26. }
  27. protected int getArrowCount() {
  28. return arrowCount;
  29. }
  30. protected void incrementArrowCount() {
  31. arrowCount++;
  32. }
  33. @Override
  34. public void run() {
  35. if (!isActive()) {
  36. Archery.removeFromTracker(this);
  37. Bukkit.getScheduler().cancelTask(taskId);
  38. }
  39. }
  40. }