Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.5] Cannot change custom item durability in use()


RCKraken
 Share

Recommended Posts

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
Link to comment
Share on other sites

  • RCKraken changed the title to [1.16.5] Cannot change custom item durability in use()

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.