mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-23 00:49:31 +00:00
270 lines
11 KiB
Diff
270 lines
11 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: wangxyper <wangxyper@163.com>
|
|
Date: Mon, 9 Jan 2023 13:55:50 +0800
|
|
Subject: [PATCH] Hearse: Add config system and rename a class
|
|
|
|
Original license: MIT
|
|
Original project: https://github.com/NaturalCodeClub/HearseRewrite
|
|
|
|
diff --git a/src/main/java/co/earthme/hearse/HearseConfig.java b/src/main/java/co/earthme/hearse/HearseConfig.java
|
|
index 0a1de52bcdf675b9bfcbf14d39959818a7a0cbbb..73b5e76660b5162a7a0b327ddc7dcc3295b86699 100644
|
|
--- a/src/main/java/co/earthme/hearse/HearseConfig.java
|
|
+++ b/src/main/java/co/earthme/hearse/HearseConfig.java
|
|
@@ -1,5 +1,49 @@
|
|
package co.earthme.hearse;
|
|
|
|
+import org.bukkit.configuration.InvalidConfigurationException;
|
|
+import org.bukkit.configuration.file.YamlConfiguration;
|
|
+import java.io.File;
|
|
+import java.io.IOException;
|
|
+
|
|
public class HearseConfig {
|
|
+ private static final YamlConfiguration configEntry = new YamlConfiguration();
|
|
+ private static final File CONFIG_FILE = new File("hearse.yml");
|
|
+
|
|
+ public static void init(){
|
|
+ try {
|
|
+ configEntry.load(CONFIG_FILE);
|
|
+ }catch (IOException ignored){
|
|
+ } catch (InvalidConfigurationException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ configEntry.options().copyDefaults(true);
|
|
+ }
|
|
+
|
|
+ public static void save(){
|
|
+ try {
|
|
+ configEntry.save(CONFIG_FILE);
|
|
+ } catch (IOException e) {
|
|
+ e.printStackTrace();
|
|
+ }
|
|
+ }
|
|
+
|
|
+ public static int getInt(String key,int def){
|
|
+ configEntry.addDefault(key,def);
|
|
+ return configEntry.getInt(key);
|
|
+ }
|
|
+
|
|
+ public static long getLong(String key,int def){
|
|
+ configEntry.addDefault(key,def);
|
|
+ return configEntry.getLong(key);
|
|
+ }
|
|
+
|
|
+ public static String getString(String key,String def){
|
|
+ configEntry.addDefault(key,def);
|
|
+ return configEntry.getString(key);
|
|
+ }
|
|
|
|
+ public static boolean getBoolean(String key,boolean def){
|
|
+ configEntry.addDefault(key,def);
|
|
+ return configEntry.getBoolean(key);
|
|
+ }
|
|
}
|
|
diff --git a/src/main/java/co/earthme/hearse/server/ServerHook.java b/src/main/java/co/earthme/hearse/server/ServerEntityTickHook.java
|
|
similarity index 60%
|
|
rename from src/main/java/co/earthme/hearse/server/ServerHook.java
|
|
rename to src/main/java/co/earthme/hearse/server/ServerEntityTickHook.java
|
|
index 524a55c3298a079e416c742641af55725a602a2b..8da657836933ae6080e6594ff57dff84155e1820 100644
|
|
--- a/src/main/java/co/earthme/hearse/server/ServerHook.java
|
|
+++ b/src/main/java/co/earthme/hearse/server/ServerEntityTickHook.java
|
|
@@ -1,36 +1,51 @@
|
|
package co.earthme.hearse.server;
|
|
|
|
+import co.earthme.hearse.HearseConfig;
|
|
import co.earthme.hearse.concurrent.WorkerThread;
|
|
import co.earthme.hearse.concurrent.WorkerThreadPoolExecutor;
|
|
import net.minecraft.server.MinecraftServer;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.world.entity.Entity;
|
|
-import org.apache.logging.log4j.LogManager;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
-public class ServerHook {
|
|
+public class ServerEntityTickHook {
|
|
private static volatile boolean firstTick = false;
|
|
private static final AtomicInteger threadId = new AtomicInteger();
|
|
- private static final WorkerThreadPoolExecutor worker = new WorkerThreadPoolExecutor(
|
|
- Runtime.getRuntime().availableProcessors(),
|
|
- Runtime.getRuntime().availableProcessors(),
|
|
- 100,
|
|
- TimeUnit.MILLISECONDS,
|
|
- new LinkedBlockingQueue<>(),
|
|
- task -> {
|
|
- WorkerThread workerThread = new WorkerThread(task,"Hearse-Worker-Thread # "+threadId.getAndIncrement());
|
|
- return workerThread;
|
|
- }
|
|
- );
|
|
+ private static WorkerThreadPoolExecutor worker;
|
|
+ private static boolean asyncEntityEnabled;
|
|
|
|
public static void executeAsyncTask(Runnable task){
|
|
+ if (!asyncEntityEnabled){
|
|
+ throw new IllegalStateException();
|
|
+ }
|
|
worker.execute(task);
|
|
}
|
|
|
|
+ public static void init(){
|
|
+ final boolean asyncEntityEnabled1 = HearseConfig.getBoolean("enable-async-entity",true);
|
|
+ final int workerCount = HearseConfig.getInt("async-entity-worker-count",Runtime.getRuntime().availableProcessors());
|
|
+ if (asyncEntityEnabled1){
|
|
+ worker = new WorkerThreadPoolExecutor(
|
|
+ workerCount,
|
|
+ workerCount,
|
|
+ 100,
|
|
+ TimeUnit.MILLISECONDS,
|
|
+ new LinkedBlockingQueue<>(),
|
|
+ task -> {
|
|
+ return new WorkerThread(task,"Hearse-Worker-Thread # "+threadId.getAndIncrement());
|
|
+ }
|
|
+ );
|
|
+ }
|
|
+ asyncEntityEnabled = asyncEntityEnabled1;
|
|
+ }
|
|
+
|
|
public static void executeAsyncTaskWithMainThreadCallback(Runnable task,Runnable callBack){
|
|
+ if (!asyncEntityEnabled){
|
|
+ throw new IllegalStateException();
|
|
+ }
|
|
worker.executeWithSubTask(task,callBack);
|
|
}
|
|
|
|
@@ -39,12 +54,15 @@ public class ServerHook {
|
|
firstTick = true;
|
|
return;
|
|
}
|
|
+ if (!asyncEntityEnabled){
|
|
+ return;
|
|
+ }
|
|
worker.runAllSubTasks();
|
|
}
|
|
|
|
public static void callAsyncEntityTick(Entity entity, ServerLevel level){
|
|
MinecraftServer.getServer().executeMidTickTasks();
|
|
- worker.execute(()->{
|
|
+ Runnable task = ()->{
|
|
entity.activatedPriorityReset = false;
|
|
if (!entity.isRemoved()) {
|
|
entity.checkDespawn();
|
|
@@ -63,6 +81,11 @@ public class ServerHook {
|
|
throwable.printStackTrace();
|
|
}
|
|
}
|
|
- });
|
|
+ };
|
|
+ if (!asyncEntityEnabled){
|
|
+ task.run();
|
|
+ return;
|
|
+ }
|
|
+ worker.execute(task);
|
|
}
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
index e0e169a4403926ff6be004de1bf5ec2079acb440..4a9fc14ba51f8177242c0573d37fd1b4742aa0ae 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -1,11 +1,9 @@
|
|
package net.minecraft.server;
|
|
|
|
-import co.earthme.hearse.server.ServerHook;
|
|
+import co.earthme.hearse.HearseConfig;
|
|
+import co.earthme.hearse.server.ServerEntityTickHook;
|
|
import com.google.common.base.Splitter;
|
|
import com.google.common.collect.ImmutableList;
|
|
-import co.aikar.timings.Timings;
|
|
-import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
|
|
-import com.google.common.base.Stopwatch;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Maps;
|
|
import com.google.common.collect.Sets;
|
|
@@ -86,7 +84,6 @@ import net.minecraft.server.level.ServerChunkCache;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
import net.minecraft.server.level.ServerPlayerGameMode;
|
|
-import net.minecraft.server.level.TicketType;
|
|
import net.minecraft.server.level.progress.ChunkProgressListener;
|
|
import net.minecraft.server.level.progress.ChunkProgressListenerFactory;
|
|
import net.minecraft.server.network.ServerConnectionListener;
|
|
@@ -110,17 +107,14 @@ import net.minecraft.util.NativeModuleLister;
|
|
import net.minecraft.util.ProgressListener;
|
|
import net.minecraft.util.RandomSource;
|
|
import net.minecraft.util.SignatureValidator;
|
|
-import net.minecraft.util.Unit;
|
|
import net.minecraft.util.datafix.DataFixers;
|
|
import net.minecraft.util.profiling.EmptyProfileResults;
|
|
import net.minecraft.util.profiling.ProfileResults;
|
|
import net.minecraft.util.profiling.ProfilerFiller;
|
|
import net.minecraft.util.profiling.ResultField;
|
|
-import net.minecraft.util.profiling.SingleTickProfiler;
|
|
import net.minecraft.util.profiling.jfr.JvmProfiler;
|
|
import net.minecraft.util.profiling.jfr.callback.ProfiledDuration;
|
|
import net.minecraft.util.profiling.metrics.profiling.ActiveMetricsRecorder;
|
|
-import net.minecraft.util.profiling.metrics.profiling.InactiveMetricsRecorder;
|
|
import net.minecraft.util.profiling.metrics.profiling.MetricsRecorder;
|
|
import net.minecraft.util.profiling.metrics.profiling.ServerMetricsSamplersProvider;
|
|
import net.minecraft.util.profiling.metrics.storage.MetricsPersister;
|
|
@@ -184,12 +178,6 @@ import net.minecraft.world.level.levelgen.PatrolSpawner;
|
|
import net.minecraft.world.level.levelgen.PhantomSpawner;
|
|
import net.minecraft.world.level.levelgen.WorldDimensions;
|
|
import net.minecraft.world.level.levelgen.presets.WorldPresets;
|
|
-import org.bukkit.Bukkit;
|
|
-import org.bukkit.craftbukkit.CraftServer;
|
|
-import org.bukkit.craftbukkit.Main;
|
|
-import org.bukkit.craftbukkit.util.CraftChatMessage;
|
|
-import org.bukkit.craftbukkit.util.LazyPlayerSet;
|
|
-import org.bukkit.event.player.AsyncPlayerChatPreviewEvent;
|
|
import org.bukkit.event.server.ServerLoadEvent;
|
|
// CraftBukkit end
|
|
|
|
@@ -411,6 +399,8 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
// Paper end
|
|
Runtime.getRuntime().addShutdownHook(new org.bukkit.craftbukkit.util.ServerShutdownThread(this));
|
|
this.paperConfigurations = services.paperConfigurations(); // Paper
|
|
+ HearseConfig.init();
|
|
+ ServerEntityTickHook.init();
|
|
}
|
|
// CraftBukkit end
|
|
|
|
@@ -923,6 +913,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
}
|
|
if (!hasLoggedStop && isDebugging()) io.papermc.paper.util.TraceUtil.dumpTraceForThread("Server stopped"); // Paper
|
|
// Paper start - kill main thread, and kill it hard
|
|
+ HearseConfig.save(); //Hearse
|
|
shutdownThread = Thread.currentThread();
|
|
org.spigotmc.WatchdogThread.doStop(); // Paper
|
|
if (!isSameThread()) {
|
|
@@ -1408,7 +1399,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
|
|
|
|
++this.tickCount;
|
|
this.tickChildren(shouldKeepTicking);
|
|
- ServerHook.callPostTick();
|
|
+ ServerEntityTickHook.callPostTick();
|
|
if (i - this.lastServerStatus >= 5000000000L) {
|
|
this.lastServerStatus = i;
|
|
this.status.setPlayers(new ServerStatus.Players(this.getMaxPlayers(), this.getPlayerCount()));
|
|
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
index bc3cc08cd6effb9328ec74e206fe24bafc4e3d16..68523cb53573baa8ca98177f40acac3745cd625a 100644
|
|
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
|
|
@@ -2,7 +2,7 @@ package net.minecraft.server.level;
|
|
|
|
import co.aikar.timings.TimingHistory;
|
|
import co.earthme.hearse.concurrent.WorkerThread;
|
|
-import co.earthme.hearse.server.ServerHook;
|
|
+import co.earthme.hearse.server.ServerEntityTickHook;
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
import com.google.common.collect.Lists;
|
|
import com.google.common.collect.Sets;
|
|
@@ -651,7 +651,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
|
|
}
|
|
org.spigotmc.ActivationRange.activateEntities(this); // Spigot
|
|
this.entityTickList.forEach((entity) -> {
|
|
- ServerHook.callAsyncEntityTick(entity,this);
|
|
+ ServerEntityTickHook.callAsyncEntityTick(entity,this);
|
|
});
|
|
this.tickBlockEntities();
|
|
}
|