Jump to content

[1.6.4][SOLVED]Checking If The Mob Is Looking At The Player


pokeyletsplays

Recommended Posts

Yes I realize 1.6.4 is old I plan on updating soon but I have my reasons for staying on 1.6.4 for now so lets just leave it at that.

 

So what I want to do is make it so if the mob is looking at the player it applies effects to the player like slowness or weakness ect...

 

I have no idea how to go about this.

Here is my code so far anyway.

 

 

package Main.Pokey.Mine_EE_Mobs;
import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.passive.EntityBat;
import net.minecraft.entity.passive.EntityChicken;
import net.minecraft.entity.passive.EntityCow;
import net.minecraft.entity.passive.EntityPig;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;


public class EntitySuccubus extends EntityMob
{
public int Texture = 0;
public EntitySuccubus(World par1World)
{
super(par1World);
this.setSize(0.5F, 2.0F);
this.tasks.addTask(6, new EntityAIWander(this, 1.0D));
this.tasks.addTask(1, new EntityAISwimming(this));
this.tasks.addTask(4, new EntityAIAttackOnCollide(this, 1.0D, false));
this.tasks.addTask(5, new EntityAIWander(this, 10.0D));
this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
this.tasks.addTask(6, new EntityAILookIdle(this));
this.targetTasks.addTask(1, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));
this.targetTasks.addTask(2, new EntityAIHurtByTarget(this, false));
this.Texture();
}





 private void Texture() {
	 if(worldObj.provider.isHellWorld == true && worldObj.isRemote)
		{
			Texture = 1;
			for (int i = 0; i < 2; ++i)
	        {
	            this.worldObj.spawnParticle("smoke", this.posX + (this.rand.nextDouble() - 0.5D) * (double)this.width, this.posY + this.rand.nextDouble() * (double)this.height, this.posZ + (this.rand.nextDouble() - 0.5D) * (double)this.width, 0.0D, 0.0D, 0.0D);
	            this.worldObj.spawnParticle("flame", this.posX + (this.rand.nextDouble() - 0.5D) * (double)this.width, this.posY + this.rand.nextDouble() * (double)this.height, this.posZ + (this.rand.nextDouble() - 0.5D) * (double)this.width, 0.0D, 0.0D, 0.0D);
	        }
		}
		 if(worldObj.provider.isHellWorld == false)
		{
			Texture = 0;
		}

}





private void setSuccubusHealth(int par1) {
   	  this.dataWatcher.updateObject(16, new Byte((byte)par1));

	}

	public int getSuccubusHealth() {
		 return this.dataWatcher.getWatchableObjectByte(16);
	}

protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(16, new Byte((byte)0));
    }

@Override
protected boolean isAIEnabled() {
return true;
}
@Override

protected String getLivingSound()
{
return "mob.zombie.say";
}

@Override
protected String getHurtSound()
{
return "mob.zombie.hurt";
}

@Override
protected String getDeathSound()
{
return "mob.zombie.death";
}
@Override
protected void applyEntityAttributes()
{
super.applyEntityAttributes();
    this.getEntityAttribute(SharedMonsterAttributes.followRange).setAttribute(20.0D);
    this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setAttribute(0.167D);
this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setAttribute(3.0D);
}



 public void onLivingUpdate()
    {
	 super.onLivingUpdate();
		this.Texture();


		if(Minecraft.getMinecraft().objectMouseOver.entityHit != null){
		    if(Minecraft.getMinecraft().objectMouseOver.entityHit instanceof EntitySuccubus){
		       this.posY = 20;
		    }
		}

		if(this.entityToAttack == this.findPlayerToAttack())
		{
			this.setSprinting(true);
		}

		if(worldObj.provider.isHellWorld == true)
		{
			this.isImmuneToFire = true;
			this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setAttribute(6.0D);
			 this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setAttribute(0.382D);
			 this.setSprinting(true);

		}
		 if(worldObj.provider.isHellWorld == false)
		{
			 this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setAttribute(3.0D);
			 this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setAttribute(0.267D);
		}


		this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setAttribute(this.getSuccubusHealth() + 5);	    
   }


 public boolean hitByEntity(Entity entity)
    {
	 if (entity instanceof EntityPlayer) {
		if(((EntityPlayer)entity).getCurrentEquippedItem() != null )
	      {
	         
			  ItemStack hand = ((EntityPlayer)entity).getCurrentEquippedItem();
			  
		       /*  if(hand.getItem().itemID == Item.swordWood.itemID)
		         {
	        	 return false;
	         }*/
		}

	 else
	 {
        return false;
    }
    }
	return false;
	 }

 public boolean attackEntityAsMob(Entity entity) {
	 super.attackEntityAsMob(entity);
	    if (entity instanceof EntityPlayer) {
	    	
	    	this.setSuccubusHealth(this.getSuccubusHealth() + 3);
	    	this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setAttribute(this.getMaxHealth() + 3);
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.moveSlowdown.id,600, 3));
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.confusion.id,600, 5));
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.hunger.id,600, 3));
	    	this.setHealth(this.getHealth() + this.getSuccubusHealth());
	    	
	    	}
		return true;
	   
	}

