Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

  • Author

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.