From f8529e4cc28f5db81dd0d6640ee8a308bb117157 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Thu, 14 Mar 2024 16:06:35 -0400 Subject: [PATCH] Use reflect for VirtualThread In order to keep run on Java 17 and use Java 21 as option to use VirtualThread. --- ...e-Virtual-Thread-for-async-scheduler.patch | 25 ++++++++++++++----- ...l.patch => 0076-Chat-Image-protocol.patch} | 4 +-- 2 files changed, 21 insertions(+), 8 deletions(-) rename patches/server/{0076-Leaf-chat-image-protocol.patch => 0076-Chat-Image-protocol.patch} (98%) diff --git a/patches/server/0072-Ability-to-use-Virtual-Thread-for-async-scheduler.patch b/patches/server/0072-Ability-to-use-Virtual-Thread-for-async-scheduler.patch index 4ef7642b..cd623958 100644 --- a/patches/server/0072-Ability-to-use-Virtual-Thread-for-async-scheduler.patch +++ b/patches/server/0072-Ability-to-use-Virtual-Thread-for-async-scheduler.patch @@ -17,25 +17,33 @@ index ee166ad76bd2c143e04e4bf6bd9991bc90ab99a5..13f3f8628143ffc22a2d2c2129ea4836 tryPreloadClass("org.slf4j.helpers.FormattingTuple"); tryPreloadClass("org.slf4j.helpers.BasicMarker"); diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java -index 9c1aff17aabd062640e3f451a2ef8c50a7c62f10..5274fe2618a74389d613f6eae292f1128cf307d3 100644 +index 9c1aff17aabd062640e3f451a2ef8c50a7c62f10..fe45b0a4a6846662a1935bcddac791b45f09bf21 100644 --- a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java +++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java -@@ -25,6 +25,7 @@ package org.bukkit.craftbukkit.scheduler; +@@ -25,30 +25,50 @@ package org.bukkit.craftbukkit.scheduler; import com.destroystokyo.paper.ServerSchedulerReportingWrapper; import com.google.common.util.concurrent.ThreadFactoryBuilder; +import org.galemc.gale.virtualthread.VirtualThreadService; import org.bukkit.plugin.Plugin; ++import java.lang.reflect.Method; import java.util.ArrayList; -@@ -38,17 +39,29 @@ import java.util.concurrent.TimeUnit; + import java.util.Iterator; + import java.util.List; + import java.util.concurrent.Executor; + import java.util.concurrent.Executors; + import java.util.concurrent.SynchronousQueue; ++import java.util.concurrent.ThreadFactory; + import java.util.concurrent.ThreadPoolExecutor; + import java.util.concurrent.TimeUnit; public class CraftAsyncScheduler extends CraftScheduler { - private final ThreadPoolExecutor executor = new ThreadPoolExecutor( - 4, Integer.MAX_VALUE,30L, TimeUnit.SECONDS, new SynchronousQueue<>(), - new ThreadFactoryBuilder().setNameFormat("Craft Scheduler Thread - %1$d").setUncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(net.minecraft.server.MinecraftServer.LOGGER)).build()); // Paper -+ private final Executor executor; // Leaf - use super class ++ private Executor executor; // Leaf - use super class private final Executor management = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder() .setNameFormat("Craft Async Scheduler Management Thread").setUncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(net.minecraft.server.MinecraftServer.LOGGER)).build()); // Paper private final List temp = new ArrayList<>(); @@ -47,8 +55,13 @@ index 9c1aff17aabd062640e3f451a2ef8c50a7c62f10..5274fe2618a74389d613f6eae292f112 + + // Leaf start - Ability to use Virtual Thread for async scheduler + if (VirtualThreadService.getJavaMajorVersion() >= VirtualThreadService.minimumJavaMajorVersionWithoutFeaturePreview && org.dreeam.leaf.config.modules.opt.VT4BukkitScheduler.enabled) { -+ executor = Executors.newThreadPerTaskExecutor(VirtualThreadService.get().createFactory()); -+ return; ++ try { ++ Method newThreadPerTaskExecutor = Executors.class.getMethod("newThreadPerTaskExecutor", ThreadFactory.class); ++ executor = (Executor) newThreadPerTaskExecutor.invoke(null, VirtualThreadService.get().createFactory()); ++ //executor = Executors.newThreadPerTaskExecutor(Thread.ofVirtual().name("Craft Scheduler Thread - %1$d").factory()); // Leaf - When Minecraft using Java21 ++ return; ++ } catch (Exception ignored) { ++ } + } + + executor = new ThreadPoolExecutor( diff --git a/patches/server/0076-Leaf-chat-image-protocol.patch b/patches/server/0076-Chat-Image-protocol.patch similarity index 98% rename from patches/server/0076-Leaf-chat-image-protocol.patch rename to patches/server/0076-Chat-Image-protocol.patch index f9b6c6f9..dbc58371 100644 --- a/patches/server/0076-Leaf-chat-image-protocol.patch +++ b/patches/server/0076-Chat-Image-protocol.patch @@ -1,7 +1,7 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: HaHaWTH Date: Wed, 13 Mar 2024 18:11:10 +0800 -Subject: [PATCH] Leaf-chat-image-protocol +Subject: [PATCH] Chat Image protocol diff --git a/src/main/java/org/dreeam/leaf/config/modules/network/ProtocolSupport.java b/src/main/java/org/dreeam/leaf/config/modules/network/ProtocolSupport.java @@ -20,7 +20,7 @@ index 3e5e27efac1791cce961ec144405d8215ab87996..1c1abbb3a74a519520d2c1229721887c @ConfigInfo(baseName = "xaero-map-server-id") diff --git a/src/main/java/top/leavesmc/leaves/protocol/ChatImageProtocol.java b/src/main/java/top/leavesmc/leaves/protocol/ChatImageProtocol.java new file mode 100644 -index 0000000000000000000000000000000000000000..5e97da51249a45db93e4b5d2921784d58596abde +index 0000000000000000000000000000000000000000..4ee75d7ed8ffda621102c11cb3294dffc5dd40d1 --- /dev/null +++ b/src/main/java/top/leavesmc/leaves/protocol/ChatImageProtocol.java @@ -0,0 +1,149 @@