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

The following examples show how to use net.minecraft.item.ItemStack#damage() . 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: StandardWrenchItem.java    From Galacticraft-Rewoven with MIT License 6 votes vote down vote up
private void use(PlayerEntity player, BlockState state, WorldAccess iWorld, BlockPos pos, ItemStack stack) {
    Block block = state.getBlock();
    if (block instanceof Rotatable) {
        StateManager<Block, BlockState> manager = block.getStateManager();
        Collection<Property<?>> collection = manager.getProperties();
        String string_1 = Registry.BLOCK.getId(block).toString();
        if (!collection.isEmpty()) {
            CompoundTag compoundTag_1 = stack.getOrCreateSubTag("wrenchProp");
            String string_2 = compoundTag_1.getString(string_1);
            Property<?> property = manager.getProperty(string_2);
            if (property == null) {
                property = collection.iterator().next();
            }
            if (property.getName().equals("facing")) {
                BlockState blockState_2 = cycle(state, property, player.isSneaking());
                iWorld.setBlockState(pos, blockState_2, 18);
                stack.damage(2, player, (playerEntity) -> playerEntity.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
            }
        }
    }
}
 
Example 2
Source File: HallowedLogBlock.java    From the-hallow with MIT License 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
	ItemStack stack = player.getStackInHand(hand);
	if (stack.isEmpty() || !(stack.getItem() instanceof MiningToolItem)) {
		return ActionResult.PASS;
	}
	
	MiningToolItem tool = (MiningToolItem) stack.getItem();
	if (stripped != null && (tool.isEffectiveOn(state) || tool.getMiningSpeed(stack, state) > 1.0F)) {
		world.playSound(player, pos, SoundEvents.ITEM_AXE_STRIP, SoundCategory.BLOCKS, 1.0F, 1.0F);
		if (!world.isClient) {
			BlockState target = stripped.getDefaultState().with(LogBlock.AXIS, state.get(LogBlock.AXIS));
			world.setBlockState(pos, target);
			stack.damage(1, player, consumedPlayer -> consumedPlayer.sendToolBreakStatus(hand));
		}
		return ActionResult.SUCCESS;
	}
	return ActionResult.PASS;
}
 
Example 3
Source File: ColoredPumpkinBlock.java    From the-hallow with MIT License 6 votes vote down vote up
@Override
public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity playerEntity, Hand hand, BlockHitResult blockHitResult) {
	ItemStack stack = playerEntity.getStackInHand(hand);

	if (stack.getItem() == Items.SHEARS) {
		if (!world.isClient) {
			Direction side = blockHitResult.getSide();
			Direction facingTowardPlayer = side.getAxis() == Direction.Axis.Y ? playerEntity.getHorizontalFacing().getOpposite() : side;

			world.playSound(null, blockPos, SoundEvents.BLOCK_PUMPKIN_CARVE, SoundCategory.BLOCKS, 1.0F, 1.0F);
			world.setBlockState(blockPos, HallowedBlocks.CARVED_PUMPKIN_COLORS.get(this.color).getDefaultState().with(CarvedPumpkinBlock.FACING, facingTowardPlayer), 11);
			ItemEntity itemEntity = new ItemEntity(world, blockPos.getX() + 0.5D + facingTowardPlayer.getOffsetX() * 0.65D, blockPos.getY() + 0.1D, blockPos.getZ() + 0.5D + facingTowardPlayer.getOffsetZ() * 0.65D, new ItemStack(Items.PUMPKIN_SEEDS, 4));

			itemEntity.setVelocity(0.05D * (double) facingTowardPlayer.getOffsetX() + world.random.nextDouble() * 0.02D, 0.05D, 0.05D * (double) facingTowardPlayer.getOffsetZ() + world.random.nextDouble() * 0.02D);
			world.spawnEntity(itemEntity);
			stack.damage(1, playerEntity, (playerEntityVar) -> {
				playerEntityVar.sendToolBreakStatus(hand);
			});
		}

		return ActionResult.SUCCESS;
	}

	return super.onUse(blockState, world, blockPos, playerEntity, hand, blockHitResult);
}
 
Example 4
Source File: ChickenEntityMixin.java    From carpet-extra with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean interactMob(PlayerEntity playerEntity_1, Hand hand_1)
{
    ItemStack stack = playerEntity_1.getStackInHand(hand_1);
    if (CarpetExtraSettings.chickenShearing && stack.getItem() == Items.SHEARS && !this.isBaby())
    {
        if (!this.world.isClient)
        {
            this.damage(DamageSource.GENERIC, 1);
            this.dropItem(Items.FEATHER, 1);
            stack.damage(1, (LivingEntity)playerEntity_1, ((playerEntity_1x) -> {
                playerEntity_1x.sendToolBreakStatus(hand_1);
            }));
        }
    }
    return super.interactMob(playerEntity_1, hand_1);
}
 
Example 5
Source File: UnlitWallTorchBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
    if (player.getStackInHand(hand).getItem() instanceof FlintAndSteelItem) {
        world.setBlockState(pos, Blocks.WALL_TORCH.getDefaultState().with(WallTorchBlock.FACING, state.get(WallTorchBlock.FACING)));
        ItemStack stack = player.getStackInHand(hand).copy();
        stack.damage(1, player, (playerEntity -> {
        }));
        player.setStackInHand(hand, stack);
    }

    return super.onUse(state, world, pos, player, hand, hit);
}
 
Example 6
Source File: UnlitTorchBlock.java    From Galacticraft-Rewoven with MIT License 5 votes vote down vote up
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
    if (player.getStackInHand(hand).getItem() instanceof FlintAndSteelItem) {
        world.setBlockState(pos, Blocks.TORCH.getDefaultState());
        ItemStack stack = player.getStackInHand(hand).copy();
        stack.damage(1, player, (playerEntity -> {
        }));
        player.setStackInHand(hand, stack);
    }
    return super.onUse(state, world, pos, player, hand, hit);
}
 
