Jump to content

How to make mob apply potion effect. [Code not working]


HappleAcks

Recommended Posts

I currently have the following code, but my mob doesn't apply potion effects when it hits you.

 

@Override
public boolean attackEntityAsMob(Entity par1)
{
	if (super.attackEntityAsMob(par1))
	{
		if (par1 instanceof EntityLiving)
		{
			((EntityLiving)par1).addPotionEffect(new PotionEffect(Potion.blindness.id, 12 * 20, 0));
		}
		return true;
	} else
		return false;
}

Link to comment
Share on other sites

Try this.  I might have some of the syntax wrong as I'm doing this from memory.

 

 

 

 

@Override

public boolean attackEntityAsMob(Entity par1) {

 

if (super.attackEntityAsMob(par1)) {

 

if (par1 instanceof EntityLiving) {

 

EntityPlayer player = (EntityPlayer) par1;

 

if (player != null) {

 

player.addPotionEffect(new PotionEffect(Potion.blindness.id, 12 * 20, 0));

 

return true;

 

}

 

}

 

} else {

 

return false;

 

}

 

 

 

  • Like 1

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

If that entity attacks a non-player we will have issues.

@Override
   public boolean attackEntityAsMob(Entity par1) {
   
      if (super.attackEntityAsMob(par1)) {
      
         if (par1 instanceof EntityPlayer ) {
         
            EntityPlayer player = (EntityPlayer) par1;
            
            if (player != null) {
         
               player.addPotionEffect(new PotionEffect(Potion.blindness.id, 12 * 20, 0));
               
               return true;
            
            }
            
         }
         
      } else {
      
         return false;
         
   }

 

ALSO EntityPlayer does NOT extend EntityLiving.

Link to comment
Share on other sites

It will not have an issue if it attacks a non Player other than it won't apply the potion, but that seemed to be the goal from original post.

 

I have used this setup many times with zero issues.

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

It will not have an issue if it attacks a non Player other than it won't apply the potion, but that seemed to be the goal from original post.

 

I have used this setup many times with zero issues.

This is your code:

      if (super.attackEntityAsMob(par1)) {
      
         if (par1 instanceof EntityLiving) {
         
            EntityPlayer player = (EntityPlayer) par1;
            
            if (player != null) {
         
               player.addPotionEffect(new PotionEffect(Potion.blindness.id, 12 * 20, 0));
               
               return true;
            
            }
            
         }
         
      } else {
      
         return false;
         
   }

 

If par1 instanceof EntityLiving, par1; par1 CANNOT be cast to EntityPlayer as EntityPlayer does NOT extend EntityLiving.

Link to comment
Share on other sites

Good point darty11.  I did have a typo in what I put up, forgot to change one of his variables.

 

After reading his posts, it looks like he wants it to be any living thing, so probably needs this.

 

 

 

 

@Override

  public boolean attackEntityAsMob(Entity par1) {

 

      if (super.attackEntityAsMob(par1)) {

     

        if (par1 instanceof EntityLivingBase) {

       

            EntityLivingBase e = (EntityLivingBase) par1;

           

            if (e != null) {

       

              e.addPotionEffect(new PotionEffect(Potion.blindness.id, 12 * 20, 0));

             

              return true;

           

            }

           

        }

       

      } else {

     

        return false;

       

  }

 

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

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.