|
1 | 1 | package com.github.quiltservertools.ledger.mixin.entities; |
2 | 2 |
|
3 | 3 | import com.github.quiltservertools.ledger.callbacks.ItemPickUpCallback; |
| 4 | +import com.llamalad7.mixinextras.sugar.Local; |
| 5 | +import com.llamalad7.mixinextras.sugar.Share; |
| 6 | +import com.llamalad7.mixinextras.sugar.ref.LocalRef; |
| 7 | +import net.minecraft.entity.Entity; |
| 8 | +import net.minecraft.entity.EntityType; |
4 | 9 | import net.minecraft.entity.ItemEntity; |
5 | 10 | import net.minecraft.entity.player.PlayerEntity; |
| 11 | +import net.minecraft.item.ItemStack; |
| 12 | +import net.minecraft.world.World; |
6 | 13 | import org.spongepowered.asm.mixin.Mixin; |
7 | | -import org.spongepowered.asm.mixin.Unique; |
| 14 | +import org.spongepowered.asm.mixin.Shadow; |
8 | 15 | import org.spongepowered.asm.mixin.injection.At; |
9 | 16 | import org.spongepowered.asm.mixin.injection.Inject; |
10 | 17 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
11 | 18 |
|
12 | 19 | @Mixin(ItemEntity.class) |
13 | | -public abstract class ItemEntityMixin { |
14 | | - @Unique |
15 | | - private ItemEntity itemEntity; |
| 20 | +public abstract class ItemEntityMixin extends Entity { |
| 21 | + |
| 22 | + @Shadow |
| 23 | + public abstract ItemStack getStack(); |
| 24 | + |
| 25 | + public ItemEntityMixin(EntityType<?> type, World world) { |
| 26 | + super(type, world); |
| 27 | + } |
16 | 28 |
|
17 | 29 | @Inject(method = "onPlayerCollision", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerInventory;insertStack(Lnet/minecraft/item/ItemStack;)Z")) |
18 | | - private void storeEntity(PlayerEntity player, CallbackInfo ci) { |
19 | | - itemEntity = (ItemEntity) (Object) this; |
20 | | - itemEntity.copyFrom(itemEntity); |
| 30 | + public void storeItemStack(PlayerEntity player, CallbackInfo ci, @Local ItemStack itemStack, @Share("originalItemStack") LocalRef<ItemStack> originalItemStackRef) { |
| 31 | + originalItemStackRef.set(itemStack.copy()); |
21 | 32 | } |
22 | 33 |
|
23 | | - @Inject(method = "onPlayerCollision", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;sendPickup(Lnet/minecraft/entity/Entity;I)V")) |
24 | | - private void logPlayerItemPickUp(PlayerEntity player, CallbackInfo ci) { |
25 | | - ItemPickUpCallback.EVENT.invoker().pickUp(itemEntity, player); |
| 34 | + // insertStack modifies the ItemStack instance of the ItemEntity |
| 35 | + // The player may not be able to pick up all items from the stack |
| 36 | + @Inject(method = "onPlayerCollision", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerInventory;insertStack(Lnet/minecraft/item/ItemStack;)Z", shift = At.Shift.AFTER)) |
| 37 | + private void logPlayerItemPickUp(PlayerEntity player, CallbackInfo ci, @Local ItemStack modifiedItemStack, @Share("originalItemStack") LocalRef<ItemStack> originalItemStackRef) { |
| 38 | + var originalItemStack = originalItemStackRef.get(); |
| 39 | + |
| 40 | + int modifiedCount = modifiedItemStack.getCount(); |
| 41 | + int originalCount = originalItemStack.getCount(); |
| 42 | + if (modifiedCount < originalCount) { |
| 43 | + ItemEntity itemEntityCopy = new ItemEntity(this.getEntityWorld(), this.getX(), this.getY(), this.getZ(), originalItemStackRef.get().copyWithCount(originalCount - modifiedCount)); |
| 44 | + ItemPickUpCallback.EVENT.invoker().pickUp(itemEntityCopy, player); |
| 45 | + } |
26 | 46 | } |
27 | 47 | } |
0 commit comments