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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Thanks, I've now installed a slightly newer version and the server is at least starting up now.
    • i have the same issue. Found 1 Create mod class dependency(ies) in createdeco-1.3.3-1.19.2.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Found 11 Create mod class dependency(ies) in createaddition-fabric+1.19.2-20230723a.jar, which are missing from the current create-1.19.2-0.5.1.i.jar Detailed walkthrough of mods which rely on missing Create mod classes: Mod: createaddition-fabric+1.19.2-20230723a.jar Missing classes of create: com/simibubi/create/compat/jei/category/sequencedAssembly/JeiSequencedAssemblySubCategory com/simibubi/create/compat/recipeViewerCommon/SequencedAssemblySubCategoryType com/simibubi/create/compat/rei/CreateREI com/simibubi/create/compat/rei/EmptyBackground com/simibubi/create/compat/rei/ItemIcon com/simibubi/create/compat/rei/category/CreateRecipeCategory com/simibubi/create/compat/rei/category/WidgetUtil com/simibubi/create/compat/rei/category/animations/AnimatedBlazeBurner com/simibubi/create/compat/rei/category/animations/AnimatedKinetics com/simibubi/create/compat/rei/category/sequencedAssembly/ReiSequencedAssemblySubCategory com/simibubi/create/compat/rei/display/CreateDisplay Mod: createdeco-1.3.3-1.19.2.jar Missing classes of create: com/simibubi/create/content/kinetics/fan/SplashingRecipe
    • The crash points to moonlight lib - try other builds or make a test without this mod and the mods requiring it
    • Do you have shaders enabled? There is an issue with the mod simpleclouds - remove this mod or disable shaders, if enabled  
    • Maybe you need to create file in assets/<modid>/items/<itemname>.json with content like this:   { "model": { "type": "minecraft:model", "model": "modname:item/itemname" } }  
  • Topics

×
×
  • Create New...

Important Information

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