|
53 | 53 | import net.minecraft.world.entity.ExperienceOrb; |
54 | 54 | import net.minecraft.world.entity.LivingEntity; |
55 | 55 | import net.minecraft.world.entity.SlotAccess; |
| 56 | +import net.minecraft.world.entity.item.ItemEntity; |
56 | 57 | import net.minecraft.world.entity.player.Player; |
57 | 58 | import net.minecraft.world.inventory.ClickAction; |
58 | 59 | import net.minecraft.world.inventory.Slot; |
|
79 | 80 | import net.neoforged.bus.api.EventPriority; |
80 | 81 | import net.neoforged.neoforge.capabilities.Capabilities; |
81 | 82 | import net.neoforged.neoforge.common.NeoForge; |
| 83 | +import net.neoforged.neoforge.event.entity.player.ItemEntityPickupEvent; |
82 | 84 | import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent; |
83 | 85 | import net.neoforged.neoforge.event.level.BlockDropsEvent; |
84 | 86 | import net.neoforged.neoforge.fluids.FluidType; |
@@ -339,7 +341,20 @@ public boolean mineBlock(ItemStack stack, Level world, BlockState state, BlockPo |
339 | 341 | recursiveMineBlock.set(false); |
340 | 342 | } |
341 | 343 | totalDrops.forEach(itemStack -> { |
342 | | - ItemHandlerHelper.giveItemToPlayer(p, itemStack); |
| 344 | + // Create an ItemEntity for the stack, and fire the pickup event. |
| 345 | + // This lets Sophisticated Backpacks (SB) and similar mods try to intercept the stack. |
| 346 | + // The stacks can have more than the max stack size, let's hope it won't cause issues. |
| 347 | + var itemEntity = new ItemEntity(world, p.getX(), p.getY(), p.getZ(), itemStack); |
| 348 | + // Set delay to 0 since we will pick up the item immediately. Otherwise, SB won't process the entity. |
| 349 | + itemEntity.setNoPickUpDelay(); |
| 350 | + var pickupEvent = new ItemEntityPickupEvent.Pre(p, itemEntity); |
| 351 | + NeoForge.EVENT_BUS.post(pickupEvent); |
| 352 | + |
| 353 | + // Ignore canPickup and assume that mods correctly updated the item entity. |
| 354 | + // Let's at least check isRemoved() just in case, but I am not sure if it's needed. |
| 355 | + if (!itemEntity.isRemoved()) { |
| 356 | + ItemHandlerHelper.giveItemToPlayer(p, itemEntity.getItem()); |
| 357 | + } |
343 | 358 | }); |
344 | 359 | totalDrops = null; |
345 | 360 | world.getEntitiesOfClass(ExperienceOrb.class, |
|
0 commit comments