Skip to content

Commit

Permalink
Use virtual threads for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Nov 24, 2024
1 parent 0ead36c commit 910989f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.ApiStatus;

import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;
Expand All @@ -30,6 +31,13 @@ static FancyNpcsPlugin get() {

ScheduledExecutorService getNpcThread();

/**
* Creates a new thread with the given name and runnable.
* Warning: Do not use this method, it is for internal use only.
*/
@ApiStatus.Internal
Thread newThread(String name, Runnable runnable);

FancyScheduler getScheduler();

Function<NpcData, Npc> getNpcAdapter();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.oliver.fancynpcs.api.actions.executor;

import de.oliver.fancynpcs.api.FancyNpcsPlugin;
import de.oliver.fancynpcs.api.Npc;
import de.oliver.fancynpcs.api.actions.ActionTrigger;
import org.bukkit.entity.Player;
Expand All @@ -22,12 +23,13 @@ public static void execute(ActionTrigger trigger, Npc npc, Player player) {

ActionExecutionContext context = new ActionExecutionContext(trigger, npc, player);
runningContexts.put(key, context);
new Thread(() -> {

FancyNpcsPlugin.get().newThread("FancyNpcs-ActionExecutor", () -> {
while (context.hasNext()) {
context.runNext();
}
context.terminate();
}, "NpcActionExecutor").start();
}).start();
}

private static String getKey(ActionTrigger trigger, Npc npc, Player player) {
Expand Down
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ paper {
tasks {
runServer {
minecraftVersion(findProperty("minecraftVersion").toString())
// minecraftVersion("1.20.6")
// minecraftVersion("1.19.4")

downloadPlugins {
hangar("ViaVersion", "5.0.3")
hangar("ViaBackwards", "5.0.3")
hangar("ViaVersion", "5.1.1")
hangar("ViaBackwards", "5.1.1")
hangar("PlaceholderAPI", "2.11.6")
// modrinth("multiverse-core", "4.3.11")
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/oliver/fancynpcs/FancyNpcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@ private void checkIfPluginVersionUpdated() {
}
}

@Override
public Thread newThread(String name, Runnable runnable) {
return Thread.ofVirtual().name(name).unstarted(runnable);
}

public ExtendedFancyLogger getFancyLogger() {
return fancyLogger;
}
Expand Down

0 comments on commit 910989f

Please sign in to comment.