Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.craftbukkit.scheduler;
2
3import org.bukkit.plugin.Plugin;
4import org.bukkit.scheduler.BukkitTask;
5
6public class CraftTask implements Comparable<Object>, BukkitTask {
7
8 private final Runnable task;
9 private final boolean syncTask;
10 private long executionTick;
11 private final long period;
12 private final Plugin owner;
13 private final int idNumber;
14
15 private static Integer idCounter = 1;
16 private static Object idCounterSync = new Object();
17
18 CraftTask(Plugin owner, Runnable task, boolean syncTask) {
19 this(owner, task, syncTask, -1, -1);
20 }
21
22 CraftTask(Plugin owner, Runnable task, boolean syncTask, long executionTick) {
23 this(owner, task, syncTask, executionTick, -1);
24 }
25
26 CraftTask(Plugin owner, Runnable task, boolean syncTask, long executionTick, long period) {
27 this.task = task;
28 this.syncTask = syncTask;
29 this.executionTick = executionTick;
30 this.period = period;
31 this.owner = owner;
32 this.idNumber = CraftTask.getNextId();
33 }
34
35 static int getNextId() {
36 synchronized (idCounterSync) {
37 idCounter++;
38 return idCounter;
39 }
40 }
41
42 Runnable getTask() {
43 return task;
44 }
45
46 public boolean isSync() {
47 return syncTask;
48 }
49
50 long getExecutionTick() {
51 return executionTick;
52 }
53
54 long getPeriod() {
55 return period;
56 }
57
58 public Plugin getOwner() {
59 return owner;
60 }
61
62 void updateExecution() {
63 executionTick += period;
64 }
65
66 public int getTaskId() {
67 return getIdNumber();
68 }
69
70 int getIdNumber() {
71 return idNumber;
72 }
73
74 public int compareTo(Object other) {
75 if (!(other instanceof CraftTask)) {
76 return 0;
77 } else {
78 CraftTask o = (CraftTask) other;
79 long timeDiff = executionTick - o.getExecutionTick();
80 if (timeDiff > 0) {
81 return 1;
82 } else if (timeDiff < 0) {
83 return -1;
84 } else {
85 CraftTask otherCraftTask = (CraftTask) other;
86 return getIdNumber() - otherCraftTask.getIdNumber();
87 }
88 }
89 }
90
91 @Override
92 public boolean equals(Object other) {
93
94 if (other == null) {
95 return false;
96 }
97
98 if (!(other instanceof CraftTask)) {
99 return false;
100 }
101
102 CraftTask otherCraftTask = (CraftTask) other;
103 return otherCraftTask.getIdNumber() == getIdNumber();
104 }
105
106 @Override
107 public int hashCode() {
108 return getIdNumber();
109 }
110}