BloodMagic/src/main/java/WayofTime/bloodmagic/entity/mob/EntitySentientSpecter.java

341 lines
11 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.entity.mob;
import java.util.UUID;
import net.minecraft.block.Block;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IEntityOwnable;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackMelee;
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.EntityCreeper;
import net.minecraft.entity.monster.EntityGhast;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.passive.EntityHorse;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityTippedArrow;
import net.minecraft.init.Enchantments;
import net.minecraft.init.Items;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemBow;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.potion.PotionEffect;
import net.minecraft.server.management.PreYggdrasilConverter;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.World;
import WayofTime.bloodmagic.entity.ai.EntityAIAttackRangedBow;
import WayofTime.bloodmagic.entity.ai.EntityAIFollowOwner;
import WayofTime.bloodmagic.entity.ai.EntityAIOwnerHurtByTarget;
import WayofTime.bloodmagic.entity.ai.EntityAIOwnerHurtTarget;
import com.google.common.base.Optional;
public class EntitySentientSpecter extends EntityMob implements IEntityOwnable
{
protected static final DataParameter<Byte> TAMED = EntityDataManager.<Byte>createKey(EntityTameable.class, DataSerializers.BYTE);
protected static final DataParameter<Optional<UUID>> OWNER_UNIQUE_ID = EntityDataManager.<Optional<UUID>>createKey(EntityTameable.class, DataSerializers.OPTIONAL_UNIQUE_ID);
private final EntityAIAttackRangedBow aiArrowAttack = new EntityAIAttackRangedBow(this, 1.0D, 20, 15.0F);
private final EntityAIAttackMelee aiAttackOnCollide = new EntityAIAttackMelee(this, 1.0D, false);
private final int attackPriority = 2;
public EntitySentientSpecter(World worldIn)
{
super(worldIn);
this.setSize(0.6F, 1.95F);
// ((PathNavigateGround) getNavigator()).setCanSwim(false);
this.tasks.addTask(0, new EntityAISwimming(this));
this.tasks.addTask(attackPriority, aiAttackOnCollide);
this.tasks.addTask(3, new EntityAIFollowOwner(this, 1.0D, 10.0F, 2.0F));
this.tasks.addTask(5, new EntityAIWander(this, 1.0D));
this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
this.tasks.addTask(7, new EntityAILookIdle(this));
this.targetTasks.addTask(1, new EntityAIOwnerHurtByTarget(this));
this.targetTasks.addTask(2, new EntityAIOwnerHurtTarget(this));
this.targetTasks.addTask(3, new EntityAINearestAttackableTarget<EntityPlayer>(this, EntityPlayer.class, true));
this.targetTasks.addTask(3, new EntityAIHurtByTarget(this, true, new Class[0]));
this.setCombatTask();
// this.targetTasks.addTask(8, new EntityAINearestAttackableTarget<EntityAnimal>(this, EntityAnimal.class, false));
}
@Override
protected void entityInit()
{
super.entityInit();
this.dataManager.register(TAMED, Byte.valueOf((byte) 0));
this.dataManager.register(OWNER_UNIQUE_ID, Optional.<UUID>absent());
}
@Override
protected void applyEntityAttributes()
{
super.applyEntityAttributes();
getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(40.0D);
getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(6.0D);
getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.27D);
}
public void setCombatTask()
{
if (this.worldObj != null && !this.worldObj.isRemote)
{
this.tasks.removeTask(this.aiAttackOnCollide);
this.tasks.removeTask(this.aiArrowAttack);
ItemStack itemstack = this.getHeldItemMainhand();
if (itemstack != null && itemstack.getItem() instanceof ItemBow)
{
int i = 20;
if (this.worldObj.getDifficulty() != EnumDifficulty.HARD)
{
i = 40;
}
this.aiArrowAttack.setAttackCooldown(i);
this.tasks.addTask(attackPriority, this.aiArrowAttack);
} else
{
this.tasks.addTask(attackPriority, this.aiAttackOnCollide);
}
}
}
@Override
public void setItemStackToSlot(EntityEquipmentSlot slotIn, ItemStack stack)
{
super.setItemStackToSlot(slotIn, stack);
if (!this.worldObj.isRemote && slotIn == EntityEquipmentSlot.MAINHAND)
{
this.setCombatTask();
}
}
public boolean isStationary()
{
return false;
}
@Override
protected boolean canDespawn()
{
//TODO: Change so that it despawns if not tamed after testing.
return false;
}
@Override
public void writeEntityToNBT(NBTTagCompound tag)
{
super.writeEntityToNBT(tag);
if (this.getOwnerId() == null)
{
tag.setString("OwnerUUID", "");
} else
{
tag.setString("OwnerUUID", this.getOwnerId().toString());
}
}
@Override
public void readEntityFromNBT(NBTTagCompound tag)
{
super.readEntityFromNBT(tag);
String s = "";
if (tag.hasKey("OwnerUUID", 8))
{
s = tag.getString("OwnerUUID");
} else
{
String s1 = tag.getString("Owner");
s = PreYggdrasilConverter.convertMobOwnerIfNeeded(this.getServer(), s1);
}
if (!s.isEmpty())
{
try
{
this.setOwnerId(UUID.fromString(s));
this.setTamed(true);
} catch (Throwable var4)
{
this.setTamed(false);
}
}
this.setCombatTask();
}
//TODO: Change to fit the given AI
public boolean shouldAttackEntity(EntityLivingBase attacker, EntityLivingBase owner)
{
if (!(attacker instanceof EntityCreeper) && !(attacker instanceof EntityGhast))
{
if (attacker instanceof EntityWolf)
{
EntityWolf entitywolf = (EntityWolf) attacker;
if (entitywolf.isTamed() && entitywolf.getOwner() == owner)
{
return false;
}
}
return attacker instanceof EntityPlayer && owner instanceof EntityPlayer && !((EntityPlayer) owner).canAttackPlayer((EntityPlayer) attacker) ? false : !(attacker instanceof EntityHorse) || !((EntityHorse) attacker).isTame();
} else
{
return false;
}
}
public void attackEntityWithRangedAttack(EntityLivingBase target, float p_82196_2_)
{
EntityTippedArrow entitytippedarrow = new EntityTippedArrow(this.worldObj, this); //TODO: Change to an arrow created by the Sentient Bow
double d0 = target.posX - this.posX;
double d1 = target.getEntityBoundingBox().minY + (double) (target.height / 3.0F) - entitytippedarrow.posY;
double d2 = target.posZ - this.posZ;
double d3 = (double) MathHelper.sqrt_double(d0 * d0 + d2 * d2);
entitytippedarrow.setThrowableHeading(d0, d1 + d3 * 0.2, d2, 1.6F, (float) (14 - this.worldObj.getDifficulty().getDifficultyId() * 4));
int i = EnchantmentHelper.getMaxEnchantmentLevel(Enchantments.POWER, this);
int j = EnchantmentHelper.getMaxEnchantmentLevel(Enchantments.PUNCH, this);
entitytippedarrow.setDamage((double) (p_82196_2_ * 2.0F) + this.rand.nextGaussian() * 0.25D + (double) ((float) this.worldObj.getDifficulty().getDifficultyId() * 0.11F));
if (i > 0)
{
entitytippedarrow.setDamage(entitytippedarrow.getDamage() + (double) i * 0.5D + 0.5D);
}
if (j > 0)
{
entitytippedarrow.setKnockbackStrength(j);
}
boolean burning = this.isBurning();
burning = burning || EnchantmentHelper.getMaxEnchantmentLevel(Enchantments.FLAME, this) > 0;
if (burning)
{
entitytippedarrow.setFire(100);
}
ItemStack itemstack = this.getHeldItem(EnumHand.OFF_HAND);
if (itemstack != null && itemstack.getItem() == Items.TIPPED_ARROW)
{
entitytippedarrow.setPotionEffect(itemstack);
} else if (true) //TODO: Add potion effects to the arrows
{
entitytippedarrow.addEffect(new PotionEffect(MobEffects.SLOWNESS, 600));
}
this.playSound(SoundEvents.ENTITY_SKELETON_SHOOT, 1.0F, 1.0F / (this.getRNG().nextFloat() * 0.4F + 0.8F));
this.worldObj.spawnEntityInWorld(entitytippedarrow);
}
public boolean isTamed()
{
return (((Byte) this.dataManager.get(TAMED)).byteValue() & 4) != 0;
}
public void setTamed(boolean tamed)
{
byte b0 = ((Byte) this.dataManager.get(TAMED)).byteValue();
if (tamed)
{
this.dataManager.set(TAMED, Byte.valueOf((byte) (b0 | 4)));
} else
{
this.dataManager.set(TAMED, Byte.valueOf((byte) (b0 & -5)));
}
// this.setupTamedAI();
}
@Override
protected SoundEvent getAmbientSound()
{
return SoundEvents.ENTITY_COW_AMBIENT;
}
@Override
protected SoundEvent getHurtSound()
{
return SoundEvents.ENTITY_COW_HURT;
}
@Override
protected SoundEvent getDeathSound()
{
return SoundEvents.ENTITY_COW_DEATH;
}
@Override
protected void playStepSound(BlockPos pos, Block block)
{
this.playSound(SoundEvents.ENTITY_COW_STEP, 0.15F, 1.0F);
}
/**
* Returns the volume for the sounds this mob makes.
*/
@Override
protected float getSoundVolume()
{
return 0.4F;
}
@Override
public UUID getOwnerId()
{
return (UUID) (this.dataManager.get(OWNER_UNIQUE_ID)).orNull();
}
public void setOwnerId(UUID uuid)
{
this.dataManager.set(OWNER_UNIQUE_ID, Optional.fromNullable(uuid));
}
@Override
public EntityLivingBase getOwner()
{
try
{
UUID uuid = this.getOwnerId();
return uuid == null ? null : this.worldObj.getPlayerEntityByUUID(uuid);
} catch (IllegalArgumentException var2)
{
return null;
}
}
public void setOwner(EntityPlayer player)
{
setOwnerId(player.getUniqueID());
}
}