Jump to content

[1.7.10] Sitting Isn't Unique To The Entity


qpwoeiruty

Recommended Posts

I have a custom entity that when sitting changes position like a wolf. However, despite it working fine, when another of my custom entity is walking, the sitting entity starts to perform the walking animation even though I have specified it to be only when it isn't walking.

 

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);

	this.setSize(0.5F, 0.5F);

	this.getNavigator().setAvoidsWater(true);

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

	this.setTamed(false);

}

@Override
protected void applyEntityAttributes() {

	super.applyEntityAttributes();

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

}

@Override
public void onLivingUpdate() {

	super.onLivingUpdate();

	this.moveTimer();

	if(!this.worldObj.isRemote) {

		CreatePacketServerSide.sendS2CEntitySync(this);

	}

}

@Override
public boolean attackEntityAsMob(Entity entity) {

	this.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);

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

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

			this.setTamed(true);
			this.setPathToEntity((PathEntity) null);
			this.setAttackTarget((EntityLivingBase) null);
			this.func_152115_b(entityPlayer.getUniqueID().toString());
			this.playTameEffect(true);
			this.worldObj.setEntityState(this, (byte) 7);

		} else {

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

		}

	}

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

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

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

		} else {

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

		}

	}

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

		this.aiSit.setSitting(!this.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", this.currentXP);
	compound.setInteger("currentLevel", this.currentLevel);

	compound.setInteger("requiredXP", this.requiredXP);

	compound.setFloat("moveTimer", this.moveTimer);

}

@Override
public void readEntityFromNBT(NBTTagCompound compound) {

	super.readEntityFromNBT(compound);

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

	this.requiredXP = compound.getInteger("requiredXP");

	this.moveTimer = compound.getFloat("moveTimer");

}


@Override
public EntityAgeable createChild(EntityAgeable entityAgeable) {

	return null;

}


public void addXP(int xp) {

	this.currentXP = this.currentXP + xp;

	setBlocklingLevel();

}

public void calculateRequiredXP() {

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

}

public void setBlocklingLevel() {

	if (this.currentXP > this.requiredXP) {

		this.currentLevel = this.currentLevel + 1;

		this.calculateRequiredXP();

		this.currentXP = 0;

		if(!this.worldObj.isRemote) {

			CreatePacketServerSide.sendS2CEntitySync(this);

		}

	}

}

public void moveTimer() {

	if(this.moveTimerSwitch) {

		this.moveTimer += 2.5F;

	}

	if(!this.moveTimerSwitch) {

		this.moveTimer -= 2.5F;

	}

	if(this.moveTimer >= 10.0F) {

		this.moveTimerSwitch = false;

	}

	if(this.moveTimer <= -10.0) {

		this.moveTimerSwitch = true;

	}

}

public int getLevel() {

	return this.currentLevel;

}

public void setLevel(int currentLevel) {

	this.currentLevel = currentLevel;

}

public float getMoveTimer() {

	return this.moveTimer;

}

public void setMoveTimer(float moveTimer) {

	this.moveTimer = moveTimer;

}

}

 

package com.blocklings.model;

import net.minecraft.client.model.ModelBase;
import net.minecraft.client.model.ModelRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;

import com.blocklings.entity.EntityBlockling;

