Java Code Examples for net.minecraft.entity.player.EntityPlayer#dropItem()

The following examples show how to use net.minecraft.entity.player.EntityPlayer#dropItem() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContainerPlayerExpandedCrafting.java    From Cyberware with MIT License 6 votes vote down vote up
@Override
public void onContainerClosed(EntityPlayer playerIn)
{
	super.onContainerClosed(playerIn);

	for (int i = 0; i < 9; ++i)
	{
		ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);

		if (itemstack != null)
		{
			playerIn.dropItem(itemstack, false);
		}
	}

	this.craftResult.setInventorySlotContents(0, (ItemStack)null);
}
 
Example 2
Source File: WorldUtils.java    From WearableBackpacks with MIT License 6 votes vote down vote up
/** Spawns an ItemStack as if it was dropped from an entity on death. */
public static EntityItem dropStackFromEntity(Entity entity, ItemStack stack, float speed) {
	EntityPlayer player = ((entity instanceof EntityPlayer) ? (EntityPlayer)entity : null);
	EntityItem item;
	if (player == null) {
		double y = entity.posY + entity.getEyeHeight() - 0.3;
		item = spawnItem(entity.world, entity.posX, y, entity.posZ, stack);
		if (item == null) return null;
		item.setPickupDelay(40);
		float f1 = RandomUtils.getFloat(0.5F);
		float f2 = RandomUtils.getFloat((float)Math.PI * 2.0F);
		item.motionX = -Math.sin(f2) * f1;
		item.motionY = 0.2;
		item.motionZ = Math.cos(f2) * f1;
		return item;
	} else item = player.dropItem(stack, true, false);
	if (item != null) {
		item.motionX *= speed / 4;
		item.motionZ *= speed / 4;
	}
	return item;
}
 
Example 3
Source File: ContainerCustomSlotClick.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void pressDropKey(int slotNum, EntityPlayer player, boolean wholeStack)
{
    SlotItemHandlerGeneric slot = this.getSlotItemHandler(slotNum);
    ItemStack stackSlot = slot != null ? slot.getStack() : ItemStack.EMPTY;

    if (stackSlot.isEmpty() == false && slot.canTakeStack(this.player))
    {
        ItemStack stackDrop = slot.decrStackSize(wholeStack ? stackSlot.getCount() : 1);

        if (stackDrop.isEmpty() == false)
        {
            slot.onTake(player, stackDrop);
            player.dropItem(stackDrop, true);
        }
    }
}
 
Example 4
Source File: ContainerCustomSlotClick.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void leftClickOutsideInventory(EntityPlayer player)
{
    ItemStack stackCursor = this.inventoryPlayer.getItemStack();

    if (stackCursor.isEmpty() == false)
    {
        int max = stackCursor.getMaxStackSize();

        while (stackCursor.getCount() > max)
        {
            ItemStack stackDrop = stackCursor.copy();
            stackDrop.setCount(max);
            player.dropItem(stackDrop, true);
        }

        player.dropItem(stackCursor, true);
        this.inventoryPlayer.setItemStack(ItemStack.EMPTY);
    }
}
 
Example 5
Source File: ContainerPlayerTFC.java    From TFC2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onContainerClosed(EntityPlayer player)
{
	if(!player.world.isRemote)
	{
		super.onContainerClosed(player);

		for (int i = 0; i < 9; ++i)
		{
			ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);
			if (!itemstack.isEmpty())
				player.dropItem(itemstack, false);
		}

		this.craftResult.setInventorySlotContents(0, ItemStack.EMPTY);
	}
}
 
Example 6
Source File: GTHelperFluid.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean doClickableFluidContainerEmptyThings(EntityPlayer player, EnumHand hand, World world,
		BlockPos pos, IC2Tank tank) {
	ItemStack playerStack = player.getHeldItem(hand);
	if (!playerStack.isEmpty()) {
		FluidActionResult result = FluidUtil.tryEmptyContainer(playerStack, tank, tank.getCapacity()
				- tank.getFluidAmount(), player, true);
		if (result.isSuccess()) {
			playerStack.shrink(1);
			ItemStack resultStack = result.getResult();
			if (!resultStack.isEmpty()) {
				if (!player.inventory.addItemStackToInventory(resultStack)) {
					player.dropItem(resultStack, false);
				}
			}
			return true;
		}
	}
	return false;
}
 
