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

The following are Jave code examples for showing how to use addItemStackToInventory() of the net.minecraft.entity.player.EntityPlayer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Bewitchment   File: ItemFilledBowl.java   View Source Code Vote up 5 votes
@Override
public void onFoodEaten(ItemStack stack, World worldIn, EntityPlayer player) {
	if (stack.getTagCompound() == null) {
		stack.setTagCompound(new NBTTagCompound()); //not really supposed to happen ingame since you only get the stews with NBT values assigned but it prevents crashing
	}
	final FoodStats foodStats = player.getFoodStats();
	foodStats.setFoodLevel(foodStats.getFoodLevel() + stack.getTagCompound().getInteger("hunger"));
	foodStats.setFoodSaturationLevel(foodStats.getFoodLevel() + stack.getTagCompound().getFloat("saturation"));
	player.addItemStackToInventory(new ItemStack(Items.BOWL, 1));
}
 
Example 2
Project: Never-Enough-Currency   File: CurrencyUtils.java   View Source Code Vote up 5 votes
public static void subtractFromInventory(EntityPlayer playerIn, float amount) {
	float amountToGiveBack = getCurrencyNoWallet(playerIn) - amount;

	if (amountToGiveBack >= 0) {
		for (int i = 0; i < playerIn.inventory.getSizeInventory(); i++) {
			if (playerIn.inventory.getStackInSlot(i) != ItemStack.EMPTY && playerIn.inventory.getStackInSlot(i).getItem() instanceof ItemMoneyBase) {
				playerIn.inventory.setInventorySlotContents(i, ItemStack.EMPTY);
			}
		}

		for (ItemStack stack : CurrencyUtils.itemMoneyAmount(amountToGiveBack)) {
			playerIn.addItemStackToInventory(stack);
		}
	}
}
 
Example 3
Project: Bewitchment   File: TileCauldron.java   View Source Code Vote up 4 votes
@SuppressWarnings("ConstantConditions")
public boolean useCauldron(EntityPlayer player, EnumHand hand, ItemStack heldItem) {
	if (!world.isRemote) {
		if (heldItem.isEmpty()) {
			if (!getContainer().isEmpty()) {
				giveItem(player, hand, ItemStack.EMPTY, getContainer());
				setContainer(ItemStack.EMPTY);
			} else if (inv.isFull() && hasIngredients() && mode != Mode.RITUAL) {
				itemRitualLogic();
			}
			return true;
		}
		//Held Item is not empty
		if (heldItem.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP)) {
			handleLiquid(heldItem.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, EnumFacing.UP));
		} else if (heldItem.getItem() == Items.POTIONITEM && PotionUtils.getEffectsFromStack(heldItem).isEmpty()) {
			int level = inv.getFluidAmount();
			if (level < BUCKET_VOLUME && (inv.getFluid() == null || inv.hasFluid(FluidRegistry.WATER))) {
				play(SoundEvents.ITEM_BUCKET_FILL, 1F, 1F);
				giveItem(player, hand, heldItem, new ItemStack(Items.GLASS_BOTTLE));
				FluidStack fluidStack = new FluidStack(FluidRegistry.WATER, 250);
				inv.fill(fluidStack, true);
			}
		} else if (heldItem.getItem() == Items.GLASS_BOTTLE) {
			if (mode == Mode.POTION) {
				potionRecipeLogic(player, hand, heldItem);
			} else if (mode == Mode.CUSTOM) {
				potionCustomLogic(player, hand, heldItem);
			}
		} else if (heldItem.getItem() == Items.BOWL) {
			ItemStack stewToGive = new ItemStack(ModItems.stew);
			int hunger = 0;
			float saturation = 0f;
			float multiplier = 1.0f;
			for (ItemStack stack : ingredients) {
				if (CauldronRegistry.getFoodValues().get(stack.getItem()) != null) {
					CauldronFoodValue foodValue = CauldronRegistry.getFoodValues().get(stack.getItem());
					hunger += foodValue.hunger * multiplier;
					saturation += foodValue.saturation * multiplier;
					multiplier *= 0.75;
				}
			}
			NBTTagCompound nbt = new NBTTagCompound();
			stewToGive.setTagCompound(nbt);
			nbt.setInteger("hunger", hunger);
			nbt.setFloat("saturation", saturation);

			if (player.addItemStackToInventory(stewToGive)) { //if the player has enough inventory space
				heldItem.setCount(heldItem.getCount() - 1);
				ingredients.clear();
				inv.setFluid(null);
				setContainer(ItemStack.EMPTY);
			}
		} else if (getContainer().isEmpty()) {
			ItemStack copy = heldItem.copy();
			setContainer(copy);
			player.setHeldItem(hand, ItemStack.EMPTY);
		}
	}
	return true;
}
 