@Override
protected boolean isValidLightLevel()
{
return true;
}
/**
* (abstract) Protected helper method to write subclass entity data to NBT.
*/
public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
{
    super.writeEntityToNBT(par1NBTTagCompound);
    par1NBTTagCompound.setInteger("Health", this.getSuccubusHealth());

   // par1NBTTagCompound.setInteger("TP", this.Teleport);
}

/**
* (abstract) Protected helper method to read subclass entity data from NBT.
*/
public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
{
    super.readEntityFromNBT(par1NBTTagCompound);
    this.setSuccubusHealth(par1NBTTagCompound.getInteger("Health"));

}


}

 

Its best to only ask questions when you did most of the work yourself.

Link to comment
Share on other sites

Perform a ray trace from the Mob's forward looking vector, and check if the hit entity is a Living Entity

 

MovingObjectPosition movingObjectPosition = par5EntityPlayer.rayTrace(par1, par2);
Entity entity = movingObjectPosition.entityHit;

if(entity != null)
{
    // Implement your checks and logic here
}

I require Java, both the coffee and the code :)

Link to comment
Share on other sites

Perform a ray trace from the Mob's forward looking vector, and check if the hit entity is a Living Entity

 

MovingObjectPosition movingObjectPosition = par5EntityPlayer.rayTrace(par1, par2);
Entity entity = movingObjectPosition.entityHit;

if(entity != null)
{
    // Implement your checks and logic here
}

so would i put this code in my onupdate method? or what?

also what would i use for par1 and par2?

Its best to only ask questions when you did most of the work yourself.

Link to comment
Share on other sites

so would i put this code in my onupdate method? or what?

also what would i use for par1 and par2?

 

float distance = 200;
rayTrace(distance, 1.0F)

 

Quote From: http://jabelarminecraft.blogspot.com/p/minecraft-forge-172-finding-block.html

The 200 defines how far the rayTrace will go.  200 seems to allow it to go basically to the edge of loaded chunks, but you can change the number if it makes sense in your use case. The 1.0F defines the "partial ticks" that I believe is used to filter out execution of the method for those cases where a lot of ray tracing is done and could cause lag.

 

This is from 1.7.2 but the ray tracer shouldnt have changed, so it should work with 1.6.4

I require Java, both the coffee and the code :)

Link to comment
Share on other sites

so would i put this code in my onupdate method? or what?

also what would i use for par1 and par2?

 

float distance = 200;
rayTrace(distance, 1.0F)

 

Quote From: http://jabelarminecraft.blogspot.com/p/minecraft-forge-172-finding-block.html

The 200 defines how far the rayTrace will go.  200 seems to allow it to go basically to the edge of loaded chunks, but you can change the number if it makes sense in your use case. The 1.0F defines the "partial ticks" that I believe is used to filter out execution of the method for those cases where a lot of ray tracing is done and could cause lag.

 

This is from 1.7.2 but the ray tracer shouldnt have changed, so it should work with 1.6.4

 

i put this in my onupdate method

