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

The following are Jave code examples for showing how to use setHeldItem() 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: pnc-repressurized   File: FluidUtils.java   View Source Code Vote up 6 votes
private static boolean doFluidInteraction(TileEntity te, EnumFacing face, EntityPlayer player, EnumHand hand, boolean isInserting) {
    ItemStack stack = player.getHeldItem(hand);
    IFluidHandlerItem stackHandler = FluidUtil.getFluidHandler(stack);
    if (stackHandler != null && te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, face)) {
        int capacity = stackHandler.getTankProperties()[0].getCapacity();
        IFluidHandler handler = te.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, face);
        PlayerInvWrapper invWrapper = new PlayerInvWrapper(player.inventory);
        FluidActionResult result = isInserting ?
                FluidUtil.tryEmptyContainerAndStow(player.getHeldItem(hand), handler, invWrapper, capacity, player) :
                FluidUtil.tryFillContainerAndStow(player.getHeldItem(hand), handler, invWrapper, capacity, player);
        if (result.isSuccess()) {
            player.setHeldItem(hand, result.getResult());
            return true;
        }
    }
    return false;
}
 
Example 2
Project: genera   File: EntityFaerie.java   View Source Code Vote up 6 votes
@Override
public boolean processInteract(EntityPlayer player, EnumHand hand) {
    if (!player.world.isRemote) {
        ItemStack stack = player.getHeldItem(hand);
        if (stack.getItem() == ItemGlassJar && stack.getItemDamage() == 0) {
            this.setDead();
            stack.setItemDamage(1);
            NBTTagCompound nbt = stack.getTagCompound();
            if (nbt == null) return false;
            nbt.setFloat("size", faerieInformation.getSize());
            nbt.setInteger("type", faerieInformation.getType());
            nbt.setDouble("max_health", faerieInformation.getMaxHealth());
            nbt.setInteger("level", faerieInformation.getLevel());
            nbt.setInteger("current_exp", faerieInformation.getCurrentExp());
            player.setHeldItem(hand, stack);
        }
    }
    return super.processInteract(player, hand);
}
 
Example 3
Project: genera   File: ItemGlassJar.java   View Source Code Vote up 6 votes
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
    if (!worldIn.isRemote) {
        ItemStack stack = player.getHeldItem(hand);
        if (stack.getMetadata() == 1 && !(worldIn.getTileEntity(pos) instanceof TileFaerieHome)) {
            NBTTagCompound nbt = stack.getTagCompound();
            if (nbt == null) return EnumActionResult.FAIL;
            EntityFaerie faerie = new EntityFaerie(worldIn, nbt.getDouble("max_health"), nbt.getInteger("type"), nbt.getFloat("size"), nbt.getInteger("level"), nbt.getInteger("current_exp"));
            faerie.setPosition((double) pos.getX(), (double) pos.up().getY(), (double) pos.getZ());
            worldIn.spawnEntity(faerie);
            player.setHeldItem(hand, new ItemStack(GeneraItems.ItemGlassJar, 1, 0));
            return EnumActionResult.SUCCESS;
        }
    }
    return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}
 
Example 4
Project: Bewitchment   File: ItemTalisman.java   View Source Code Vote up 6 votes
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
	if (!world.isRemote) {
		IBaublesItemHandler baubles = BaublesApi.getBaublesHandler(player);
		for (int i = 0; i < baubles.getSlots(); i++)
			if (baubles.getStackInSlot(i).isEmpty() && baubles.isItemValidForSlot(i, player.getHeldItem(hand), player)) {
				baubles.setStackInSlot(i, player.getHeldItem(hand).copy());
				if (!player.capabilities.isCreativeMode) {
					player.setHeldItem(hand, ItemStack.EMPTY);
				}
				onEquipped(player.getHeldItem(hand), player);
				break;
			}
	}
	return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}
 
Example 5
Project: harshencastle   File: BloodCollector.java   View Source Code Vote up 6 votes
public boolean fill(EntityPlayer player, EnumHand hand, int amount)
{
	boolean flag = false; 
	ItemStack stack = player.getHeldItem(hand);
	NBTTagCompound nbt = getNBT(stack);

	if(nbt.getInteger("Blood") + amount <= 50)
	{
		nbt.setInteger("Blood", nbt.getInteger("Blood") + amount);
		flag = true;
	}
	stack.setItemDamage(metaChange(nbt));
       stack.setTagCompound(nbt);
       player.setHeldItem(hand, stack);
	return flag;
}
 
