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 Removed since useless 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 8ca3f0e06f163d9ae2910c9fb2475af70b39b943..6f0f56a5ae2edbe25cc061b64289a1c00cfb6c0d 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -1020,6 +1020,8 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop blockCache = null; + private java.util.concurrent.ConcurrentHashMap blockCache = null; public static final class ExplosionBlockCache { @@ -457,7 +457,7 @@ public class Explosion { int j; // Paper start - optimise explosions - this.blockCache = new it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap<>(); + this.blockCache = new java.util.concurrent.ConcurrentHashMap<>(); // Leaf this.chunkPosCache = new long[CHUNK_CACHE_WIDTH * CHUNK_CACHE_WIDTH]; java.util.Arrays.fill(this.chunkPosCache, ChunkPos.INVALID_CHUNK_POS); @@ -571,13 +571,10 @@ public class Explosion { int k1 = Mth.floor(this.z + (double) f2 + 1.0D); List 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; @@ -623,21 +620,25 @@ public class Explosion { // CraftBukkit end } - double d12 = (1.0D - d7) * this.getBlockDensity(vec3d, entity, blockCache, blockPos) * (double) this.damageCalculator.getKnockbackMultiplier(entity); // Paper - Optimize explosions + // Leaf start - Nacho - Async explosion + //double d12 = (1.0D - d7) * this.getBlockDensity(vec3d, entity, blockCache, blockPos) * (double) this.damageCalculator.getKnockbackMultiplier(entity); // 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) { 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); + //d8 *= d13; + //d9 *= d13; + //d10 *= d13; + Vec3 vec3d1 = new Vec3(finalD8 * d13, finalD9 * d13, finalD10 * d13); // CraftBukkit start - Call EntityKnockbackEvent if (entity instanceof LivingEntity) { @@ -648,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) { if (!entityhuman.isSpectator() && (!entityhuman.isCreative() || !entityhuman.getAbilities().flying) && !level.paperConfig().environment.disableExplosionKnockback) { // Paper - Option to disable explosion knockback this.hitPlayers.put(entityhuman, vec3d1); } - } + }}));// Leaf end - Nacho - Async explosion entity.onExplosionHit(this.source); } @@ -878,7 +877,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 java.util.concurrent.CompletableFuture getBlockDensity(Vec3 vec3d, Entity entity, ExplosionBlockCache[] blockCache, BlockPos.MutableBlockPos blockPos) { // Paper - optimise explosions + return java.util.concurrent.CompletableFuture.supplyAsync(() -> { if (!this.level.paperConfig().environment.optimizeExplosions) { return this.getSeenFraction(vec3d, entity, blockCache, blockPos); // Paper - optimise explosions } @@ -890,6 +891,8 @@ public class Explosion { } return blockDensity; + }, org.dreeam.leaf.async.AsyncExplosions.EXECUTOR); + // Leaf end - Nacho - Async explosion } static class CacheKey { diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 4b00b6d66ebe578778e103335747183b9e44c94c..63f9cfaac4a3b1e7e3a5cceedd9cb58be3a5b139 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -490,6 +490,7 @@ public final class CraftServer implements Server { this.potionBrewer = new io.papermc.paper.potion.PaperPotionBrewer(console); // Paper - custom potion mixes datapackManager = new io.papermc.paper.datapack.PaperDatapackManager(console.getPackRepository()); // Paper org.leavesmc.leaves.protocol.core.LeavesProtocolManager.init(); // Leaves - protocol + org.dreeam.leaf.async.AsyncExplosions.initExecutor(org.dreeam.leaf.config.modules.async.AsyncExplosion.useFixedPoolForTNT, org.dreeam.leaf.config.modules.async.AsyncExplosion.fixedPoolSize); // Leaf - Nacho - Async explosion } public boolean getCommandBlockOverride(String command) { 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..3a755d81e252355d38f1e7dac624225c72a81d78 --- /dev/null +++ b/src/main/java/org/dreeam/leaf/async/AsyncExplosions.java @@ -0,0 +1,23 @@ +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.shutdown(); + } catch (Exception e) { + e.printStackTrace(); + EXECUTOR.shutdownNow(); + } + } + } +} diff --git a/src/main/java/org/dreeam/leaf/config/modules/async/AsyncExplosion.java b/src/main/java/org/dreeam/leaf/config/modules/async/AsyncExplosion.java new file mode 100644 index 0000000000000000000000000000000000000000..f31538b219b942cf74ed826aa2768554bc8a1a72 --- /dev/null +++ b/src/main/java/org/dreeam/leaf/config/modules/async/AsyncExplosion.java @@ -0,0 +1,27 @@ +package org.dreeam.leaf.config.modules.async; + +import com.electronwill.nightconfig.core.file.CommentedFileConfig; +import org.dreeam.leaf.config.ConfigInfo; +import org.dreeam.leaf.config.DoNotLoad; +import org.dreeam.leaf.config.EnumConfigCategory; +import org.dreeam.leaf.config.IConfigModule; + +public class AsyncExplosion implements IConfigModule { + + @Override + public EnumConfigCategory getCategory() { + return EnumConfigCategory.ASYNC; + } + + @Override + public String getBaseName() { + return "async_explosion"; + } + + @ConfigInfo(baseName = "enabled") + public static boolean enabled = true; + @ConfigInfo(baseName = "use-fixed-pool") + public static boolean useFixedPoolForTNT = true; + @ConfigInfo(baseName = "fixed-pool-size") + public static int fixedPoolSize = 500; +}