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

The following examples show how to use net.minecraft.entity.player.EntityPlayer#getHeldItem() . These examples are extracted from open source projects. 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
@Override
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);

	TileOrbHolder te = getTE(worldIn, pos);

	if (!te.containsCell()) {
		if (heldItem.getItem() == ModItems.ORB || heldItem.getItem() == ModItems.PEARL_NACRE) {
			te.setItemStack(heldItem.copy());
			te.getItemStack().setCount(1);
			heldItem.shrink(1);
		} else return false;

	} else {
		ItemStack stack = te.getItemStack().copy();

		te.setItemStack(ItemStack.EMPTY);
		if (playerIn.inventory.addItemStackToInventory(stack)) playerIn.openContainer.detectAndSendChanges();
		else {
			EntityItem entityItem = new EntityItem(worldIn, pos.getX(), pos.getY() + 1, pos.getZ(), stack);
			worldIn.spawnEntity(entityItem);
		}
	}
	te.markDirty();
	return true;
}
 
Example 2
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
    ItemStack itemstack = player.getHeldItem(hand);
    IBlockState state = worldIn.getBlockState(pos);
    if (facing == EnumFacing.UP && player.canPlayerEdit(pos.offset(facing), facing, itemstack) && state.getBlock().canSustainPlant(state, worldIn, pos, EnumFacing.UP, this) && worldIn.isAirBlock(pos.up()))
    {
        worldIn.setBlockState(pos.up(), content.plant.createState());

        if (player instanceof EntityPlayerMP)
        {
            CriteriaTriggers.PLACED_BLOCK.trigger((EntityPlayerMP) player, pos.up(), itemstack);
        }

        itemstack.shrink(1);
        return EnumActionResult.SUCCESS;
    } else
    {
        return EnumActionResult.FAIL;
    }
}
 
Example 3
@SubscribeEvent
public static void onStun(TickEvent.WorldTickEvent event) {
	if (event.side == Side.CLIENT) return;

	for (EntityFairy fairy : event.world.getEntities(EntityFairy.class, input -> input != null && !input.isDead)) {

		fairy.stunned = false;

		secondary:
		for (EntityPlayer player : event.world.getEntitiesWithinAABB(EntityPlayer.class, fairy.getEntityBoundingBox().grow(5))) {

			for (EnumHand hand : EnumHand.values()) {
				ItemStack stack = player.getHeldItem(hand);

				if (stack.getItem() == ModItems.FAIRY_BELL) {
					fairy.stunned = true;
					break secondary;
				}
			}
		}
	}
}
 
Example 4
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand)
{
    ItemStack stack = player.getHeldItem(hand);

    if (world.isRemote)
    {
        return new ActionResult<ItemStack>(EnumActionResult.PASS, stack);
    }

    if (player.isSneaking())
    {
        RayTraceResult rayTraceResult = this.rayTrace(world, player, true);

        if (rayTraceResult != null && rayTraceResult.typeOfHit != RayTraceResult.Type.ENTITY
            && player.rotationPitch > 80.0f)
        {
            this.clearData(stack);
        }
    }

    return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
}
 
Example 5
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand)
{
    ItemStack stack = player.getHeldItem(hand);

    if (world.isRemote == false)
    {
        // These two lines are to fix the UUID being missing the first time the GUI opens,
        // if the item is grabbed from the creative inventory or from JEI or from /give
        NBTUtils.getUUIDFromItemStack(stack, "UUID", true);
        player.openContainer.detectAndSendChanges();

        player.openGui(EnderUtilities.instance, ReferenceGuiIds.GUI_ID_PICKUP_MANAGER, world,
                (int)player.posX, (int)player.posY, (int)player.posZ);
    }

    return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
}
 
Example 6
Source Project: Sakura_mod   File: BlockBarrel.java    License: MIT License 6 votes vote down vote up
@Override
  public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
      if (world.isRemote) {
          return true;
      }
ItemStack stack = player.getHeldItem(hand);
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileEntityBarrel) {
    IFluidHandlerItem handler = FluidUtil.getFluidHandler(ItemHandlerHelper.copyStackWithSize(stack, 1));
    if (handler != null) {
        FluidUtil.interactWithFluidHandler(player, hand, tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side));
        return true;
    }
    player.openGui(SakuraMain.instance, SakuraGuiHandler.ID_BARREL, world, pos.getX(), pos.getY(), pos.getZ());
    return true;
}
return true;
  }
 