Example 7
Source File: GTHelperFluid.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean doClickableFluidContainerFillThings(EntityPlayer player, EnumHand hand, World world,
		BlockPos pos, IC2Tank tank) {
	ItemStack playerStack = player.getHeldItem(hand);
	if (!playerStack.isEmpty()) {
		FluidActionResult result = FluidUtil.tryFillContainer(playerStack, tank, tank.getCapacity(), player, true);
		if (result.isSuccess()) {
			playerStack.shrink(1);
			ItemStack resultStack = result.getResult();
			if (!resultStack.isEmpty()) {
				if (!player.inventory.addItemStackToInventory(resultStack)) {
					player.dropItem(resultStack, false);
				}
			}
			return true;
		}
	}
	return false;
}
 
Example 8
Source File: ItemKatana.java    From Sakura_mod with MIT License 6 votes vote down vote up
@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
	super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected);
	if(worldIn.isRemote) return;
	if(entityIn instanceof EntityPlayer){
		EntityPlayer player = (EntityPlayer) entityIn;
		ItemStack mainhand =player.getHeldItem(EnumHand.MAIN_HAND);
		ItemStack offhand =player.getHeldItem(EnumHand.OFF_HAND);
		boolean flag1 =!(mainhand.isEmpty())&&!(offhand.isEmpty()),
				flag2 = mainhand.getItem() instanceof ItemKatana && offhand.getItem() instanceof ItemKatana;
		if(flag1&&flag2) {
            player.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, ItemStack.EMPTY);
            player.dropItem(offhand, false);
            player.sendStatusMessage(new TextComponentTranslation("sakura.katana.wrong_duel", new Object()), false);
		}
	}
}
 
Example 9
Source File: ItemKotachi.java    From Sakura_mod with MIT License 6 votes vote down vote up
@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
	super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected);
	if(worldIn.isRemote) return;
	if(entityIn instanceof EntityPlayer){
		EntityPlayer player = (EntityPlayer) entityIn;
		ItemStack mainhand =player.getHeldItem(EnumHand.MAIN_HAND);
		ItemStack offhand =player.getHeldItem(EnumHand.OFF_HAND);
		boolean flag1 =!(mainhand.isEmpty())&&!(offhand.isEmpty()),
				flag2 = mainhand.getItem() instanceof ItemKotachi && offhand.getItem() instanceof ItemKotachi;
		if(flag1&&flag2) {
            player.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, ItemStack.EMPTY);
            player.dropItem(offhand, false);
            player.sendStatusMessage(new TextComponentTranslation("sakura.katana.wrong_duel", new Object()), false);
		}
	}
}
 
Example 10
Source File: ItemShinai.java    From Sakura_mod with MIT License 6 votes vote down vote up
@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
	super.onUpdate(stack, worldIn, entityIn, itemSlot, isSelected);
	if(worldIn.isRemote) return;
	if(entityIn instanceof EntityPlayer){
		EntityPlayer player = (EntityPlayer) entityIn;
		ItemStack mainhand =player.getHeldItem(EnumHand.MAIN_HAND);
		ItemStack offhand =player.getHeldItem(EnumHand.OFF_HAND);
		boolean flag1 =!(mainhand.isEmpty())&&!(offhand.isEmpty()),
				flag2 = mainhand.getItem() instanceof ItemShinai||offhand.getItem() instanceof ItemShinai;
		if(flag1&&flag2) {
            player.setItemStackToSlot((mainhand.getItem() instanceof ItemShinai)?EntityEquipmentSlot.OFFHAND:EntityEquipmentSlot.MAINHAND, ItemStack.EMPTY);
            player.dropItem((mainhand.getItem() instanceof ItemShinai)?offhand:mainhand, false);
            player.sendStatusMessage(new TextComponentTranslation("sakura.katana.wrong_duel_shinai", new Object()), false);
		}
	}
}
 
Example 11
Source File: CachedRecipeData.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public boolean performRecipe(EntityPlayer player) {
    this.lastTickChecked = -1L;
    if (!checkRecipeValid()) {
        return false;
    }
    if (!consumeRecipeItems(false)) {
        this.lastTickChecked = -1L;
        return false;
    }
    ForgeHooks.setCraftingPlayer(player);
    NonNullList<ItemStack> remainingItems = recipe.getRemainingItems(inventory);
    ForgeHooks.setCraftingPlayer(null);
    for (ItemStack itemStack : remainingItems) {
        itemStack = itemStack.copy();
        ItemStackKey stackKey = new ItemStackKey(itemStack);
        int remainingAmount = itemStack.getCount() - itemSourceList.insertItem(stackKey, itemStack.getCount(), false, InsertMode.HIGHEST_PRIORITY);
        if (remainingAmount > 0) {
            itemStack.setCount(remainingAmount);
            player.addItemStackToInventory(itemStack);
            if (itemStack.getCount() > 0) {
                player.dropItem(itemStack, false, false);
            }
        }
    }
    this.lastTickChecked = -1L;
    return true;
}
 
