Jump to content

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


RCKraken

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.
Note: Your post will require moderator approval before it will be visible.

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • JVM info: Oracle Corporation - 1.8.0_45 - 25.45-b02 java.net.preferIPv4Stack=true Found java version 1.8.0_45 Added Lets Encrypt root certificates as additional trust Extracting json Considering minecraft client jar javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target     at sun.security.ssl.Alerts.getSSLException(Unknown Source)     at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)     at sun.security.ssl.Handshaker.fatalSE(Unknown Source)     at sun.security.ssl.Handshaker.fatalSE(Unknown Source)     at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)     at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)     at sun.security.ssl.Handshaker.processLoop(Unknown Source)     at sun.security.ssl.Handshaker.process_record(Unknown Source)     at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)     at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)     at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)     at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)     at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)     at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)     at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)     at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)     at java.net.HttpURLConnection.getResponseCode(Unknown Source)     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)     at net.minecraftforge.installer.DownloadUtils.getConnection(DownloadUtils.java:243)     at net.minecraftforge.installer.DownloadUtils.downloadManifest(DownloadUtils.java:307)     at net.minecraftforge.installer.json.Util.getVanillaVersion(Util.java:73)     at net.minecraftforge.installer.actions.ClientInstall.run(ClientInstall.java:89)     at net.minecraftforge.installer.InstallerPanel.run(InstallerPanel.java:423)     at net.minecraftforge.installer.SimpleInstaller.launchGui(SimpleInstaller.java:175)     at net.minecraftforge.installer.SimpleInstaller.main(SimpleInstaller.java:147) Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target     at sun.security.validator.PKIXValidator.doBuild(Unknown Source)     at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)     at sun.security.validator.Validator.validate(Unknown Source)     at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)     at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)     at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)     ... 21 more Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target     at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)     at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)     at java.security.cert.CertPathBuilder.build(Unknown Source)     ... 27 more Failed to download version manifest, can not find client jar URL.   me sale esto en el docuento que crea el archivo cuando me da error
    • Hola, buenas, escribo porque me sale este error al descargar forge-1.16.5-36.2.34-installer, el cliente, como puedo solucionarlo ?
    • Well, as the configurations imply regardless of version, it simply removes an entity or block entity when it crashes while ticking. The only time this really occurs is when unexpected behavior occurs or the mod itself is programmed badly, which I've rarely seen in small mods. So, unless you're doing something ridiculous that is outside the bounds of survival gameplay, I don't expect you ever to run into this issue. Additionally, if the issue occurs, you'll be able to see the action within the output log, so you'll know that it happened.   As I didn't understand the question originally after posing it to the discord, one of the members mentioned that the 'optimization modpack' might be using this to hide errors caused by the performance mods. Additionally, you shouldn't normally expect errors to occur, so it makes no sense to leave the option on until you need to recover the world after the crash already happens.
    • Well, the method in question for the logic would be via LocateCommand#locateStructure which delegates to ChunkGenerator#findNearestMapStructure. You would of course need the ServerLevel as structures can only exist in the world. So, what context are you executing this in such that you don't have the level?
    • I would look at PlayerRenderer#renderHand to see how a hand is rendered normally. Then it's just creating the java model and replacing it via RenderArmEvent and canceling it. If you also want to replace the rendering of whatever is in the hand as well, use RenderHandEvent instead.
  • Topics

×
×
  • Create New...

Important Information

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