-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use SingleThreadScheduler in Folia (#78)
- Loading branch information
Showing
5 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/BukkitScheduler.java
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.turikhay.mc.mapmodcompanion.spigot; | ||
|
||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.util.logging.Level; | ||
|
||
public class BukkitScheduler implements PluginScheduler { | ||
private final Plugin plugin; | ||
|
||
public BukkitScheduler(Plugin plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public void cleanUp() { | ||
} | ||
|
||
@Override | ||
public void schedule(Runnable r) { | ||
if (plugin.getServer().isPrimaryThread()) { | ||
executeTask(r); | ||
} else { | ||
plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, () -> executeTask(r)); | ||
} | ||
} | ||
|
||
private void executeTask(Runnable r) { | ||
try { | ||
r.run(); | ||
} catch (Throwable t) { | ||
plugin.getLogger().log(Level.SEVERE, "Failed to execute the task", t); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BukkitScheduler{}"; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/FoliaSupport.java
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.turikhay.mc.mapmodcompanion.spigot; | ||
|
||
public class FoliaSupport { | ||
private static Boolean IS_FOLIA_SERVER; | ||
|
||
public static boolean isFoliaServer() { | ||
if (IS_FOLIA_SERVER == null) { | ||
boolean isIt = true; | ||
try { | ||
Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); | ||
} catch (Throwable t) { | ||
isIt = false; | ||
} | ||
IS_FOLIA_SERVER = isIt; | ||
} | ||
return IS_FOLIA_SERVER; | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/PluginScheduler.java
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.turikhay.mc.mapmodcompanion.spigot; | ||
|
||
import com.turikhay.mc.mapmodcompanion.Disposable; | ||
|
||
public interface PluginScheduler extends Disposable { | ||
void schedule(Runnable r); | ||
} |
36 changes: 36 additions & 0 deletions
36
spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/SingleThreadScheduler.java
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.turikhay.mc.mapmodcompanion.spigot; | ||
|
||
import com.turikhay.mc.mapmodcompanion.DaemonThreadFactory; | ||
import com.turikhay.mc.mapmodcompanion.ILogger; | ||
|
||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
|
||
public class SingleThreadScheduler implements PluginScheduler { | ||
|
||
private final ScheduledThreadPoolExecutor service; | ||
|
||
public SingleThreadScheduler(ILogger logger) { | ||
this.service = new ScheduledThreadPoolExecutor( | ||
1, | ||
new DaemonThreadFactory(logger, SingleThreadScheduler.class) | ||
); | ||
service.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); | ||
} | ||
|
||
@Override | ||
public void cleanUp() { | ||
service.shutdown(); | ||
} | ||
|
||
@Override | ||
public void schedule(Runnable r) { | ||
service.submit(r); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SingleThreadScheduler{" + | ||
"service=" + service + | ||
'}'; | ||
} | ||
} |