Skip to content

Commit 318ca67

Browse files
v0.2.5 - Add option to use ticks instead of TimeUnits
1 parent a904902 commit 318ca67

5 files changed

Lines changed: 192 additions & 80 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ buildscript {
77
allprojects {
88
apply plugin: 'java'
99

10-
version = '0.2.4'
10+
version = '0.2.5'
1111

1212
repositories {
1313

common/src/main/java/com/tcoded/folialib/util/TimeConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ public static long toTicks(long time, TimeUnit unit) {
88
return unit.toMillis(time) / 50;
99
}
1010

11+
public static long toMillis(long delay) {
12+
return delay * 50L;
13+
}
1114
}

platform/common/src/main/java/com/tcoded/folialib/impl/ServerImplementation.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public interface ServerImplementation {
3535

3636
// ----- Run Later -----
3737

38+
/**
39+
* Folia: Synced with the server daylight cycle tick
40+
* Paper: Synced with the server main thread
41+
* Spigot: Synced with the server main thread
42+
* @param runnable Task to run
43+
* @param delay Delay before execution in ticks
44+
* @return WrappedTask instance
45+
*/
46+
WrappedTask runLater(Runnable runnable, long delay);
47+
3848
/**
3949
* Folia: Synced with the server daylight cycle tick
4050
* Paper: Synced with the server main thread
@@ -46,6 +56,16 @@ public interface ServerImplementation {
4656
*/
4757
WrappedTask runLater(Runnable runnable, long delay, TimeUnit unit);
4858

59+
/**
60+
* Folia: Async
61+
* Paper: Async
62+
* Spigot: Async
63+
* @param runnable Task to run
64+
* @param delay Delay before execution in ticks
65+
* @return WrappedTask instance
66+
*/
67+
WrappedTask runLaterAsync(Runnable runnable, long delay);
68+
4969
/**
5070
* Folia: Async
5171
* Paper: Async
@@ -59,6 +79,17 @@ public interface ServerImplementation {
5979

6080
// ----- Global Timers -----
6181

82+
/**
83+
* Folia: Synced with the server daylight cycle tick
84+
* Paper: Synced with the server main thread
85+
* Spigot: Synced with the server main thread
86+
* @param runnable Task to run
87+
* @param delay Delay before first execution in ticks
88+
* @param period Delay between executions in ticks
89+
* @return WrappedTask instance
90+
*/
91+
WrappedTask runTimer(Runnable runnable, long delay, long period);
92+
6293
/**
6394
* Folia: Synced with the server daylight cycle tick
6495
* Paper: Synced with the server main thread
@@ -71,6 +102,17 @@ public interface ServerImplementation {
71102
*/
72103
WrappedTask runTimer(Runnable runnable, long delay, long period, TimeUnit unit);
73104

105+
/**
106+
* Folia: Async
107+
* Paper: Async
108+
* Spigot: Async
109+
* @param runnable Task to run
110+
* @param delay Delay before first execution in ticks
111+
* @param period Delay between executions in ticks
112+
* @return WrappedTask instance
113+
*/
114+
WrappedTask runTimerAsync(Runnable runnable, long delay, long period);
115+
74116
/**
75117
* Folia: Async
76118
* Paper: Async
@@ -96,6 +138,17 @@ public interface ServerImplementation {
96138
*/
97139
CompletableFuture<Void> runAtLocation(Location location, Runnable runnable);
98140

141+
/**
142+
* Folia: Synced with the tick of the region of the chunk of the location
143+
* Paper: Synced with the server main thread
144+
* Spigot: Synced with the server main thread
145+
* @param location Location to run the task at
146+
* @param runnable Task to run
147+
* @param delay Delay before execution in ticks
148+
* @return WrappedTask instance
149+
*/
150+
WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay);
151+
99152
/**
100153
* Folia: Synced with the tick of the region of the chunk of the location
101154
* Paper: Synced with the server main thread
@@ -108,6 +161,18 @@ public interface ServerImplementation {
108161
*/
109162
WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay, TimeUnit unit);
110163

164+
/**
165+
* Folia: Synced with the tick of the region of the chunk of the location
166+
* Paper: Synced with the server main thread
167+
* Spigot: Synced with the server main thread
168+
* @param location Location to run the task at
169+
* @param runnable Task to run
170+
* @param delay Delay before first execution in ticks
171+
* @param period Delay between executions in ticks
172+
* @return WrappedTask instance
173+
*/
174+
WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period);
175+
111176
/**
112177
* Folia: Synced with the tick of the region of the chunk of the location
113178
* Paper: Synced with the server main thread
@@ -144,6 +209,17 @@ public interface ServerImplementation {
144209
*/
145210
CompletableFuture<EntityTaskResult> runAtEntityWithFallback(Entity entity, Runnable runnable, Runnable fallback);
146211

