Skip to content
Open
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 @@ -18,7 +18,9 @@

import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;

Expand Down Expand Up @@ -63,6 +65,28 @@ private ServerEntityEvents() {
}
});

/**
* Called during {@link ItemEntity#tick()} on the logical server if an entity tries to pick up any {@link ItemEntity}.
*
* <p>Picking up of an item is determined by {@link ItemEntity#onPlayerCollision(PlayerEntity)}.
*
* <p>Returning {@code false} prevents vanilla from handling the pick-up.
*/
public static final Event<ItemPickup> ITEM_PICKUP = EventFactory.createArrayBacked(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a strong belief that this should be in ServerLivingEntityEvents in the fabric-entity-events-v1 module.

ItemPickup.class,
(callbacks) -> (playerEntity, itemEntity, itemStack) -> {
for (ItemPickup callback : callbacks) {
boolean shouldContinue = callback.onPickup(playerEntity, itemEntity, itemStack);

if (!shouldContinue) {
return false;
}
}

return true;
}
);

@FunctionalInterface
public interface Load {
void onLoad(Entity entity, ServerWorld world);
Expand All @@ -77,4 +101,9 @@ public interface Unload {
public interface EquipmentChange {
void onChange(LivingEntity livingEntity, EquipmentSlot equipmentSlot, ItemStack previousStack, ItemStack currentStack);
}

@FunctionalInterface
public interface ItemPickup {
boolean onPickup(PlayerEntity playerEntity, ItemEntity itemEntity, ItemStack itemStack);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.event.lifecycle;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;

@Mixin(ItemEntity.class)
public class ItemEntityMixin {
@Inject(method = "onPlayerCollision", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerInventory;insertStack(Lnet/minecraft/item/ItemStack;)Z"), cancellable = true)
Copy link
Contributor

@maityyy maityyy Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(mojmap)

I think we need a more general solution that supports not only player but also mobs. I.e. the event should accept LivingEntity instead of Player and be invoked also in Mob::pickUpItem or before Mob::pickUpItem (Mob::aiStep and ObtainRaidLeaderBannerGoal::tick).

edit: But the problem is that Mob::pickupItem has overrides and can also be called by other mods (but can't remember if this is a problem for FAPI tbh)

private void onPlayerPickup(PlayerEntity playerEntity, CallbackInfo ci) {
ItemEntity itemEntity = (ItemEntity) (Object) this;
boolean allowPickup = ServerEntityEvents.ITEM_PICKUP.invoker().onPickup(playerEntity, itemEntity, itemEntity.getStack());

if (!allowPickup) {
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"ChunkGeneratingMixin",
"ChunkHolderMixin",
"DataPackContentsMixin",
"ItemEntityMixin",
"LivingEntityMixin",
"MinecraftServerMixin",
"PlayerManagerMixin",
Expand Down