Jump to content

Recommended Posts

Posted

I created Mob i put Ai to protect me when its tamed but when someone atackes me he doesnt do anything he runs away xD and i have no idea what im missing so pls help

 

 

package com.medamobs.entities;

 

import net.minecraft.entity.Entity;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.IEntityOwnable;

import net.minecraft.entity.SharedMonsterAttributes;

import net.minecraft.entity.ai.EntityAIHurtByTarget;

import net.minecraft.entity.ai.EntityAILookIdle;

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

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.EntityPlayerMP;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.pathfinding.PathEntity;

import net.minecraft.scoreboard.Team;

import net.minecraft.util.ChatComponentTranslation;

import net.minecraft.world.World;

 

import com.google.common.base.Strings;

import com.medamobs.AI.MedabotAIFollowOwner;

import com.medamobs.AI.MedamobsEntityAIOwnerHurtByTarget;

import com.medamobs.AI.MedamobsEntityAIOwnerHurtTarget;

import com.medamobs.AI.MedamobsEntityAITargetNonTamed;

import com.medamobs.AI.MetabotAISit;

import com.medamobs.item.MedamobsItem;

 

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

public class MedabotBase extends EntityMob implements IEntityOwnable

{

    protected MetabotAISit aiSit = new MetabotAISit(this);

 

public MedabotBase(World par1World, String par2Str)

{

super(par1World);

this.texture = par2Str;

        this.setSize(0.6F, 1.8F);

        this.getNavigator().setAvoidsWater(true);

        this.tasks.addTask(1, new EntityAISwimming(this));

        this.tasks.addTask(2, this.aiSit);

this.tasks.addTask(3, new MedabotAIFollowOwner(this, 1.0D, 10.0F, 2.0F));

        this.tasks.addTask(6, new EntityAIWander(this, 1.0D));

        this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));

        this.tasks.addTask(9, new EntityAILookIdle(this));

        this.targetTasks.addTask(1, new MedamobsEntityAIOwnerHurtByTarget(this));

        this.targetTasks.addTask(2, new MedamobsEntityAIOwnerHurtTarget(this));

        this.targetTasks.addTask(3, new EntityAIHurtByTarget(this, true));

        this.targetTasks.addTask(4, new MedamobsEntityAITargetNonTamed(this, EntitySheep.class, 200, false));

       

}

 

    public boolean isAIEnabled()

    {

        return true;

    }

 

public void applyEntityAttributes()

{

super.applyEntityAttributes();

this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(30.0D);

this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(6.0D);

this.getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(25.0D);

this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.3D);

this.getEntityAttribute(SharedMonsterAttributes.knockbackResistance).setBaseValue(0.2D);

    }

 

protected String getLivingSound()

{

return "mob.skeleton.say";

}

 

protected String getDeathSound()

{

return "mob.skeleton.death";

}

 

protected String getHurtSound()

{

return "mob.skeleton.hurt";

}

 

public boolean getCanSpawnHere()

{

return this.worldObj.difficultySetting.getDifficultyId() > 0 && this.isValidLightLevel() && super.getCanSpawnHere();

}

 

public int getAttackStrenght(Entity entity)

{

return 8;

}

 

protected int getDropItemId()

