From b9d3147d3be6d8a35fc7fbf1fab922ec73a74636 Mon Sep 17 00:00:00 2001 From: Bjarne Koll Date: Sun, 27 Apr 2025 14:19:42 +0200 Subject: [PATCH] Use correct placed block position for sound (#12410) Previously the server attempted to compute the block placed by using the BlockPlaceContext. This approach however fails on replacable blocks, as the BlockPlaceContext computes this during its construction, which happened after the actual world modification. The commit reworks this approach and now stores metadata in the InteractionResult which can later be read. The diff is structured to allow for easy future expansion of the tracked metadata. --- .../features/0027-Optimize-Hoppers.patch | 6 +-- .../world/InteractionResult.java.patch | 40 +++++++++++++++++++ .../minecraft/world/item/BlockItem.java.patch | 9 +++++ .../minecraft/world/item/ItemStack.java.patch | 7 ++-- 4 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 paper-server/patches/sources/net/minecraft/world/InteractionResult.java.patch diff --git a/paper-server/patches/features/0027-Optimize-Hoppers.patch b/paper-server/patches/features/0027-Optimize-Hoppers.patch index 6aa172b31e..6cf9616f88 100644 --- a/paper-server/patches/features/0027-Optimize-Hoppers.patch +++ b/paper-server/patches/features/0027-Optimize-Hoppers.patch @@ -48,7 +48,7 @@ index 0000000000000000000000000000000000000000..24a2090e068ad3c0d08705050944abdf + } +} diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java -index ea85cac4a41075efe8525c40755e7ebac6ca9dea..7af29d3dc7b337d74cee5df7cbca35c420643370 100644 +index 79bc1b7d9f640d2322814177eb3e921da8671e87..f1373fd5fdebb9f4600ba7f32a5df6188de3a0e9 100644 --- a/net/minecraft/server/MinecraftServer.java +++ b/net/minecraft/server/MinecraftServer.java @@ -1706,6 +1706,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop edit) { ++ return new InteractionResult.Success(this.swingSource, this.itemContext, edit.apply(this.paperSuccessContext)); ++ } ++ ++ public Success(final net.minecraft.world.InteractionResult.SwingSource swingSource, final net.minecraft.world.InteractionResult.ItemContext itemContext) { ++ this(swingSource, itemContext, PaperSuccessContext.DEFAULT); ++ } ++ // Paper end - track more context in interaction result + @Override + public boolean consumesAction() { + return true; + } + + public InteractionResult.Success heldItemTransformedTo(ItemStack stack) { +- return new InteractionResult.Success(this.swingSource, new InteractionResult.ItemContext(true, stack)); ++ return new InteractionResult.Success(this.swingSource, new InteractionResult.ItemContext(true, stack), this.paperSuccessContext); // Paper - track more context in interaction result + } + + public InteractionResult.Success withoutItem() { +- return new InteractionResult.Success(this.swingSource, InteractionResult.ItemContext.NONE); ++ return new InteractionResult.Success(this.swingSource, InteractionResult.ItemContext.NONE, this.paperSuccessContext); // Paper - track more context in interaction result + } + + public boolean wasItemInteraction() { diff --git a/paper-server/patches/sources/net/minecraft/world/item/BlockItem.java.patch b/paper-server/patches/sources/net/minecraft/world/item/BlockItem.java.patch index bbcf05ba06..a529dd9ad7 100644 --- a/paper-server/patches/sources/net/minecraft/world/item/BlockItem.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/item/BlockItem.java.patch @@ -56,6 +56,15 @@ level.playSound( player, clickedPos, +@@ -88,7 +_,7 @@ + ); + level.gameEvent(GameEvent.BLOCK_PLACE, clickedPos, GameEvent.Context.of(player, blockState)); + itemInHand.consume(1, player); +- return InteractionResult.SUCCESS; ++ return InteractionResult.SUCCESS.configurePaper(e -> e.placedBlockAt(clickedPos.immutable())); // Paper - track placed block position from block item + } + } + } @@ -137,8 +_,19 @@ protected boolean canPlace(BlockPlaceContext context, BlockState state) { diff --git a/paper-server/patches/sources/net/minecraft/world/item/ItemStack.java.patch b/paper-server/patches/sources/net/minecraft/world/item/ItemStack.java.patch index 6d3893213c..443385a232 100644 --- a/paper-server/patches/sources/net/minecraft/world/item/ItemStack.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/item/ItemStack.java.patch @@ -23,7 +23,7 @@ } } }; -@@ -373,10 +_,167 @@ +@@ -373,10 +_,166 @@ return InteractionResult.PASS; } else { Item item = this.getItem(); @@ -175,10 +175,9 @@ + } + + // SPIGOT-1288 - play sound stripped from BlockItem -+ if (this.item instanceof BlockItem) { ++ if (this.item instanceof BlockItem && success.paperSuccessContext().placedBlockPosition() != null) { + // Paper start - Fix spigot sound playing for BlockItem ItemStacks -+ BlockPos pos = new net.minecraft.world.item.context.BlockPlaceContext(context).getClickedPos(); -+ net.minecraft.world.level.block.state.BlockState state = serverLevel.getBlockState(pos); ++ net.minecraft.world.level.block.state.BlockState state = serverLevel.getBlockState(success.paperSuccessContext().placedBlockPosition()); + net.minecraft.world.level.block.SoundType soundType = state.getSoundType(); + // Paper end - Fix spigot sound playing for BlockItem ItemStacks + serverLevel.playSound(player, clickedPos, soundType.getPlaceSound(), net.minecraft.sounds.SoundSource.BLOCKS, (soundType.getVolume() + 1.0F) / 2.0F, soundType.getPitch() * 0.8F);