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

The following examples show how to use net.minecraft.entity.player.EntityPlayer#setHeldItem() . 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: BlockDoubanjiangBarrel.java    From TofuCraftReload with MIT License 6 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) {
	if(playerIn.getHeldItem(hand).getItem() ==Items.GLASS_BOTTLE && hasSoy(state)){
		ItemStack doubanjiang = new ItemStack(ItemLoader.sauce_bottle,1,3);
		if(playerIn.getHeldItem(hand).getCount()>1){
			playerIn.getHeldItem(hand).shrink(1);
			playerIn.inventory.addItemStackToInventory(doubanjiang);
		}
		else{
		playerIn.setHeldItem(hand, doubanjiang);
		}
		worldIn.setBlockState(pos, this.withFerm(8), 2);
	}
	return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}
 
Example 2
Source File: ItemSheath.java    From Sakura_mod with MIT License 6 votes vote down vote up
public void sheath_In(EntityPlayer player) {
	ItemStack item_l = player.getHeldItemMainhand();
	ItemStack item_r = player.getHeldItemOffhand();
	if(item_r.getItem()==ItemLoader.KATANA){
		player.playSound(SoundEvents.BLOCK_ANVIL_HIT, 1F, 1F);
		player.setHeldItem(EnumHand.MAIN_HAND, ItemLoader.KATANA_SHEATH.setBlade(item_r));
		player.setHeldItem(EnumHand.OFF_HAND, ItemStack.EMPTY);
	}else if(item_r.getItem()==ItemLoader.SAKURAKATANA){
		player.playSound(SoundEvents.BLOCK_ANVIL_HIT, 1F, 1F);
		player.setHeldItem(EnumHand.MAIN_HAND, ItemLoader.SAKURAKATANA_SHEATH.setBlade(item_r));
		player.setHeldItem(EnumHand.OFF_HAND, ItemStack.EMPTY);
	}else if(item_l.getItem()==ItemLoader.KATANA){
		player.playSound(SoundEvents.BLOCK_ANVIL_HIT, 1F, 1F);
		player.setHeldItem(EnumHand.OFF_HAND, ItemLoader.KATANA_SHEATH.setBlade(item_l));
		player.setHeldItem(EnumHand.MAIN_HAND, ItemStack.EMPTY);
	}else if(item_l.getItem()==ItemLoader.SAKURAKATANA){
		player.playSound(SoundEvents.BLOCK_ANVIL_HIT, 1F, 1F);
		player.setHeldItem(EnumHand.OFF_HAND, ItemLoader.SAKURAKATANA_SHEATH.setBlade(item_l));
		player.setHeldItem(EnumHand.MAIN_HAND, ItemStack.EMPTY);
	}	
}
 
Example 3
Source File: EmptySyringe.java    From EmergingTechnology with MIT License 6 votes vote down vote up
@Override
public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer playerIn, EntityLivingBase target,
        EnumHand hand) {

    if (playerIn.world.isRemote) {
        return false;
    }

    String entityId = EntityList.getKey(target).toString();

    ItemStack itemStack = ModTissueProvider.getSyringeItemStackByEntityId(entityId);

    if (!StackHelper.isItemStackEmpty(itemStack)) {
        ItemStack newItemStack = stack.copy();
        newItemStack.shrink(1);
        playerIn.setHeldItem(hand, newItemStack);
        playerIn.addItemStackToInventory(itemStack);
        return true;
    }

    return false;
}
 
Example 4
Source File: MetaItem.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    ItemStack stack = player.getHeldItem(hand);
    ItemStack originalStack = stack.copy();
    for (IItemBehaviour behaviour : getBehaviours(stack)) {
        ActionResult<ItemStack> behaviourResult = behaviour.onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ);
        stack = behaviourResult.getResult();
        if (behaviourResult.getType() != EnumActionResult.PASS) {
            if (!ItemStack.areItemStacksEqual(originalStack, stack))
                player.setHeldItem(hand, stack);
            return behaviourResult.getType();
        } else if (stack.isEmpty()) {
            player.setHeldItem(hand, ItemStack.EMPTY);
            return EnumActionResult.PASS;
        }
    }
    return EnumActionResult.PASS;
}
 
Example 5
Source File: AbstractUsableBehaviour.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public boolean useItemDurability(EntityPlayer player, EnumHand hand, ItemStack stack, ItemStack replacementStack) {
    int usesLeft = getUsesLeft(stack);
    if (!player.capabilities.isCreativeMode) {
        if (--usesLeft <= 0) {
            if (replacementStack.isEmpty()) {
                //if replacement stack is empty, just shrink resulting stack
                stack.shrink(1);
            } else {
                //otherwise, update held item to replacement stack
                player.setHeldItem(hand, replacementStack);
            }
            return true;
        }
        setUsesLeft(stack, usesLeft);
    }
    return true;
}
 