{

return Item.getIdFromItem(MedamobsItem.Meta);

}

 

    protected void entityInit()

    {

        super.entityInit();

        this.dataWatcher.addObject(16, Byte.valueOf((byte)0));

        this.dataWatcher.addObject(17, "");

    }

 

    /**

    * (abstract) Protected helper method to write subclass entity data to NBT.

    */

    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)

    {

        super.writeEntityToNBT(par1NBTTagCompound);

 

        if (this.getOwnerName() == null)

        {

            par1NBTTagCompound.setString("Owner", "");

        }

        else

        {

            par1NBTTagCompound.setString("Owner", this.getOwnerName());

        }

 

        par1NBTTagCompound.setBoolean("Sitting", this.isSitting());

    }

 

    /**

    * (abstract) Protected helper method to read subclass entity data from NBT.

    */

    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)

    {

        super.readEntityFromNBT(par1NBTTagCompound);

        String s = par1NBTTagCompound.getString("Owner");

 

        if (s.length() > 0)

        {

            this.setOwner(s);

            this.setTamed(true);

        }

 

        this.aiSit.setSitting(par1NBTTagCompound.getBoolean("Sitting"));

        this.setSitting(par1NBTTagCompound.getBoolean("Sitting"));

    }

 

    public boolean isTamed()

    {

        return (this.dataWatcher.getWatchableObjectByte(16) & 4) != 0;

    }

 

    public void setTamed(boolean par1)

    {

        byte b0 = this.dataWatcher.getWatchableObjectByte(16);

 

        if (par1)

        {

            this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 | 4)));

        }

        else

        {

            this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 & -5)));

        }

    }

 

    public boolean isSitting()

    {

        return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;

    }

 

    public void setSitting(boolean par1)

    {

        byte b0 = this.dataWatcher.getWatchableObjectByte(16);

 

        if (par1)

        {

            this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 | 1)));

        }

        else

        {

            this.dataWatcher.updateObject(16, Byte.valueOf((byte)(b0 & -2)));

        }

    }

 

    public String getOwnerName()

    {

        return this.dataWatcher.getWatchableObjectString(17);

    }

 

    public void setOwner(String par1Str)

    {

        this.dataWatcher.updateObject(17, par1Str);

    }

 

    public EntityLivingBase func_130012_q()

    {

        return this.worldObj.getPlayerEntityByName(this.getOwnerName());

    }

 

 

    public boolean func_142018_a(EntityLivingBase par1EntityLivingBase, EntityLivingBase par2EntityLivingBase)

    {

        return true;

    }

 

    public Team getTeam()

    {

        if (this.isTamed())

        {

            EntityLivingBase entitylivingbase = this.func_130012_q();

 

            if (entitylivingbase != null)

            {

                return entitylivingbase.getTeam();

            }

        }

 

        return super.getTeam();

    }

 

    public boolean isOnSameTeam(EntityLivingBase par1EntityLivingBase)

    {

        if (this.isTamed())

        {

            EntityLivingBase entitylivingbase1 = this.func_130012_q();

 

            if (par1EntityLivingBase == entitylivingbase1)

            {

                return true;

            }

 

            if (entitylivingbase1 != null)

            {

                return entitylivingbase1.isOnSameTeam(par1EntityLivingBase);

            }

        }

 

        return super.isOnSameTeam(par1EntityLivingBase);

    }

 

    public Entity getOwner()

    {

        return this.func_130012_q();

    }

   

/**

    * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.

    */

    public boolean interact(EntityPlayer player)

    {

if (!(player instanceof EntityPlayerMP)) return false;

 

        ItemStack itemstack = player.inventory.getCurrentItem();

        boolean isOwner = player.getCommandSenderName().equalsIgnoreCase(this.getOwnerName()) && !this.worldObj.isRemote;

       

        if (this.isTamed())

        {

            if (!isOwner)

            {

            if (itemstack == null)

            {

            return false;

            }

player.addChatMessage(new ChatComponentTranslation("You are not the Owner!"));

player.addChatMessage(new ChatComponentTranslation("The Owner is" + this.getOwnerName()));

            }

           

 

            if (isOwner && itemstack == null)

            {

                this.aiSit.setSitting(!this.isSitting());

                if(this.isSitting() == false){player.addChatMessage(new ChatComponentTranslation("Medabot:Off"));}

                if(this.isSitting() == true){player.addChatMessage(new ChatComponentTranslation("Medabot:On"));}

                this.isJumping = false;

                this.setPathToEntity((PathEntity)null);

                this.setTarget((Entity)null);

                this.setAttackTarget((EntityLivingBase)null);

            }

        }

       

        else

        {

        if (itemstack == null)

        {

        return false;

        }

        if (itemstack != null && itemstack.getItem() == MedamobsItem.Kabutomedal)

        {

       

        if (!player.capabilities.isCreativeMode)

        {

        --itemstack.stackSize;

        }

 

        if (itemstack.stackSize <= 0)

        {

        player.inventory.setInventorySlotContents(player.inventory.currentItem, (ItemStack)null);

        }

 

        if (!this.worldObj.isRemote)

        {

        if (this.rand.nextInt(3) == 0)

        {

        this.setTamed(true);

        this.setPathToEntity((PathEntity)null);

        this.setAttackTarget((EntityLivingBase)null);

        this.aiSit.setSitting(true);

        this.setHealth(20.0F);

        this.setOwner(player.getCommandSenderName());

        this.worldObj.setEntityState(this, (byte)7);

        player.addChatMessage(new ChatComponentTranslation("MedabotCreated"));

        }

        else

        {

        this.worldObj.setEntityState(this, (byte)6);

        }

        }

        return false;

        }

        }

        return super.interact(player);

    }

 

public final String texture;

 

    @SideOnly(Side.CLIENT)

public String getTexture()

    {

    return com.medamobs.lib.Strings.MODDID + ":textures/entity/" + texture + ".png";

}

 

@Override

public String func_152113_b() {

// TODO Auto-generated method stub

return null;

}

 

}

 

 

 

 

Posted

Here you go my ai class for OwnerHurtbyTarget and OwnerHurtTarget

 

 

 

OwnerHurtbyTarget

 

package com.medamobs.AI;

 

import java.util.Iterator;

import java.util.List;

 

import net.minecraft.entity.EntityCreature;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.ai.EntityAITarget;

import net.minecraft.util.AxisAlignedBB;

 