Example 6
Project: Got-Wood   File: SappyLog.java   View Source Code Vote up 6 votes
@Override //&& state.getValue(GENERATED)==1
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
		EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
	if(heldItem == null){
		return super.onBlockActivated(worldIn, pos, state, playerIn, hand, heldItem, side, hitX, hitY, hitZ);
	}
	
	if( !worldIn.isRemote  && heldItem.getItem() == Items.BOWL && side != EnumFacing.UP && side != EnumFacing.DOWN){
		
                  ItemStack itemstack1 = new ItemStack(ItemRegistry.rubber_sap);

                  if (--heldItem.stackSize == 0)
                  {
                      playerIn.setHeldItem(hand, itemstack1);
                  }
                  else if (!playerIn.inventory.addItemStackToInventory(itemstack1))
                  {
                      playerIn.dropItem(itemstack1, false);
                  }
	}
	return super.onBlockActivated(worldIn, pos, state, playerIn, hand, heldItem, side, hitX, hitY, hitZ);
	
}
 
Example 7
Project: Got-Wood   File: BlockSappyLog.java   View Source Code Vote up 6 votes
@Override //&& state.getValue(GENERATED)==1
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
	ItemStack heldItem = playerIn.getHeldItem(hand);

	if (heldItem.isEmpty()) {
		return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
	}

	if (!worldIn.isRemote && heldItem.getItem() == Items.BOWL && facing != EnumFacing.UP && facing != EnumFacing.DOWN) {

		ItemStack itemstack1 = new ItemStack(ItemRegistry.rubber_sap);

		heldItem.shrink(1);
		if (heldItem.isEmpty()) {
			playerIn.setHeldItem(hand, itemstack1);
		} else if (!playerIn.inventory.addItemStackToInventory(itemstack1)) {
			playerIn.dropItem(itemstack1, false);
		}
	}
	return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);

}
 
Example 8
Project: FoodCraft-Reloaded   File: ItemFruitJuice.java   View Source Code Vote up 5 votes
@Nonnull
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (worldIn.getBlockState(pos).getBlock() instanceof BlockCake) {
        worldIn.setBlockState(pos, FoodCraftReloaded.getLoader(FruitEnumLoader.class).get().getInstanceMap(BlockFruitCake.class).get(fruitType).getDefaultState().withProperty(BlockCake.BITES, worldIn.getBlockState(pos).getValue(BlockCake.BITES)));
        player.setHeldItem(hand, new ItemStack(FCRItems.GLASS_BOTTLE));
        return EnumActionResult.SUCCESS;
    }
    return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}
 
Example 9
Project: genera   File: BlockAltar.java   View Source Code Vote up 5 votes
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand,
                                EnumFacing side, float hitX, float hitY, float hitZ) {
    ItemStack handStack = player.getHeldItem(hand);
    if (handStack.getItem() == GeneraItems.ItemGlassJar &&
            handStack.getMetadata() == 1) {
        NBTTagCompound nbt = handStack.getTagCompound();
        if (nbt == null) return false;
        PotionType potion;
        player.getEntityData().setInteger("genera.sacrifices_made", player.getEntityData().getInteger("genera.sacrifices_made") + 1);
        switch (nbt.getInteger("type")) {
            case 0:
                potion = PotionType.getPotionTypeForName("regeneration");
                if (potion == null) return false;
                player.setHeldItem(hand, PotionUtils.addPotionToItemStack(new ItemStack(Items.POTIONITEM), potion));
                return false;
            case 1:
                potion = PotionType.getPotionTypeForName("night_vision");
                if (potion == null) return false;
                player.setHeldItem(hand, PotionUtils.addPotionToItemStack(new ItemStack(Items.POTIONITEM), potion));
                return false;
            case 2:
                potion = PotionType.getPotionTypeForName("leaping");
                if (potion == null) return false;
                player.setHeldItem(hand, PotionUtils.addPotionToItemStack(new ItemStack(Items.POTIONITEM), potion));
                return false;
        }
    }
    return false;
}
 