Example 7
Source File: LivingEntityMixin.java    From the-hallow with MIT License 5 votes vote down vote up
@Inject(method = "eatFood(Lnet/minecraft/world/World;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;decrement(I)V"), cancellable = true)
public void eatFood(World world, ItemStack stack, CallbackInfoReturnable<ItemStack> info) {
	if (stack.isDamageable()) {
		stack.damage(1, (LivingEntity) (Object) this, (entity) -> entity.sendToolBreakStatus(getActiveHand()));
		info.setReturnValue(stack);
	}
}
 
Example 8
Source File: CarpetDispenserBehaviours.java    From carpet-extra with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected ItemStack dispenseSilently(BlockPointer blockPointer_1, ItemStack itemStack_1)
{
    if (!CarpetExtraSettings.dispensersTillSoil)
        return super.dispenseSilently(blockPointer_1, itemStack_1);
    
    World world = blockPointer_1.getWorld();
    Direction direction = blockPointer_1.getBlockState().get(DispenserBlock.FACING);
    BlockPos front = blockPointer_1.getBlockPos().offset(direction);
    BlockPos down = blockPointer_1.getBlockPos().down().offset(direction);
    BlockState frontState = world.getBlockState(front);
    BlockState downState = world.getBlockState(down);
    
    if (isFarmland(frontState) || isFarmland(downState))
        return itemStack_1;
    
    if (canDirectlyTurnToFarmland(frontState))
        world.setBlockState(front, Blocks.FARMLAND.getDefaultState());
    else if (canDirectlyTurnToFarmland(downState))
        world.setBlockState(down, Blocks.FARMLAND.getDefaultState());
    else if (frontState.getBlock() == Blocks.COARSE_DIRT)
        world.setBlockState(front, Blocks.DIRT.getDefaultState());
    else if (downState.getBlock() == Blocks.COARSE_DIRT)
        world.setBlockState(down, Blocks.DIRT.getDefaultState());
    
    if (itemStack_1.damage(1, world.random, null))
        itemStack_1.setCount(0);
    
    return itemStack_1;
}
 
Example 9
Source File: Shearables.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void shearEntity(ItemStack stack, IWorld world, BlockPos pos, IShearable target) {
	if (!(target instanceof Entity)) {
		throw new IllegalArgumentException("Tried to call shearEntity on something that was not an entity!");
	}

	Entity entity = (Entity) target;

	List<ItemStack> drops = target.onSheared(stack, world, pos, EnchantmentHelper.getLevel(Enchantments.FORTUNE, stack));
	Random rand = world.getRandom();

	for (ItemStack drop : drops) {
		ItemEntity item = entity.dropStack(drop, 1.0F);

		if (item == null) {
			continue;
		}

		float accelerationX = (rand.nextFloat() - rand.nextFloat()) * 0.1F;
		float accelerationY = rand.nextFloat() * 0.05F;
		float accelerationZ = (rand.nextFloat() - rand.nextFloat()) * 0.1F;

		item.setVelocity(item.getVelocity().add(accelerationX, accelerationY, accelerationZ));
	}

	if (stack.damage(1, world.getRandom(), null)) {
		stack.setCount(0);
	}
}
 
Example 10
Source File: PumpcownEntity.java    From the-hallow with MIT License 4 votes vote down vote up
@Override
public boolean interactMob(PlayerEntity player, Hand hand) {
	ItemStack stack = player.getStackInHand(hand);
	if (stack.getItem() == Items.SHEARS && this.getBreedingAge() >= 0) {
		this.world.addParticle(ParticleTypes.EXPLOSION, this.getX(), this.getY() + (double) (this.getHeight() / 2.0F), this.getZ(), 0.0D, 0.0D, 0.0D);
		if (!this.world.isClient) {
			this.remove();
			
			if (this.world.getDimension().getType() == HallowedDimensions.THE_HALLOW) {
				this.world.createExplosion(this, this.getX(), this.getY(), this.getZ(), 3.0F, Explosion.DestructionType.BREAK);
			} else {
				CowEntity cow = EntityType.COW.create(this.world);
				cow.updatePositionAndAngles(this.getX(), this.getY(), this.getZ(), this.yaw, this.pitch);
				cow.setHealth(this.getHealth());
				cow.bodyYaw = this.bodyYaw;
				if (this.hasCustomName()) {
					cow.setCustomName(this.getCustomName());
				}
				this.world.spawnEntity(cow);
			}
			
			for (int i = 0; i < 5; ++i) {
				this.world.spawnEntity(new ItemEntity(this.world, this.getX(), this.getY() + (double) this.getHeight(), this.getZ(), new ItemStack(STEM_FEATURE.getBlock())));
			}
			
			stack.damage(1, player, ((player_1) -> {
				player_1.sendToolBreakStatus(hand);
			}));
			this.playSound(SoundEvents.ENTITY_MOOSHROOM_SHEAR, 1.0F, 1.0F);
		}
		return true;
	} else if (stack.getItem() == Items.BOWL && this.getBreedingAge() >= 0 && !player.abilities.creativeMode) {
		stack.decrement(1);
		ItemStack stew = new ItemStack(HallowedItems.PUMPKIN_STEW);
		
		if (stack.isEmpty()) {
			player.setStackInHand(hand, stew);
		} else if (!player.inventory.insertStack(stew)) {
			player.dropItem(stew, false);
		}
		
		this.playSound(SoundEvents.ENTITY_MOOSHROOM_MILK, 1.0F, 1.0F);
		
		return true;
	} else {
		return super.interactMob(player, hand);
	}
}