public class ModelBlockling extends ModelBase {

ModelRenderer body;
ModelRenderer leftArm;
ModelRenderer rightArm;
ModelRenderer leftEye;
ModelRenderer rightEye;
ModelRenderer leftFoot;
ModelRenderer rightFoot;

public ModelBlockling() {

	textureWidth = 64;
	textureHeight = 32;

	body = new ModelRenderer(this, 0, 0);
	body.addBox(-4F, -4F, -4F, 8, 8, ;
	body.setRotationPoint(0F, 18.5F, 0F);
	body.setTextureSize(64, 32);
	body.mirror = true;
	setRotation(body, 0.1047198F, 0F, 0F);
	leftArm = new ModelRenderer(this, 32, 0);
	leftArm.addBox(-5F, 0F, -1F, 2, 3, 3);
	leftArm.setRotationPoint(0F, 18.5F, 0F);
	leftArm.setTextureSize(64, 32);
	leftArm.mirror = true;
	setRotation(leftArm, -0.8726646F, 0F, 0F);
	rightArm = new ModelRenderer(this, 42, 0);
	rightArm.addBox(3F, 0F, -1F, 2, 3, 3);
	rightArm.setRotationPoint(0F, 18.5F, 0F);
	rightArm.setTextureSize(64, 32);
	rightArm.mirror = true;
	setRotation(rightArm, -0.8726646F, 0F, 0F);
	leftEye = new ModelRenderer(this, 32, 12);
	leftEye.addBox(-2F, 0F, -4.5F, 1, 2, 1);
	leftEye.setRotationPoint(0F, 18.5F, 0F);
	leftEye.setTextureSize(64, 32);
	leftEye.mirror = true;
	setRotation(leftEye, 0.1047198F, 0F, 0F);
	rightEye = new ModelRenderer(this, 36, 12);
	rightEye.addBox(1F, 0F, -4.5F, 1, 2, 1);
	rightEye.setRotationPoint(0F, 18.5F, 0F);
	rightEye.setTextureSize(64, 32);
	rightEye.mirror = true;
	setRotation(rightEye, 0.1047198F, 0F, 0F);
	leftFoot = new ModelRenderer(this, 32, 6);
	leftFoot.addBox(-3.5F, 2.5F, -1F, 3, 3, 3);
	leftFoot.setRotationPoint(0F, 18.5F, 0F);
	leftFoot.setTextureSize(64, 32);
	leftFoot.mirror = true;
	setRotation(leftFoot, 0F, 0F, 0F);
	rightFoot = new ModelRenderer(this, 44, 6);
	rightFoot.addBox(0.5F, 2.5F, -1F, 3, 3, 3);
	rightFoot.setRotationPoint(0F, 18.5F, 0F);
	rightFoot.setTextureSize(64, 32);
	rightFoot.mirror = true;
	setRotation(rightFoot, 0F, 0F, 0F);

}

public void render(Entity entity, float f, float f1, float f2, float f3, float f4, float f5) {

	super.render(entity, f, f1, f2, f3, f4, f5);
	setRotationAngles(f, f1, f2, f3, f4, f5, entity);
	body.render(f5);
	leftArm.render(f5);
	rightArm.render(f5);
	leftEye.render(f5);
	rightEye.render(f5);
	leftFoot.render(f5);
	rightFoot.render(f5);

}

private void setRotation(ModelRenderer model, float x, float y, float z) {

	model.rotateAngleX = x;
	model.rotateAngleY = y;
	model.rotateAngleZ = z;

}

public void setRotationAngles(float f, float f1, float f2, float f3, float f4, float f5, Entity entity) {

	super.setRotationAngles(f, f1, f2, f3, f4, f5, entity);

}

public void setLivingAnimations(EntityLivingBase entityLivingBase, float f1, float f2, float f3) {

	EntityBlockling entityBlockling = (EntityBlockling) entityLivingBase;

	float moveTimer = entityBlockling.getMoveTimer();

	if(entityBlockling.isSitting()) {

		body.offsetY = 0.1F;
		leftEye.offsetY = 0.1F;
		rightEye.offsetY = 0.1F;

		leftArm.offsetY = 0.1F;
		rightArm.offsetY = 0.1F;

		System.out.println("a");

	} 

	if(!entityBlockling.isSitting()) {

		body.rotateAngleX = ((moveTimer * f2) / 100) + 0.05F;
		leftEye.rotateAngleX = ((moveTimer * f2) / 100) + 0.05F;
		rightEye.rotateAngleX = ((moveTimer * f2) / 100) + 0.05F;

		leftFoot.rotateAngleX = (moveTimer * f2 * 2) / -40;
		rightFoot.rotateAngleX = (moveTimer * f2 * 2) / 40;

		leftArm.rotateAngleX = ((moveTimer * f2 * 2) / 25) - 0.9F;
		rightArm.rotateAngleX = ((moveTimer * f2 * 2) / -25) - 0.9F;

		body.offsetY = 0.0F;
		leftEye.offsetY = 0.0F;
		rightEye.offsetY = 0.0F;

		leftArm.offsetY = 0.0F;
		rightArm.offsetY = 0.0F;

		System.out.println("b");

	}

}

}

Link to comment
Share on other sites

That sort of thing usually indicates some sort of static field or method is involved.  However, I can't see any problem when quickly looking at your code -- it seems you're just extending the standard EntityTameable functionality for sitting.  And you don't have any static fields or methods that would mess you up.

 

One thing I've never liked about the sitting is that the AI sitting field is private so you need another field in the entity and have to keep them synced -- it would be better to simply have a getter in the sitting AI class.  However, that isn't your problem here either.

 

I don't see any static fields or methods in your code that could cause trouble. 

 

So the next idea might be related to client-server syncing.  The isSitting field is (in EntityTameable) stored in the datawatcher which should sync automatically.  Are you doing anything weird to separately sync the fields of your entity?  Or are you using the datawatcher yourself?

 

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.