-
-
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 4 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
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() | ||
} |
181 changes: 181 additions & 0 deletions
181
...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,181 @@ | ||
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 io.papermc.paper.threadedregions.scheduler.RegionScheduler; | ||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
import org.bukkit.Location; | ||
import org.bukkit.Server; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
public class FoliaSchedulerImpl implements Scheduler { | ||
|
||
public final Plugin plugin; | ||
private final Server server; | ||
|
||
private final GlobalRegionScheduler globalRegionScheduler; | ||
private final AsyncScheduler asyncScheduler; | ||
private final RegionScheduler regionScheduler; | ||
|
||
public FoliaSchedulerImpl(Plugin plugin) { | ||
this.plugin = plugin; | ||
this.server = this.plugin.getServer(); | ||
|
||
this.globalRegionScheduler = this.server.getGlobalRegionScheduler(); | ||
this.asyncScheduler = this.server.getAsyncScheduler(); | ||
this.regionScheduler = this.server.getRegionScheduler(); | ||
} | ||
|
||
@Override | ||
public boolean isGlobal() { | ||
return this.server.isGlobalTickThread(); | ||
} | ||
|
||
@Override | ||
public boolean isTick() { | ||
return this.server.isPrimaryThread(); | ||
} | ||
|
||
@Override | ||
public boolean isEntity(Entity entity) { | ||
return this.server.isOwnedByCurrentRegion(entity); | ||
} | ||
|
||
@Override | ||
public boolean isRegion(Location location) { | ||
return this.server.isOwnedByCurrentRegion(location); | ||
} | ||
|
||
@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.asyncScheduler.runNow(plugin, runnable -> task.run())); | ||
} | ||
|
||
@Override | ||
public Task async(Location location, Runnable task) { | ||
return new FoliaTaskImpl(this.regionScheduler.run(plugin, location, runnable -> task.run())); | ||
} | ||
|
||
@Override | ||
public Task async(Entity entity, Runnable task) { | ||
return new FoliaTaskImpl(entity.getScheduler().run(plugin, runnable -> task.run(), null)); | ||
} | ||
|
||
@Override | ||
public Task laterSync(Runnable task, Duration delay) { | ||
if (delay.isZero()) { | ||
delay = Duration.ofMillis(1); | ||
} | ||
|
||
return new FoliaTaskImpl(this.globalRegionScheduler.runDelayed( | ||
plugin, | ||
runnable -> task.run(), | ||
delay.toMillis()) | ||
); | ||
} | ||
|
||
@Override | ||
public Task laterAsync(Runnable task, Duration delay) { | ||
return new FoliaTaskImpl(this.globalRegionScheduler.runDelayed( | ||
plugin, | ||
runnable -> task.run(), | ||
delay.toMillis()) | ||
); | ||
} | ||
|
||
@Override | ||
public Task laterAsync(Location location, Runnable task, Duration delay) { | ||
return new FoliaTaskImpl(this.regionScheduler.runDelayed( | ||
plugin, | ||
location, | ||
runnable -> task.run(), | ||
delay.toMillis()) | ||
); | ||
} | ||
|
||
@Override | ||
public Task laterAsync(Entity entity, Runnable task, Duration delay) { | ||
return new FoliaTaskImpl(entity.getScheduler().runDelayed( | ||
plugin, | ||
runnable -> task.run(), | ||
null, | ||
delay.toMillis()) | ||
); | ||
} | ||
|
||
@Override | ||
public Task timerSync(Runnable task, Duration delay, Duration period) { | ||
if (delay.isZero()) { | ||
delay = Duration.ofMillis(1); | ||
} | ||
|
||
if (period.isZero()) { | ||
period = Duration.ofMillis(1); | ||
} | ||
|
||
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 Task timerAsync(Location location, Runnable task, Duration delay, Duration period) { | ||
return new FoliaTaskImpl(this.regionScheduler.runAtFixedRate( | ||
plugin, | ||
location, | ||
runnable -> task.run(), | ||
delay.toMillis(), | ||
period.toMillis()) | ||
); | ||
} | ||
|
||
@Override | ||
public Task timerAsync(Entity entity, Runnable task, Duration delay, Duration period) { | ||
return new FoliaTaskImpl(entity.getScheduler().runAtFixedRate( | ||
plugin, | ||
runnable -> task.run(), | ||
null, | ||
delay.toMillis(), | ||
period.toMillis()) | ||
); | ||
} | ||
|
||
@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; | ||
} | ||
} |
Oops, something went wrong.
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.