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-01-05 10:07:43 +01:00
index 82f26186156a487f29ad3abff3f68852e5b8a1f9..3088cf508427ca03804bfa4c081bb6dfcce07d45 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
2023-12-03 04:41:35 +01:00
@@ -433,8 +433,8 @@ public class ServerPlayerGameMode {
2023-08-21 05:49:20 +02:00
isCorrectTool = flag1; // Paper
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-04 21:18:59 +01:00
+ block.playerDestroy(this.level, this.player, pos, iblockdata1, tileentity, itemstack1, event.isDropItems(), false); // Paper
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-01-04 21:18:59 +01:00
index 6e7a9f68aa3a5084c8eea9fd8721272260734289..2594c8e233114b21e5b00acb5ad7012b004a0ef2 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
2023-12-06 17:34:54 +01:00
@@ -84,8 +84,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-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) { // Paper
+ super.playerDestroy(world, player, pos, state, blockEntity, tool, includeDrops, dropExp); // Paper
2023-08-21 05:49:20 +02:00
if (!world.isClientSide && blockEntity instanceof BeehiveBlockEntity) {
BeehiveBlockEntity tileentitybeehive = (BeehiveBlockEntity) blockEntity;
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-01-05 10:07:43 +01:00
index 38d587c93a9ae5706633608f681f18684b7a631d..c450caed1a8d14f3bd56d0336a9bf72a5adca14f 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
2023-12-06 17:34:54 +01:00
@@ -423,10 +423,18 @@ public class Block extends BlockBehaviour implements ItemLike {
2023-08-21 05:49:20 +02:00
return this.defaultBlockState();
}
+ @io.papermc.paper.annotation.DoNotUse // Paper - method below allows better control of item drops
public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) {
+ // Paper start
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) {
2023-08-21 05:49:20 +02:00
+ // Paper end
player.awardStat(Stats.BLOCK_MINED.get(this));
player.causeFoodExhaustion(0.005F, org.bukkit.event.entity.EntityExhaustionEvent.ExhaustionReason.BLOCK_MINED); // CraftBukkit - EntityExhaustionEvent
+ if (includeDrops) { // Paper
Block.dropResources(state, world, pos, blockEntity, player, tool);
+ } // Paper
}
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-01-04 21:18:59 +01:00
index 0f0750f8c790d0db72a0e6b277449a1461674890..a6a257027d60bfda8cb975eca7f255fb1bd1e8d5 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
2023-12-06 17:34:54 +01:00
@@ -96,8 +96,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-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) { // Paper
+ super.playerDestroy(world, player, pos, Blocks.AIR.defaultBlockState(), blockEntity, tool, includeDrops, dropExp); // Paper
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-01-04 21:18:59 +01:00
index f05998e0af1e844f19bf86b74f652a9901088c37..9ebe74e235d425fde985a6180857dc4039ecfedf 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
2023-12-06 17:34:54 +01:00
@@ -33,8 +33,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-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) { // Paper
+ super.playerDestroy(world, player, pos, state, blockEntity, tool, includeDrops, dropExp); // Paper
2023-08-21 05:49:20 +02:00
// Paper start
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-01-04 21:18:59 +01:00
index c79f3a8885a5ffc9ebac51992e63df14929d9f24..5c0127ecbbafd406f450f8707c4563bfea7a0214 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
2023-12-06 17:34:54 +01:00
@@ -173,8 +173,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-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) { // Paper
+ super.playerDestroy(world, player, pos, state, blockEntity, tool, includeDrops, dropExp); // Paper
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
2023-09-25 01:05:05 +02:00
index 0000000000000000000000000000000000000000..7c435f7079b429873f33d7bade82eca0c6b45842
2023-08-21 05:49:20 +02:00
--- /dev/null
+++ b/src/test/java/io/papermc/paper/world/block/BlockPlayerDestroyOverrideTest.java
2023-09-25 01:05:05 +02:00
@@ -0,0 +1,47 @@
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;
2023-08-21 05:49:20 +02:00
+import org.bukkit.support.AbstractTestingBase;
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
+
+public class BlockPlayerDestroyOverrideTest extends AbstractTestingBase {
+
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
+ }
+}