Example 4
Project: ArcaneMagic   File: BlockAnalyzer.java   View Source Code Vote up 4 votes
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand,
		EnumFacing facing, float hitX, float hitY, float hitZ)
{
	if (world.isRemote)
	{
		return true;
	}
	TileEntity teUnchecked = world.getTileEntity(pos);
	if (!(teUnchecked instanceof TileEntityAnalyzer))
	{
		return false;
	}
	TileEntityAnalyzer te = (TileEntityAnalyzer) teUnchecked;
	ItemStack stack = player.getHeldItem(hand);

	if (!stack.isEmpty() && !player.isSneaking())
	{
		ItemStack insertStack = stack.copy();
		if (stack.getItem().equals(ModRegistry.BLANK_PARCHMENT))
		{
			List<NotebookCategory> unlockable = ArcaneMagicAPI.getAnalyzer()
					.getAnalysisResults(te.getStack(0).copy());

			if (!te.getStack(0).isEmpty() && unlockable.size() > 0 && te.getStack(1).isEmpty())
			{

				stack.shrink(1);
				te.setStack(1, new ItemStack(ModRegistry.BLANK_PARCHMENT));
			}
		} else if (te.getStack(0).isEmpty())
		{
			stack.shrink(1);
			insertStack.setCount(1);
			player.setHeldItem(hand, stack);
			te.setPlayer(player);
			te.setStack(0, insertStack);

			te.markDirty();
			world.playSound(player, pos, SoundEvents.ENTITY_ITEMFRAME_ADD_ITEM, SoundCategory.BLOCKS, 1, 1);

		}
	} else if (player.isSneaking())
	{
		ItemStack toExtract = te.getStack(0).copy();
		if (!toExtract.isEmpty())
		{
			if (!world.isRemote)
			{
				if (player.addItemStackToInventory(toExtract.copy()))
				{
					te.setStack(0, ItemStack.EMPTY);
					te.markDirty();
				}
			} else
			{
				world.playSound(player, pos, SoundEvents.ENTITY_ITEMFRAME_REMOVE_ITEM, SoundCategory.BLOCKS, 1, 1);
			}
		}
	}

	return true;
}
 
Example 5
Project: ArcaneMagic   File: BlockWritingDesk.java   View Source Code Vote up 4 votes
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand,
		EnumFacing facing, float hitX, float hitY, float hitZ)
{
	if (world.isRemote)
	{
		return true;
	}
	TileEntity te = world.getTileEntity(pos);
	if (!(te instanceof TileEntityWritingDesk))
	{
		return false;
	}

	ItemStack stack = player.getHeldItem(hand);

	int slot = 0;
	IItemHandler cap = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);

	if (stack != null && !stack.isEmpty() && !player.isSneaking())
	{
		ItemStack insertStack = stack.copy();
		ItemStack remain = cap.insertItem(slot, insertStack, false);
		if (remain.getCount() != insertStack.getCount())
		{
			if (!world.isRemote)
			{
				player.setHeldItem(hand, remain);
				te.markDirty();
			} else
			{
				world.playSound(player, pos, SoundEvents.ENTITY_ITEMFRAME_ADD_ITEM, SoundCategory.BLOCKS, 1, 1);
			}
		}

	} else if (player.isSneaking())
	{
		ItemStack toExtract = cap.getStackInSlot(slot);
		if (toExtract != null && !toExtract.isEmpty())
		{
			if (!world.isRemote)
			{
				if (player.addItemStackToInventory(toExtract.copy()))
				{
					cap.getStackInSlot(slot).setCount(0);
					te.markDirty();
				}
			} else
			{
				world.playSound(player, pos, SoundEvents.ENTITY_ITEMFRAME_REMOVE_ITEM, SoundCategory.BLOCKS, 1, 1);
			}
		}
	}

	return true;
}