Example 6
Source File: GTItemSprayCan.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public EnumActionResult onItemUseFirst(EntityPlayer playerIn, World world, BlockPos pos, EnumFacing side,
		float hitX, float hitY, float hitZ, EnumHand handIn) {
	if (!playerIn.isSneaking() && handIn == EnumHand.MAIN_HAND) {
		ItemStack playerStack = playerIn.getHeldItemMainhand();
		NBTTagCompound nbt = StackUtil.getNbtData(playerStack);
		if (nbt.hasKey(COLOR)) {
			EnumDyeColor dye = EnumDyeColor.byDyeDamage(nbt.getInteger(COLOR));
			if (colorBlock(world.getBlockState(pos), world, pos, null, dye)) {
				if (playerStack.getItemDamage() < playerStack.getMaxDamage()) {
					playerStack.damageItem(1, playerIn);
					if (!IC2.platform.isSimulating()) {
						IC2.audioManager.playOnce(playerIn, Ic2Sounds.painterUse);
					}
				} else {
					playerIn.setHeldItem(handIn, GTMaterialGen.get(GTItems.sprayCanEmpty));
					if (!IC2.platform.isSimulating()) {
						playerIn.playSound(SoundEvents.ENTITY_ITEM_BREAK, 1.0F, 1.0F);
					}
				}
				return EnumActionResult.SUCCESS;
			}
		}
	}
	return EnumActionResult.PASS;
}
 
Example 7
Source File: BlockAltarConfession.java    From Wizardry with GNU Lesser General Public License v3.0 6 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 heldItem = playerIn.getHeldItem(hand);

	//if (isStructureComplete(worldIn, pos)) {
	TileHaloInfuser infuser = getTE(worldIn, pos);
	if (infuser == null) return heldItem.isEmpty();

	if (!infuser.getHalo().isEmpty()) {
		playerIn.setHeldItem(hand, infuser.extractHalo());
		playerIn.openContainer.detectAndSendChanges();
	} else if (heldItem.getItem() == ModItems.FAKE_HALO) {
		infuser.setHalo(heldItem);
		playerIn.setHeldItem(hand, ItemStack.EMPTY);
		playerIn.openContainer.detectAndSendChanges();
	}
	//}
	return heldItem.isEmpty();
}
 
Example 8
Source File: BlockAltarSacrament.java    From Wizardry with GNU Lesser General Public License v3.0 6 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 heldItem = playerIn.getHeldItem(hand);

	//if (isStructureComplete(worldIn, pos)) {
		TileHaloInfuser infuser = getTE(worldIn, pos);
		if (infuser == null) return heldItem.isEmpty();

		if (!infuser.getHalo().isEmpty()) {
			playerIn.setHeldItem(hand, infuser.extractHalo());
			playerIn.openContainer.detectAndSendChanges();
		} else if (heldItem.getItem() == ModItems.FAKE_HALO) {
			infuser.setHalo(heldItem);
			playerIn.setHeldItem(hand, ItemStack.EMPTY);
			playerIn.openContainer.detectAndSendChanges();
		}
	//}
	return heldItem.isEmpty();
}
 
Example 9
Source File: TileEntityBarrel.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
private boolean tryApplyUpgrade(EntityPlayer player, EnumHand hand, int slot, Item item, int meta)
{
    ItemStack stack = player.getHeldItem(hand);

    if (stack.getItem() == item && stack.getMetadata() == meta)
    {
        if (this.itemHandlerUpgrades.insertItem(slot, new ItemStack(EnderUtilitiesItems.ENDER_PART, 1, meta), false).isEmpty())
        {
            if (player.capabilities.isCreativeMode == false)
            {
                stack.shrink(1);
                player.setHeldItem(hand, stack.isEmpty() ? ItemStack.EMPTY : stack);
            }

            this.getWorld().playSound(null, this.getPos(), SoundEvents.ENTITY_ITEMFRAME_PLACE, SoundCategory.BLOCKS, 1f, 1f);

            return true;
        }
    }

    return false;
}
 