Example 10
Project: chesttransporter   File: ItemChestTransporter.java   View Source Code Vote up 5 votes
private void placeChest(TransportableChest chest, ItemStack stack, EntityPlayer player, EnumHand hand, World world, BlockPos pos, EnumFacing face)
{
    BlockPos chestPos = getChestCoords(world, pos, face);

    if (!chest.canPlaceChest(world, chestPos, player, stack))
        return;

    ItemStack chestStack = chest.createChestStack(stack);
    if (chestStack.isEmpty()) return;

    player.setHeldItem(hand, chestStack);
    EnumActionResult result = chestStack.onItemUse(player, world, pos, hand, face, 0.0f, 0.0f, 0.0f);
    player.setHeldItem(hand, stack);
    if (result != EnumActionResult.SUCCESS)
    {
        return;
    }

    TileEntity tile = world.getTileEntity(chestPos);
    if (tile == null) return;

    if (chest.copyTileEntity())
    {
        NBTTagCompound nbt = getTagCompound(stack).getCompoundTag("ChestTile");
        nbt = chest.modifyTileCompound(nbt, world, pos, player, stack);
        world.setTileEntity(chestPos, TileEntity.create(world, nbt));
    } else
    {
        IInventory inv = (IInventory) tile;
        moveItemsIntoChest(stack, inv);
    }

    getTagCompound(stack).removeTag("ChestType");
    getTagCompound(stack).removeTag("ChestName");

    chest.onChestPlaced(world, pos, player, stack);

    damageItem(stack, player);
}
 
Example 11
Project: Bewitchment   File: TileCauldron.java   View Source Code Vote up 5 votes
private void giveItem(EntityPlayer player, EnumHand hand, ItemStack heldItem, ItemStack toGive) {
	if (heldItem.isEmpty() || heldItem.getCount() - 1 == 0) {
		player.setHeldItem(hand, toGive);
		heldItem.shrink(1);
	} else if (!player.inventory.addItemStackToInventory(toGive)) {
		player.dropItem(toGive, false);
	} else if (player instanceof EntityPlayerMP) {
		((EntityPlayerMP) player).sendContainerToPlayer(player.inventoryContainer);
	}
}
 
Example 12
Project: FoodCraft-Reloaded   File: ItemVegetableJuice.java   View Source Code Vote up 5 votes
@Nonnull
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (worldIn.getBlockState(pos).getBlock() instanceof BlockCake) {
        worldIn.setBlockState(pos, FoodCraftReloaded.getLoader(VegetableEnumLoader.class).get().getInstanceMap(BlockVegetableCake.class).get(vegetableType).getDefaultState().withProperty(BlockCake.BITES, worldIn.getBlockState(pos).getValue(BlockCake.BITES)));
        player.setHeldItem(hand, new ItemStack(FCRItems.GLASS_BOTTLE));
        return EnumActionResult.SUCCESS;
    }
    return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}
 
Example 13
Project: Backmemed   File: EntityArmorStand.java   View Source Code Vote up 5 votes
private void swapItem(EntityPlayer player, EntityEquipmentSlot p_184795_2_, ItemStack p_184795_3_, EnumHand hand)
{
    ItemStack itemstack = this.getItemStackFromSlot(p_184795_2_);

    if (itemstack.func_190926_b() || (this.disabledSlots & 1 << p_184795_2_.getSlotIndex() + 8) == 0)
    {
        if (!itemstack.func_190926_b() || (this.disabledSlots & 1 << p_184795_2_.getSlotIndex() + 16) == 0)
        {
            if (player.capabilities.isCreativeMode && itemstack.func_190926_b() && !p_184795_3_.func_190926_b())
            {
                ItemStack itemstack2 = p_184795_3_.copy();
                itemstack2.func_190920_e(1);
                this.setItemStackToSlot(p_184795_2_, itemstack2);
            }
            else if (!p_184795_3_.func_190926_b() && p_184795_3_.func_190916_E() > 1)
            {
                if (itemstack.func_190926_b())
                {
                    ItemStack itemstack1 = p_184795_3_.copy();
                    itemstack1.func_190920_e(1);
                    this.setItemStackToSlot(p_184795_2_, itemstack1);
                    p_184795_3_.func_190918_g(1);
                }
            }
            else
            {
                this.setItemStackToSlot(p_184795_2_, p_184795_3_);
                player.setHeldItem(hand, itemstack);
            }
        }
    }
}
 
