Guest User

Untitled

a guest
May 20th, 2015
174
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class TaskHandler {
  2.  
  3. //Static Objects
  4. private static TaskHandler instance;
  5.  
  6. //Static Methods
  7. public static TaskHandler getInstance() {
  8. if (instance == null) instance = new TaskHandler();
  9. return instance;
  10. }
  11.  
  12. //Objects
  13. private ArrayList<Task> taskList;
  14. private ArrayList<Task> addList;
  15. private ArrayList<Task> removeList;
  16.  
  17. private int taskID;
  18. private boolean update;
  19.  
  20. //Constructor
  21. public TaskHandler() {
  22. taskList = new ArrayList<Task>();
  23. addList = new ArrayList<Task>();
  24. removeList = new ArrayList<Task>();
  25.  
  26. taskID = 0;
  27. update = false;
  28.  
  29. initTask();
  30. }
  31.  
  32. //Object Methods
  33. private void initTask() {
  34. taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Task.plugin, new Runnable() {
  35. @Override
  36. public void run() {
  37. if (update) {
  38. for (Task task: addList) {
  39. taskList.add(task);
  40. }
  41. addList.clear();
  42.  
  43. for (Task task: removeList) {
  44. taskList.remove(task);
  45. }
  46. removeList.clear();
  47.  
  48. update = false;
  49. }
  50.  
  51. for (Task task: taskList) {
  52. task.updateTicks();
  53. }
  54. }
  55. }, 0, 1);
  56. }
  57.  
  58. private void cancelTask() {
  59. Bukkit.getScheduler().cancelTask(taskID);
  60. }
  61.  
  62. public void addTask(Task task) {
  63. addList.add(task);
  64. update = true;
  65. }
  66.  
  67. public void removeTask(Task task) {
  68. if (!taskList.contains(task)) return;
  69. removeList.add(task);
  70. update = true;
  71. }
  72. }
RAW Paste Data