Example 7
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand)
{
    ItemStack stack = player.getHeldItem(hand);

    if (player.isSneaking())
    {
        if (world.isRemote == false)
        {
            this.setTarget(stack, player, true);
        }

        return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
    }

    return new ActionResult<ItemStack>(EnumActionResult.PASS, stack);
}
 
Example 8
Source Project: Sakura_mod   File: ItemShinai.java    License: 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 9
@Override
public boolean onRightClick(EntityPlayer playerIn, EnumHand hand, EnumFacing facing, CuboidRayTraceResult hitResult) {
    ItemStack itemStack = playerIn.getHeldItem(hand);
    if(!itemStack.isEmpty() && itemStack.hasCapability(GregtechCapabilities.CAPABILITY_MALLET, null)) {
        ISoftHammerItem softHammerItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_MALLET, null);

        if (getWorld().isRemote) {
            return true;
        }
        if(!softHammerItem.damageItem(DamageValues.DAMAGE_FOR_SOFT_HAMMER, false)) {
            return false;
        }

        if (isTransformUp) {
            setTransformUp(false);
            playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.transformer.message_transform_down",
                energyContainer.getInputVoltage(), energyContainer.getInputAmperage(), energyContainer.getOutputVoltage(), energyContainer.getOutputAmperage()));
            return true;
        } else {
            setTransformUp(true);
            playerIn.sendMessage(new TextComponentTranslation("gregtech.machine.transformer.message_transform_up",
                energyContainer.getInputVoltage(), energyContainer.getInputAmperage(), energyContainer.getOutputVoltage(), energyContainer.getOutputAmperage()));
            return true;
        }
    }
    return false;
}
 
Example 10
@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
	if (world.isRemote) {
		return EnumActionResult.PASS;
	}
	IBlockState state = world.getBlockState(pos);
	TileEntity te = world.getTileEntity(pos);
	if (state.getBlock() == Blocks.CHEST && te instanceof TileEntityChest) {
		TileEntityChest chest = (TileEntityChest) te;
		ItemStack[] items = new ItemStack[chest.getSizeInventory()];
		for (int i = 0; i < items.length; i++) {
			items[i] = chest.getStackInSlot(i);
			chest.setInventorySlotContents(i, ItemStack.EMPTY);
		}
		IBlockState newState = BlocksItemsBetterChests.betterchest.getDefaultState().withProperty(BlockBetterChest.directions, state.getValue(BlockChest.FACING));
		world.setBlockState(pos, newState, 2);
		TileEntityBChest newte = new TileEntityBChest();
		world.setTileEntity(pos, newte);
		for (int i = 0; i < items.length; i++) {
			newte.getChestPart().setInventorySlotContents(i, items[i]);
		}
		world.notifyBlockUpdate(pos, state, newState, 1);
		ItemStack heldItem = player.getHeldItem(hand);
		heldItem.setCount(heldItem.getCount() - 1);
		return EnumActionResult.SUCCESS;
	}
	return EnumActionResult.PASS;
}
 
Example 11
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
{
    ItemStack stack = playerIn.getHeldItem(handIn);
    Optional<ContentGuiBase> gui = getGui(stack.getMetadata());
    if (gui.isPresent())
    {
        playerIn.openGui(CustomStuff4.INSTANCE, gui.get().getGuiId(), worldIn, handIn.ordinal(), -1, 0);
        return new ActionResult<>(EnumActionResult.SUCCESS, stack);
    } else
    {
        return new ActionResult<>(EnumActionResult.PASS, stack);
    }
}
 
