Skip to content

Commit

Permalink
Post EntityItemPickupEvent's when adding items to player inventory.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlgorithmX2 committed Dec 22, 2017
1 parent 1c687d0 commit b61c77a
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions src/main/java/mod/chiselsandbits/helpers/ModUtil.java
Expand Up @@ -46,6 +46,9 @@
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.fml.common.eventhandler.Event.Result;

public class ModUtil
{
Expand Down Expand Up @@ -371,25 +374,58 @@ public static void feedPlayer(
is = bp.inv.insertItem( is );
}

if ( is != null && !player.inventory.addItemStackToInventory( is ) )
if ( ModUtil.isEmpty( is ) )
return;

ei.setEntityItemStack( is );
EntityItemPickupEvent event = new EntityItemPickupEvent( player, ei );

if ( MinecraftForge.EVENT_BUS.post( event ) )
{
ei.setEntityItemStack( is );
world.spawnEntityInWorld( ei );
// canceled...
spawnItem( world, ei );
}
else
{
if ( !ei.isSilent() )
if ( event.getResult() != Result.DENY )
{
ei.worldObj.playSound( (EntityPlayer) null, ei.posX, ei.posY, ei.posZ, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ( ( itemRand.nextFloat() - itemRand.nextFloat() ) * 0.7F + 1.0F ) * 2.0F );
is = ei.getEntityItem();

if ( is != null && !player.inventory.addItemStackToInventory( is ) )
{
ei.setEntityItemStack( is );
spawnItem( world, ei );
}
else
{
if ( !ei.isSilent() )
{
ei.worldObj.playSound( (EntityPlayer) null, ei.posX, ei.posY, ei.posZ, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ( ( itemRand.nextFloat() - itemRand.nextFloat() ) * 0.7F + 1.0F ) * 2.0F );
}
}

player.inventory.markDirty();

if ( player.inventoryContainer != null )
{
player.inventoryContainer.detectAndSendChanges();
}

}
else
spawnItem( world, ei );
}

player.inventory.markDirty();
}

if ( player.inventoryContainer != null )
{
player.inventoryContainer.detectAndSendChanges();
}
private static void spawnItem(
World world,
EntityItem ei )
{
if ( world.isRemote ) // no spawning items on the client.
return;

world.spawnEntityInWorld( ei );
}

public static boolean containsAtLeastOneOf(
Expand Down

0 comments on commit b61c77a

Please sign in to comment.