Example 12
Source File: ContainerSpecialCrafting.java    From TFC2 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Called to transfer a stack from one inventory to the other eg. when shift clicking.
 * @return null if successful, the original item stack otherwise
 */
@Override
public ItemStack transferStackInSlotTFC(EntityPlayer player, int slotNum)
{
	Slot slot = (Slot)this.inventorySlots.get(slotNum);
	if (slot == null || !slot.getHasStack())  return ItemStack.EMPTY;
	ItemStack slotStack = slot.getStack();
	ItemStack origStack = slotStack.copy(); 
	InventoryPlayer ip = player.inventory;

	// From Crafting Grid Output to inventory
	if (slot instanceof SlotSpecialCraftingOutput)
	{
		if (slotNum == 0 && !ip.addItemStackToInventory(slotStack))
			return ItemStack.EMPTY;
	}
	// From inventory to Hotbar
	else if (slotNum >= 1 && slotNum < 28 && !this.mergeItemStack(slotStack, 28, 37, false))
		return ItemStack.EMPTY;
	// From Hotbar to inventory
	else if (slotNum >= 28 && slotNum < 37 && !this.mergeItemStack(slotStack, 1, 28, false))
		return ItemStack.EMPTY;

	if (slotStack.isEmpty())
		slot.putStack(ItemStack.EMPTY);
	else
		slot.onSlotChanged();

	if (slotStack.getCount() == origStack.getCount())
		return ItemStack.EMPTY;

	ItemStack itemstack2 = slot.onTake(player, slotStack);
	if (slotNum == 0)
		player.dropItem(itemstack2, false);

	return origStack;
}
 
Example 13
Source File: ItemEnderTool.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean plantItemFromInventorySlot(World world, EntityPlayer player, EnumHand hand,
        IItemHandler inv, int slot, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
{
    boolean ret = false;
    ItemStack plantStack = inv.getStackInSlot(slot);

    if (plantStack.isEmpty() == false && plantStack.getItem() instanceof IPlantable)
    {
        plantStack = inv.extractItem(slot, 1, false);

        if (plantStack.isEmpty())
        {
            return false;
        }

        ItemStack stackHand = player.getHeldItem(hand);
        EntityUtils.setHeldItemWithoutEquipSound(player, hand, plantStack);

        if (plantStack.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ) == EnumActionResult.SUCCESS)
        {
            ret = true;
        }

        EntityUtils.setHeldItemWithoutEquipSound(player, hand, stackHand);

        if (plantStack.isEmpty() == false)
        {
            plantStack = InventoryUtils.tryInsertItemStackToInventory(inv, plantStack);

            if (plantStack.isEmpty() == false)
            {
                player.dropItem(plantStack, false, true);
            }
        }

        player.inventoryContainer.detectAndSendChanges();
    }

    return ret;
}
 
Example 14
Source File: ContainerCustomSlotClick.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void rightClickOutsideInventory(EntityPlayer player)
{
    ItemStack stackCursor = this.inventoryPlayer.getItemStack();

    if (stackCursor.isEmpty() == false)
    {
        ItemStack stackDrop = stackCursor.splitStack(1);
        player.dropItem(stackDrop, true);
        this.inventoryPlayer.setItemStack(stackCursor.isEmpty() ? ItemStack.EMPTY : stackCursor);
    }
}
 
Example 15
Source File: ItemNigari.java    From TofuCraftReload with MIT License 4 votes vote down vote up
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
	if (worldIn.isRemote)
		return new ActionResult<ItemStack>(EnumActionResult.PASS, ItemStack.EMPTY);
	RayTraceResult var4 = this.rayTrace(worldIn, playerIn, true);
	ItemStack itemStackIn = playerIn.getHeldItem(handIn);

	if (var4 == null) {
		return new ActionResult<ItemStack>(EnumActionResult.PASS, itemStackIn);
	} else {
		if (var4.typeOfHit == RayTraceResult.Type.BLOCK) {
			BlockPos targetPos = var4.getBlockPos();
			Block var11 = worldIn.getBlockState(targetPos).getBlock();
			Block var13 = null;

			if (var11 == BlockLoader.SOYMILK) {
				var13 = BlockLoader.KINUTOFU;
			} else if (var11 == BlockLoader.SOYMILKHELL) {
				var13 = BlockLoader.TOFUHELL;
			} else if (var11 == BlockLoader.ZUNDASOYMILK) {
				var13 = BlockLoader.TOFUZUNDA;
			}

			if (var13 != null) {
				worldIn.playSound(playerIn, targetPos.add(0.5, 0.5, 0.5),
						var13.getSoundType(var13.getDefaultState(), worldIn, targetPos, playerIn).getBreakSound(),
						SoundCategory.BLOCKS,
						(var13.getSoundType(var13.getDefaultState(), worldIn, targetPos, playerIn).getVolume()
								+ 1.0F) / 2.0F,
						var13.getSoundType(var13.getDefaultState(), worldIn, targetPos, playerIn).getPitch()
								* 0.8F);

				worldIn.setBlockState(targetPos, var13.getDefaultState());

				if (!playerIn.capabilities.isCreativeMode) {
					itemStackIn.shrink(1);

					ItemStack container = new ItemStack(this.getContainerItem());

					if (itemStackIn.getCount() <= 0) {
						return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, container);
					}

					if (!playerIn.inventory.addItemStackToInventory(container)) {
						playerIn.dropItem(container, false);
					}
				}
			}
		}
		return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemStackIn);
	}
}
 