Example 12
@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos,
        EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand)
{
    ItemStack stack = player.getHeldItem(hand);

    if (LinkMode.fromStack(stack) == LinkMode.ENABLED &&
        OwnerData.canAccessSelectedModule(stack, ModuleType.TYPE_LINKCRYSTAL, player) == false)
    {
        return EnumActionResult.FAIL;
    }

    if (world.isRemote == false)
    {
        if (this.useBucketOnFluidBlock(world, player, stack) == EnumActionResult.SUCCESS)
        {
            return EnumActionResult.SUCCESS;
        }

        TileEntity te = world.getTileEntity(pos);

        if (te != null && te.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side))
        {
            // If we are in bind mode, bind the bucket to the targeted tank and then return
            if (BucketMode.fromStack(stack) == BucketMode.BIND)
            {
                return super.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ);
            }

            return this.useBucketOnTank(world, pos, side, player, stack);
        }
    }

    return EnumActionResult.PASS;
}
 
Example 13
Source Project: OpenModsLib   File: ItemOpenBlock.java    License: MIT License 5 votes vote down vote up
/**
 * Replicates the super method, but with our own hooks
 */
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
	final IBlockState clickedBlockState = world.getBlockState(pos);
	final Block clickedBlock = clickedBlockState.getBlock();

	if (!clickedBlock.isReplaceable(world, pos)) pos = pos.offset(facing);

	ItemStack stack = player.getHeldItem(hand);

	if (!isStackValid(stack, player)) return EnumActionResult.FAIL;
	if (!player.canPlayerEdit(pos, facing, stack)) return EnumActionResult.FAIL;
	if (!world.mayPlace(this.block, pos, false, facing, (Entity)null)) return EnumActionResult.FAIL;

	final int itemMetadata = getMetadata(stack.getMetadata());

	if (this.block instanceof OpenBlock) {
		final OpenBlock openBlock = (OpenBlock)this.block;
		if (!openBlock.canBlockBePlaced(world, pos, hand, facing, hitX, hitY, hitZ, itemMetadata, player)) return EnumActionResult.FAIL;
	}

	final IBlockState newBlockState = this.block.getStateForPlacement(world, pos, facing, hitX, hitY, hitZ, itemMetadata, player, hand);

	if (placeBlockAt(stack, player, world, pos, facing, hitX, hitY, hitZ, newBlockState)) {
		final SoundType soundType = this.block.getSoundType(newBlockState, world, pos, player);
		world.playSound(player, pos, soundType.getPlaceSound(), SoundCategory.BLOCKS, (soundType.getVolume() + 1.0F) / 2.0F, soundType.getPitch() * 0.8F);
		afterBlockPlaced(stack, player, world, pos);
	}

	return EnumActionResult.SUCCESS;
}
 
Example 14
Source Project: Sakura_mod   File: BlockPepperSplint.java    License: MIT License 5 votes vote down vote up
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
		EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
	ItemStack heldSeed = playerIn.getHeldItem(hand);
	if(worldIn.isRemote) return true;
	if(ItemStack.areItemsEqual(heldSeed, new ItemStack(ItemLoader.MATERIAL, 1, 19))){
		worldIn.setBlockState(pos, BlockLoader.PEPPERCROP.getDefaultState().withProperty(BlockPepperCrop.AGE, 0));
		if(!playerIn.isCreative())heldSeed.shrink(1);
		return true;
	}
	return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}
 
Example 15
@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) {
	ItemStack stack = player.getHeldItem(hand);
	if (stack.getItemDamage() == 1) {
		player.setActiveHand(hand);
		return new ActionResult<>(EnumActionResult.SUCCESS, stack);
	}
	return new ActionResult<>(EnumActionResult.FAIL, stack);
}
 
Example 16
Source Project: Sakura_mod   File: ItemBroom.java    License: MIT License 5 votes vote down vote up
/**
    * Called when a Block is right-clicked with this Item
    */
