Skip to content
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

[TESTED] Added support for multithreaded servers (Like Folia) #35

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
33 changes: 33 additions & 0 deletions src/main/java/com/jeff_media/updatechecker/ThreadScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jeff_media.updatechecker;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadScheduler {
public static ScheduledExecutorService runTask(Runnable task) {
return runTask(task, 1/20L, TimeUnit.MILLISECONDS);
}

public static ScheduledExecutorService runTask(Runnable task, long delay, TimeUnit timeUnit) {
// Create a ScheduledExecutorService with a single thread
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Paulem79 marked this conversation as resolved.
Show resolved Hide resolved

scheduler.schedule(task, delay, timeUnit);

return scheduler;
}

public static ScheduledExecutorService scheduleRepeatingTask(Runnable task, long delay, long period, TimeUnit timeUnit) {
// Create a ScheduledExecutorService with a single thread
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

scheduler.scheduleAtFixedRate(task, delay, period, timeUnit);

return scheduler;
}

public static void stopScheduler(ScheduledExecutorService scheduler) {
if(!scheduler.isShutdown()) scheduler.shutdown();
}
}
26 changes: 15 additions & 11 deletions src/main/java/com/jeff_media/updatechecker/UpdateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

/**
Expand Down Expand Up @@ -81,7 +83,9 @@ public class UpdateChecker {
private BiConsumer<CommandSender[], String> onSuccess = (requesters, latestVersion) -> {
};
private String paidDownloadLink = null;
private int taskId = -1;
//private int taskId = -1;
@Nullable
private ScheduledExecutorService task = null;
private int timeout = 0;
private String usedVersion;
private String userAgentString = null;
Expand Down Expand Up @@ -277,12 +281,12 @@ public boolean isSuppressUpToDateMessage() {
public UpdateChecker checkEveryXHours(double hours) {
double minutes = hours * 60;
double seconds = minutes * 60;
long ticks = ((int) seconds) * 20L;
long milliseconds = ((int) seconds) * 1000L;
stop();
if (ticks > 0) {
taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> checkNow(Bukkit.getConsoleSender()), ticks, ticks);
if (milliseconds > 0) {
task = ThreadScheduler.scheduleRepeatingTask(() -> checkNow(Bukkit.getConsoleSender()), milliseconds, milliseconds, TimeUnit.MILLISECONDS);
} else {
taskId = -1;
task = null;
}
return this;
}
Expand All @@ -293,10 +297,10 @@ public UpdateChecker checkEveryXHours(double hours) {
* its previous task.
*/
public UpdateChecker stop() {
if (taskId != -1) {
Bukkit.getScheduler().cancelTask(taskId);
if (task != null) {
ThreadScheduler.stopScheduler(task);
}
taskId = -1;
task = null;
return this;
}

Expand All @@ -320,7 +324,7 @@ public UpdateChecker checkNow(@Nullable CommandSender... requesters) {
userAgentString = UserAgentBuilder.getDefaultUserAgent().build();
}

Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
ThreadScheduler.runTask(() -> {

UpdateCheckEvent updateCheckEvent;

Expand All @@ -345,12 +349,12 @@ public UpdateChecker checkNow(@Nullable CommandSender... requesters) {
updateCheckEvent = new UpdateCheckEvent(UpdateCheckSuccess.SUCCESS);
} catch (final IOException exception) {
updateCheckEvent = new UpdateCheckEvent(UpdateCheckSuccess.FAIL);
Bukkit.getScheduler().runTask(plugin, () -> getOnFail().accept(requesters, exception));
ThreadScheduler.runTask(() -> getOnFail().accept(requesters, exception));
}

UpdateCheckEvent finalUpdateCheckEvent = updateCheckEvent.setRequesters(requesters);

Bukkit.getScheduler().runTask(plugin, () -> {
ThreadScheduler.runTask(() -> {

if (finalUpdateCheckEvent.getSuccess() == UpdateCheckSuccess.SUCCESS) {
getOnSuccess().accept(requesters, latestVersion);
Expand Down