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

Fix player ChangeInventoryEvent.Pickup.Pre (API 8 edition) #3748

Merged
merged 1 commit into from
Oct 31, 2022
Merged
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
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 @@ -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);
}
}