Example 16
Source File: ModularUIContainer.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int index) {
    Slot slot = inventorySlots.get(index);
    if (!slot.canTakeStack(player)) {
        return ItemStack.EMPTY;
    }
    if (!slot.getHasStack()) {
        //return empty if we can't transfer it
        return ItemStack.EMPTY;
    }
    ItemStack stackInSlot = slot.getStack();
    ItemStack stackToMerge = slotMap.get(slot).onItemTake(player, stackInSlot.copy(), true);
    boolean fromContainer = !slotMap.get(slot).getSlotLocationInfo().isPlayerInventory;
    if (!attemptMergeStack(stackToMerge, fromContainer, true)) {
        return ItemStack.EMPTY;
    }
    int itemsMerged;
    if (stackToMerge.isEmpty() || slotMap.get(slot).canMergeSlot(stackToMerge)) {
        itemsMerged = stackInSlot.getCount() - stackToMerge.getCount();
    } else {
        //if we can't have partial stack merge, we have to use all the stack
        itemsMerged = stackInSlot.getCount();
    }
    int itemsToExtract = itemsMerged;
    itemsMerged += transferredPerTick.get(player.world);
    if (itemsMerged > stackInSlot.getMaxStackSize()) {
        //we can merge at most one stack at a time
        return ItemStack.EMPTY;
    }
    transferredPerTick.increment(player.world, itemsToExtract);
    //otherwise, perform extraction and merge
    ItemStack extractedStack = stackInSlot.splitStack(itemsToExtract);
    if (stackInSlot.isEmpty()) {
        slot.putStack(ItemStack.EMPTY);
    } else {
        slot.onSlotChanged();
    }
    extractedStack = slotMap.get(slot).onItemTake(player, extractedStack, false);
    ItemStack resultStack = extractedStack.copy();
    if (!attemptMergeStack(extractedStack, fromContainer, false)) {
        resultStack = ItemStack.EMPTY;
    }
    if (!extractedStack.isEmpty()) {
        player.dropItem(extractedStack, false, false);
        resultStack = ItemStack.EMPTY;
    }
    return resultStack;
}
 
Example 17
Source File: ItemPetContract.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer player, EntityLivingBase target, EnumHand hand)
{
    if (target instanceof EntityTameable)
    {
        if (player.getEntityWorld().isRemote == false)
        {
            if (this.isSigned(stack))
            {
                UUID targetOwner = ((EntityTameable) target).getOwnerId();
                UUID signedOwner = this.getOwnerUUID(stack);

                // This contract is signed by the current owner of the target,
                // and the contract is for this target entity, and the player is not the owner
                // -> change the owner of the target.
                if (targetOwner != null && targetOwner.equals(signedOwner) &&
                    target.getUniqueID().equals(this.getSubjectUUID(stack)) &&
                    player.getUniqueID().equals(signedOwner) == false)
                {
                    ((EntityTameable) target).setTamed(true);
                    ((EntityTameable) target).setOwnerId(player.getUniqueID());

                    // See EntityTameable#handleStatusUpdate()
                    player.getEntityWorld().setEntityState(target, (byte) 7);

                    stack.shrink(1);
                }
            }
            // Blank contract - if the target is tamed, and owned by the player, sign the contract
            else
            {
                if (((EntityTameable) target).isTamed() && ((EntityTameable) target).isOwner(player))
                {
                    ItemStack stackSigned = stack.copy();
                    stackSigned.setCount(1);

                    this.signContract(stackSigned, player, target);

                    if (player.addItemStackToInventory(stackSigned) == false)
                    {
                        player.dropItem(stackSigned, false);
                    }

                    stack.shrink(1);
                }
            }
        }

        return true;
    }

    return super.itemInteractionForEntity(stack, player, target, hand);
}