Java Code Examples for net.minecraft.item.ItemStack#attemptDamageItem()

The following examples show how to use net.minecraft.item.ItemStack#attemptDamageItem() . 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: GTUtility.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Applies specific amount of damage to item, either to durable items (which implement IDamagableItem)
 * or to electric items, which have capability IElectricItem
 * Damage amount is equal to EU amount used for electric items
 *
 * @return if damage was applied successfully
 */
//TODO get rid of that
public static boolean doDamageItem(ItemStack itemStack, int vanillaDamage, boolean simulate) {
    Item item = itemStack.getItem();
    if (item instanceof IToolItem) {
        //if item implements IDamagableItem, it manages it's own durability itself
        IToolItem damagableItem = (IToolItem) item;
        return damagableItem.damageItem(itemStack, vanillaDamage, simulate);

    } else if (itemStack.hasCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null)) {
        //if we're using electric item, use default energy multiplier for textures
        IElectricItem capability = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
        int energyNeeded = vanillaDamage * ConfigHolder.energyUsageMultiplier;
        //noinspection ConstantConditions
        return capability.discharge(energyNeeded, Integer.MAX_VALUE, true, false, simulate) == energyNeeded;

    } else if (itemStack.isItemStackDamageable()) {
        if (!simulate && itemStack.attemptDamageItem(vanillaDamage, new Random(), null)) {
            //if we can't accept more damage, just shrink stack and mark it as broken
            //actually we would play broken animation here, but we don't have an entity who holds item
            itemStack.shrink(1);
        }
        return true;
    }
    return false;
}
 
Example 2
Source File: CropHandler.java    From BetterChests with GNU Lesser General Public License v3.0 6 votes vote down vote up
static boolean makeFarmland(World world, BlockPos pos, IBetterChest chest, boolean simulate) {
	BlockPos below = pos.down();
	IBlockState blockBelow = world.getBlockState(below);
	boolean farmland = false;
	if (blockBelow.getBlock() == Blocks.DIRT || blockBelow.getBlock() == Blocks.GRASS) {
		int hoe = InvUtil.findInInvInternal(chest, null, test -> test.getItem() instanceof ItemHoe);
		if (hoe != -1) {
			farmland = true;
			if (!simulate) {
				ItemStack tool = chest.getStackInSlot(hoe);
				tool.attemptDamageItem(1, world.rand, null);
				if (tool.getItemDamage() > tool.getMaxDamage()) {
					tool.setItemDamage(0);
					tool.setCount(tool.getCount() - 1);
				}

				world.setBlockState(below, Blocks.FARMLAND.getDefaultState());
			}
		}
	}

	return farmland;
}
 
Example 3
Source File: ItemFukumame.java    From TofuCraftReload with MIT License 5 votes vote down vote up
@Override
public ItemStack dispenseStack(IBlockSource source, ItemStack stack) {
    if (stack.getItemDamage() >= stack.getMaxDamage()) return stack;

    EnumFacing enumfacing = source.getBlockState().getValue(BlockDispenser.FACING); // getFacing
    World world = source.getWorld();
    double d0 = source.getX() + (double) ((float) enumfacing.getFrontOffsetX() * 1.125F);
    double d1 = source.getY() + (double) ((float) enumfacing.getFrontOffsetY() * 1.125F);
    double d2 = source.getZ() + (double) ((float) enumfacing.getFrontOffsetZ() * 1.125F);

    for (int i = 0; i < 8; i++) {
        EntityFukumame fukumame = new EntityFukumame(world, d0, d1, d2);
        fukumame.shoot(enumfacing.getFrontOffsetX(), (enumfacing.getFrontOffsetY()), enumfacing.getFrontOffsetZ(), this.getProjectileVelocity(), this.getProjectileInaccuracy());

        applyEffect(fukumame, stack);

        if (!world.isRemote) {
            world.spawnEntity(fukumame);
        }
    }

    if (stack.isItemStackDamageable()) {
        stack.getItem();
        stack.attemptDamageItem(1, Item.itemRand, null);
    }
    source.getWorld().playEvent(1000, source.getBlockPos(), 0);
    return stack;
}
 
Example 4
Source File: GTTileAutocrafter.java    From GT-Classic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void tryToDamageItems(ItemStack stack) {
	if (stack.isItemStackDamageable()) {
		if (this.isRendering()) {
			stack.attemptDamageItem(1, this.world.rand, null);
		}
	}
}
 
Example 5
Source File: GTItemDuctTape.java    From GT-Classic with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ItemStack getContainerItem(ItemStack itemStack) {
	ItemStack copy = itemStack.copy();
	return copy.attemptDamageItem(1, itemRand, null) ? ItemStack.EMPTY : copy;
}