Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Whenever I spawn my entity in water after a few seconds it will just sink to the bottom of the water and stay there until it drowns just bobbing along as if it were above the water. I don't really know why as I copied the AI straight from the wolf class and the wolves don't sink like that. Could it be to do with the fact the size of the entity changes as it grows when it levels up, I really don't know.

Add the next rule to your constructors and you are finished:

"this.tasks.addTask(0, new EntityAISwimming(this));";

  • Author

This is all of it, however a lot is probably not relevant.

 

package com.blocklings.entity;

import java.util.List;
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.EntityAIFollowOwner;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIOwnerHurtByTarget;
import net.minecraft.entity.ai.EntityAIOwnerHurtTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.IInvBasic;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.pathfinding.PathEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

import com.blocklings.gui.InventoryBlockling;
import com.blocklings.main.Blocklings;
import com.blocklings.network.CreatePacketServerSide;

public class EntityBlockling extends EntityTameable implements IInvBasic{

Random random = new Random();

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

public int requiredXP = 200;

public double maxHealth = 2.0F;
public double attackDamage = 1.0D;
public double moveSpeed = 0.20D;

public float moveTimer = 0.0F;
public boolean moveTimerSwitch = true;

public float attackTimer = 0.0F;
public boolean attackTimerSwitch = true;
public boolean isAttacking = false;

public boolean hasSword = false;

public float width = 0.5F;
public float height = 0.55F;

public InventoryBlockling gear;

public EntityBlockling(World world) {

	super(world);

	this.setSize(this.width, this.height);

	this.getNavigator().setAvoidsWater(true);
	this.getNavigator().setCanSwim(true);

	this.tasks.addTask(1, new EntityAISwimming(this));
	this.tasks.addTask(2, this.aiSit);
	this.tasks.addTask(3, new EntityAIAttackOnCollide(this, 1.0D, false));
	this.tasks.addTask(4, new EntityAIFollowOwner(this, 1.0D, 6.0F, 3.0F));
	this.tasks.addTask(5, new EntityAIWander(this, 1.0D));
	this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
	this.tasks.addTask(7, new EntityAILookIdle(this));

	this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, true));
	this.targetTasks.addTask(2, new EntityAIOwnerHurtByTarget(this));
	this.targetTasks.addTask(3, new EntityAIOwnerHurtTarget(this));

	this.setTamed(false);

	this.currentLevel = random.nextInt(10) + 1;

	this.setupGear();

}

@Override
protected void applyEntityAttributes() {

	super.applyEntityAttributes();

	this.maxHealth = 20.0D;
	this.attackDamage = 1.0D;
	this.moveSpeed = 0.20D;

	this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(this.maxHealth);
	this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(this.moveSpeed);
	this.getAttributeMap().registerAttribute(SharedMonsterAttributes.attackDamage);
	this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(this.attackDamage);

}

@Override
public void onLivingUpdate() {

	super.onLivingUpdate();

	if (this.attackTimer > 0.0F) {

		this.attackTimer -= 1.0F;

	}

	this.hasSword = true;

	this.setEntitySize();

	this.setBlocklingLevel();

	this.calculateRequiredXP();

	this.setMaxHealth();
	this.setAttackDamage();
	this.setMoveSpeed();

	if(!this.worldObj.isRemote) {

		CreatePacketServerSide.sendS2CEntitySync(this);

	}

}

@Override
public boolean attackEntityAsMob(Entity entity) {

	this.setXP(((int) this.attackDamage + random.nextInt(5) + 1) + this.currentXP);

	if (this.attackTimer <= 1.0F) {

		this.attackTimer = (float) random.nextInt(4) + 8;

	}

	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.openGui(Blocklings.modInstance, 0, this.worldObj, this.getEntityId(), 0, 0);

		} else {

			entityPlayer.openGui(Blocklings.modInstance, 0, this.worldObj, this.getEntityId(), 0, 0);

		}

	}

	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 boolean getCanSpawnHere() {

	int i = MathHelper.floor_double(this.posX);
	int j = MathHelper.floor_double(this.boundingBox.minY);
	int k = MathHelper.floor_double(this.posZ);

	int l = this.worldObj.getFullBlockLightValue(i, j, k);
	boolean s = this.worldObj.canBlockSeeTheSky(i, j, k);
	boolean b = this.worldObj.getBlock(i, j - 1, k) == Blocks.grass;

	List list = worldObj.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox(posX, posY, posZ, posX + 1, posY + 1, posZ + 1).expand(16D, 512D, 16D));

	return list.size() < 5 && l > 8 && b && s;

}

@Override
public void onDeath(DamageSource ds) {
	super.onDeath(ds);

	if (gear != null && !this.worldObj.isRemote) {

		for (int i = 0; i < gear.getSizeInventory(); ++i) {

			ItemStack itemstack = gear.getStackInSlot(i);

			if (itemstack != null) {

				this.entityDropItem(itemstack, 0.0F);

			}

		}

	}

}