float distance = 200;

		MovingObjectPosition movingObjectPosition = EntityPlayer.rayTrace(200, 1.0F);
		Entity entity = movingObjectPosition.entityHit;

		if(entity != null)
		{
		    // Implement your checks and logic here
		}

but it gives me this error Cannot make a static reference to the non-static method rayTrace(double, float) from the type EntityLivingBase

Its best to only ask questions when you did most of the work yourself.

Link to comment
Share on other sites

does it need to be replaced with what i want to be looked at or who i want looking at it?

If you want to detect when a mob is looking at a player, in the update code type

 

// "this" is a reference to the entity, it is a basic Java keyword that represents the instance
MovingObjectPosition mop = this.rayTrace(200, 1.0F)
Entity entity = mop.entityHit;

I require Java, both the coffee and the code :)

Link to comment
Share on other sites

does it need to be replaced with what i want to be looked at or who i want looking at it?

If you want to detect when a mob is looking at a player.

my update code sofar.

// This is a reference to the entity, it is a basic Java keyword that represents the instance
MovingObjectPosition mop = this.rayTrace(200, 1.0F)
Entity entity = mop.entityHit;

 

the game crashes when the entity hits the player.

float distance = 200;

		MovingObjectPosition mop = this.rayTrace(200, 1.0F);
				Entity entity = mop.entityHit;

		if(entity != null)
		{
			if(entity instanceof EntityPlayer){
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.moveSlowdown.id,600, 3));
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.confusion.id,600, 5));
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.hunger.id,600, 3));
		}
		}

Its best to only ask questions when you did most of the work yourself.

Link to comment
Share on other sites

the game crashes when the entity hits the player.

float distance = 200;

		MovingObjectPosition mop = this.rayTrace(200, 1.0F);
				Entity entity = mop.entityHit;

		if(entity != null)
		{
			if(entity instanceof EntityPlayer){
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.moveSlowdown.id,600, 3));
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.confusion.id,600, 5));
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.hunger.id,600, 3));
		}
		}

 

On which line does it crash?

I require Java, both the coffee and the code :)

Link to comment
Share on other sites

the game crashes when the entity hits the player.

float distance = 200;

		MovingObjectPosition mop = this.rayTrace(200, 1.0F);
				Entity entity = mop.entityHit;

		if(entity != null)
		{
			if(entity instanceof EntityPlayer){
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.moveSlowdown.id,600, 3));
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.confusion.id,600, 5));
	    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.hunger.id,600, 3));
		}
		}

 

On which line does it crash?

Entity entity = mop.entityHit;

Its best to only ask questions when you did most of the work yourself.

Link to comment
Share on other sites

That's because the raytrace is returning null, check if null first

 

now it doesn't crash but it doesn't work.

float distance = 200;

		MovingObjectPosition mop = this.rayTrace(200, 1.0F);

		if(mop != null)
		{
			Entity entity = mop.entityHit;
			if(entity != null)
			{
				if(entity instanceof EntityPlayer){
		    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.moveSlowdown.id,600, 3));
		    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.confusion.id,600, 5));
		    	((EntityPlayer) entity).addPotionEffect(new PotionEffect(Potion.hunger.id,600, 3));
			}
			}
		}

Its best to only ask questions when you did most of the work yourself.

Link to comment
Share on other sites

I was just looking through some of the vanilla code and the base EntityMob class has this method

 

protected Entity findPlayerToAttack() 
{
        EntityPlayer var1 = this.worldObj.getClosestVulnerablePlayerToEntity(this, 16.0D);
        return var1 != null && this.canEntityBeSeen(var1)?var1:null;
}

 

You can use that, execute it and the nearest player will be the entity.

I require Java, both the coffee and the code :)

Link to comment
Share on other sites

I was just looking through some of the vanilla code and the base EntityMob class has this method

 

protected Entity findPlayerToAttack() 
{
        EntityPlayer var1 = this.worldObj.getClosestVulnerablePlayerToEntity(this, 16.0D);
        return var1 != null && this.canEntityBeSeen(var1)?var1:null;
}

 

You can use that, execute it and the nearest player will be the entity.

Awesome Thanks :D

Its best to only ask questions when you did most of the work yourself.

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.