Skip to content

Commit

Permalink
Fix player ChangeInventoryEvent.Pickup.Pre
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix authored and Faithcaio committed Oct 31, 2022
1 parent 4d96651 commit 205ef9b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -103,8 +102,8 @@ public static boolean callPlayerChangeInventoryPickupPreEvent(final Player playe
if (event.custom().isPresent()) {
final List<ItemStackSnapshot> 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();
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,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);
}
}

0 comments on commit 205ef9b

Please sign in to comment.