Jump to content

[1.10.2] Error on use for custom healing item


T-10a

Recommended Posts

Hello,

I've made an item that is drunken to heal the player, and when it is drunk it reduces the uses by one. However, when I drink it in survival, a weird bug occurs:

A red -1 appears on the icon, which briefly changes to a -3. When I hit something with it, it'll be instantly destroyed.

What have I done wrong?

[spoiler=Images]

BHkaHd9.pngSbOvA4v.png

 

[spoiler=Item Code]

package com.t10a.crystalflask.items;

import com.t10a.crystalflask.CrystalFlask;
import com.t10a.crystalflask.Reference;
import com.t10a.crystalflask.init.ModItems;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.List;

public class ItemCrystalFlask extends Item
{
    public int uses;
    public ItemCrystalFlask()
    {
        setUnlocalizedName(Reference.MOD_ID + "." + Reference.ItemBase.ESTUS.getUnlocalizedName());
        setRegistryName(Reference.ItemBase.ESTUS.getRegistryName());
        setCreativeTab(CrystalFlask.ESTUSTAB);
        this.setMaxStackSize(1);
        this.uses = 5;
        this.setMaxDamage(uses);
    }

    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, List<String> list, boolean par4)
    {
        list.add("A crystallised flask, containing a magical flame. Drink to restore HP.");
        list.add("(totally not an estus flask)");
    }

    public int getMaxItemUseDuration(ItemStack stack)
    {
        return 32;
    }

    public EnumAction getItemUseAction(ItemStack stack)
    {
        return EnumAction.DRINK;
    }

    public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand)
    {
        playerIn.setActiveHand(hand);
        return new ActionResult(EnumActionResult.SUCCESS, itemStackIn);
    }

    @Nullable
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityLivingBase entityLiving)
    {
        if (entityLiving instanceof EntityPlayer && !((EntityPlayer)entityLiving).capabilities.isCreativeMode)
        {
            --stack.stackSize;
        }

        if (!worldIn.isRemote)
        {
            entityLiving.addPotionEffect(new PotionEffect(MobEffects.INSTANT_HEALTH));
            stack.damageItem(1, entityLiving);
        }

        if (entityLiving instanceof EntityPlayer && !((EntityPlayer)entityLiving).capabilities.isCreativeMode)
        {
            --stack.stackSize;
        }

        return super.onItemUseFinish(stack, worldIn, entityLiving);
    }
}

 

If I'm asking a whole bunch of questions, please don't get angry. I'm trying to learn.

Link to comment
Share on other sites

Whoops, didn't see that. However, after fixing it and adding a tidbit of code that tells the game if it's empty or not, it'll just ignore it and allow the usage when (nearly) empty anyway.

 

package com.t10a.crystalflask.items;

import com.t10a.crystalflask.CrystalFlask;
import com.t10a.crystalflask.Reference;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.List;

public class ItemCrystalFlask extends Item
{
    public int uses;
    public ItemCrystalFlask()
    {
        setUnlocalizedName(Reference.MOD_ID + "." + Reference.ItemBase.ESTUS.getUnlocalizedName());
        setRegistryName(Reference.ItemBase.ESTUS.getRegistryName());
        setCreativeTab(CrystalFlask.ESTUSTAB);
        this.setMaxStackSize(1);
        this.uses = 5;
        this.setMaxDamage(uses);
    }

    public boolean getIsEmpty()
    {
        if (this.getMaxDamage() == 1)
            return true;
        else
            return false;
    }

    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, List<String> list, boolean par4)
    {
        list.add("A crystallised flask, containing a magical flame. Drink to restore HP.");
        list.add("(totally not an estus flask)");
    }

    public int getMaxItemUseDuration(ItemStack stack)
    {
        return 32;
    }

    public EnumAction getItemUseAction(ItemStack stack)
    {
        if (getIsEmpty())
            return EnumAction.NONE;
        else return EnumAction.DRINK;
    }

    public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn, EnumHand hand)
    {
        playerIn.setActiveHand(hand);
        return new ActionResult(EnumActionResult.SUCCESS, itemStackIn);
    }

    @Nullable
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityLivingBase entityLiving)
    {
        if (!worldIn.isRemote)
        {
            entityLiving.addPotionEffect(new PotionEffect(MobEffects.INSTANT_HEALTH));

        }

        if (entityLiving instanceof EntityPlayer && !((EntityPlayer)entityLiving).capabilities.isCreativeMode)
        {
            stack.damageItem(1, entityLiving);
        }

        return super.onItemUseFinish(stack, worldIn, entityLiving);
    }
}

 

If I'm asking a whole bunch of questions, please don't get angry. I'm trying to learn.

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.