From 637bc6dd2593166a4ce6e7a2be9e6b05661bf6da Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Wed, 14 Feb 2024 03:05:47 -0500 Subject: [PATCH] [ci skip] Rebase, optimize and fix Async Explosion patch --- .../0039-NachoSpigot-Async-Explosion.patch | 171 ++++++++++-------- 1 file changed, 91 insertions(+), 80 deletions(-) diff --git a/patches/unapplied/server/0039-NachoSpigot-Async-Explosion.patch b/patches/unapplied/server/0039-NachoSpigot-Async-Explosion.patch index b649aa86..0b665d5b 100644 --- a/patches/unapplied/server/0039-NachoSpigot-Async-Explosion.patch +++ b/patches/unapplied/server/0039-NachoSpigot-Async-Explosion.patch @@ -7,10 +7,10 @@ Original code by CobbleSword, licensed under GPL v3 You can find the original code on https://github.com/CobbleSword/NachoSpigot diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java -index 312ba9705533f5790a95ed484d32ca46e9377887..40e8d55244f53ac867dad0f56994e60ca83a80f0 100644 +index 0f3e73414ec946915c0a048c3174d516c1c93d38..b4a7f9cc14118be03e164341597c22eca2e7c8a9 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java -@@ -988,6 +988,8 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop list = this.level.getEntities(this.source, new AABB((double) i, (double) l, (double) j1, (double) j, (double) i1, (double) k1), (com.google.common.base.Predicate) entity -> entity.isAlive() && !entity.isSpectator()); // Paper - Fix lag from explosions processing dead entities + Vec3 vec3d = new Vec3(this.x, this.y, this.z); +- Iterator iterator = list.iterator(); + + final BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos(); // Paper - optimise explosions + +- while (iterator.hasNext()) { +- Entity entity = (Entity) iterator.next(); +- ++ for (Entity entity : list) { // Leaf - Improve readability + if (!entity.ignoreExplosion(this)) { + double d7 = Math.sqrt(entity.distanceToSqr(vec3d)) / (double) f2; + +@@ -620,21 +619,26 @@ public class Explosion { + // CraftBukkit end + } + +- double d12 = (1.0D - d7) * this.getBlockDensity(vec3d, entity, blockCache, blockPos); // Paper - Optimize explosions ++ // Leaf start - Nacho - Async explosion ++ //double d12 = (1.0D - d7) * this.getBlockDensity(vec3d, entity, blockCache, blockPos); // Paper - Optimize explosions + double finalD8 = d8; + double finalD9 = d9; + double finalD10 = d10; -+ this.getBlockDensity(vec3d, entity).thenAccept((d12) -> io.papermc.paper.util.MCUtil.ensureMain(() -> { -+ - double d13 = (1.0D - d7) * d12; ++ this.getBlockDensity(vec3d, entity, blockCache, blockPos).thenAccept((d12) -> io.papermc.paper.util.MCUtil.ensureMain(() -> { ++ // Leaf - Nacho end + double d13; - // CraftBukkit start -@@ -259,8 +269,8 @@ public class Explosion { - // - Damaging ComplexEntityPart while forward the damage to EntityEnderDragon - // - Damaging EntityEnderDragon does nothing - // - EntityEnderDragon hitbock always covers the other parts and is therefore always present -- if (entity instanceof EnderDragonPart) { -- continue; -+ if (!(entity instanceof EnderDragonPart)) { -+ return; +- if (entity instanceof LivingEntity) { +- LivingEntity entityliving = (LivingEntity) entity; +- ++ if (entity instanceof LivingEntity entityliving) { // Leaf - Improve readability + d13 = entity instanceof Player && level.paperConfig().environment.disableExplosionKnockback ? 0 : ProtectionEnchantment.getExplosionKnockbackAfterDampener(entityliving, d12); // Paper - Option to disable explosion knockback + } else { + d13 = d12; } - CraftEventFactory.entityDamage = this.source; -@@ -280,8 +290,8 @@ public class Explosion { - } +- d8 *= d13; +- d9 *= d13; +- d10 *= d13; +- Vec3 vec3d1 = new Vec3(d8, d9, d10); ++ // Leaf start - Nacho - Async explosion ++ //d8 *= d13; ++ //d9 *= d13; ++ //d10 *= d13; ++ Vec3 vec3d1 = new Vec3(finalD8 * d13, finalD9 * d13, finalD10 * d13); - CraftEventFactory.entityDamage = null; -- if (entity.lastDamageCancelled) { // SPIGOT-5339, SPIGOT-6252, SPIGOT-6777: Skip entity if damage event was cancelled -- continue; -+ if (!entity.lastDamageCancelled) { // SPIGOT-5339, SPIGOT-6252, SPIGOT-6777: Skip entity if damage event was cancelled -+ return; + // CraftBukkit start - Call EntityKnockbackEvent + if (entity instanceof LivingEntity) { +@@ -645,13 +649,11 @@ public class Explosion { } // CraftBukkit end - double d14; -@@ -294,10 +304,7 @@ public class Explosion { - d14 = d13; - } - -- d8 *= d14; -- d9 *= d14; -- d10 *= d14; -- Vec3 vec3d1 = new Vec3(d8, d9, d10); -+ Vec3 vec3d1 = new Vec3(finalD8 * d14, finalD9 * d14, finalD10 * d14); - entity.setDeltaMovement(entity.getDeltaMovement().add(vec3d1)); - if (entity instanceof Player) { -@@ -306,7 +313,7 @@ public class Explosion { - if (!entityhuman.isSpectator() && (!entityhuman.isCreative() || !entityhuman.getAbilities().flying) && !level.paperConfig().environment.disableExplosionKnockback) { // Paper - Disable explosion knockback +- if (entity instanceof Player) { +- Player entityhuman = (Player) entity; +- ++ if (entity instanceof Player entityhuman) { // Leaf - Improve readability + if (!entityhuman.isSpectator() && (!entityhuman.isCreative() || !entityhuman.getAbilities().flying) && !level.paperConfig().environment.disableExplosionKnockback) { // Paper - Option to disable explosion knockback this.hitPlayers.put(entityhuman, vec3d1); } - } -+ }})); ++ }}));// Leaf - Nacho end } } } -@@ -539,7 +546,8 @@ public class Explosion { +@@ -871,7 +873,9 @@ public class Explosion { private BlockInteraction() {} } // Paper start - Optimize explosions -- private float getBlockDensity(Vec3 vec3d, Entity entity) { -+ private CompletableFuture getBlockDensity(Vec3 vec3d, Entity entity) { +- private float getBlockDensity(Vec3 vec3d, Entity entity, ExplosionBlockCache[] blockCache, BlockPos.MutableBlockPos blockPos) { // Paper - optimise explosions ++ // Leaf start - Nacho - Async explosion ++ private CompletableFuture getBlockDensity(Vec3 vec3d, Entity entity, ExplosionBlockCache[] blockCache, BlockPos.MutableBlockPos blockPos) { // Paper - optimise explosions + return CompletableFuture.supplyAsync(() -> { if (!this.level.paperConfig().environment.optimizeExplosions) { - return getSeenPercent(vec3d, entity); + return this.getSeenFraction(vec3d, entity, blockCache, blockPos); // Paper - optimise explosions } -@@ -551,6 +559,7 @@ public class Explosion { +@@ -883,6 +887,8 @@ public class Explosion { } return blockDensity; + }, AsyncExplosions.EXECUTOR); ++ // Leaf - Nacho end } static class CacheKey { diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java -index 804a784cae66cdc876b94c94373478438d75fd5b..6ab032f7e5f3d4381d4135d40e20e510ceeecfed 100644 +index a1317e374066d2abe20064bc47e20f4b19db9705..e42ad816ebf76d543c0f21f027f43e86d4898bec 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java -@@ -485,6 +485,7 @@ public final class CraftServer implements Server { +@@ -483,6 +483,7 @@ public final class CraftServer implements Server { } datapackManager = new io.papermc.paper.datapack.PaperDatapackManager(console.getPackRepository()); // Paper - top.leavesmc.leaves.protocol.JadeProtocol.init(); // Leaves - Jade -+ org.dreeam.leaf.async.AsyncExplosions.initExecutor(org.dreeam.leaf.LeafConfig.useFixedPoolForTNT, org.dreeam.leaf.LeafConfig.fixedPoolSize); + top.leavesmc.leaves.protocol.core.LeavesProtocolManager.init(); // Leaves - protocol ++ org.dreeam.leaf.async.AsyncExplosions.initExecutor(org.dreeam.leaf.LeafConfig.useFixedPoolForTNT, org.dreeam.leaf.LeafConfig.fixedPoolSize); // Leaf - Nacho - Async explosion } public boolean getCommandBlockOverride(String command) { diff --git a/src/main/java/org/dreeam/leaf/LeafConfig.java b/src/main/java/org/dreeam/leaf/LeafConfig.java -index 32980bd223a68e7ac2c953e953434d1fef6e0f9f..5c71d9dd8c362ca8a940a4b75d31414f03c56315 100644 +index f798dcc331e9251ccb1161b251f7f2d5a94fa7a3..0c8c5f970221ef72c5d7139fc18568608f528d16 100644 --- a/src/main/java/org/dreeam/leaf/LeafConfig.java +++ b/src/main/java/org/dreeam/leaf/LeafConfig.java -@@ -205,6 +205,8 @@ public class LeafConfig { - public static boolean enableAsyncEntityTrackerInitialized; - public static boolean enableSyncEventCallsOnAsyncThreads = true; - public static boolean enableSyncEventCallsOnAsyncThreadsInitialized; +@@ -207,6 +207,8 @@ public class LeafConfig { + public static int asyncPathfindingKeepalive = 60; + public static boolean cacheMinecartCollision = false; + public static boolean skipMapItemDataUpdatesIfMapDoesNotHaveCraftMapRenderer = true; + public static boolean useFixedPoolForTNT = false; + public static int fixedPoolSize = 500; private static void performance() { boolean asyncMobSpawning = getBoolean("performance.enable-async-mob-spawning", enableAsyncMobSpawning, "Whether or not asynchronous mob spawning should be enabled.", -@@ -279,6 +281,8 @@ public class LeafConfig { - enableSyncEventCallsOnAsyncThreadsInitialized = true; - enableSyncEventCallsOnAsyncThreads = syncEventCalls; - } +@@ -270,6 +272,8 @@ public class LeafConfig { + cacheMinecartCollision = getBoolean("performance.cache-minecart-collision", cacheMinecartCollision, + "Cache the minecart collision result to prevent massive stacked minecart lag the server."); + skipMapItemDataUpdatesIfMapDoesNotHaveCraftMapRenderer = getBoolean("performance.skip-map-item-data-updates-if-map-does-not-have-craftmaprenderer", skipMapItemDataUpdatesIfMapDoesNotHaveCraftMapRenderer); + useFixedPoolForTNT = getBoolean("performance.async-explosion.use-fixed-pools-for-explosions", useFixedPoolForTNT); + fixedPoolSize = getInt("performance.async-explosion.fixed-pools-size", fixedPoolSize); } @@ -153,26 +161,29 @@ index 32980bd223a68e7ac2c953e953434d1fef6e0f9f..5c71d9dd8c362ca8a940a4b75d31414f public static boolean jadeProtocol = false; diff --git a/src/main/java/org/dreeam/leaf/async/AsyncExplosions.java b/src/main/java/org/dreeam/leaf/async/AsyncExplosions.java new file mode 100644 -index 0000000000000000000000000000000000000000..caacc7e1d180b13644fbfc44a1ae6eb925268fef +index 0000000000000000000000000000000000000000..880f2f8abc3c28bda1feec24a876693bbf6810e9 --- /dev/null +++ b/src/main/java/org/dreeam/leaf/async/AsyncExplosions.java -@@ -0,0 +1,18 @@ +@@ -0,0 +1,22 @@ +package org.dreeam.leaf.async; ++ ++import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; -+import java.util.concurrent.ThreadPoolExecutor; + +public class AsyncExplosions { -+ public static ThreadPoolExecutor EXECUTOR; ++ public static ExecutorService EXECUTOR; + + public static void initExecutor(boolean fixed, int size) { -+ if (fixed) -+ EXECUTOR = (ThreadPoolExecutor) Executors.newFixedThreadPool(size); -+ else -+ EXECUTOR = (ThreadPoolExecutor) Executors.newCachedThreadPool(); ++ EXECUTOR = fixed ? Executors.newFixedThreadPool(size) : Executors.newCachedThreadPool(); + } + + public static void stopExecutor() { -+ if (EXECUTOR != null) EXECUTOR.shutdown(); ++ if (EXECUTOR != null) { ++ try { ++ EXECUTOR.shutdownNow(); ++ } catch (Exception e) { ++ e.printStackTrace(); ++ } ++ } + } +} -\ No newline at end of file