Java Code Examples for net.minecraftforge.items.ItemHandlerHelper#giveItemToPlayer()

The following examples show how to use net.minecraftforge.items.ItemHandlerHelper#giveItemToPlayer() . 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: Filler.java    From EmergingTechnology 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 (worldIn.isRemote) {
        return true;
    }

    ItemStack itemStackHeld = playerIn.getHeldItemMainhand();
    Item itemHeld = itemStackHeld.getItem();

    if (itemHeld == Items.BUCKET) {
        ItemHandlerHelper.giveItemToPlayer(playerIn, new ItemStack(Items.WATER_BUCKET));
        itemStackHeld.shrink(1);
    }

    return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}
 
Example 2
Source File: GTItemFluidTube.java    From GT-Classic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public ActionResult<ItemStack> tryPickUpFluid(@Nonnull World world, @Nonnull EntityPlayer player,
		@Nonnull EnumHand hand, ItemStack itemstack, FluidStack fluidStack) {
	RayTraceResult mop = this.rayTrace(world, player, true);
	ActionResult<ItemStack> ret = ForgeEventFactory.onBucketUse(player, world, itemstack, mop);
	if (ret != null)
		return ret;
	if (mop == null) {
		return ActionResult.newResult(EnumActionResult.PASS, itemstack);
	}
	BlockPos clickPos = mop.getBlockPos();
	if (world.isBlockModifiable(player, clickPos)) {
		FluidActionResult result = FluidUtil.tryPickUpFluid(itemstack, player, world, clickPos, mop.sideHit);
		if (result.isSuccess()) {
			ItemHandlerHelper.giveItemToPlayer(player, result.getResult());
			itemstack.shrink(1);
			return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
		}
	}
	return ActionResult.newResult(EnumActionResult.FAIL, itemstack);
}
 
Example 3
Source File: GTTileRedstoneTransmitter.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean onRightClick(EntityPlayer player, EnumHand arg1, EnumFacing arg2, Side arg3) {
	ItemStack slotStack = this.getStackInSlot(0);
	if (slotStack.isEmpty() || !player.isSneaking()) {
		return false;
	}
	ItemHandlerHelper.giveItemToPlayer(player, slotStack.copy());
	slotStack.shrink(1);
	this.onBlockBreak();
	this.targetPos = null;
	IC2.audioManager.playOnce(player, Ic2Sounds.wrenchUse);
	return true;
}
 
Example 4
Source File: GTTileEnergyTransmitter.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean onRightClick(EntityPlayer player, EnumHand arg1, EnumFacing arg2, Side arg3) {
	ItemStack slotStack = this.getStackInSlot(0);
	if (slotStack.isEmpty() || !player.isSneaking()) {
		return false;
	}
	ItemHandlerHelper.giveItemToPlayer(player, slotStack.copy());
	slotStack.shrink(1);
	this.targetPos = null;
	IC2.audioManager.playOnce(player, Ic2Sounds.wrenchUse);
	return true;
}
 
Example 5
Source File: GTTileDisplayScreen.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean onRightClick(EntityPlayer player, EnumHand arg1, EnumFacing arg2, Side arg3) {
	ItemStack slotStack = this.getStackInSlot(0);
	if (slotStack.isEmpty() || !player.isSneaking()) {
		return false;
	}
	ItemHandlerHelper.giveItemToPlayer(player, slotStack.copy());
	slotStack.shrink(1);
	this.resetTargetPos();
	IC2.audioManager.playOnce(player, Ic2Sounds.wrenchUse);
	return true;
}
 
Example 6
Source File: GTTileTesseractSlave.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean onRightClick(EntityPlayer player, EnumHand arg1, EnumFacing arg2, Side arg3) {
	ItemStack slotStack = this.getStackInSlot(0);
	if (slotStack.isEmpty() || !player.isSneaking()) {
		return false;
	}
	ItemHandlerHelper.giveItemToPlayer(player, slotStack.copy());
	slotStack.shrink(1);
	setTarget(null);
	this.targetPos = null;
	IC2.audioManager.playOnce(player, Ic2Sounds.wrenchUse);
	return true;
}
 