@Override
   public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
   {
       ItemStack itemstack = player.getHeldItem(hand);

       if (!player.canPlayerEdit(pos.offset(facing), facing, itemstack))
       {
           return EnumActionResult.FAIL;
       }
	IBlockState iblockstate = worldIn.getBlockState(pos);
	Block block = iblockstate.getBlock();

	if (facing != EnumFacing.DOWN && worldIn.getBlockState(pos.up()).getMaterial() == Material.AIR && ((worldIn.getBlockState(pos).getMaterial() == Material.GROUND||worldIn.getBlockState(pos).getMaterial() ==Material.GRASS) && !(block instanceof BlockFarmland||block instanceof BlockGrassPath)))
	{
	    IBlockState iblockstate1 = Blocks.GRASS_PATH.getDefaultState();
	    worldIn.playSound(player, pos, SoundEvents.BLOCK_GRASS_STEP, SoundCategory.BLOCKS, 1.2F, 1.2F);
	    worldIn.playSound(player, pos, SoundEvents.BLOCK_GRASS_PLACE, SoundCategory.BLOCKS, 1.2F, 1.2F);
	    if (!worldIn.isRemote)
	    {
	        worldIn.setBlockState(pos, iblockstate1, 11);
	        itemstack.damageItem(1, player);
	    }

	    return EnumActionResult.SUCCESS;
	}
	return EnumActionResult.PASS;
   }
 
Example 17
@NotNull
@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
	ItemStack stack = player.getHeldItem(hand);
	if (stack.getItemDamage() != 1) {
		BlockPos offset = pos.offset(side);
		IBlockState offsetState = world.getBlockState(offset);
		if (offsetState.getBlock().isAir(offsetState, world, offset) || offsetState.getBlock().isReplaceable(world, offset)) {

			if (!world.mayPlace(ModBlocks.JAR, offset, false, side, player)) return PASS;

			boolean replacable = world.getBlockState(pos).getBlock().isReplaceable(world, pos);
			boolean success = world.setBlockState(replacable ? pos : offset, ModBlocks.JAR.getDefaultState());
			if (success) {
				world.playSound(pos.getX(), pos.getY(), pos.getZ(), SoundEvents.BLOCK_GLASS_PLACE, SoundCategory.BLOCKS, 1, 1, false);

				if (!player.isCreative())
					stack.shrink(1);

				TileEntity tileEntity = world.getTileEntity(replacable ? pos : offset);
				if (tileEntity instanceof TileJar) {
					TileJar jar = (TileJar) tileEntity;
					jar.fairy = FairyData.deserialize(NBTHelper.getCompound(stack, "fairy"));
					jar.markDirty();
					world.checkLight(replacable ? pos : offset);
				}
			}

			return SUCCESS;
		}
	}
	return PASS;
}
 
Example 18
Source Project: TofuCraftReload   File: ItemTofuShield.java    License: MIT License 4 votes vote down vote up
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
    ItemStack itemstack = playerIn.getHeldItem(handIn);
    playerIn.setActiveHand(handIn);
    return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, itemstack);
}
 
Example 19
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
    ItemStack itemstack = player.getHeldItem(hand);

    if (!itemstack.isEmpty() && player.canPlayerEdit(pos, facing, itemstack))
    {
        IBlockState iblockstate = worldIn.getBlockState(pos);
        Block block = iblockstate.getBlock();
        BlockPos blockpos = pos;

        if ((facing != EnumFacing.UP || block != this.block) && !block.isReplaceable(worldIn, pos))
        {
            blockpos = pos.offset(facing);
            iblockstate = worldIn.getBlockState(blockpos);
            block = iblockstate.getBlock();
        }

        if (block == this.block)
        {
            int i = iblockstate.getValue(BlockSnow.LAYERS);

            if (i < 8)
            {
                IBlockState iblockstate1 = iblockstate.withProperty(BlockSnow.LAYERS, i + 1);
                AxisAlignedBB axisalignedbb = iblockstate1.getCollisionBoundingBox(worldIn, blockpos);

                if (axisalignedbb != Block.NULL_AABB && worldIn.checkNoEntityCollision(axisalignedbb.offset(blockpos)) && worldIn.setBlockState(blockpos, iblockstate1, 10))
                {
                    SoundType soundtype = this.block.getSoundType(iblockstate1, worldIn, pos, player);
                    worldIn.playSound(player, blockpos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);

                    if (player instanceof EntityPlayerMP)
                    {
                        CriteriaTriggers.PLACED_BLOCK.trigger((EntityPlayerMP)player, pos, itemstack);
                    }

                    itemstack.shrink(1);
                    return EnumActionResult.SUCCESS;
                }
            }
        }

        return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
    }
    else
    {
        return EnumActionResult.FAIL;
    }
}
 
Example 20
Source Project: TofuCraftReload   File: ItemNigari.java    License: 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);
	}
}