Example 14
Project: Zombe-Modpack   File: PlayerControllerMP.java   View Source Code Vote up 5 votes
public EnumActionResult processRightClick(EntityPlayer player, World worldIn, EnumHand stack)
{
    if (this.currentGameType == GameType.SPECTATOR)
    {
        return EnumActionResult.PASS;
    }
    else
    {
        this.syncCurrentPlayItem();
        this.connection.sendPacket(new CPacketPlayerTryUseItem(stack));
        ItemStack itemstack = player.getHeldItem(stack);

        if (player.getCooldownTracker().hasCooldown(itemstack.getItem()))
        {
            return EnumActionResult.PASS;
        }
        else
        {
            int i = itemstack.func_190916_E();
            ActionResult<ItemStack> actionresult = itemstack.useItemRightClick(worldIn, player, stack);
            ItemStack itemstack1 = actionresult.getResult();

            if (itemstack1 != itemstack || itemstack1.func_190916_E() != i)
            {
                player.setHeldItem(stack, itemstack1);
            }

            return actionresult.getType();
        }
    }
}
 
Example 15
Project: Clef   File: PacketCreateInstrument.java   View Source Code Vote up 4 votes
@Override
public void execute(Side side, EntityPlayer player)
{
    if(Clef.config.creatableInstruments == 0)
    {
        return;
    }

    TileEntity te = player.world.getTileEntity(pos);
    if(te instanceof TileEntityInstrumentPlayer)
    {
        TileEntityInstrumentPlayer player1 = (TileEntityInstrumentPlayer)te;
        boolean full = true;
        for(int i = 0; i < 9 ; i++)
        {
            if(player1.getStackInSlot(i) == null)
            {
                full = false;
                break;
            }
        }
        if(full)
        {
            ItemStack is1 = player.getHeldItemMainhand();
            if(is1.getItem() == Items.NAME_TAG && is1.hasDisplayName())
            {
                Instrument ins = null;
                for(Instrument instrument : InstrumentLibrary.instruments)
                {
                    if(instrument.info.itemName.equals(instrumentName))
                    {
                        ins = instrument;
                        break;
                    }
                }
                if(ins == null)
                {
                    if(Clef.config.creatableInstruments == 1 || Clef.config.creatableInstruments == 3)
                    {
                        InstrumentLibrary.requestInstrument(instrumentName, player);
                    }
                }
                else if(Clef.config.creatableInstruments < 2)
                {
                    return;
                }

                for(int i = 0; i < 9; i++)
                {
                    player1.setInventorySlotContents(i, null);
                }
                ItemStack is = new ItemStack(Clef.itemInstrument, 1, 0);
                NBTTagCompound tag = new NBTTagCompound();
                tag.setString("itemName", instrumentName);
                is.setTagCompound(tag);
                InventoryHelper.spawnItemStack(player.world, pos.getX() + 0.5D, pos.getY() + 1D, pos.getZ() + 0.5D, is);
                player.world.playSound(null, pos.getX() + 0.5D, pos.getY() + 1D, pos.getZ() + 0.5D, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ((player.world.rand.nextFloat() - player.world.rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
                player1.markDirty();
                player.setHeldItem(EnumHand.MAIN_HAND, ItemStack.EMPTY);
                player.inventory.markDirty();
                player1.justCreatedInstrument = true;
            }
        }
    }
    return;
}
 
Example 16
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 17
Project: CustomWorldGen   File: EntityMooshroom.java   View Source Code Vote up 4 votes
@SuppressWarnings("unused")
public boolean processInteract(EntityPlayer player, EnumHand hand, @Nullable ItemStack stack)
{
    if (stack != null && stack.getItem() == Items.BOWL && this.getGrowingAge() >= 0 && !player.capabilities.isCreativeMode)
    {
        if (--stack.stackSize == 0)
        {
            player.setHeldItem(hand, new ItemStack(Items.MUSHROOM_STEW));
        }
        else if (!player.inventory.addItemStackToInventory(new ItemStack(Items.MUSHROOM_STEW)))
        {
            player.dropItem(new ItemStack(Items.MUSHROOM_STEW), false);
        }

        return true;
    }
    else if (false && stack != null && stack.getItem() == Items.SHEARS && this.getGrowingAge() >= 0) //Forge Disable, Moved to onSheared
    {
        this.setDead();
        this.worldObj.spawnParticle(EnumParticleTypes.EXPLOSION_LARGE, this.posX, this.posY + (double)(this.height / 2.0F), this.posZ, 0.0D, 0.0D, 0.0D, new int[0]);

        if (!this.worldObj.isRemote)
        {
            EntityCow entitycow = new EntityCow(this.worldObj);
            entitycow.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotationYaw, this.rotationPitch);
            entitycow.setHealth(this.getHealth());
            entitycow.renderYawOffset = this.renderYawOffset;

            if (this.hasCustomName())
            {
                entitycow.setCustomNameTag(this.getCustomNameTag());
            }

            this.worldObj.spawnEntityInWorld(entitycow);

            for (int i = 0; i < 5; ++i)
            {
                this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, this.posX, this.posY + (double)this.height, this.posZ, new ItemStack(Blocks.RED_MUSHROOM)));
            }

            stack.damageItem(1, player);
            this.playSound(SoundEvents.ENTITY_MOOSHROOM_SHEAR, 1.0F, 1.0F);
        }

        return true;
    }
    else
    {
        return super.processInteract(player, hand, stack);
    }
}
 