import com.medamobs.entities.MedabotBase;

 

public class MedamobsEntityAIOwnerHurtByTarget extends EntityAITarget

{

    MedabotBase theDefendingTameable;

    EntityLivingBase theOwnerAttacker;

    private int field_142051_e;

private int field_142052_b;

private boolean entityCallsForHelp;

 

    public MedamobsEntityAIOwnerHurtByTarget(MedabotBase par1EntityTameable)

    {

        super(par1EntityTameable, false);

        this.theDefendingTameable = par1EntityTameable;

        this.setMutexBits(1);

    }

 

    /**

    * Returns whether the EntityAIBase should begin execution.

    */

    public boolean shouldExecute()

    {

        int i = this.taskOwner.func_142015_aE();

        return i != this.field_142052_b && this.isSuitableTarget(this.taskOwner.getAITarget(), false);

    }

 

    /**

    * Execute a one shot task or start executing a continuous task

    */

    public void startExecuting()

    {

        this.taskOwner.setAttackTarget(this.taskOwner.getAITarget());

        this.field_142052_b = this.taskOwner.func_142015_aE();

 

        if (this.entityCallsForHelp)

        {

            double d0 = this.getTargetDistance();

            List list = this.taskOwner.worldObj.getEntitiesWithinAABB(this.taskOwner.getClass(), AxisAlignedBB.getBoundingBox(this.taskOwner.posX, this.taskOwner.posY, this.taskOwner.posZ, this.taskOwner.posX + 1.0D, this.taskOwner.posY + 1.0D, this.taskOwner.posZ + 1.0D).expand(d0, 10.0D, d0));

            Iterator iterator = list.iterator();

 

            while (iterator.hasNext())

            {

                EntityCreature entitycreature = (EntityCreature)iterator.next();

 

                if (this.taskOwner != entitycreature && entitycreature.getAttackTarget() == null && !entitycreature.isOnSameTeam(this.taskOwner.getAITarget()))

                {

                    entitycreature.setAttackTarget(this.taskOwner.getAITarget());

                }

            }

        }

 

        super.startExecuting();

    }

}

 

 

 

OwnerHurtTarget

 

package com.medamobs.AI;

 

import java.util.Iterator;

import java.util.List;

 

import net.minecraft.entity.EntityCreature;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.ai.EntityAITarget;

import net.minecraft.util.AxisAlignedBB;

 

import com.medamobs.entities.MedabotBase;

 

public class MedamobsEntityAIOwnerHurtTarget extends EntityAITarget

{

MedabotBase theEntityTameable;

    EntityLivingBase theTarget;

    private int field_142050_e;

private boolean entityCallsForHelp;

private int field_142052_b;

 

    public MedamobsEntityAIOwnerHurtTarget(MedabotBase par1EntityTameable)

    {

        super(par1EntityTameable, false);

        this.theEntityTameable = par1EntityTameable;

        this.setMutexBits(1);

    }

 

    /**

    * Returns whether the EntityAIBase should begin execution.

    */

    public boolean shouldExecute()

    {

        if (!this.theEntityTameable.isTamed())

        {

            return false;

        }

        else

        {

            EntityLivingBase entitylivingbase = this.theEntityTameable.func_130012_q();

 

            if (entitylivingbase == null)

            {

                return false;

            }

            else

            {

                this.theTarget = entitylivingbase.getLastAttacker();

                int i = entitylivingbase.getLastAttackerTime();

                return i != this.field_142050_e && this.isSuitableTarget(this.theTarget, false) && this.theEntityTameable.func_142018_a(this.theTarget, entitylivingbase);

            }

        }

    }

 

    /**

    * Execute a one shot task or start executing a continuous task

    */

    public void startExecuting()

    {

        this.taskOwner.setAttackTarget(this.taskOwner.getAITarget());

        this.field_142052_b = this.taskOwner.func_142015_aE();

 

        if (this.entityCallsForHelp)

        {

            double d0 = this.getTargetDistance();

            List list = this.taskOwner.worldObj.getEntitiesWithinAABB(this.taskOwner.getClass(), AxisAlignedBB.getBoundingBox(this.taskOwner.posX, this.taskOwner.posY, this.taskOwner.posZ, this.taskOwner.posX + 1.0D, this.taskOwner.posY + 1.0D, this.taskOwner.posZ + 1.0D).expand(d0, 10.0D, d0));

            Iterator iterator = list.iterator();

 

            while (iterator.hasNext())

            {

                EntityCreature entitycreature = (EntityCreature)iterator.next();

 

                if (this.taskOwner != entitycreature && entitycreature.getAttackTarget() == null && !entitycreature.isOnSameTeam(this.taskOwner.getAITarget()))

                {

                    entitycreature.setAttackTarget(this.taskOwner.getAITarget());

                }

            }

        }

 

        super.startExecuting();

    }

}

 

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.