Jump to content

[1.19] trying to make an item give 2 hearts of absorption when held, what am i doing wrong?


Recommended Posts

Posted

hey guys, i want to make my item give the player 2 hearts of absorption when theyre holding it (and when they arent take those hearts away). thing is, when i switch to it and off of it rapidly it glitches and gives either too much or none. can anyone help me here? below is my code

 

@Override
    public void inventoryTick(ItemStack p_41404_, Level p_41405_, Entity p_41406_, int p_41407_, boolean p_41408_) {
        super.inventoryTick(p_41404_, p_41405_, p_41406_, p_41407_, p_41408_);

        if (p_41406_ instanceof Player player){
            if (player.getMainHandItem() == p_41404_){
                if (!flag) {
                    player.setAbsorptionAmount(player.getAbsorptionAmount() + 4);
                    flag = true;
                }
            } else {
                if (flag) {
                    player.setAbsorptionAmount(player.getAbsorptionAmount() - 4);
                    flag = false;
                }
            }
        }
    }

 

Posted
2 hours ago, yeatfan119 said:

hey guys, i want to make my item give the player 2 hearts of absorption when theyre holding it (and when they arent take those hearts away). thing is, when i switch to it and off of it rapidly it glitches and gives either too much or none. can anyone help me here? below is my code

 

@Override
    public void inventoryTick(ItemStack p_41404_, Level p_41405_, Entity p_41406_, int p_41407_, boolean p_41408_) {
        super.inventoryTick(p_41404_, p_41405_, p_41406_, p_41407_, p_41408_);

        if (p_41406_ instanceof Player player){
            if (player.getMainHandItem() == p_41404_){
                if (!flag) {
                    player.setAbsorptionAmount(player.getAbsorptionAmount() + 4);
                    flag = true;
                }
            } else {
                if (flag) {
                    player.setAbsorptionAmount(player.getAbsorptionAmount() - 4);
                    flag = false;
                }
            }
        }
    }

 

First check if you are on the server side if(!level.isClientSide()). You don't need to check if the item in the player's main hand is the same as your item, you can use the last boolean variable, its name is isSelected. If isSelected is false then flag to false, if true check if(!flag) next add absorptions and set flag to true.

Posted

first of all, there is a single instance of YourItem existing on the server while the game is running, and between zero and 123456789 item stack instances - in containers, in player inventories, in machine inventories... you see where i'm going with this. you can not say "private boolean flag" - if two players have stacks of that item, your logic will get mixed up. any information needs to be held withing ItemStack. yes it can be done but it isn't needed in your case.

next, tick events fire every 1/20 of the second (some even twice per 1/20 of the second). should your code run that often? any of your code? because  players can not detect game changes that quick. to execute every second, always start this event handler with " if level.whateverTotalGameTimeIsCalledNow % 20 == 15" to do your thing once per second (for some things even that is too often).

ok, now you need to decide how the event will work. one way is to apply a two seconds of existing potion effect every two seconds and done. that's how most people do it because it's simple. and yes, when you apply potion effect you can specify to hide particles. other option is what you tries but you have to remember things on player or on ItemStack object; doable as well.

next, getMainHandItem is ok until things start working, but after that, consider curios and other slots too.

finally item comparison - i don't see where you compare the item to your item. to do that, either do itemStack.is(someItemTag) or itemStack.getItem.equals(MyItems.MyItem). or do instanceof in the previous thing if you expect subclasses.

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.