Jump to content

[1.7.2] Custom Entity Won't Sit


qpwoeiruty

Recommended Posts

My custom entity will sit, but then it will still move around at its own accord, I have no idea why this is happening.

 

package com.blocklings.entity;

import java.util.Random;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIBeg;
import net.minecraft.entity.ai.EntityAIFollowOwner;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILeapAtTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIMate;
import net.minecraft.entity.ai.EntityAIOwnerHurtByTarget;
import net.minecraft.entity.ai.EntityAIOwnerHurtTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAITargetNonTamed;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.pathfinding.PathEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;

import com.blocklings.network.CreatePacketServerSide;

public class EntityBlockling extends EntityTameable {

Random random = new Random();

public int currentXP = 0;
public int currentLevel = 1;

public int requiredXP = 200;

public float moveTimer = 0;
public boolean moveTimerSwitch = true;

public EntityBlockling(World world) {

	super(world);

	setSize(0.5F, 0.5F);

	getNavigator().setAvoidsWater(true);

        tasks.addTask(1, new EntityAISwimming(this));
        tasks.addTask(2, this.aiSit);
        tasks.addTask(3, new EntityAILeapAtTarget(this, 0.4F));
        tasks.addTask(4, new EntityAIAttackOnCollide(this, 1.0D, true));
        tasks.addTask(5, new EntityAIFollowOwner(this, 1.0D, 10.0F, 2.0F));
        tasks.addTask(6, new EntityAIMate(this, 1.0D));
        tasks.addTask(7, new EntityAIWander(this, 1.0D));
        tasks.addTask(8, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
        tasks.addTask(9, new EntityAILookIdle(this));
        
        targetTasks.addTask(1, new EntityAIOwnerHurtByTarget(this));
        targetTasks.addTask(2, new EntityAIOwnerHurtTarget(this));
        targetTasks.addTask(3, new EntityAIHurtByTarget(this, true));

	setTamed(false);

}

@Override
protected void applyEntityAttributes() {

	super.applyEntityAttributes();

	getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(10.0D);
	getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.3D);
	getAttributeMap().registerAttribute(SharedMonsterAttributes.attackDamage);
	getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(2.0D);

}

@Override
public void onLivingUpdate() {

	super.onLivingUpdate();

	moveTimer();

	if(!worldObj.isRemote) {

		CreatePacketServerSide.sendS2CEntitySync(this);

	}

}

@Override
public boolean attackEntityAsMob(Entity entity) {

	addXP(random.nextInt(5));

	return entity.attackEntityFrom(DamageSource.causeMobDamage(this), (float) getEntityAttribute(SharedMonsterAttributes.attackDamage).getAttributeValue());

}

@Override
public boolean interact(EntityPlayer entityPlayer) {

	ItemStack itemstack = entityPlayer.inventory.getCurrentItem();
	Item yellowFlower = Item.getItemFromBlock(Blocks.yellow_flower);
	Item redFlower = Item.getItemFromBlock(Blocks.red_flower);
	Item tallFlower = Item.getItemFromBlock(Blocks.double_plant);

	EntityLivingBase owner = this.getOwner();

	if(itemstack != null && (itemstack.getItem() == yellowFlower || itemstack.getItem() == redFlower || itemstack.getItem() == tallFlower) && !this.isTamed() && !this.worldObj.isRemote) {

		if(random.nextInt(3) == 0) {

			setTamed(true);
			setPathToEntity((PathEntity) null);
			setAttackTarget((EntityLivingBase) null);
			setOwner(entityPlayer.getCommandSenderName());
			playTameEffect(true);
			worldObj.setEntityState(this, (byte) 7);

		} else {

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

		}

	}

	if(entityPlayer == owner && entityPlayer.isSneaking() && !worldObj.isRemote) {

		if(getCustomNameTag().length() > 0) {

			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + " - " + getCustomNameTag() + " -"));
			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "Level: " + EnumChatFormatting.WHITE + "" + EnumChatFormatting.ITALIC + "" + currentLevel));
			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "XP: " + EnumChatFormatting.WHITE + "" + EnumChatFormatting.ITALIC + "" + currentXP + "/" + requiredXP));
			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "Max Health: " + EnumChatFormatting.WHITE + "" + EnumChatFormatting.ITALIC + "" + (int) getHealth() + "/" + (int) getEntityAttribute(SharedMonsterAttributes.maxHealth).getAttributeValue()));

		} else {

			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + " - " + "Your Blockling" + " -"));
			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "" + "Level: " + EnumChatFormatting.WHITE + "" + EnumChatFormatting.ITALIC + "" + currentLevel));
			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "" + "XP: " + EnumChatFormatting.WHITE + "" + EnumChatFormatting.ITALIC + "" + currentXP + "/" + requiredXP));
			entityPlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "Max Health: " + EnumChatFormatting.WHITE + "" + EnumChatFormatting.ITALIC + "" + (int) getHealth() + "/" + (int) getEntityAttribute(SharedMonsterAttributes.maxHealth).getAttributeValue()));

		}

	}

	if (entityPlayer == owner && !entityPlayer.isSneaking() && !this.worldObj.isRemote) {

		setSitting(!isSitting());

	}

	return super.interact(entityPlayer);

}