Example 7
Source File: GTItemFluidTube.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ActionResult<ItemStack> tryPlaceFluid(@Nonnull World world, @Nonnull EntityPlayer player,
		@Nonnull EnumHand hand, ItemStack itemstack, FluidStack fluidStack) {
	RayTraceResult mop = this.rayTrace(world, player, false);
	ActionResult<ItemStack> ret = ForgeEventFactory.onBucketUse(player, world, itemstack, mop);
	if (ret != null)
		return ret;
	if (mop == null || mop.typeOfHit != RayTraceResult.Type.BLOCK) {
		return ActionResult.newResult(EnumActionResult.PASS, itemstack);
	}
	BlockPos clickPos = mop.getBlockPos();
	if (world.isBlockModifiable(player, clickPos)) {
		BlockPos targetPos = clickPos.offset(mop.sideHit);
		if (player.canPlayerEdit(targetPos, mop.sideHit, itemstack)) {
			FluidActionResult result = FluidUtil.tryPlaceFluid(player, world, targetPos, itemstack, fluidStack);
			if (result.isSuccess() && !player.capabilities.isCreativeMode) {
				player.addStat(StatList.getObjectUseStats(this));
				itemstack.shrink(1);
				ItemStack emptyStack = new ItemStack(GTItems.testTube);
				if (itemstack.isEmpty()) {
					return ActionResult.newResult(EnumActionResult.SUCCESS, emptyStack);
				} else {
					ItemHandlerHelper.giveItemToPlayer(player, emptyStack);
					return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
				}
			}
		}
	}
	return ActionResult.newResult(EnumActionResult.FAIL, itemstack);
}
 
Example 8
Source File: GTBlockMortar.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand h,
		EnumFacing facing, float hitX, float hitY, float hitZ) {
	ItemStack playerStack = player.getHeldItemMainhand();
	if (playerStack.isEmpty()) {
		return false;
	}
	int matches = 0;
	for (IRecipeInput inputMatcher : INPUT_LIST) {
		if (inputMatcher.matches(playerStack)) {
			matches++;
		}
	}
	if (matches == 0) {
		return false;
	}
	if (IC2.platform.isSimulating()) {
		int chance = this == GTBlocks.ironMortar ? 2 : 15;
		if (worldIn.rand.nextInt(chance) != 0) {
			return true;
		}
		for (MultiRecipe recipe : RECIPE_LIST.getRecipeList()) {
			IRecipeInput inputStack = recipe.getInputs().get(0);
			ItemStack outputStack = recipe.getOutputs().getAllOutputs().get(0);
			if (inputStack.matches(playerStack)) {
				playerStack.shrink(inputStack.getAmount());
				ItemHandlerHelper.giveItemToPlayer(player, outputStack.copy());
			}
		}
	}
	worldIn.playSound(player, pos, SoundEvents.BLOCK_ANVIL_BREAK, SoundCategory.BLOCKS, 1.0F, 1.0F);
	return true;
}
 
Example 9
Source File: ItemBreakingTracker.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void onItemBroken(PlayerEntity player, ItemStack stack)
{
    int scrappingLevel = EnchantmentHelper.getEnchantmentLevel(SurvivalistMod.SCRAPING.get(), stack);

    if (player.getClass().getName().equals("com.rwtema.extrautils2.fakeplayer.XUFakePlayer"))
        return;

    boolean fortune = rnd.nextDouble() > 0.9 / (1 + scrappingLevel);

    ItemStack ret = null;

    for (Triple<ItemStack, ItemStack, ItemStack> scraping : scrapingRegistry)
    {
        ItemStack source = scraping.getLeft();

        if (source.getItem() != stack.getItem())
            continue;

        ItemStack good = scraping.getMiddle();
        ItemStack bad = scraping.getRight();

        ret = fortune ? good.copy() : bad.copy();

        break;
    }

    if (ret != null)
    {
        SurvivalistMod.LOGGER.debug("Item broke (" + stack + ") and the player got " + ret + " in return!");

        SurvivalistMod.channel.sendTo(new ScrapingMessage(stack, ret), ((ServerPlayerEntity) player).connection.netManager, NetworkDirection.PLAY_TO_CLIENT);

        ItemHandlerHelper.giveItemToPlayer(player, ret);
    }
}
 
