diff --git a/src/main/java/org/spongepowered/common/event/inventory/InventoryEventFactory.java b/src/main/java/org/spongepowered/common/event/inventory/InventoryEventFactory.java index 0fe32d24213..395ff027c50 100644 --- a/src/main/java/org/spongepowered/common/event/inventory/InventoryEventFactory.java +++ b/src/main/java/org/spongepowered/common/event/inventory/InventoryEventFactory.java @@ -88,8 +88,7 @@ public class InventoryEventFactory { - - public static boolean callPlayerChangeInventoryPickupPreEvent(final Player player, final ItemEntity itemToPickup) { + public static boolean callPlayerInventoryPickupEvent(final Player player, final ItemEntity itemToPickup) { final ItemStack stack = itemToPickup.getItem(); final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(stack); final ChangeInventoryEvent.Pickup.Pre event = @@ -103,8 +102,8 @@ public static boolean callPlayerChangeInventoryPickupPreEvent(final Player playe if (event.custom().isPresent()) { final List list = event.custom().get(); if (list.isEmpty()) { - itemToPickup.getItem().setCount(0); - return false; + stack.setCount(0); + return true; } final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext(); @@ -124,12 +123,24 @@ public static boolean callPlayerChangeInventoryPickupPreEvent(final Player playe if (!TrackingUtil.processBlockCaptures(context)) { return false; } - itemToPickup.getItem().setCount(0); + stack.setCount(0); + return true; + } else { + final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext(); + final TransactionalCaptureSupplier transactor = context.getTransactor(); + final boolean added; + try (final EffectTransactor ignored = transactor.logPlayerInventoryChangeWithEffect(player, PlayerInventoryTransaction.EventCreator.PICKUP)) { + added = player.inventory.add(stack); + } + + if (!TrackingUtil.processBlockCaptures(context)) { + return false; + } + return added; } - return true; } - public static ItemStack callInventoryPickupEvent(final Container inventory, final ItemEntity item, final ItemStack stack) { + public static ItemStack callHopperInventoryPickupEvent(final Container inventory, final ItemEntity item, final ItemStack stack) { try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) { frame.pushCause(inventory); diff --git a/src/mixins/java/org/spongepowered/common/mixin/inventory/event/world/entity/item/ItemEntityMixin_Inventory.java b/src/mixins/java/org/spongepowered/common/mixin/inventory/event/world/entity/item/ItemEntityMixin_Inventory.java index 02243b837ee..ad4b5a3340d 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/inventory/event/world/entity/item/ItemEntityMixin_Inventory.java +++ b/src/mixins/java/org/spongepowered/common/mixin/inventory/event/world/entity/item/ItemEntityMixin_Inventory.java @@ -28,56 +28,18 @@ import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; -import org.checkerframework.checker.nullness.qual.NonNull; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.common.event.inventory.InventoryEventFactory; -import org.spongepowered.common.event.tracking.PhaseContext; -import org.spongepowered.common.event.tracking.PhaseTracker; -import org.spongepowered.common.event.tracking.TrackingUtil; -import org.spongepowered.common.event.tracking.context.transaction.EffectTransactor; -import org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier; -import org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction; @Mixin(ItemEntity.class) public abstract class ItemEntityMixin_Inventory { - @Shadow private int pickupDelay; - - @Inject( - method = "playerTouch", - at = @At( - value = "INVOKE", - ordinal = 0, - target = "Lnet/minecraft/world/item/ItemStack;getItem()Lnet/minecraft/world/item/Item;"), - cancellable = true - ) - private void spongeImpl$ThrowPickupEvent(final Player entityIn, final CallbackInfo ci) { - if (this.pickupDelay == 0) { - if (!InventoryEventFactory.callPlayerChangeInventoryPickupPreEvent(entityIn, (ItemEntity) (Object) this)) { - ci.cancel(); - } - } - } - @Redirect(method = "playerTouch", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/player/Inventory;add(Lnet/minecraft/world/item/ItemStack;)Z")) private boolean spongeImpl$throwPickupEventForAddItem(final Inventory inventory, final ItemStack itemStack, final Player player) { - final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext(); - final TransactionalCaptureSupplier transactor = context.getTransactor(); - final boolean added; - try (final EffectTransactor ignored = transactor.logPlayerInventoryChangeWithEffect(player, PlayerInventoryTransaction.EventCreator.PICKUP)) { - added = inventory.add(itemStack); - } - - if (!TrackingUtil.processBlockCaptures(context)) { - return false; // if PickupEvent was cancelled return false - } - return added; + return InventoryEventFactory.callPlayerInventoryPickupEvent(player, (ItemEntity) (Object) this); } } diff --git a/src/mixins/java/org/spongepowered/common/mixin/inventory/event/world/level/block/entity/HopperBlockEntityMixin_Inventory.java b/src/mixins/java/org/spongepowered/common/mixin/inventory/event/world/level/block/entity/HopperBlockEntityMixin_Inventory.java index db17f04c9f7..c84a961a34e 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/inventory/event/world/level/block/entity/HopperBlockEntityMixin_Inventory.java +++ b/src/mixins/java/org/spongepowered/common/mixin/inventory/event/world/level/block/entity/HopperBlockEntityMixin_Inventory.java @@ -161,6 +161,6 @@ public abstract class HopperBlockEntityMixin_Inventory { at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/entity/HopperBlockEntity;addItem(Lnet/minecraft/world/Container;Lnet/minecraft/world/Container;Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/core/Direction;)Lnet/minecraft/world/item/ItemStack;")) private static ItemStack impl$onPutStackInInventoryAllSlots(final Container source, final Container destination, final ItemStack stack, final Direction direction, final Container d2, final ItemEntity entity) { - return InventoryEventFactory.callInventoryPickupEvent(destination, entity, stack); + return InventoryEventFactory.callHopperInventoryPickupEvent(destination, entity, stack); } }