Example 10
Source File: BlockMisoBarrel.java    From TofuCraftReload with 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) {
    if (playerIn.getHeldItem(hand).getItem() == Items.BUCKET && hasSoy(state)) {

        if (playerIn.getHeldItem(hand).getCount() > 1) {
            playerIn.getHeldItem(hand).shrink(1);
            playerIn.inventory.addItemStackToInventory(FluidUtil.getFilledBucket(new FluidStack(BlockLoader.SOYSAUCE_FLUID, 1000)));
        } else {
            playerIn.setHeldItem(hand, FluidUtil.getFilledBucket(new FluidStack(BlockLoader.SOYSAUCE_FLUID, 1000)));
        }
        worldIn.setBlockState(pos, this.withFerm(8), 2);
    }
    return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}
 
Example 11
Source File: EntityHaloInfusionItem.java    From Wizardry with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean processInitialInteract(EntityPlayer player, EnumHand hand) {
	TileEntity tile = world.getTileEntity(infuserPos);
	if (!(tile instanceof TileHaloInfuser)) {
		world.removeEntity(this);
		return false;
	}

	ItemStack heldItem = player.getHeldItem(hand);
	if (heldItem.isEmpty()) {
		ItemStack slotted = getHaloInfusionItem().getStack();
		if (!slotted.isEmpty()) {
			player.setHeldItem(hand, slotted);
			setHaloInfusionItem(HaloInfusionItemRegistry.EMPTY, false);
			return true;
		} else return true;
	}

	for (HaloInfusionItem haloInfusionItem : HaloInfusionItemRegistry.getItems()) {
		if (haloInfusionItem.getStack().isItemEqual(heldItem)) {
			setHaloInfusionItem(haloInfusionItem, false);

			// todo: remove from hand
			return true;
		}
	}

	return true;
}
 
Example 12
Source File: BarrelModeFluid.java    From ExNihiloAdscensio with MIT License 5 votes vote down vote up
@Override
public boolean onBlockActivated(World world, TileBarrel barrel, BlockPos pos, IBlockState state,
		EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) {
	ItemStack stack = player.getHeldItem(EnumHand.MAIN_HAND);

	if (stack != null) {
		ItemStack remainder = getHandler(barrel).insertItem(0, stack, false);

		int size = remainder == null ? 0 : remainder.stackSize;

		if (stack.getItem().hasContainerItem(stack)) {
			ItemStack container = stack.getItem().getContainerItem(stack);

			// Should always be 1 but LET'S JUST MAKE SURE
			container.stackSize = stack.stackSize - size;

			if (!player.inventory.addItemStackToInventory(container)) {
				player.world.spawnEntity(
						new EntityItem(player.world, player.posX, player.posY, player.posZ, container));
			}
		}

		player.setHeldItem(EnumHand.MAIN_HAND, remainder);
	}

	return false;
}
 
Example 13
Source File: Hydroponic.java    From EmergingTechnology with MIT License 4 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) {

    if (worldIn.isRemote) {
        return true;
    }

    HydroponicTileEntity tileEntity = (HydroponicTileEntity) worldIn.getTileEntity(pos);
    ItemStack itemStackHeld = playerIn.getHeldItemMainhand();
    Item itemHeld = itemStackHeld.getItem();

    if (itemHeld == Items.WATER_BUCKET || itemHeld == Items.LAVA_BUCKET) {

        Fluid fluid = itemHeld == Items.WATER_BUCKET ? FluidRegistry.WATER : FluidRegistry.LAVA;

        int waterFilled = tileEntity.fluidHandler.fill(new FluidStack(fluid, 1000), true);
        
        if (waterFilled > 0 && !playerIn.isCreative()) {
            playerIn.setHeldItem(EnumHand.MAIN_HAND, new ItemStack(Items.BUCKET));
        }

    } else if (itemHeld instanceof UniversalBucket) {

        UniversalBucket bucket = (UniversalBucket) itemHeld;
        FluidStack fluidStack = bucket.getFluid(itemStackHeld);

        if (tileEntity.fluidHandler.canFillFluidType(fluidStack)) {
            tileEntity.fluidHandler.fill(fluidStack, true);
            playerIn.setHeldItem(EnumHand.MAIN_HAND, bucket.getEmpty());
        }

    } else if (HydroponicHelper.isItemStackValid(itemStackHeld) && tileEntity.itemHandler.getStackInSlot(0).isEmpty()) {

        ItemStack remainder = tileEntity.itemHandler.insertItem(0, itemStackHeld.copy(), false);

        if (!playerIn.isCreative()) {
            playerIn.setHeldItem(EnumHand.MAIN_HAND, remainder);
        }

        return true;

    } else if (PlantHelper.isPlantItem(itemHeld) && facing == EnumFacing.UP) {

        return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);

        // Otherwise open the gui
    } else {
        playerIn.openGui(EmergingTechnology.instance, Reference.GUI_HYDROPONIC, worldIn, pos.getX(), pos.getY(),
                pos.getZ());
        return true;
    }

    return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}
 
