package com.apmods.magicraft.entity;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IProjectile;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import com.apmods.magicraft.main.SpellLib;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class EntitySpell extends EntityThrowable implements IProjectile{
private int arrowShake;
private Entity shootingEntity;
public EntitySpell(World par1World) {
super(par1World);
this.setThrowableHeading(motionX, motionY, motionZ, 4.5f, 1F);
}
public EntitySpell(World par1World, EntityLivingBase player, int spell) {
super(par1World, player);
this.setSpell(spell);
float speed = 4.5f;
if(spell == 7){
speed = 0;
}
this.shootingEntity = player;
this.setThrowableHeading(motionX, motionY, motionZ, speed, 1F);
}
public EntitySpell(World par1World, EntityLivingBase par2EntityLivingBase, EntityLivingBase par3EntityLivingBase, float par4, float par5)
{
super(par1World);
this.renderDistanceWeight = 10.0D;
this.shootingEntity = par2EntityLivingBase;
this.posY = par2EntityLivingBase.posY + (double)par2EntityLivingBase.getEyeHeight() - 0.10000000149011612D;
double d0 = par3EntityLivingBase.posX - par2EntityLivingBase.posX;
double d1 = par3EntityLivingBase.boundingBox.minY + (double)(par3EntityLivingBase.height / 3.0F) - this.posY;
double d2 = par3EntityLivingBase.posZ - par2EntityLivingBase.posZ;
double d3 = (double)MathHelper.sqrt_double(d0 * d0 + d2 * d2);
if (d3 >= 1.0E-7D)
{
float f2 = (float)(Math.atan2(d2, d0) * 180.0D / Math.PI) - 90.0F;
float f3 = (float)(-(Math.atan2(d1, d3) * 180.0D / Math.PI));
double d4 = d0 / d3;
double d5 = d2 / d3;
this.setLocationAndAngles(par2EntityLivingBase.posX + d4, this.posY, par2EntityLivingBase.posZ + d5, f2, f3);
this.yOffset = 0.0F;
float f4 = (float)d3 * 0.2F;
this.setThrowableHeading(d0, d1 + (double)f4, d2, par4, par5);
}
}
@Override
protected void entityInit() {
//Spell
this.dataWatcher.addObject(20, Integer.valueOf(1));
}
@Override
public void readEntityFromNBT(NBTTagCompound nbt) {
super.readEntityFromNBT(nbt);
this.setSpell(nbt.getInteger("spell"));
}
@Override
public void writeEntityToNBT(NBTTagCompound nbt) {
super.writeEntityToNBT(nbt);
nbt.setFloat("spell", this.getSpell());
}
@Override
protected void onImpact(MovingObjectPosition mop) {
Block block = this.worldObj.getBlock(mop.blockX, mop.blockY, mop.blockZ);
if(mop.entityHit != null && mop.entityHit instanceof EntityLivingBase){
EntityLivingBase entity = (EntityLivingBase) mop.entityHit;
float f4 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
int damage = 0;
double knockback = 0;
if(this.getSpell() == SpellLib.STUPEFY){
entity.addPotionEffect(new PotionEffect(Potion.moveSlowdown.id, 20, 0));
damage = 6;
knockback = 0.3;
}
else if(this.getSpell() == SpellLib.AVADA_KEDAVRA){
damage = 100;
knockback = 0.2;
}
else if(this.getSpell() == SpellLib.EXPELLIARMUS){
damage = 1;
knockback = 0.2;
if(entity instanceof EntityPlayer){
EntityPlayer player = (EntityPlayer) entity;
ItemStack item = player.getCurrentEquippedItem();
if(item != null){
player.setCurrentItemOrArmor(0, null);
this.dropItem(item.getItem(), item.stackSize);
}
// this.worldObj.spawnEntityInWorld(new EntityItem(this.worldObj, player.posX + rand.nextInt(), player.posY + 2, player.posZ + rand.nextInt(), item.copy()));
}
else{
if(entity.getEquipmentInSlot(0) != null){
ItemStack item = entity.getEquipmentInSlot(0);
if(item != null){
entity.setCurrentItemOrArmor(0, null);
this.dropItem(item.getItem(), item.stackSize);
}
}
}
}
else if(this.getSpell() == SpellLib.EXPULSO){
this.worldObj.createExplosion(this, this.posX, this.posY, this.posZ, 3.0f, true);
}
else if(this.getSpell() == SpellLib.CONFRINGO){
this.worldObj.newExplosion(this, this.posX, this.posY, this.posZ, 3.0f, true, true);
}
else if(this.getSpell() == SpellLib.INCENDIO){
entity.setFire(5);
}
else if(this.getSpell() == SpellLib.AGUAMENTI){
entity.extinguish();
}
else if(this.getSpell() == SpellLib.ALARTE_ASCENDARE){
entity.addVelocity(0, 2.5, 0);
}
entity.attackEntityFrom(DamageSource.magic, 1.0f * damage);
entity.addVelocity(this.motionX * knockback * 0.6000000238418579D / (double)f4, knockback, this.motionZ * knockback * 0.6000000238418579D / (double)f4);
}
else if( block != Blocks.air){
if(this.getSpell() == SpellLib.DURO){
this.worldObj.setBlock(mop.blockX, mop.blockY, mop.blockZ, Blocks.stone);
}
else if(this.getSpell() == SpellLib.EXPULSO){
this.worldObj.createExplosion(this, this.posX, this.posY, this.posZ, 3.0f, true);
}
else if(this.getSpell() == SpellLib.CONFRINGO){
this.worldObj.newExplosion(this, this.posX, this.posY, this.posZ, 3.0f, true, true);
}
else if(this.getSpell() == SpellLib.INCENDIO){
if(mop.sideHit == 1){
this.worldObj.setBlock((int)this.posX, (int)this.posY, (int)this.posZ, Blocks.fire);
Random rand = new Random();
if(rand.nextInt(3) == 0){
if(this.worldObj.getBlock((int)this.posX + 1, (int)this.posY, (int)this.posZ) == Blocks.air){
this.worldObj.setBlock((int)this.posX + 1, (int)this.posY, (int)this.posZ, Blocks.fire);
}
}
if(rand.nextInt(3) == 0){
if(this.worldObj.getBlock((int)this.posX - 1, (int)this.posY, (int)this.posZ) == Blocks.air){
this.worldObj.setBlock((int)this.posX - 1, (int)this.posY, (int)this.posZ, Blocks.fire);
}
}
if(rand.nextInt(3) == 0){
if(this.worldObj.getBlock((int)this.posX, (int)this.posY, (int)this.posZ - 1) == Blocks.air){
this.worldObj.setBlock((int)this.posX, (int)this.posY, (int)this.posZ - 1, Blocks.fire);
}
}
if(rand.nextInt(3) == 0){
if(this.worldObj.getBlock((int)this.posX, (int)this.posY, (int)this.posZ + 1) == Blocks.air){
this.worldObj.setBlock((int)this.posX, (int)this.posY, (int)this.posZ + 1, Blocks.fire);
}
}
if(rand.nextInt(3) == 0){
if(this.worldObj.getBlock((int)this.posX + 1, (int)this.posY, (int)this.posZ + 1) == Blocks.air){
this.worldObj.setBlock((int)this.posX + 1, (int)this.posY, (int)this.posZ + 1, Blocks.fire);
}
}
if(rand.nextInt(3) == 0){
if(this.worldObj.getBlock((int)this.posX + 1, (int)this.posY, (int)this.posZ - 1) == Blocks.air){
this.worldObj.setBlock((int)this.posX + 1, (int)this.posY, (int)this.posZ - 1, Blocks.fire);
}
}
if(rand.nextInt(3) == 0){
if(this.worldObj.getBlock((int)this.posX - 1, (int)this.posY, (int)this.posZ + 1) == Blocks.air){
this.worldObj.setBlock((int)this.posX - 1, (int)this.posY, (int)this.posZ + 1, Blocks.fire);
}
}
if(rand.nextInt(3) == 0){
if(this.worldObj.getBlock((int)this.posX - 1, (int)this.posY, (int)this.posZ - 1) == Blocks.air){
this.worldObj.setBlock((int)this.posX - 1, (int)this.posY, (int)this.posZ - 1, Blocks.fire);
}
}
}
}
else if(this.getSpell() == SpellLib.AGUAMENTI){
// if(mop.sideHit == 1){
// this.worldObj.setBlock((int)this.posX, (int)this.posY + 1, (int)this.posZ, Blocks.flowing_water);
// }
// else if(mop.sideHit == 0){
// this.worldObj.setBlock((int)this.posX, (int)this.posY - 1, (int)this.posZ, Blocks.flowing_water);
// }
// else if(mop.sideHit == 2){
// this.worldObj.setBlock((int)this.posX + 1, (int)this.posY, (int)this.posZ, Blocks.flowing_water);
// }
// else if(mop.sideHit == 4){
// this.worldObj.setBlock((int)this.posX - 1, (int)this.posY, (int)this.posZ, Blocks.flowing_water);
// }
// else if(mop.sideHit == 3){
// this.worldObj.setBlock((int)this.posX, (int)this.posY, (int)this.posZ + 1, Blocks.flowing_water);
// }
// else if(mop.sideHit == 5){
// this.worldObj.setBlock((int)this.posX, (int)this.posY, (int)this.posZ - 1, Blocks.flowing_water);
// }
this.worldObj.setBlock((int)this.posX, (int)this.posY, (int)this.posZ, Blocks.flowing_water);
}
else{
this.setDead();
}
}
this.setDead();
}
@Override
public float getGravityVelocity(){
return 0;
}
@SideOnly(Side.CLIENT)
public int getBrightnessForRender(float par1)
{
return 15728880;
}
/**
* Gets how bright this entity is.
*/
public float getBrightness(float par1)
{
return 1.0F;
}
public void onUpdate(){
super.onUpdate();
// if(this.getSpell() == SpellLib.LUMOS){
// this.addLight();
// if (this.isDead) {
// this.worldObj.updateLightByType(EnumSkyBlock.Block,
// (int) this.posX, (int) this.posY, (int) this.posZ);
// }
// }
Random rand = new Random();
if(rand.nextInt(2) == 1){
this.worldObj.spawnParticle("enchantmenttable", this.posX + rand.nextFloat()- 0.6f, this.posY , this.posZ + rand.nextFloat() - 0.6f, rand.nextFloat() - 0.5f, 0, rand.nextFloat() - 0.5f);
}
else{
this.worldObj.spawnParticle("enchantmenttable", this.posX - rand.nextFloat(), this.posY, this.posZ - rand.nextFloat(), rand.nextFloat() - 0.5f, 0, rand.nextFloat() - 0.5f);
}
}
public void onEntityUpdate(){
super.onEntityUpdate();
if(this.getSpell() == SpellLib.LUMOS){
this.addLight();
}
}
public int getSpell(){
return this.dataWatcher.getWatchableObjectInt(20);
}
private void setSpell(int f){
this.dataWatcher.updateObject(20, Integer.valueOf(f));
}
private void addLight() {
System.out.println("light");
this.worldObj.setLightValue(EnumSkyBlock.Block, (int) this.posX,
(int) this.posY, (int) this.posZ, 16);
this.worldObj.markBlockRangeForRenderUpdate((int) this.posX,
(int) this.posY, (int) this.posX, 12, 12, 12);
this.worldObj.markBlockForUpdate((int) this.posX, (int) this.posY,
(int) this.posZ);
this.worldObj.updateLightByType(EnumSkyBlock.Block, (int) this.posX,
(int) this.posY + 1, (int) this.posZ);
this.worldObj.updateLightByType(EnumSkyBlock.Block, (int) this.posX,
(int) this.posY - 1, (int) this.posZ);
this.worldObj.updateLightByType(EnumSkyBlock.Block,
(int) this.posX + 1, (int) this.posY, (int) this.posZ);
this.worldObj.updateLightByType(EnumSkyBlock.Block,
(int) this.posX - 1, (int) this.posY, (int) this.posZ);
this.worldObj.updateLightByType(EnumSkyBlock.Block, (int) this.posX,
(int) this.posY, (int) this.posZ + 1);
this.worldObj.updateLightByType(EnumSkyBlock.Block, (int) this.posX,
(int) this.posY, (int) this.posZ - 1);
}
public void setShootingEntity(EntityLivingBase entity){
this.shootingEntity = entity;
}
}