package pokefenn.totemic.ceremony;

import net.minecraft.block.IGrowable;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.passive.EntityChicken;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
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 CeremonyZaphkielWaltz extends Ceremony
{

    public CeremonyZaphkielWaltz(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 = 6;

        if(!world.isRemote && context.getTime() % 20 == 0)
        {
            for(EntityItem entity : EntityUtil.getEntitiesInRange(EntityItem.class, world, pos, radius, radius))
            {
                EntityItem item = entity;
                if(item.getItem().getItem() == Items.EGG)
                {
                    if(world.rand.nextInt(4) == 0)
                    {
                        EntityChicken chicken = new EntityChicken(world);
                        chicken.setPosition(entity.posX, entity.posY, entity.posZ);
                        world.spawnEntity(chicken);
                        if(item.getItem().getCount() == 1)
                            item.setDead();
                        else
                        {
                            ItemStack stack = item.getItem().copy();
                            stack.shrink(1);
                            item.setItem(stack);
                        }
                    }
                }
            }
        }

        if(context.getTime() % 5 == 0)
        {
            for(int i = -radius; i <= radius; i++)
                for(int j = -radius; j <= radius; j++)
                    for(int k = -radius; k <= radius; k++)
                    {
                        BlockPos p = pos.add(i, j, k);
                        IBlockState s = world.getBlockState(p);
                        if(s.getBlock() == Blocks.SAPLING)
                        {
                            world.setBlockState(p, ModBlocks.cedar_sapling.getDefaultState(), 3);
                            spawnParticles(world, p.getX() + 0.5, p.getY() + 0.5, p.getZ() + 0.5);
                        }
                        else if(s.getBlock() instanceof IGrowable && s.getBlock().getTickRandomly())
                        {
                            if(world.rand.nextInt(4) < 3)
                            {
                                if(!world.isRemote)
                                    s.getBlock().updateTick(world, p, world.getBlockState(p), world.rand);
                                spawnParticles(world, p.getX() + 0.5, p.getY() + 0.5, p.getZ() + 0.5);
                            }
                        }
                    }
        }
    }

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

    @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();
            world.spawnParticle(EnumParticleTypes.VILLAGER_HAPPY, x + dx, y + dy, z + dz, 0, velY, 0);
        }
    }
}

@@ -29,16 +29,91 @@
 emony;%0A%0A
+import net.minecraft.block.Block;%0Aimport net.minecraft.block.BlockSapling;%0A
 import n
@@ -142,16 +142,16 @@
 owable;%0A
-
 import n
@@ -288,42 +288,8 @@
 en;%0A
-import net.minecraft.init.Blocks;%0A
 impo
@@ -440,16 +440,16 @@
 ockPos;%0A
-
 import n
@@ -474,16 +474,61 @@
 .World;%0A
+import net.minecraftforge.common.IPlantable;%0A
 import p
@@ -2468,16 +2468,20 @@
 kState s
+tate
  = world
@@ -2527,41 +2527,132 @@
     
-if(s.getBlock() == Blocks.SAPLING
+Block block = state.getBlock();%0A                        if(block instanceof BlockSapling && block != ModBlocks.cedar_sapling
 )%0A  
@@ -2930,28 +2930,22 @@
 else if(
-s.getB
+(b
 lock
-()
  instanc
@@ -2962,23 +2962,48 @@
 ble 
-&& s.getB
+%7C%7C block instanceof IPlantable) && b
 lock
-()
 .get
@@ -3224,20 +3224,13 @@
     
-s.getB
+b
 lock
-()
 .upd
@@ -3247,38 +3247,21 @@
 rld, p, 
-world.getBlockS
+s
 tate
-(p)
 , world.

Make Zaphkiel Waltz affect more types of crops. Closes #67