-
Notifications
You must be signed in to change notification settings - Fork 7
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
add TaskId? #5
Draft
Anon8281
wants to merge
1
commit into
main
Choose a base branch
from
dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
add TaskId? #5
Changes from all commits
Commits
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 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 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 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 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 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 |
---|---|---|
|
@@ -10,12 +10,17 @@ | |
import org.bukkit.entity.Entity; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.util.HashMap; | ||
import java.util.Random; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class FoliaScheduler implements TaskScheduler { | ||
|
||
final Plugin plugin; | ||
|
||
static final ConcurrentHashMap<Integer, MyScheduledTask> tasks = new ConcurrentHashMap<>(); | ||
|
||
public FoliaScheduler(Plugin plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
@@ -46,7 +51,7 @@ public boolean isRegionThread(Location location) { | |
|
||
@Override | ||
public MyScheduledTask runTask(Runnable runnable) { | ||
return new FoliaScheduledTask(globalRegionScheduler.run(plugin, task -> runnable.run())); | ||
return new FoliaScheduledTask(globalRegionScheduler.run(plugin, task -> runnable.run()), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
|
@@ -55,19 +60,19 @@ public MyScheduledTask runTaskLater(Runnable runnable, long delay) { | |
if (delay <= 0) { | ||
return runTask(runnable); | ||
} | ||
return new FoliaScheduledTask(globalRegionScheduler.runDelayed(plugin, task -> runnable.run(), delay)); | ||
return new FoliaScheduledTask(globalRegionScheduler.runDelayed(plugin, task -> runnable.run(), delay), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTaskTimer(Runnable runnable, long delay, long period) { | ||
//Folia exception: Delay ticks may not be <= 0 | ||
delay = getOneIfNotPositive(delay); | ||
return new FoliaScheduledTask(globalRegionScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay, period)); | ||
return new FoliaScheduledTask(globalRegionScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay, period), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTask(Plugin plugin, Runnable runnable) { | ||
return new FoliaScheduledTask(globalRegionScheduler.run(plugin, task -> runnable.run())); | ||
return new FoliaScheduledTask(globalRegionScheduler.run(plugin, task -> runnable.run()), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
|
@@ -76,19 +81,19 @@ public MyScheduledTask runTaskLater(Plugin plugin, Runnable runnable, long delay | |
if (delay <= 0) { | ||
return runTask(plugin, runnable); | ||
} | ||
return new FoliaScheduledTask(globalRegionScheduler.runDelayed(plugin, task -> runnable.run(), delay)); | ||
return new FoliaScheduledTask(globalRegionScheduler.runDelayed(plugin, task -> runnable.run(), delay), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTaskTimer(Plugin plugin, Runnable runnable, long delay, long period) { | ||
//Folia exception: Delay ticks may not be <= 0 | ||
delay = getOneIfNotPositive(delay); | ||
return new FoliaScheduledTask(globalRegionScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay, period)); | ||
return new FoliaScheduledTask(globalRegionScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay, period), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTask(Location location, Runnable runnable) { | ||
return new FoliaScheduledTask(regionScheduler.run(plugin, location, task -> runnable.run())); | ||
return new FoliaScheduledTask(regionScheduler.run(plugin, location, task -> runnable.run()), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
|
@@ -97,19 +102,19 @@ public MyScheduledTask runTaskLater(Location location, Runnable runnable, long d | |
if (delay <= 0) { | ||
return runTask(runnable); | ||
} | ||
return new FoliaScheduledTask(regionScheduler.runDelayed(plugin, location, task -> runnable.run(), delay)); | ||
return new FoliaScheduledTask(regionScheduler.runDelayed(plugin, location, task -> runnable.run(), delay), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTaskTimer(Location location, Runnable runnable, long delay, long period) { | ||
//Folia exception: Delay ticks may not be <= 0 | ||
delay = getOneIfNotPositive(delay); | ||
return new FoliaScheduledTask(regionScheduler.runAtFixedRate(plugin, location, task -> runnable.run(), delay, period)); | ||
return new FoliaScheduledTask(regionScheduler.runAtFixedRate(plugin, location, task -> runnable.run(), delay, period), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTask(Entity entity, Runnable runnable) { | ||
return new FoliaScheduledTask(entity.getScheduler().run(plugin, task -> runnable.run(), null)); | ||
return new FoliaScheduledTask(entity.getScheduler().run(plugin, task -> runnable.run(), null), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
|
@@ -118,50 +123,50 @@ public MyScheduledTask runTaskLater(Entity entity, Runnable runnable, long delay | |
if (delay <= 0) { | ||
return runTask(entity, runnable); | ||
} | ||
return new FoliaScheduledTask(entity.getScheduler().runDelayed(plugin, task -> runnable.run(), null, delay)); | ||
return new FoliaScheduledTask(entity.getScheduler().runDelayed(plugin, task -> runnable.run(), null, delay), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTaskTimer(Entity entity, Runnable runnable, long delay, long period) { | ||
//Folia exception: Delay ticks may not be <= 0 | ||
delay = getOneIfNotPositive(delay); | ||
return new FoliaScheduledTask(entity.getScheduler().runAtFixedRate(plugin, task -> runnable.run(), null, delay, period)); | ||
return new FoliaScheduledTask(entity.getScheduler().runAtFixedRate(plugin, task -> runnable.run(), null, delay, period), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTaskAsynchronously(Runnable runnable) { | ||
return new FoliaScheduledTask(asyncScheduler.runNow(plugin, task -> runnable.run())); | ||
return new FoliaScheduledTask(asyncScheduler.runNow(plugin, task -> runnable.run()), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTaskLaterAsynchronously(Runnable runnable, long delay) { | ||
//Folia exception: Delay ticks may not be <= 0 | ||
delay = getOneIfNotPositive(delay); | ||
return new FoliaScheduledTask(asyncScheduler.runDelayed(plugin, task -> runnable.run(), delay * 50L, TimeUnit.MILLISECONDS)); | ||
return new FoliaScheduledTask(asyncScheduler.runDelayed(plugin, task -> runnable.run(), delay * 50L, TimeUnit.MILLISECONDS), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTaskTimerAsynchronously(Runnable runnable, long delay, long period) { | ||
return new FoliaScheduledTask(asyncScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay * 50, period * 50, TimeUnit.MILLISECONDS)); | ||
return new FoliaScheduledTask(asyncScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay * 50, period * 50, TimeUnit.MILLISECONDS), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTaskAsynchronously(Plugin plugin, Runnable runnable) { | ||
return new FoliaScheduledTask(asyncScheduler.runNow(plugin, task -> runnable.run())); | ||
return new FoliaScheduledTask(asyncScheduler.runNow(plugin, task -> runnable.run()), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTaskLaterAsynchronously(Plugin plugin, Runnable runnable, long delay) { | ||
//Folia exception: Delay ticks may not be <= 0 | ||
delay = getOneIfNotPositive(delay); | ||
return new FoliaScheduledTask(asyncScheduler.runDelayed(plugin, task -> runnable.run(), delay * 50L, TimeUnit.MILLISECONDS)); | ||
return new FoliaScheduledTask(asyncScheduler.runDelayed(plugin, task -> runnable.run(), delay * 50L, TimeUnit.MILLISECONDS), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
public MyScheduledTask runTaskTimerAsynchronously(Plugin plugin, Runnable runnable, long delay, long period) { | ||
//Folia exception: Delay ticks may not be <= 0 | ||
delay = getOneIfNotPositive(delay); | ||
return new FoliaScheduledTask(asyncScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay * 50, period * 50, TimeUnit.MILLISECONDS)); | ||
return new FoliaScheduledTask(asyncScheduler.runAtFixedRate(plugin, task -> runnable.run(), delay * 50, period * 50, TimeUnit.MILLISECONDS), cleanAndGetNextId()); | ||
} | ||
|
||
@Override | ||
|
@@ -191,7 +196,42 @@ public void cancelTasks(Plugin plugin) { | |
asyncScheduler.cancelTasks(plugin); | ||
} | ||
|
||
@Override | ||
public void cancel(int taskId) { | ||
tasks.get(taskId).cancel(); | ||
} | ||
|
||
@Override | ||
public void cancel(MyScheduledTask task) { | ||
task.cancel(); | ||
} | ||
|
||
private long getOneIfNotPositive(long x) { | ||
return x <= 0 ? 1L : x; | ||
} | ||
|
||
private synchronized int cleanAndGetNextId() { | ||
int taskId; | ||
|
||
handleClean(); | ||
|
||
do { | ||
taskId = new Random().nextInt(); | ||
} while (tasks.containsKey(taskId)); | ||
|
||
return taskId; | ||
} | ||
|
||
private synchronized void handleClean() { | ||
HashMap<Integer, MyScheduledTask> tasksToRemove = new HashMap<>(); | ||
|
||
tasks.forEach((id, task) -> { | ||
FoliaScheduledTask foliaScheduledTask = (FoliaScheduledTask) task; | ||
if (task == null || foliaScheduledTask.isFinished()) { | ||
tasksToRemove.put(id, task); | ||
} | ||
}); | ||
|
||
tasksToRemove.forEach(tasks::remove); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe there is a more appropriate way |
||
} |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don’t use random, we will still be have to use brute force until we find a free task ID.