Jump to content

[1.15.2][SOLVED] Making an item add hunger/food, play the animation, and not break?


Nolwest

Recommended Posts

I'm just getting back into the saddle from Minecraft modding back when I still used Skype, with much greater Java knowledge. I decided to make a little testing mod that, so far, just adds in some staff(hitting, not magic) weapon types into the game. And, because why not, I decided one of the fun ones to add would be Bread Staff, that has super low uses but can be eaten to act as a piece of bread for some durability. Of course, this means I have to override the method that would normally consume it, but doing that just made it not heal any hunger what so ever. The rest works great, it gets eaten, sound and animation plays, durability goes down by 1, and can break the weapon too. I haven't been able to find the actual code that adds hunger, but I have yet to dig super deep, and feel like this might be a problem I shouldn't just copy that code in, because I'm not sure what isn't working here. Code below:

package com.nolwest.testmod.items;

//imports not shown


//I have a util class for ItemTier, letting me just add them in like in this constructor.
//StaffItem extends Tiered Item. No part extends Food.
public class BreadStaff extends StaffItem{
    public BreadStaff() {
        super(new ItemTier(0,4,1, -1, 18, Items.BREAD),  new Properties().group(TestMod.TEST_TAB)
                .food(new Food.Builder().hunger(5).saturation(0.6F).build()));
    }

  
  
  //Code overriding the normal Item.onItemUseFinish
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving) {
        if (stack.isFood()) {
            worldIn.playSound((PlayerEntity)null, entityLiving.getPosX(), entityLiving.getPosY(), entityLiving.getPosZ(), entityLiving.getEatSound(stack), SoundCategory.NEUTRAL, 1.0F, 1.0F + (worldIn.rand.nextFloat() - worldIn.rand.nextFloat()) * 0.4F);
            for(Pair<EffectInstance, Float> pair : stack.getItem().getFood().getEffects()) {
                if (!worldIn.isRemote && pair.getLeft() != null && worldIn.rand.nextFloat() < pair.getRight()) {
                    entityLiving.addPotionEffect(new EffectInstance(pair.getLeft()));
                }
            }
            if (!(entityLiving instanceof PlayerEntity) || !((PlayerEntity)entityLiving).abilities.isCreativeMode) {
                stack.damageItem(1, entityLiving, (p_220045_0_) -> {
                    p_220045_0_.sendBreakAnimation(EquipmentSlotType.MAINHAND);
                });
            }
        }

        return stack;
    }

}

Thanks for the help guys. Hopefully one of you know what dumb thing I did! Let me know if you need to see any other classes.

 

EDIT: Got code from LivingEntity not PlayerEntity for onFoodEaten(). After fixing that, it works great.

Edited by Nolwest
fixed the issue, marking post
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
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.