@Override
public void onInventoryChanged(InventoryBasic var1) {

}

@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.setDouble("maxHealth", this.maxHealth);

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

	NBTTagList nbttaglist = new NBTTagList();

	for (int i = 0; i < this.gear.getSizeInventory(); ++i) {

		ItemStack itemstack = this.gear.getStackInSlot(i);

		if (itemstack != null) {

			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound1.setByte("Slot", (byte) i);
			itemstack.writeToNBT(nbttagcompound1);
			nbttaglist.appendTag(nbttagcompound1);

		}

		compound.setTag("Items", nbttaglist);

	}

}

@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.maxHealth = compound.getDouble("maxHealth");

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

	NBTTagList nbttaglist = compound.getTagList("Items", 10);

	this.setupGear();

	for (int i = 0; i < nbttaglist.tagCount(); ++i) {

		NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
		int j = nbttagcompound1.getByte("Slot") & 255;

		if (j >= 0 && j < this.gear.getSizeInventory()) {

			this.gear.setInventorySlotContents(j, ItemStack.loadItemStackFromNBT(nbttagcompound1));

		}

	}

}


@Override
public EntityAgeable createChild(EntityAgeable entityAgeable) {

	return null;

}

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 setEntitySize() {

	if(this.currentLevel <= 10) {

		this.width = (float) (this.currentLevel * 0.14F) + 0.35F;
		this.height = (float) (this.currentLevel * 0.14F) + 0.4F;

	} else if(this.currentLevel > 10) {

		this.width = (float) (10 * 0.14F) + 0.35F;
		this.height = (float) (10 * 0.14F) + 0.4F;

	}

	setSize(this.width, this.height);

}

public void setMaxHealth() {

	if(this.currentLevel <= 10) {

		this.maxHealth = this.currentLevel * 2;

	} else if(this.currentLevel > 10) {

		this.maxHealth = 20;

	}

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

	if(this.getHealth() > this.maxHealth) {

		this.heal(0.0F);

	}

}

public void setAttackDamage() {

	if(this.currentLevel <= 10) {

		this.attackDamage = this.currentLevel;

	} else if(this.currentLevel > 10) {

		this.attackDamage = 10;

	}

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

}

public void setMoveSpeed() {

	if(this.currentLevel <= 10) {

		this.moveSpeed = 0.20D + ((double) this.currentLevel / 100);

	} else if(this.currentLevel > 10) {

		this.moveSpeed = 0.30D;

	}

	this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(this.moveSpeed);

}

private void setupGear() {

	InventoryBlockling gear1 = this.gear;

	this.gear = new InventoryBlockling("Inventory", howManySlots());

	this.gear.func_110133_a(this.getCommandSenderName());

	if (gear1 != null) {

		gear1.func_110132_b(this);

		int i = Math.min(gear1.getSizeInventory(), this.gear.getSizeInventory());

		for (int j = 0; j < i; ++j) {

			ItemStack itemstack = gear1.getStackInSlot(j);

			if (itemstack != null) {

				this.gear.setInventorySlotContents(j, itemstack.copy());

			}

		}

		gear1 = null;
	}

	this.gear.func_110134_a(this);

}

public int howManySlots() {

	return 22;

}

public int getLevel() {

	return this.currentLevel;

}

public void setLevel(int currentLevel) {

	this.currentLevel = currentLevel;

}

public int getXP() {

	return this.currentXP;

}

public void setXP(int currentXP) {

	this.currentXP = currentXP;

}

public double getMaximumHealth() {

	return this.maxHealth;

}

public void setMaximumHealth(double maxHealth) {

	this.maxHealth = maxHealth;

}

public double getAttackDamage() {

	return this.attackDamage;

}

public void setAttackDamage(double attackDamage) {

	this.attackDamage = attackDamage;

}

public int getRequiredXP() {

	return this.requiredXP;

}

public void setRequiredXP(int requiredXP) {

	this.requiredXP = requiredXP;

}

public float getAttackTimer() {

	return this.attackTimer;

}

public void setAttackTimer(float attackTimer) {

	this.attackTimer = attackTimer;

}

public boolean getHasSword() {

	return this.hasSword;

}

public void setHasSword(boolean hasSword) {

	this.hasSword = hasSword;

}

}

  • Author

That's already there, I have all the required AI for the entity to swim I believe but it just isn't working.

  • Author

Yeah the others work fine. It is literally just when they end up in water, no matter how, they just start to drown in water that is too deep.

I don't have enough experience about entities to help further  :'( sorry... I searched all the way till class Entity starting from Skeleton, Zombie and Cow, but no clue.. :-\ You might try pathfinding if it has something related, but not really sure

Check out my blog!

http://www.whov.altervista.org

  • Author

Well pathfinding is also a bit buggy, not sure if it is related but it could be. No worries, it isn't the end of the world... yet xD

That is weird.  It does seem like your code should be correct.  I don't have time to check it out now, but will see if my own entities with similar wolf-like AI have trouble swimming.  I think they do okay, but will check.

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

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

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.