Example 18
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;
}
 
Example 19
Project: Clef   File: BlockInstrumentPlayer.java   View Source Code Vote up 4 votes
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ)
{
    TileEntity te = worldIn.getTileEntity(pos);
    if(te instanceof TileEntityInstrumentPlayer)
    {
        TileEntityInstrumentPlayer player = (TileEntityInstrumentPlayer)te;
        boolean hasSlot = false;
        ItemStack is = playerIn.getHeldItemMainhand();
        if(is.getItem() == Clef.itemInstrument && !playerIn.isSneaking())
        {
            if(is.getTagCompound() == null && !worldIn.isRemote)
            {
                InstrumentLibrary.assignRandomInstrument(is);
            }
            //Find a free slot
            for(int i = 0; i < player.getSizeInventory(); i++)
            {
                ItemStack is1 = player.getStackInSlot(i);
                if(is1.isEmpty())
                {
                    hasSlot = true;
                    worldIn.playSound(null, pos.getX() + 0.5D, pos.getY() + 1D, pos.getZ() + 0.5D, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ((worldIn.rand.nextFloat() - worldIn.rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
                    if(!worldIn.isRemote)
                    {
                        player.setInventorySlotContents(i, is);
                        player.markDirty();
                        playerIn.setHeldItem(EnumHand.MAIN_HAND, ItemStack.EMPTY);
                        playerIn.inventory.markDirty();
                        worldIn.notifyBlockUpdate(pos, state, state, 3);
                    }
                    break;
                }
            }
        }
        else if(is.getItem() == Items.NAME_TAG && is.hasDisplayName())
        {
            boolean full = true;
            for(int i = 0; i < 9 ; i++)
            {
                if(player.getStackInSlot(i).isEmpty())
                {
                    full = false;
                    break;
                }
            }
            if(!full)
            {
                return false;
            }
            if(worldIn.isRemote)
            {
                for(Instrument instrument : InstrumentLibrary.instruments)
                {
                    if(instrument.info.itemName.equalsIgnoreCase(is.getDisplayName()))
                    {
                        Clef.channel.sendToServer(new PacketCreateInstrument(instrument.info.itemName, pos));
                        break;
                    }
                }
            }
            return true;
        }
        if(!hasSlot && !player.justCreatedInstrument)
        {
            FMLNetworkHandler.openGui(playerIn, Clef.instance, 0, worldIn, pos.getX(), pos.getY(), pos.getZ());
        }
        return true;
    }
    return false;
}
 
Example 20
Project: VanillaExtras   File: BlockJar.java   View Source Code Vote up 4 votes
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
		EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
	ItemStack itemstack = playerIn.getHeldItemMainhand();
	if (itemstack.isEmpty()) {
		return true;
	} else {

		Item item = playerIn.getHeldItemMainhand().getItem();

		if (item == Items.BUCKET) {

			itemstack.shrink(1);

			if (itemstack.isEmpty()) {
				playerIn.setHeldItem(hand, new ItemStack(Items.WATER_BUCKET));
			} else if (!playerIn.inventory.addItemStackToInventory(new ItemStack(Items.WATER_BUCKET))) {
				playerIn.dropItem(new ItemStack(Items.WATER_BUCKET), false);
			}

			return true;
		} else if (item == Items.GLASS_BOTTLE) {

			itemstack.shrink(1);

			ItemStack itemstack1 = PotionUtils.addPotionToItemStack(new ItemStack(Items.POTIONITEM),
					PotionTypes.WATER);

			if (itemstack.isEmpty()) {
				playerIn.setHeldItem(hand, itemstack1);
			} else if (!playerIn.inventory.addItemStackToInventory(itemstack1)) {
				playerIn.dropItem(itemstack1, false);
			} else if (playerIn instanceof EntityPlayerMP) {
				((EntityPlayerMP) playerIn).sendContainerToPlayer(playerIn.inventoryContainer);
			}

			return true;
		}
		return false;
	}

}