Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EntityHarvestBlockEvent #11951

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.papermc.paper.event.entity;

import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.EntityEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;
import java.util.List;

/**
* This event is called whenever an entity harvests a block.
* <br>
* For the player case please use {@link org.bukkit.event.player.PlayerHarvestBlockEvent}
* <br>
* A 'harvest' is when a block drops an item (usually some sort of crop) and
* changes state, but is not broken in order to drop the item.
* <br>
* This event is not called for when a block is broken, to handle that, listen
* for {@link org.bukkit.event.block.BlockBreakEvent} and
* {@link org.bukkit.event.block.BlockDropItemEvent}.
*/
@NullMarked
public class EntityHarvestBlockEvent extends EntityEvent implements Cancellable {

private static final HandlerList HANDLER_LIST = new HandlerList();

private boolean cancel = false;
private final Block harvestedBlock;
private final List<ItemStack> itemsHarvested;

@ApiStatus.Internal
public EntityHarvestBlockEvent(final Entity entity, final Block harvestedBlock, final List<ItemStack> itemsHarvested) {
super(entity);
this.harvestedBlock = harvestedBlock;
this.itemsHarvested = itemsHarvested;
}

/**
* Gets the block that is being harvested.
*
* @return The block that is being harvested
*/
public Block getHarvestedBlock() {
return harvestedBlock;
}

/**
* Gets a list of items that are being harvested from this block.
*
* @return A list of items that are being harvested from this block
*/
public List<ItemStack> getItemsHarvested() {
return itemsHarvested;
}

@Override
public boolean isCancelled() {
return cancel;
}

@Override
public void setCancelled(boolean cancel) {
this.cancel = cancel;
}

@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import io.papermc.paper.event.entity.EntityHarvestBlockEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

/**
* This event is called whenever a player harvests a block.
* <br>
* For cases involving entities, please use {@link EntityHarvestBlockEvent}.
* <br>
* A 'harvest' is when a block drops an item (usually some sort of crop) and
* changes state, but is not broken in order to drop the item.
* <br>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- a/net/minecraft/world/level/block/CaveVines.java
+++ b/net/minecraft/world/level/block/CaveVines.java
@@ -23,7 +_,23 @@
@@ -23,7 +_,31 @@

static InteractionResult use(@Nullable Entity entity, BlockState state, Level level, BlockPos pos) {
if (state.getValue(BERRIES)) {
Expand All @@ -18,6 +18,14 @@
+ for (org.bukkit.inventory.ItemStack itemStack : event.getItemsHarvested()) {
+ Block.popResource(level, pos, org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(itemStack));
+ }
+ } else if (entity != null) {
+ io.papermc.paper.event.entity.EntityHarvestBlockEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityHarvestBlockEvent(level, pos, entity, java.util.Collections.singletonList(new ItemStack(Items.GLOW_BERRIES, 1)));
+ if (event.isCancelled()) {
+ return InteractionResult.SUCCESS; // We need to return a success either way, because making it PASS or FAIL will result in a bug where cancelling while harvesting w/ block in hand places block
+ }
+ for (org.bukkit.inventory.ItemStack itemStack : event.getItemsHarvested()) {
+ Block.popResource(level, pos, org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(itemStack));
+ }
+ } else {
+ Block.popResource(level, pos, new ItemStack(Items.GLOW_BERRIES, 1));
+ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
stack.shrink(1);
return blockState;
} else {
@@ -277,6 +_,14 @@
@@ -277,11 +_,40 @@
}

public static BlockState extractProduce(Entity entity, BlockState state, Level level, BlockPos pos) {
Expand All @@ -48,6 +48,33 @@
if (!level.isClientSide) {
Vec3 vec3 = Vec3.atLowerCornerWithOffset(pos, 0.5, 1.01, 0.5).offsetRandom(level.random, 0.7F);
ItemEntity itemEntity = new ItemEntity(level, vec3.x(), vec3.y(), vec3.z(), new ItemStack(Items.BONE_MEAL));
itemEntity.setDefaultPickUpDelay();
- level.addFreshEntity(itemEntity);
+ // Paper start - call HarvestBlockEvent for the item dropped
+ // level.addFreshEntity(itemEntity); // used later
+ java.util.ArrayList<org.bukkit.inventory.ItemStack> itemsHarvested = new java.util.ArrayList<>();
+ if (entity instanceof Player player) {
+ org.bukkit.event.player.PlayerHarvestBlockEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerHarvestBlockEvent(level, pos, player, InteractionHand.MAIN_HAND, java.util.Collections.singletonList(itemEntity.getItem()));
+ itemsHarvested.addAll(event.getItemsHarvested());
+ if (event.isCancelled()) {
+ return state;
+ }
+ } else {
+ io.papermc.paper.event.entity.EntityHarvestBlockEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityHarvestBlockEvent(level, pos, entity, java.util.Collections.singletonList(itemEntity.getItem()));
+ itemsHarvested.addAll(event.getItemsHarvested());
+ if (event.isCancelled()) {
+ return state;
+ }
+ }
+ for (org.bukkit.inventory.ItemStack itemStack : itemsHarvested) {
+ ItemEntity harvestItemEntity = new ItemEntity(level, vec3.x(), vec3.y(), vec3.z(), org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(itemStack));
+ harvestItemEntity.setDefaultPickUpDelay();
+ level.addFreshEntity(harvestItemEntity);
+ }
+ // Paper end
}

BlockState blockState = empty(entity, state, level, pos);
@@ -296,14 +_,39 @@
return blockState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
import org.bukkit.event.entity.EntityEnterLoveModeEvent;
import org.bukkit.event.entity.EntityExhaustionEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import io.papermc.paper.event.entity.EntityHarvestBlockEvent;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.entity.EntityKnockbackByEntityEvent;
import org.bukkit.event.entity.EntityKnockbackEvent;
Expand Down Expand Up @@ -344,11 +345,22 @@ public static EntityEnterLoveModeEvent callEntityEnterLoveModeEvent(net.minecraf
return entityEnterLoveModeEvent;
}

/**
* Entity Harvest Block Event
*/
public static EntityHarvestBlockEvent callEntityHarvestBlockEvent(Level world, BlockPos blockposition, net.minecraft.world.entity.Entity who, List<ItemStack> itemsToHarvest) {
List<org.bukkit.inventory.ItemStack> bukkitItemsToHarvest = itemsToHarvest.stream().map(CraftItemStack::asBukkitCopy).collect(Collectors.toList());
org.bukkit.entity.Entity entity = who.getBukkitEntity();
EntityHarvestBlockEvent entityHarvestBlockEvent = new EntityHarvestBlockEvent(entity, CraftBlock.at(world, blockposition), bukkitItemsToHarvest);
Bukkit.getPluginManager().callEvent(entityHarvestBlockEvent);
return entityHarvestBlockEvent;
}

/**
* Player Harvest Block Event
*/
public static PlayerHarvestBlockEvent callPlayerHarvestBlockEvent(Level world, BlockPos blockposition, net.minecraft.world.entity.player.Player who, InteractionHand enumhand, List<ItemStack> itemsToHarvest) {
List<org.bukkit.inventory.ItemStack> bukkitItemsToHarvest = new ArrayList<>(itemsToHarvest.stream().map(CraftItemStack::asBukkitCopy).collect(Collectors.toList()));
List<org.bukkit.inventory.ItemStack> bukkitItemsToHarvest = itemsToHarvest.stream().map(CraftItemStack::asBukkitCopy).collect(Collectors.toList());
Doc94 marked this conversation as resolved.
Show resolved Hide resolved
Player player = (Player) who.getBukkitEntity();
PlayerHarvestBlockEvent playerHarvestBlockEvent = new PlayerHarvestBlockEvent(player, CraftBlock.at(world, blockposition), CraftEquipmentSlot.getHand(enumhand), bukkitItemsToHarvest);
Bukkit.getPluginManager().callEvent(playerHarvestBlockEvent);
Expand Down