Jump to content

Recommended Posts

Posted (edited)

The durability of my custom Item goes down only for a second then goes back to where it was before I clicked. I'd really appreciate if someone can point me in the right direction to get this working.

    @Override
    public ActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
        ItemStack stack = playerEntity.getItemInHand(hand);
        if (!world.isClientSide) {
            ItemStack newStack = new ItemStack(stack.getItem());
            newStack.setDamageValue(stack.getDamageValue() + 1);
            return ActionResult.success(newStack);
        }
        else
            return super.use(world, playerEntity, hand);
    }
Edited by RCKraken
  • RCKraken changed the title to [1.16.5] Cannot change custom item durability in use()
Posted

you should just apply damage to the current stack instead of creating a new stack (at least the way you are doing it) because you will lose other settings, like enchantments

Posted

Okay, so

7 hours ago, Luis_ST said:

You never put the new ItemStack into hand of the player

doesn't 

return ActionResult.success(stack);

give the player's hand the new itemstack?

Also,

5 hours ago, lupicus said:

you should just apply damage to the current stack instead of creating a new stack (at least the way you are doing it) because you will lose other settings, like enchantments

This is my new code:

    @Override
    public ActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
        if (!world.isClientSide) {
            ItemStack stack = playerEntity.getItemInHand(hand);
            stack.setDamageValue(stack.getDamageValue() + 1);
            return ActionResult.success(stack);
        }
        else
            return super.use(world, playerEntity, hand);
    }

This still does not work. I can see that ActionResult.success(stack) does get run, because of the in game animation, but the durability doesn't change.

Posted
20 minutes ago, RCKraken said:

This still does not work. I can see that ActionResult.success(stack) does get run, because of the in game animation, but the durability doesn't change.

playerEntity.getItemInHand(hand).setDamageValue(playerEntity.getItemInHand(hand).getDamageValue() + 1);

this is the best solution for your problem

Posted
5 hours ago, RCKraken said:

doesn't 


return ActionResult.success(stack);

give the player's hand the new itemstack?

No. Compare and contrast:

14 hours ago, RCKraken said:

ItemStack newStack = new ItemStack(stack.getItem()); //Creates a new stack

5 hours ago, RCKraken said:

ItemStack stack = playerEntity.getItemInHand(hand); //Does not create a new stack

 

 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)
5 hours ago, Luis_ST said:


playerEntity.getItemInHand(hand).setDamageValue(playerEntity.getItemInHand(hand).getDamageValue() + 1);

this is the best solution for your problem

the only way in which this code differs from his, which is storing the ItemInHand in a local variable, is that this is less readable. as Lupicus said, his code works, he's probably just in creative mode.

creative mode cancels any attempts at damaging items when using them

Edited by kiou.23
Posted

So just for clarification, in case anyone has the same problem, if you're trying to damage an item on right click in creative mode, it will go down to whatever you set it to and then bounce right back, but has completely normal behavior in survival. I am beyond embarrassed that I spent 2 days on this lol. Thanks everyone!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.