From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Sun, 3 Sep 2023 05:14:45 -0400 Subject: [PATCH] NachoSpigot: Async Explosion 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 0f3e73414ec946915c0a048c3174d516c1c93d38..b4a7f9cc14118be03e164341597c22eca2e7c8a9 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -1013,6 +1013,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, blockCache, blockPos).thenAccept((d12) -> io.papermc.paper.util.MCUtil.ensureMain(() -> { + // Leaf - Nacho end double d13; - 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; } - 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); // CraftBukkit start - Call EntityKnockbackEvent if (entity instanceof LivingEntity) { @@ -645,13 +649,11 @@ public class Explosion { } // CraftBukkit end entity.setDeltaMovement(entity.getDeltaMovement().add(vec3d1)); - 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 } } } @@ -871,7 +873,9 @@ public class Explosion { private BlockInteraction() {} } // Paper start - Optimize explosions - 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 this.getSeenFraction(vec3d, entity, blockCache, blockPos); // Paper - optimise explosions } @@ -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 a1317e374066d2abe20064bc47e20f4b19db9705..e42ad816ebf76d543c0f21f027f43e86d4898bec 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -483,6 +483,7 @@ public final class CraftServer implements Server { } datapackManager = new io.papermc.paper.datapack.PaperDatapackManager(console.getPackRepository()); // Paper 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 f798dcc331e9251ccb1161b251f7f2d5a94fa7a3..0c8c5f970221ef72c5d7139fc18568608f528d16 100644 --- a/src/main/java/org/dreeam/leaf/LeafConfig.java +++ b/src/main/java/org/dreeam/leaf/LeafConfig.java @@ -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.", @@ -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); } 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..880f2f8abc3c28bda1feec24a876693bbf6810e9 --- /dev/null +++ b/src/main/java/org/dreeam/leaf/async/AsyncExplosions.java @@ -0,0 +1,22 @@ +package org.dreeam.leaf.async; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class AsyncExplosions { + public static ExecutorService EXECUTOR; + + public static void initExecutor(boolean fixed, int size) { + EXECUTOR = fixed ? Executors.newFixedThreadPool(size) : Executors.newCachedThreadPool(); + } + + public static void stopExecutor() { + if (EXECUTOR != null) { + try { + EXECUTOR.shutdownNow(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } +}