RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
Task.java
1package com.osroyale.game.task;
2
3import java.util.Optional;
4import java.util.UUID;
5
44
45public abstract class Task {
46
48 private final boolean instant;
49
51 private int delay;
52
54 private boolean running;
55
57 private int counter;
58
60 private Optional<Object> key = Optional.empty();
61
63 private StackTraceElement[] creationStackTrace;
64
66 private Optional<Long> runStartTime = Optional.empty();
67
69 private final UUID uuid;
70
72 private final long taskCreationTime;
73
75 public Task(boolean instant, int delay) {
76 if (delay <= 0) {
77 instant = true;
78 delay = 1;
79 }
80 this.instant = instant;
81 this.delay = delay;
82 this.creationStackTrace = Thread.currentThread().getStackTrace();
83 this.uuid = UUID.randomUUID();
84 this.taskCreationTime = System.currentTimeMillis();
85 }
86
87 public String getTaskId() {
88 return uuid.toString();
89 }
90
92 public Task(int delay) {
93 this(false, delay);
94 }
95
96 public void setExecutionTime() {
97 runStartTime = Optional.of(System.currentTimeMillis());
98 }
99
100 public long getTaskCreationTime() {
101 return taskCreationTime;
102 }
103
104 public Optional<Long> getRunStartTime() {
105 return runStartTime;
106 }
107
108 public Long getElapsedTimeFromRunStartTime() {
109 if (runStartTime.isPresent()) {
110 return System.currentTimeMillis() - runStartTime.get();
111 }
112 return null;
113 }
114
115 public String getCreationStackTraceStr() {
116 StringBuilder sb = new StringBuilder();
117 for (StackTraceElement ste : creationStackTrace) {
118 sb.append(ste.toString()).append("\n");
119 }
120 return sb.toString();
121 }
122
123 final synchronized void process() {
124 if (++counter >= delay && running) {
125 baseExecute();
126 counter = 0;
127 }
128 }
129
130 /***
131 * Executes the abstract execute method ensuring any methods are called before and after
132 */
133 protected void baseExecute() {
134 setExecutionTime();
135 execute();
136 }
137
139 protected abstract void execute();
140
142 public boolean canRun() {
143 return true;
144 }
145
147 public synchronized final void cancel() {
148 if (running) {
149 onCancel(false);
150 running = false;
151 }
152 }
153
155 public synchronized final void cancel(boolean logout) {
156 if (running) {
157 onCancel(logout);
158 running = false;
159 }
160 }
161
163 void onLoop() { }
164
166 protected void onSchedule() { }
167
169 protected boolean canSchedule() {
170 return true;
171 }
172
174 protected void onCancel(boolean logout) { }
175
177 void onException(Exception e) {
178
179 }
180
182 public Task attach(Object newKey) {
183 key = Optional.ofNullable(newKey);
184 return this;
185 }
186
188 public boolean isInstant() {
189 return instant;
190 }
191
193 public int getDelay() {
194 return delay;
195 }
196
198 public void setDelay(int delay) {
199 this.delay = delay;
200 }
201
203 public boolean isRunning() {
204 return running;
205 }
206
208 public Optional<Object> getAttachment() {
209 return key;
210 }
211
212 synchronized void setRunning(boolean running) {
213 this.running = running;
214 }
215}
synchronized final void cancel()
Definition Task.java:147
synchronized final void cancel(boolean logout)
Definition Task.java:155
void onCancel(boolean logout)
Definition Task.java:174
void setDelay(int delay)
Definition Task.java:198
Task attach(Object newKey)
Definition Task.java:182
Task(boolean instant, int delay)
Definition Task.java:75
Optional< Object > getAttachment()
Definition Task.java:208