2023-08-21 05:49:20 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sat, 4 Mar 2023 10:52:52 -0800
Subject: [PATCH] Properly handle BlockBreakEvent#isDropItems
Setting whether a block break dropped items controlled
far more than just whether blocks dropped, like stat increases
food consumption, turtle egg count decreases, ice to water
conversions and beehive releases
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
2024-10-24 10:42:29 +02:00
index 2aee9c2fbe38076317a3de7c3fdbd6988b64b389..3bd4ab8161c29bb8df2ba496a4430393b6593476 100644
2023-08-21 05:49:20 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayerGameMode.java
2024-10-24 10:42:29 +02:00
@@ -437,8 +437,8 @@ public class ServerPlayerGameMode {
2024-01-18 18:52:00 +01:00
isCorrectTool = flag1; // Paper - Trigger bee_nest_destroyed trigger in the correct place
2023-08-21 05:49:20 +02:00
2023-12-06 17:34:54 +01:00
itemstack.mineBlock(this.level, iblockdata1, pos, this.player);
2023-08-21 05:49:20 +02:00
- if (flag && flag1 && event.isDropItems()) { // CraftBukkit - Check if block should drop items
2023-12-06 17:34:54 +01:00
- block.playerDestroy(this.level, this.player, pos, iblockdata1, tileentity, itemstack1);
2024-01-05 10:07:43 +01:00
+ if (flag && flag1/* && event.isDropItems() */) { // CraftBukkit - Check if block should drop items // Paper - fix drops not preventing stats/food exhaustion
2024-01-14 16:31:39 +01:00
+ block.playerDestroy(this.level, this.player, pos, iblockdata1, tileentity, itemstack1, event.isDropItems(), false); // Paper - fix drops not preventing stats/food exhaustion
2023-08-21 05:49:20 +02:00
}
// return true; // CraftBukkit
diff --git a/src/main/java/net/minecraft/world/level/block/BeehiveBlock.java b/src/main/java/net/minecraft/world/level/block/BeehiveBlock.java
2024-10-25 14:55:49 +02:00
index d5ed53c37bba79f84b60d616887cd5176e124f10..6c0ea0bde1c36edda92807e317ed37f8b1bdac6a 100644
2023-08-21 05:49:20 +02:00
--- a/src/main/java/net/minecraft/world/level/block/BeehiveBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/BeehiveBlock.java
2024-10-24 10:42:29 +02:00
@@ -94,8 +94,8 @@ public class BeehiveBlock extends BaseEntityBlock {
2023-08-21 05:49:20 +02:00
}
@Override
- public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) {
- super.playerDestroy(world, player, pos, state, blockEntity, tool);
2024-01-14 16:31:39 +01:00
+ public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool, boolean includeDrops, boolean dropExp) { // Paper - fix drops not preventing stats/food exhaustion
+ super.playerDestroy(world, player, pos, state, blockEntity, tool, includeDrops, dropExp); // Paper - fix drops not preventing stats/food exhaustion
2024-04-25 00:36:49 +02:00
if (!world.isClientSide && blockEntity instanceof BeehiveBlockEntity tileentitybeehive) {
2024-06-14 10:17:11 +02:00
if (!EnchantmentHelper.hasTag(tool, EnchantmentTags.PREVENTS_BEE_SPAWNS_WHEN_MINING)) {
2024-04-25 00:36:49 +02:00
tileentitybeehive.emptyAllLivingFromHive(player, state, BeehiveBlockEntity.BeeReleaseStatus.EMERGENCY);
2023-08-21 05:49:20 +02:00
diff --git a/src/main/java/net/minecraft/world/level/block/Block.java b/src/main/java/net/minecraft/world/level/block/Block.java
2024-10-24 10:42:29 +02:00
index 4d140bd83ca0e1554afad80ec4fc6186188a79d8..6fb3f551f432d7e668c606fb7bd3514408e0478a 100644
2023-08-21 05:49:20 +02:00
--- a/src/main/java/net/minecraft/world/level/block/Block.java
+++ b/src/main/java/net/minecraft/world/level/block/Block.java
2024-10-24 10:42:29 +02:00
@@ -405,10 +405,18 @@ public class Block extends BlockBehaviour implements ItemLike {
2023-08-21 05:49:20 +02:00
return this.defaultBlockState();
}
2024-02-01 10:15:57 +01:00
+ @io.papermc.paper.annotation.DoNotUse @Deprecated // Paper - fix drops not preventing stats/food exhaustion
2023-08-21 05:49:20 +02:00
public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) {
2024-01-14 16:31:39 +01:00
+ // Paper start - fix drops not preventing stats/food exhaustion
2024-01-04 21:18:59 +01:00
+ this.playerDestroy(world, player, pos, state, blockEntity, tool, true, true);
2023-08-21 05:49:20 +02:00
+ }
2024-01-04 21:18:59 +01:00
+ public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool, boolean includeDrops, boolean dropExp) {
2024-01-14 16:31:39 +01:00
+ // Paper end - fix drops not preventing stats/food exhaustion
2023-08-21 05:49:20 +02:00
player.awardStat(Stats.BLOCK_MINED.get(this));
player.causeFoodExhaustion(0.005F, org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.BLOCK_MINED); // CraftBukkit - EntityExhaustionEvent
2024-01-14 16:31:39 +01:00
+ if (includeDrops) { // Paper - fix drops not preventing stats/food exhaustion
2023-08-21 05:49:20 +02:00
Block.dropResources(state, world, pos, blockEntity, player, tool);
2024-01-14 16:31:39 +01:00
+ } // Paper - fix drops not preventing stats/food exhaustion
2023-08-21 05:49:20 +02:00
}
public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) {}
diff --git a/src/main/java/net/minecraft/world/level/block/DoublePlantBlock.java b/src/main/java/net/minecraft/world/level/block/DoublePlantBlock.java
2024-10-24 10:42:29 +02:00
index edb3b6cdb617c48140539728af1373993e78648f..4fe83bd0f355549847b66afb7e61f6f2a6d97016 100644
2023-08-21 05:49:20 +02:00
--- a/src/main/java/net/minecraft/world/level/block/DoublePlantBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/DoublePlantBlock.java
2024-10-24 10:42:29 +02:00
@@ -98,8 +98,8 @@ public class DoublePlantBlock extends BushBlock {
2023-08-21 05:49:20 +02:00
}
@Override
- public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) {
- super.playerDestroy(world, player, pos, Blocks.AIR.defaultBlockState(), blockEntity, tool);
2024-01-14 16:31:39 +01:00
+ public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool, boolean includeDrops, boolean dropExp) { // Paper - fix drops not preventing stats/food exhaustion
+ super.playerDestroy(world, player, pos, Blocks.AIR.defaultBlockState(), blockEntity, tool, includeDrops, dropExp); // Paper - fix drops not preventing stats/food exhaustion
2023-08-21 05:49:20 +02:00
}
2023-12-06 17:34:54 +01:00
protected static void preventDropFromBottomPart(Level world, BlockPos pos, BlockState state, Player player) {
2023-08-21 05:49:20 +02:00
diff --git a/src/main/java/net/minecraft/world/level/block/IceBlock.java b/src/main/java/net/minecraft/world/level/block/IceBlock.java
2024-10-24 10:42:29 +02:00
index 067a2d969ca0979ae67b600e303deec93eda0251..a94762e65853ccad38cf90b0049ca256106c0c9f 100644
2023-08-21 05:49:20 +02:00
--- a/src/main/java/net/minecraft/world/level/block/IceBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/IceBlock.java
2024-10-24 10:42:29 +02:00
@@ -34,8 +34,8 @@ public class IceBlock extends HalfTransparentBlock {
2023-08-21 05:49:20 +02:00
}
@Override
- public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) {
- super.playerDestroy(world, player, pos, state, blockEntity, tool);
2024-01-14 16:31:39 +01:00
+ public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool, boolean includeDrops, boolean dropExp) { // Paper - fix drops not preventing stats/food exhaustion
+ super.playerDestroy(world, player, pos, state, blockEntity, tool, includeDrops, dropExp); // Paper - fix drops not preventing stats/food exhaustion
2024-01-21 12:53:04 +01:00
// Paper start - Improve Block#breakNaturally API
2023-08-21 05:49:20 +02:00
this.afterDestroy(world, pos, tool);
}
diff --git a/src/main/java/net/minecraft/world/level/block/TurtleEggBlock.java b/src/main/java/net/minecraft/world/level/block/TurtleEggBlock.java
2024-10-24 10:42:29 +02:00
index 85d9829e979ef5f22aee9c346800612f479786b7..953ddb2ea6fd48e57712e30a6addf23e188e5312 100644
2023-08-21 05:49:20 +02:00
--- a/src/main/java/net/minecraft/world/level/block/TurtleEggBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/TurtleEggBlock.java
2024-10-24 10:42:29 +02:00
@@ -175,8 +175,8 @@ public class TurtleEggBlock extends Block {
2023-08-21 05:49:20 +02:00
}
@Override
- public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) {
- super.playerDestroy(world, player, pos, state, blockEntity, tool);
2024-01-14 16:31:39 +01:00
+ public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool, boolean includeDrops, boolean dropExp) { // Paper - fix drops not preventing stats/food exhaustion
+ super.playerDestroy(world, player, pos, state, blockEntity, tool, includeDrops, dropExp); // Paper - fix drops not preventing stats/food exhaustion
2023-08-21 05:49:20 +02:00
this.decreaseEggs(world, pos, state);
}
diff --git a/src/test/java/io/papermc/paper/world/block/BlockPlayerDestroyOverrideTest.java b/src/test/java/io/papermc/paper/world/block/BlockPlayerDestroyOverrideTest.java
new file mode 100644
2024-10-21 00:06:54 +02:00
index 0000000000000000000000000000000000000000..22145bf698b3d1ff0a07a3aaa8d55a19905f99ad
2023-08-21 05:49:20 +02:00
--- /dev/null
+++ b/src/test/java/io/papermc/paper/world/block/BlockPlayerDestroyOverrideTest.java
2024-10-21 00:06:54 +02:00
@@ -0,0 +1,48 @@
2023-08-21 05:49:20 +02:00
+package io.papermc.paper.world.block;
+
+import io.github.classgraph.ClassGraph;
+import io.github.classgraph.ClassInfo;
+import io.github.classgraph.MethodInfo;
+import io.github.classgraph.MethodInfoList;
+import io.github.classgraph.MethodParameterInfo;
+import io.github.classgraph.ScanResult;
+import java.util.ArrayList;
+import java.util.List;
2023-09-24 09:16:58 +02:00
+import java.util.stream.Stream;
2024-10-21 00:06:54 +02:00
+import org.bukkit.support.environment.Normal;
2023-09-24 09:16:58 +02:00
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
2023-08-21 05:49:20 +02:00
+
2023-09-24 09:16:58 +02:00
+import static org.junit.jupiter.api.Assertions.assertEquals;
2023-08-21 05:49:20 +02:00
+
2024-10-21 00:06:54 +02:00
+@Normal
+public class BlockPlayerDestroyOverrideTest {
2023-08-21 05:49:20 +02:00
+
2023-09-24 09:16:58 +02:00
+ public static Stream<ClassInfo> parameters() {
2023-08-21 05:49:20 +02:00
+ final List<ClassInfo> classInfo = new ArrayList<>();
+ try (ScanResult scanResult = new ClassGraph()
+ .enableClassInfo()
+ .enableMethodInfo()
+ .whitelistPackages("net.minecraft")
+ .scan()
+ ) {
+ for (final ClassInfo subclass : scanResult.getSubclasses("net.minecraft.world.level.block.Block")) {
+ final MethodInfoList playerDestroy = subclass.getDeclaredMethodInfo("playerDestroy");
+ if (!playerDestroy.isEmpty()) {
+ classInfo.add(subclass);
+ }
+ }
+ }
2023-09-24 09:16:58 +02:00
+ return classInfo.stream();
2023-08-21 05:49:20 +02:00
+ }
+
2023-09-24 09:16:58 +02:00
+ @ParameterizedTest
+ @MethodSource("parameters")
+ public void checkPlayerDestroyOverrides(ClassInfo overridesPlayerDestroy) {
+ final MethodInfoList playerDestroy = overridesPlayerDestroy.getDeclaredMethodInfo("playerDestroy");
+ assertEquals(1, playerDestroy.size(), overridesPlayerDestroy.getName() + " has multiple playerDestroy methods");
2023-08-21 05:49:20 +02:00
+ final MethodInfo next = playerDestroy.iterator().next();
+ final MethodParameterInfo[] parameterInfo = next.getParameterInfo();
2023-09-25 01:05:05 +02:00
+ assertEquals("boolean", parameterInfo[parameterInfo.length - 1].getTypeDescriptor().toStringWithSimpleNames(), overridesPlayerDestroy.getName() + " needs to change its override of playerDestroy");
2023-08-21 05:49:20 +02:00
+ }
+}