212+
/**
213+
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
214+
* Paper: Synced with the server main thread
215+
* Spigot: Synced with the server main thread
216+
* @param entity Entity to run the task at
217+
* @param runnable Task to run
218+
* @param delay Delay before execution in ticks
219+
* @return WrappedTask instance
220+
*/
221+
WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay);
222+
147223
/**
148224
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
149225
* Paper: Synced with the server main thread
@@ -156,6 +232,18 @@ public interface ServerImplementation {
156232
*/
157233
WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay, TimeUnit unit);
158234

235+
/**
236+
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
237+
* Paper: Synced with the server main thread
238+
* Spigot: Synced with the server main thread
239+
* @param entity Entity to run the task at
240+
* @param runnable Task to run
241+
* @param delay Delay before first execution in ticks
242+
* @param period Delay between executions in ticks
243+
* @return WrappedTask instance
244+
*/
245+
WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period);
246+
159247
/**
160248
* Folia: Synced with the tick of the region of the entity (even if the entity moves)
161249
* Paper: Synced with the server main thread

platform/folia/src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,42 +52,51 @@ public CompletableFuture<Void> runAsync(Runnable runnable) {
5252
return future;
5353
}
5454

55+
@Override
56+
public WrappedTask runLater(Runnable runnable, long delay) {
57+
return new WrappedFoliaTask(this.globalRegionScheduler.runDelayed(plugin, task -> runnable.run(), delay));
58+
}
59+
5560
@Override
5661
public WrappedTask runLater(Runnable runnable, long delay, TimeUnit unit) {
62+
return this.runLater(runnable, TimeConverter.toTicks(delay, unit));
63+
}
64+
65+
@Override
66+
public WrappedTask runLaterAsync(Runnable runnable, long delay) {
67+
return this.runLaterAsync(runnable, TimeConverter.toMillis(delay), TimeUnit.MILLISECONDS);
68+
}
69+
70+
@Override
71+
public WrappedTask runLaterAsync(Runnable runnable, long delay, TimeUnit unit) {
5772
return new WrappedFoliaTask(
58-
this.globalRegionScheduler.runDelayed(
59-
plugin, task -> runnable.run(), TimeConverter.toTicks(delay, unit)
60-
)
73+
this.asyncScheduler.runDelayed(plugin, task -> runnable.run(), delay, unit)
6174
);
6275
}
6376

6477
@Override
65-
public WrappedTask runLaterAsync(Runnable runnable, long delay, TimeUnit unit) {
78+
public WrappedTask runTimer(Runnable runnable, long delay, long period) {
6679
return new WrappedFoliaTask(
67-
this.asyncScheduler.runDelayed(
68-
plugin, task -> runnable.run(), delay, unit
69-
)
80+
this.globalRegionScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay, period)
7081
);
7182
}
7283

7384
@Override
7485
public WrappedTask runTimer(Runnable runnable, long delay, long period, TimeUnit unit) {
75-
return new WrappedFoliaTask(
76-
this.globalRegionScheduler.runAtFixedRate(
77-
plugin, task -> runnable.run(),
78-
TimeConverter.toTicks(delay, unit),
79-
TimeConverter.toTicks(period, unit)
80-
)
86+
return this.runTimer(runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit));
87+
}
88+
89+
@Override
90+
public WrappedTask runTimerAsync(Runnable runnable, long delay, long period) {
91+
return this.runTimerAsync(
92+
runnable, TimeConverter.toMillis(delay), TimeConverter.toMillis(period), TimeUnit.MILLISECONDS
8193
);
8294
}
8395

8496
@Override
8597
public WrappedTask runTimerAsync(Runnable runnable, long delay, long period, TimeUnit unit) {
8698
return new WrappedFoliaTask(
87-
this.asyncScheduler.runAtFixedRate(
88-
plugin, task -> runnable.run(),
89-
delay, period, unit
90-
)
99+
this.asyncScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay, period, unit)
91100
);
92101
}
93102

@@ -104,26 +113,29 @@ public CompletableFuture<Void> runAtLocation(Location location, Runnable runnabl
104113
}
105114

106115
@Override
107-
public WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay, TimeUnit unit) {
116+
public WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay) {
108117
return new WrappedFoliaTask(
109-
this.plugin.getServer().getRegionScheduler().runDelayed(
110-
plugin, location, task -> runnable.run(),
111-
TimeConverter.toTicks(delay, unit)
112-
)
118+
this.plugin.getServer().getRegionScheduler().runDelayed(plugin, location, task -> runnable.run(), delay)
113119
);
114120
}
115121

116122
@Override
117-
public WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period, TimeUnit unit) {
123+
public WrappedTask runAtLocationLater(Location location, Runnable runnable, long delay, TimeUnit unit) {
124+
return this.runAtLocationLater(location, runnable, TimeConverter.toTicks(delay, unit));
125+
}
126+
127+
@Override
128+
public WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period) {
118129
return new WrappedFoliaTask(
119-
this.plugin.getServer().getRegionScheduler().runAtFixedRate(
120-
plugin, location, task -> runnable.run(),
121-
TimeConverter.toTicks(delay, unit),
122-
TimeConverter.toTicks(period, unit)
123-
)
130+
this.plugin.getServer().getRegionScheduler().runAtFixedRate(plugin, location, task -> runnable.run(), delay, period)
124131
);
125132
}
126133

134+
@Override
135+
public WrappedTask runAtLocationTimer(Location location, Runnable runnable, long delay, long period, TimeUnit unit) {
136+
return this.runAtLocationTimer(location, runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit));
137+
}
138+
127139
@Override
128140
public CompletableFuture<EntityTaskResult> runAtEntity(Entity entity, Runnable runnable) {
129141
CompletableFuture<EntityTaskResult> future = new CompletableFuture<>();
@@ -159,29 +171,26 @@ public CompletableFuture<EntityTaskResult> runAtEntityWithFallback(Entity entity
159171
return future;
160172
}
161173

174+
@Override
175+
public WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay) {
176+
return new WrappedFoliaTask(entity.getScheduler().runDelayed(plugin, task -> runnable.run(), null, delay));
177+
}
178+
162179
@Override
163180
public WrappedTask runAtEntityLater(Entity entity, Runnable runnable, long delay, TimeUnit unit) {
181+
return this.runAtEntityLater(entity, runnable, TimeConverter.toTicks(delay, unit));
182+
}
183+
184+
@Override
185+
public WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period) {
164186
return new WrappedFoliaTask(
165-
entity.getScheduler().runDelayed(
166-
plugin,
167-
task -> runnable.run(),
168-
null,
169-
TimeConverter.toTicks(delay, unit)
170-
)
187+
entity.getScheduler().runAtFixedRate(plugin, task -> runnable.run(), null, delay, period)
171188
);
172189
}
173190

174191
@Override
175192
public WrappedTask runAtEntityTimer(Entity entity, Runnable runnable, long delay, long period, TimeUnit unit) {
176-
return new WrappedFoliaTask(
177-
entity.getScheduler().runAtFixedRate(
178-
plugin,
179-
task -> runnable.run(),
180-
null,
181-
TimeConverter.toTicks(delay, unit),
182-
TimeConverter.toTicks(period, unit)
183-
)
184-
);
193+
return this.runAtEntityTimer(entity, runnable, TimeConverter.toTicks(delay, unit), TimeConverter.toTicks(period, unit));
185194
}
186195

187196
@Override

0 commit comments

Comments
 (0)