Skip to content
8 changes: 3 additions & 5 deletions src/main/java/ch/njol/skript/ScriptLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import ch.njol.skript.structures.StructOptions.OptionsData;
import ch.njol.skript.test.runner.TestMode;
import ch.njol.skript.util.ExceptionUtils;
import ch.njol.skript.util.Task;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.variables.HintManager;
import ch.njol.util.OpenCloseable;
Expand Down Expand Up @@ -729,7 +728,7 @@ private static LoadingScriptInfo loadScript(Config config) {
}

// In always sync task, enable stuff
Callable<Void> callable = () -> {
Runnable runnable = () -> {
// Remove the script from the disabled scripts list
File file = config.getFile();
assert file != null;
Expand All @@ -741,13 +740,12 @@ private static LoadingScriptInfo loadScript(Config config) {

ScriptLoader.eventRegistry().events(ScriptInitEvent.class)
.forEach(event -> event.onInit(script));
return null;
};
if (isAsync()) { // Need to delegate to main thread
Task.callSync(callable);
Skript.getScheduler().callSyncGlobal(runnable);
} else { // We are in main thread, execute immediately
try {
callable.call();
runnable.run();
} catch (Exception e) {
//noinspection ThrowableNotThrown
Skript.exception(e);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/ch/njol/skript/ServerPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ public enum ServerPlatform {
/**
* Spigot, with its Bukkit API extensions. Officially supported.
*/
BUKKIT_SPIGOT("Spigot", true, true),
BUKKIT_SPIGOT("Spigot", false, false),

/**
* Paper Minecraft server, which is a Spigot fork with additional features.
* Officially supported.
*/
BUKKIT_PAPER("Paper", true, true),

/**
* Folia Minecraft server, which is a Paper fork with multi-threading.
* Officially supported.
*/
BUKKIT_FOLIA("Folia", true, true),

/**
* Glowstone (or similar) fully open source Minecraft server, which
Expand Down
94 changes: 57 additions & 37 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.skriptlang.skript.registration.SyntaxRegistry.Key;
import org.skriptlang.skript.util.ClassLoader;
import org.skriptlang.skript.util.Priority;
import org.skriptlang.skript.util.Scheduler;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -150,6 +151,8 @@ public final class Skript extends JavaPlugin implements Listener {
@Nullable
private static Skript instance = null;

private static Scheduler scheduler = null;

private static org.skriptlang.skript.@UnknownNullability Skript skript = null;
private static org.skriptlang.skript.@UnknownNullability Skript unmodifiableSkript = null;

Expand All @@ -162,6 +165,12 @@ public static Skript getInstance() {
return instance;
}

public static Scheduler getScheduler() {
if (scheduler == null)
throw new IllegalStateException();
return scheduler;
}

/**
* @return The modern Skript instance to be used for addon registration.
*/
Expand Down Expand Up @@ -234,6 +243,8 @@ public static String getRestartMessage() {
public static ServerPlatform getServerPlatform() {
if (classExists("net.glowstone.GlowServer")) {
return ServerPlatform.BUKKIT_GLOWSTONE; // Glowstone has timings too, so must check for it first
} else if (classExists("io.papermc.paper.threadedregions.RegionizedServer")) {
return ServerPlatform.BUKKIT_FOLIA;
} else if (classExists("co.aikar.timings.Timings")) {
return ServerPlatform.BUKKIT_PAPER; // Could be Sponge, but it doesn't work at all at the moment
} else if (classExists("org.spigotmc.SpigotConfig")) {
Expand Down Expand Up @@ -289,7 +300,7 @@ private static boolean checkServerPlatform() {
} else if (!serverPlatform.supported) {
Skript.warning("This server platform (" + serverPlatform.name + ") is not supported by Skript.");
Skript.warning("It will still probably work, but if it does not, you are on your own.");
Skript.warning("Skript officially supports Paper and Spigot.");
Skript.warning("Skript officially supports Paper and Folia.");
}

// If nothing got triggered, everything is probably ok
Expand Down Expand Up @@ -363,6 +374,8 @@ public static RuntimeErrorManager getRuntimeErrorManager() {

@Override
public void onEnable() {
scheduler = new Scheduler(this);

Bukkit.getPluginManager().registerEvents(this, this);
if (disabled) {
Skript.error(m_invalid_reload.toString());
Expand Down Expand Up @@ -582,7 +595,7 @@ public String name() {
info(" " + Language.get("skript.copyright"));

final long tick = testing() ? Bukkit.getWorlds().get(0).getFullTime() : 0;
Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
Skript.getScheduler().runGlobalTask(new Runnable() {
@SuppressWarnings("synthetic-access")
@Override
public void run() {
Expand Down Expand Up @@ -693,11 +706,17 @@ protected void afterErrors() {
// delay + chunk loading necessary to allow world to fully generate and start ticking before tests run.
World world = Bukkit.getWorlds().get(0);
world.setSpawnLocation(0, 0, 0);
Bukkit.getScheduler().scheduleSyncDelayedTask(Skript.getInstance(), () -> {
world.addPluginChunkTicket(0, 0, Skript.getInstance());
world.addPluginChunkTicket(100, 100, Skript.getInstance());
Bukkit.getScheduler().scheduleSyncDelayedTask(Skript.getInstance(), () -> runTests(), 100);
}, 5);
Skript.getScheduler().runGlobalDelayedTask(
() -> {
world.addPluginChunkTicket(0, 0, Skript.getInstance());
world.addPluginChunkTicket(100, 100, Skript.getInstance());
Skript.getScheduler().runRegionDelayedTask(
SkriptJUnitTest.getTestLocation(),
() -> runTests(),
100
);
}, 5
);
}
}

Expand Down Expand Up @@ -741,13 +760,12 @@ protected void afterErrors() {
|| !record.getMessage().toLowerCase(Locale.ENGLISH).startsWith("can't keep up!");
};
BukkitLoggerFilter.addFilter(filter);
Bukkit.getScheduler().scheduleSyncDelayedTask(
Skript.this,
Skript.getScheduler().runGlobalDelayedTask(
() -> BukkitLoggerFilter.removeFilter(filter),
1);
1
);
} else {
Bukkit.getScheduler().scheduleSyncDelayedTask(Skript.this,
EvtSkript::onSkriptStart);
Skript.getScheduler().runGlobalTask(EvtSkript::onSkriptStart);
}
} catch (Exception e) {
// Something went wrong, we need to make sure the exception is printed
Expand Down Expand Up @@ -795,10 +813,10 @@ public void onJoin(PlayerJoinEvent event) {
if (!event.getPlayer().hasPermission("skript.admin"))
return;

new Task(Skript.this, 0) {
@Override
public void run() {
Player player = event.getPlayer();
Player player = event.getPlayer();
Skript.getScheduler().runEntityTask(
player,
() -> {
SkriptUpdater updater = getUpdater();

// Don't actually check for updates to avoid breaking GitHub rate limit
Expand All @@ -814,8 +832,8 @@ public void run() {
Skript.info(player, SkriptUpdater.m_update_available.toString(update.id, Skript.getVersion()));
player.sendMessage(TextComponentParser.instance()
.parse("Download it at: <aqua><underlined><click:open_url:" + update.downloadUrl + ">" + update.downloadUrl));
}
};
}, null
);
}
}

Expand Down Expand Up @@ -926,24 +944,26 @@ private void runTests() {
info("Testing done, shutting down the server in " + display + " second" + (display == 1 ? "" : "s") + "...");

// Delay server shutdown to stop the server from crashing because the current tick takes a long time due to all the tests
Bukkit.getScheduler().runTaskLater(Skript.this, () -> {
info("Shutting down server.");
if (TestMode.JUNIT && !EffObjectives.isJUnitComplete())
EffObjectives.fail();

info("Collecting results to " + TestMode.RESULTS_FILE);
String results = new GsonBuilder()
.setPrettyPrinting() // Easier to read lines
.disableHtmlEscaping() // Fixes issue with "'" character in test strings going unicode
.create().toJson(TestTracker.collectResults());
try {
Files.write(TestMode.RESULTS_FILE, results.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
Skript.exception(e, "Failed to write test results.");
}
// delay by 1 tick to avoid the watchdog from thinking the shutdown tick took too long.
Bukkit.getScheduler().runTaskLater(Skript.this, () -> Bukkit.getServer().shutdown(), 1);
}, shutdownDelay.get());
Skript.getScheduler().runGlobalDelayedTask(
() -> {
info("Shutting down server.");
if (TestMode.JUNIT && !EffObjectives.isJUnitComplete())
EffObjectives.fail();

info("Collecting results to " + TestMode.RESULTS_FILE);
String results = new GsonBuilder()
.setPrettyPrinting() // Easier to read lines
.disableHtmlEscaping() // Fixes issue with "'" character in test strings going unicode
.create().toJson(TestTracker.collectResults());
try {
Files.write(TestMode.RESULTS_FILE, results.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
Skript.exception(e, "Failed to write test results.");
}
// delay by 1 tick to avoid the watchdog from thinking the shutdown tick took too long.
Skript.getScheduler().runGlobalDelayedTask(() -> Bukkit.getServer().shutdown(), 1);
}, shutdownDelay.get()
);
});
}

Expand Down Expand Up @@ -1277,7 +1297,7 @@ public void onDisable() {
beforeDisable();
}

Bukkit.getScheduler().cancelTasks(this);
getScheduler().cancelAllTasks();

for (Closeable c : closeOnDisable) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/SkriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
ScriptLoader.loadScripts(scriptFile, errorCounter)
.thenAccept(scriptInfo ->
// Code should run on server thread
Bukkit.getScheduler().scheduleSyncDelayedTask(Skript.getInstance(), () -> {
Skript.getScheduler().runGlobalTask(() -> {
Bukkit.getPluginManager().callEvent(new SkriptTestEvent()); // Run it
ScriptLoader.unloadScripts(ScriptLoader.getLoadedScripts());

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/ch/njol/skript/SkriptEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.timings.SkriptTimings;
import ch.njol.skript.util.Task;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -175,10 +174,9 @@ private static void execute(Trigger trigger, Event event) {
if (trigger.getEvent().check(event))
execute.run();
} else { // Ensure main thread
Task.callSync(() -> {
Skript.getScheduler().callSyncGlobal(() -> {
if (trigger.getEvent().check(event))
execute.run();
return null; // we don't care about a return value
});
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/ch/njol/skript/bukkitutil/ClickEventTracker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.njol.skript.bukkitutil;

import ch.njol.skript.Skript;
import ch.njol.skript.effects.EffCancelEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -47,14 +48,15 @@ public TrackedEvent(Cancellable event, EquipmentSlot hand) {
*/
private final Set<Cancellable> modifiedEvents;

public ClickEventTracker(JavaPlugin plugin) {
public ClickEventTracker() {
this.firstEvents = new HashMap<>();
this.modifiedEvents = new HashSet<>();
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin,
() -> {
firstEvents.clear();
modifiedEvents.clear();
}, 1, 1);
Skript.getScheduler().runGlobalRepeatingTask(
() -> {
firstEvents.clear();
modifiedEvents.clear();
}, 1, 1
);
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/ch/njol/skript/bukkitutil/PlayerUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ch.njol.skript.bukkitutil;

import ch.njol.skript.Skript;
import ch.njol.skript.util.Task;
import com.google.common.collect.ImmutableList;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
Expand Down Expand Up @@ -32,15 +31,14 @@ public static void updateInventory(@Nullable Player player) {
}

static {
new Task(Skript.getInstance(), 1, 1) {
@Override
public void run() {
Skript.getScheduler().runGlobalRepeatingTask(
() -> {
for (Player p : inventoryUpdateList)
p.updateInventory();

inventoryUpdateList.clear();
}
};
}, 1, 1
);
}

/**
Expand Down
Loading