@Override
protected float applyArmorCalculations(DamageSource p_70655_1_, float p_70655_2_) {

	return 1.0F;

}

@Override
public boolean isAIEnabled() {

	return true;

}

@Override
public void writeEntityToNBT(NBTTagCompound compound) {

	super.writeEntityToNBT(compound);

	compound.setInteger("currentXP", currentXP);
	compound.setInteger("currentLevel", currentLevel);

	compound.setInteger("requiredXP", requiredXP);

}

@Override
public void readEntityFromNBT(NBTTagCompound compound) {

	super.readEntityFromNBT(compound);

	currentXP = compound.getInteger("currentXP");
	currentLevel = compound.getInteger("currentLevel");

	requiredXP = compound.getInteger("requiredXP");

}


@Override
public EntityAgeable createChild(EntityAgeable entityAgeable) {

	return null;

}


public void addXP(int xp) {

	currentXP = currentXP + xp;

	setBlocklingLevel();

}

public void calculateRequiredXP() {

	requiredXP = (50 * (currentLevel * currentLevel) + (150 * currentLevel));

}

public void setBlocklingLevel() {

	if (currentXP > requiredXP) {

		currentLevel = currentLevel + 1;

		calculateRequiredXP();

		currentXP = 0;

		if(!worldObj.isRemote) {

			CreatePacketServerSide.sendS2CEntitySync(this);

		}

	}

}

public void moveTimer() {

	if(moveTimerSwitch) {

		moveTimer += 2.5F;

	}

	if(!moveTimerSwitch) {

		moveTimer -= 2.5F;

	}

	if(moveTimer >= 10.0F) {

		moveTimerSwitch = false;

	}

	if(moveTimer <= -10.0) {

		moveTimerSwitch = true;

	}

}

public int getLevel() {

	return currentLevel;

}

public void setLevel(int currentLevel) {

	this.currentLevel = currentLevel;

}

public float getMoveTimer() {

	return moveTimer;

}

public void setMoveTimer(float moveTimer) {

	this.moveTimer = moveTimer;

}

}

Link to comment
Share on other sites

It will still find it's own path, you need to set the path to null

 

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

this.isJumping = false;

this.setPathToEntity((PathEntity)null);

this.setTarget((Entity)null);

this.setAttackTarget((EntityLivingBase)null);

 

Also, use a simple methode to check if it is called, that could be a problem too.

System.out.println("test message");

 

if the methode is called, your console will say "test message"

if it is not called, well you need to change some things.

Coding, Testing, Smiling, Publishing!

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.