From 8ceb19a0654719b3e8037e3b7972adcff11c6ff6 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Mon, 4 Aug 2025 02:19:03 +0800 Subject: [PATCH] Skip BlockPhysicsEvent if no listeners --- ...ip-BlockPhysicsEvent-if-no-listeners.patch | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 leaf-server/minecraft-patches/features/0280-Skip-BlockPhysicsEvent-if-no-listeners.patch diff --git a/leaf-server/minecraft-patches/features/0280-Skip-BlockPhysicsEvent-if-no-listeners.patch b/leaf-server/minecraft-patches/features/0280-Skip-BlockPhysicsEvent-if-no-listeners.patch new file mode 100644 index 00000000..0d7e7d05 --- /dev/null +++ b/leaf-server/minecraft-patches/features/0280-Skip-BlockPhysicsEvent-if-no-listeners.patch @@ -0,0 +1,30 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> +Date: Sun, 3 Aug 2025 22:29:34 +0800 +Subject: [PATCH] Skip BlockPhysicsEvent if no listeners + +BlockPhysicsEvent creates new CraftBlock instance and clones CraftBlockData when constructed it. +It's a defensive programming stratgy and expensive if there are many neighbor updates. +However, there is no plugin listening to this event sometimes, so that just add a check to not +call it if no listeners listening to it. + +diff --git a/net/minecraft/world/level/redstone/NeighborUpdater.java b/net/minecraft/world/level/redstone/NeighborUpdater.java +index 332b33a004ab11150cca0cc2cefc26d0286648f5..3bbdfc1f1df1eaf76c3fdf977845a24c7f26ceb3 100644 +--- a/net/minecraft/world/level/redstone/NeighborUpdater.java ++++ b/net/minecraft/world/level/redstone/NeighborUpdater.java +@@ -49,6 +49,7 @@ public interface NeighborUpdater { + static void executeUpdate(Level level, BlockState state, BlockPos pos, Block neighborBlock, @Nullable Orientation orientation, boolean movedByPiston, BlockPos sourcePos) { + // Paper end - Add source block to BlockPhysicsEvent + try { ++ if (org.bukkit.event.block.BlockPhysicsEvent.getHandlerList().getRegisteredListeners().length != 0) { // Leaf - Skip BlockPhysicsEvent if no listeners + // CraftBukkit start + org.bukkit.event.block.BlockPhysicsEvent event = new org.bukkit.event.block.BlockPhysicsEvent(org.bukkit.craftbukkit.block.CraftBlock.at(level, pos), org.bukkit.craftbukkit.block.data.CraftBlockData.fromData(state), org.bukkit.craftbukkit.block.CraftBlock.at(level, sourcePos)); // Paper - Add source block to BlockPhysicsEvent + level.getCraftServer().getPluginManager().callEvent(event); +@@ -57,6 +58,7 @@ public interface NeighborUpdater { + return; + } + // CraftBukkit end ++ } // Leaf - Skip BlockPhysicsEvent if no listeners + state.handleNeighborChanged(level, pos, neighborBlock, orientation, movedByPiston); + // Spigot start + } catch (StackOverflowError ex) {