Example 10
Source File: EntityFluidCow.java    From Moo-Fluids with GNU General Public License v3.0 5 votes vote down vote up
private boolean attemptToGetFluidFromCow(final ItemStack currentItemStack,
                                         final EntityPlayer entityPlayer) {
  boolean canGetFluid = false;

  if (!currentItemStack.isEmpty() && entityFluid != null) {
    ItemStack filledItemStack = ItemHandlerHelper.copyStackWithSize(currentItemStack, 1);
    IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(filledItemStack);
    if (fluidHandlerItem != null) {
      if (fluidHandlerItem.fill(
              new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), true) == Fluid.BUCKET_VOLUME) {

        filledItemStack = fluidHandlerItem.getContainer();

        currentItemStack.shrink(1);
        if (currentItemStack.isEmpty()) {
          entityPlayer.inventory.setInventorySlotContents(
              entityPlayer.inventory.currentItem,
              filledItemStack.copy());
        } else {
          ItemHandlerHelper.giveItemToPlayer(entityPlayer, filledItemStack.copy());
        }

        canGetFluid = true;
      }
    }
  }

  return canGetFluid;
}
 
Example 11
Source File: EntityFluidCow.java    From Moo-Fluids with GNU General Public License v3.0 5 votes vote down vote up
private boolean attemptToHealCowWithFluidContainer(final ItemStack currentItemStack,
                                                   final EntityPlayer entityPlayer) {
  boolean cowHealed = false;
  if (!currentItemStack.isEmpty() && entityFluid != null) {
    IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(currentItemStack);
    if (fluidHandlerItem != null) {
      FluidStack containedFluid = fluidHandlerItem
              .drain(new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), false);
      ItemStack emptyItemStack;
      if (containedFluid != null &&
              containedFluid.getFluid().getName().equalsIgnoreCase(entityFluid.getName())) {
        fluidHandlerItem.drain(new FluidStack(entityFluid, Fluid.BUCKET_VOLUME), false);
        emptyItemStack = fluidHandlerItem.getContainer();
        currentItemStack.shrink(1);
        if (currentItemStack.isEmpty()) {
          entityPlayer.inventory.setInventorySlotContents(
                  entityPlayer.inventory.currentItem,
                  emptyItemStack.copy());
        } else {
          ItemHandlerHelper.giveItemToPlayer(entityPlayer, emptyItemStack.copy());
        }
        heal(4F);
        cowHealed = true;
      }
    }
  }
  return cowHealed;
}
 
Example 12
Source File: ChoppingBlock.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Deprecated
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult)
{
    ItemStack heldItem = player.getHeldItem(hand);

    if (worldIn.isRemote)
    {
        return (heldItem.getCount() <= 0) || ChoppingRecipe.getRecipe(worldIn, heldItem).isPresent() ?
                ActionResultType.SUCCESS : ActionResultType.PASS;
    }

    TileEntity tileEntity = worldIn.getTileEntity(pos);

    if (!(tileEntity instanceof ChoppingBlockTileEntity) || player.isSneaking())
        return ActionResultType.PASS;

    ChoppingBlockTileEntity chopper = (ChoppingBlockTileEntity) tileEntity;

    if (heldItem.getCount() <= 0)
    {
        ItemStack extracted = chopper.getSlotInventory().extractItem(0, 1, false);
        if (extracted.getCount() > 0)
        {
            ItemHandlerHelper.giveItemToPlayer(player, extracted);
            return ActionResultType.SUCCESS;
        }

        return ActionResultType.PASS;
    }

    if (ChoppingRecipe.getRecipe(worldIn, heldItem)
            .isPresent())
    {
        ItemStack remaining = chopper.getSlotInventory().insertItem(0, heldItem, false);
        if (!player.isCreative())
        {
            if (remaining.getCount() > 0)
            {
                player.setHeldItem(hand, remaining);
            }
            else
            {
                player.setHeldItem(hand, ItemStack.EMPTY);
            }
        }
        return remaining.getCount() < heldItem.getCount() ?
                ActionResultType.SUCCESS : ActionResultType.PASS;
    }

    return ActionResultType.PASS;
}