Jump to content

How do I give an item a effect?


Nieue

Recommended Posts

Hello, I've been trying to make an item so that when you hold it in your hand it gives you blindness.

But, when I have the code set up like this (in the class of that item):

@Override
    public void onUpdate (ItemStack stack, World world, Entity entity, int par4, boolean par5)
    {
        super.onUpdate(stack, world, entity, par4, par5);
        if (entity instanceof EntityPlayer)
        {
            EntityPlayer player = (EntityPlayer) entity;
            ItemStack equipped = player.getCurrentEquippedItem();
            if (equipped == stack)
            {
                player.addPotionEffect(new PotionEffect(Potion.blindness.id, 1, 3));
            }
        }

It only gives me this in-game:

KItTP0z.png

And the only effect it gives me is that I am unable to sprint...

 

Can someone help me?

Link to comment
Share on other sites

I'm not sure, but it might be because in this line:

player.addPotionEffect(new PotionEffect(Potion.blindness.id, 1, 3));

In the second parameter you wrote 1. The second parameter is for the duration, so you wrote it gives the player a blindness effect for one second, which probably isn't even long enough to show the actual blindness effect. Again I'm not completely sure, but hopefully this helps!

Link to comment
Share on other sites

The second parameter is in ticks I believe. Try something higher like 20 (which should correspond to ~1s).

player.addPotionEffect(new PotionEffect(Potion.blindness.id, 20, 3));//1 replaces with 20

Thanks, that did it :)

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.



×
×
  • Create New...

Important Information

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