Skip to content

Commit

Permalink
Pass all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Feb 21, 2024
1 parent 69426c7 commit dfec2e1
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void runDelayed(long delay) {

@Override
public void runTaskTimer(long period, long delay) {
super.setRepeatable();
super.setRepeatable(true);
if (USING_FOLIA) {
super.registerFoliaTask(Bukkit.getServer().getAsyncScheduler().runAtFixedRate(plugin, super::runTask, delay, period, TimeUnit.MILLISECONDS));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void runDelayed(long delay) {

@Override
public void runTaskTimer(long period, long delay) {
super.setRepeatable();
super.setRepeatable(true);
if (USING_FOLIA) {
entity.getScheduler().runAtFixedRate(plugin, super::runTask, null, delay, period);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public void runDelayed(long delay) {

@Override
public void runNow() {
super.setRepeatable(false);
if (bungee && !BungeeHelper.canSendBungeeMessages()) {
runTaskTimer(20, 20, () -> {
if (BungeeHelper.canSendBungeeMessages()) {
this.runNow();
this.cancel();
}
});
return;
Expand All @@ -45,11 +45,11 @@ public void runNow() {
}

public void runTaskTimer(long period, long delay, Runnable runnable) {
super.setRepeatable();
super.setRepeatable(true);
if (USING_FOLIA) {
super.registerFoliaTask(Bukkit.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, ignored -> runnable.run(), delay, period));
} else {
super.registerBukkitTask(new StargateBukkitRunnable(this::runTask)).runTaskTimer(plugin, delay, period);
super.registerBukkitTask(new StargateBukkitRunnable(runnable)).runTaskTimer(plugin, delay, period);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void runNow() {

@Override
public void runTaskTimer(long period, long delay) {
super.setRepeatable();
super.setRepeatable(true);
StargateQueuedAsyncTask task = this;
new StargateAsyncTask() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void runDelayed(long delay) {

@Override
public void runTaskTimer(long period, long delay) {
super.setRepeatable();
super.setRepeatable(true);
if (USING_FOLIA) {
ScheduledTask theTask = Bukkit.getServer().getRegionScheduler().runAtFixedRate(plugin, location, super::runTask, delay, period);
super.registerFoliaTask(theTask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public abstract class StargateTask implements Runnable{
public abstract class StargateTask implements Runnable {
protected static final boolean USING_FOLIA = NonLegacyClass.REGIONIZED_SERVER.isImplemented();
private static final int MAXIMUM_SHUTDOWN_CYCLES = 10;
private boolean taskIsRegistered = false;
Expand All @@ -33,7 +33,7 @@ public void cancel() {
}

protected void registerTask() {
if(taskIsRegistered){
if (taskIsRegistered) {
return;
}
taskIsRegistered = true;
Expand All @@ -51,9 +51,7 @@ protected void registerFoliaTask(ScheduledTask scheduledTask) {

protected BukkitRunnable registerBukkitTask(BukkitRunnable scheduledBukkitTask) {
this.scheduledBukkitTask = scheduledBukkitTask;
if (cancelled) {
cancelIfTaskHasBeenScheduled(true);
} else {
if (!cancelled) {
registerTask();
}
return scheduledBukkitTask;
Expand Down Expand Up @@ -91,7 +89,11 @@ public static void forceRunAllTasks() {
* Run the task if not already been running (should be threadsafe)
*/
protected void runTask() {
if ((running && ! repeatable)|| cancelled) {
if (cancelled) {
cancelIfTaskHasBeenScheduled(true);
return;
}
if (running && !repeatable) {
return;
}
running = true;
Expand All @@ -111,7 +113,7 @@ protected void runTask(ScheduledTask scheduledTask) {
this.runTask();
}

protected void setRepeatable(){
this.repeatable = true;
protected void setRepeatable(boolean repeatable) {
this.repeatable = repeatable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,6 @@ public static String generateLegacyTeleportMessage(String player, Portal portal)


public static boolean canSendBungeeMessages() {
return (Bukkit.getServer().getOnlinePlayers().isEmpty() || !Stargate.knowsServerName());
return (!Bukkit.getServer().getOnlinePlayers().isEmpty() && Stargate.knowsServerName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sgrewritten.stargate.Stargate;

class StargateGlobalTaskTest {

Expand Down Expand Up @@ -33,12 +34,13 @@ void run_noDuplicates() {
@Test
void run_bungee_noDuplicates() {
TestRunnable runnable = new TestRunnable(true, true);
runnable.run();
serverMock.getScheduler().performTicks(1000);
runnable.runNow();
serverMock.getScheduler().performTicks(30);
Assertions.assertFalse(runnable.hasRunBefore);
// Necessary to add a player here to avoid recursion loop
serverMock.addPlayer();
MockBukkit.unmock();
Stargate.setKnowsServerName(true);
serverMock.getScheduler().performTicks(30);
Assertions.assertTrue(runnable.hasRunBefore);
}

Expand Down

0 comments on commit dfec2e1

Please sign in to comment.