Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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,17 @@ private ServerEntityEvents() {
}
});

/**
* Called during {@link ItemEntity#tick()} if an entity tries to pick up any ItemEntity.
*
* <p>Picking up of an item is determined by {@link ItemEntity#onPlayerCollision(PlayerEntity)}.
*/
public static final Event<ItemPickup> ITEM_PICKUP = EventFactory.createArrayBacked(ServerEntityEvents.ItemPickup.class, callbacks -> (playerEntity, itemEntity, itemStack) -> {
for (ItemPickup callback : callbacks) {
callback.onPickup(playerEntity, itemEntity, itemStack);
}
});

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

@FunctionalInterface
public interface ItemPickup {
void onPickup(PlayerEntity playerEntity, ItemEntity itemEntity, ItemStack itemStack);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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("HEAD"))
private void onPlayerPickup(PlayerEntity playerEntity, CallbackInfo ci) {
ItemEntity itemEntity = (ItemEntity) (Object) this;

if (!itemEntity.getWorld().isClient && !itemEntity.cannotPickup()) {
ServerEntityEvents.ITEM_PICKUP.invoker().onPickup(playerEntity, itemEntity, itemEntity.getStack());
}
}
}
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