-
-
Notifications
You must be signed in to change notification settings - Fork 0
GH-30 Add folia scheduler #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f785f79
[WIP] Add folia scheduler basics
vLuckyyy 06f9d55
Fix folia api.
vLuckyyy 041f930
Add entity, region, location. scheduelrs.
vLuckyyy 3f7f81f
Implement bukkit scheduler.
vLuckyyy de86a4d
Add MinecraftScheduler.
Rollczi ba89a83
Fix imports
Rollczi d0a3b24
Remove getPlugin method
Rollczi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
plugins { | ||
`commons-java-17` | ||
`commons-publish` | ||
`commons-repositories` | ||
`commons-java-unit-test` | ||
} | ||
|
||
|
||
dependencies { | ||
api(project(":eternalcode-commons-shared")) | ||
|
||
compileOnlyApi("dev.folia:folia-api:1.20.1-R0.1-SNAPSHOT") | ||
|
||
api("org.jetbrains:annotations:24.1.0") | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} |
87 changes: 87 additions & 0 deletions
87
...mmons-folia/src/main/java/com/eternalcode/commons/folia/scheduler/FoliaSchedulerImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.eternalcode.commons.folia.scheduler; | ||
|
||
import com.eternalcode.commons.scheduler.Scheduler; | ||
import com.eternalcode.commons.scheduler.Task; | ||
import io.papermc.paper.threadedregions.scheduler.AsyncScheduler; | ||
import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler; | ||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
public class FoliaSchedulerImpl implements Scheduler { | ||
|
||
public final Plugin plugin; | ||
private final GlobalRegionScheduler globalRegionScheduler = Bukkit.getServer().getGlobalRegionScheduler(); | ||
private final AsyncScheduler asyncScheduler = Bukkit.getServer().getAsyncScheduler(); | ||
|
||
public FoliaSchedulerImpl(Plugin plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public Task sync(Runnable task) { | ||
return new FoliaTaskImpl(this.globalRegionScheduler.run(plugin, runnable -> task.run())); | ||
} | ||
|
||
@Override | ||
public Task async(Runnable task) { | ||
return new FoliaTaskImpl(this.globalRegionScheduler.run(plugin, runnable -> task.run())); | ||
} | ||
|
||
@Override | ||
public Task laterSync(Runnable task, Duration delay) { | ||
return new FoliaTaskImpl(this.globalRegionScheduler.runDelayed( | ||
plugin, | ||
runnable -> task.run(), | ||
delay.toMillis()) | ||
); | ||
} | ||
|
||
@Override | ||
public Task laterAsync(Runnable task, Duration delay) { | ||
return new FoliaTaskImpl(this.asyncScheduler.runDelayed( | ||
plugin, | ||
runnable -> task.run(), | ||
delay.toMillis(), | ||
TimeUnit.MILLISECONDS) | ||
); | ||
} | ||
|
||
@Override | ||
public Task timerSync(Runnable task, Duration delay, Duration period) { | ||
return new FoliaTaskImpl(this.globalRegionScheduler.runAtFixedRate( | ||
plugin, | ||
runnable -> task.run(), | ||
delay.toMillis(), | ||
period.toMillis()) | ||
); | ||
} | ||
|
||
@Override | ||
public Task timerAsync(Runnable task, Duration delay, Duration period) { | ||
return new FoliaTaskImpl(this.asyncScheduler.runAtFixedRate( | ||
plugin, | ||
runnable -> task.run(), | ||
delay.toMillis(), | ||
period.toMillis(), | ||
TimeUnit.MILLISECONDS) | ||
); | ||
} | ||
|
||
@Override | ||
public <T> CompletableFuture<T> completeSync(Supplier<T> task) { | ||
CompletableFuture<T> completable = new CompletableFuture<>(); | ||
this.globalRegionScheduler.run(plugin, scheduledTask -> completable.complete(task.get())); | ||
return completable; | ||
} | ||
|
||
@Override | ||
public <T> CompletableFuture<T> completeAsync(Supplier<T> task) { | ||
CompletableFuture<T> completable = new CompletableFuture<>(); | ||
this.asyncScheduler.runNow(plugin, scheduledTask -> completable.complete(task.get())); | ||
return completable; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...de-commons-folia/src/main/java/com/eternalcode/commons/folia/scheduler/FoliaTaskImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.eternalcode.commons.folia.scheduler; | ||
|
||
import com.eternalcode.commons.scheduler.Task; | ||
import io.papermc.paper.threadedregions.scheduler.ScheduledTask; | ||
|
||
public class FoliaTaskImpl implements Task { | ||
|
||
private final ScheduledTask rootTask; | ||
|
||
public FoliaTaskImpl(ScheduledTask rootTask) { | ||
this.rootTask = rootTask; | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
this.rootTask.cancel(); | ||
} | ||
|
||
@Override | ||
public boolean isCanceled() { | ||
return this.rootTask.isCancelled(); | ||
} | ||
|
||
@Override | ||
public boolean isAsync() { | ||
return true; | ||
vLuckyyy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.