package pokefenn.totemic.ceremony;

import net.minecraft.block.Block;
import net.minecraft.block.BlockSapling;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import pokefenn.totemic.api.ceremony.Ceremony;
import pokefenn.totemic.api.ceremony.CeremonyEffectContext;
import pokefenn.totemic.api.music.MusicInstrument;
import pokefenn.totemic.init.ModBlocks;
import pokefenn.totemic.util.EntityUtil;

public class CeremonyFertility extends Ceremony
{
    public CeremonyFertility(String name, int musicNeeded, int maxStartupTime, MusicInstrument... instruments)
    {
        super(name, musicNeeded, maxStartupTime, instruments);
    }

    @Override
    public void effect(World world, BlockPos pos, CeremonyEffectContext context)
    {
        int radius = 8;

        if(!world.isRemote && context.getTime() % (20 * 5) == 0)
        {
            for(Entity entity: EntityUtil.getEntitiesInRange(EntityLiving.class, world, pos, radius, radius))
            {
                if(entity instanceof EntityAnimal || entity instanceof EntityVillager)
                {
                    if(entity instanceof EntityAnimal && ((EntityAnimal) entity).getGrowingAge() == 0 && !((EntityAnimal) entity).isInLove())
                    {
                        EntityAnimal animal = (EntityAnimal) entity;
                        animal.setInLove(null);
                    }
                    else
                    {
                        //TODO: figure out how to make villagers mate
                        /*
                        EntityVillager villager = (EntityVillager) entity;
                        villager.setIsWillingToMate(true);
                        villager.tasks.addTask(0, new EntityAIVillagerMate(villager));
                        villager.setMating(true);
                        */
                    }
                }
            }
        }

        if(context.getTime() % 20 == 0)
        {
            for(BlockPos p: BlockPos.getAllInBoxMutable(pos.add(-radius, -radius, -radius), pos.add(radius, radius, radius)))
            {
                IBlockState state = world.getBlockState(p);
                Block block = state.getBlock();
                if(block instanceof BlockSapling && block != ModBlocks.cedar_sapling)
                {
                    world.setBlockState(p, ModBlocks.cedar_sapling.getDefaultState(), 3);
                    spawnParticles(world, p.getX() + 0.5, p.getY() + 0.5, p.getZ() + 0.5);
                }
            }
        }
    }

    @Override
    public int getEffectTime()
    {
        return SHORT;
    }

    @Override
    public int getMusicPer5()
    {
        return 6;
    }

    private void spawnParticles(World world, double x, double y, double z)
    {
        if(world.isRemote)
        {
            double dx = world.rand.nextGaussian();
            double dy = world.rand.nextGaussian() * 0.5;
            double dz = world.rand.nextGaussian();
            double velY = world.rand.nextGaussian();
            for(int i = 0; i < 10; i++)
                world.spawnParticle(EnumParticleTypes.VILLAGER_HAPPY, x + dx, y + dy, z + dz, 0, velY, 0);
        }
    }
}

@@ -1107,16 +1107,10 @@
 ) %25 
-(20 * 5)
+30
  == 
@@ -1233,50 +1233,16 @@
 dius
-))%0A            %7B%0A                if(entity
+, e -%3E e
  ins
@@ -1266,21 +1266,16 @@
 mal %7C%7C e
-ntity
  instanc
@@ -1293,18 +1293,18 @@
 illager)
+)
 %0A
- 
         
@@ -1303,33 +1303,26 @@
             
-   %7B%0A    
+%7B%0A
             
@@ -1459,34 +1459,26 @@
             
-    %7B%0A    
+%7B%0A
             
@@ -1550,20 +1550,16 @@
         
-   
- 
 animal.s
@@ -1586,33 +1586,38 @@
                 
-%7D
+break;
 %0A               
@@ -1621,21 +1621,31 @@
     
+%7D%0A
     
-else%0A
     
+        else%0A
     
@@ -1650,36 +1650,32 @@
               %7B%0A
-    
                 
@@ -1748,19 +1748,11 @@
     
-    /*%0A    
+/*%0A
     
@@ -1830,36 +1830,32 @@
                 
- 
-   
 villager.setIsWi
@@ -1865,36 +1865,32 @@
 ngToMate(true);%0A
-    
                 
@@ -1968,36 +1968,32 @@
                 
-    
 villager.setMati
@@ -2026,36 +2026,10 @@
     
-    */%0A                    %7D
+*/
 %0A   

Make fertility ceremony breed sequentially