Example 14
Source File: BlockFirepit.java    From TFC2 with GNU General Public License v3.0 4 votes vote down vote up
/*******************************************************************************
 * 1. Content
 *******************************************************************************/

@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer playerIn, 
		net.minecraft.util.EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ)
{
	TileFirepit te = (TileFirepit)world.getTileEntity(pos);
	ItemStack heldItem = playerIn.getHeldItem(hand);
	if(!world.isRemote)
	{

		if(playerIn.isSneaking() && heldItem == ItemStack.EMPTY && te.hasCookingTool())
		{
			playerIn.inventory.addItemStackToInventory(te.getCookingTool());
			te.setCookingTool(ItemStack.EMPTY);
			te.ejectContents(false);
		}
		else if(heldItem.getItem() == potItem || heldItem.getItem() == skilletItem || heldItem.getItem() == saucepanItem)
		{
			if(!te.hasCookingTool())
			{
				te.setCookingTool(heldItem);
				playerIn.setHeldItem(hand, ItemStack.EMPTY);
			}
		}
		else if(heldItem.getItem() == TFCItems.Firestarter)
		{
			return false;
		}
		else if(te.hasCookingTool())
			playerIn.openGui(TFC.instance, 6, world, pos.getX(), pos.getY(), pos.getZ());
		else
			playerIn.openGui(TFC.instance, 5, world, pos.getX(), pos.getY(), pos.getZ());
	}

	//This should occur clientside
	if(heldItem.getItem() == TFCItems.Firestarter)
	{
		return false;
	}
	return true;
}
 
Example 15
Source File: TileEntityBarrel.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean onRightClickBlock(EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ)
{
    if (this.getWorld().isRemote)
    {
        return player.isSneaking() == false;
    }

    ItemStack stack = player.getHeldItem(hand);

    if (stack.isEmpty() == false)
    {
        if (this.tryToggleLocked(stack) ||
            this.tryApplyLabel(player, hand, side) ||
            this.tryApplyUpgrade(player, hand, 1, EnderUtilitiesItems.ENDER_PART, 71) || // Structure Upgrade
            this.tryApplyUpgrade(player, hand, 2, EnderUtilitiesItems.ENDER_PART, 72) || // Capacity Upgrade
            this.tryApplyUpgrade(player, hand, 3, EnderUtilitiesItems.ENDER_PART, 73) || // Void Upgrade
            this.tryToggleCreativeMode(player, stack))
        {
            this.notifyBlockUpdate(this.getPos());
            return true;
        }

        stack = InventoryUtils.tryInsertItemStackToInventory(this.itemHandlerExternal, stack);
        player.setHeldItem(hand, stack.isEmpty() ? ItemStack.EMPTY : stack);
    }

    long time = System.currentTimeMillis();
    Long last = this.rightClickTimes.get(player.getUniqueID());

    if (last != null && time - last.longValue() < 300)
    {
        InventoryUtils.tryMoveMatchingItems(player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null), this.itemHandlerExternal);
        player.openContainer.detectAndSendChanges();
    }

    this.rightClickTimes.put(player.getUniqueID(), time);

    // When this method returns false, then the GUI is opened
    return player.isSneaking() == false;
}
 
Example 16
Source File: Light.java    From EmergingTechnology with MIT License 3 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) {

    if (worldIn.isRemote) {
        return true;
    }

    TileEntity tileEntity = worldIn.getTileEntity(pos);

    if (tileEntity instanceof LightTileEntity) {

        LightTileEntity lightTileEntity = (LightTileEntity) tileEntity;

        ItemStack itemStackHeld = playerIn.getHeldItemMainhand();

        if (LightHelper.isItemStackValidBulb(itemStackHeld)
                && lightTileEntity.itemHandler.getStackInSlot(0).isEmpty()) {

            ItemStack remainder = lightTileEntity.itemHandler.insertItem(0, itemStackHeld.copy(), false);

            if (!playerIn.isCreative()) {
                playerIn.setHeldItem(EnumHand.MAIN_HAND, remainder);
            }

            return true;
        }
    }

    playerIn.openGui(EmergingTechnology.instance, Reference.GUI_LIGHT, worldIn, pos.getX(), pos.getY(), pos.getZ());

    return true;
}