9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-25 09:59:15 +00:00

Use reflect for VirtualThread

In order to keep run on Java 17 and use Java 21 as option to use VirtualThread.
This commit is contained in:
Dreeam
2024-03-14 16:06:35 -04:00
parent e80f541f88
commit f8529e4cc2
2 changed files with 21 additions and 8 deletions

View File

@@ -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<CraftTask> 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(

View File

@@ -1,7 +1,7 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <fsjk947@gmail.com>
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 @@