package totemic_commons.pokefenn.block.music;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import totemic_commons.pokefenn.ModBlocks;
import totemic_commons.pokefenn.block.BlockTileTotemic;
import totemic_commons.pokefenn.lib.Strings;
import totemic_commons.pokefenn.network.PacketHandler;
import totemic_commons.pokefenn.network.client.PacketWindChimeSound;
import totemic_commons.pokefenn.recipe.HandlerInitiation;
import totemic_commons.pokefenn.tileentity.music.TileWindChime;
import totemic_commons.pokefenn.util.EntityUtil;
import totemic_commons.pokefenn.util.TotemUtil;

/**
 * Created by Pokefenn.
 * Licensed under MIT (If this is one of my Mods)
 */
public class BlockWindChime extends BlockTileTotemic
{
    public BlockWindChime()
    {
        super(Material.iron);
        setBlockName(Strings.WIND_CHIME_NAME);
        setBlockBounds(0.2F, 0.0F, 0.2F, 0.8F, 1F, 0.8F);
    }

    @Override
    public void onNeighborBlockChange(World world, int x, int y, int z, Block neighbor)
    {
        breakStuffs(x, y, z, world);
    }

    @Override
    public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLiving, ItemStack itemStack)
    {
        breakStuffs(x, y, z, world);
    }

    public static void breakStuffs(int x, int y, int z, World world)
    {
        if(!world.isRemote)
        {
            if(world.getTileEntity(x, y, z) instanceof TileWindChime)
            {
                if(!world.isAirBlock(x, y - 1, z) || world.isAirBlock(x, y + 1, z))
                {
                    world.setBlockToAir(x, y, z);
                    EntityUtil.dropItem(world, x, y, z, new ItemStack(ModBlocks.windChime, 1 , 0));
                }
            }
        }
    }

    @Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ)
    {
        TileWindChime tileWindChime = (TileWindChime) world.getTileEntity(x, y, z);

        if(!world.isRemote && player.isSneaking() && !tileWindChime.isPlaying)
        {
            PacketHandler.sendAround(new PacketWindChimeSound(x, y, z), world.getTileEntity(x, y, z));
            TotemUtil.playMusicFromBlockForCeremonySelector(world, x, y, z, HandlerInitiation.windChime, 0);
            MinecraftServer.getServer().worldServerForDimension(world.provider.dimensionId).func_147487_a("note", x + 0.5D, y - 0.5D, z + 0.5D, 6, 0.0D, 0.0D, 0.0D, 0.0D);
            MinecraftServer.getServer().worldServerForDimension(world.provider.dimensionId).func_147487_a("fireworksSpark", x + 0.5D, y - 0.5D, z + 0.5D, 6, 0.0D, 0.0D, 0.0D, 0.0D);

        }
        return true;
    }

    @Override
    public IIcon getIcon(int side, int meta) {
        return Blocks.planks.getIcon(0, 0);
    }

    @Override
    public int getRenderType()
    {
        return -1;
    }

    @Override
    public boolean isOpaqueCube()
    {
        return false;
    }

    @Override
    public boolean renderAsNormalBlock()
    {
        return false;
    }

    @Override
    public TileEntity createNewTileEntity(World var1, int var2)
    {
        return new TileWindChime();
    }

    /*
    @Override
    public int getMaximumMusic(World world, int x, int y, int z, boolean isFromPlayer, EntityPlayer player)
    {
        return 60;
    }

    @Override
    public int getMusicOutput(World world, int x, int y, int z, boolean isFromPlayer, EntityPlayer player)
    {
        return world.getBlock(x, y + 1, z) != null && world.getBlock(x, y, z).getMaterial() == Material.leaves ? 5 : 4;
    }

    @Override
    public int getRange(World world, int x, int y, int z, boolean isFromPlayer, EntityPlayer player)
    {
        return 7;
    }
    */
}

@@ -124,54 +124,8 @@
 al;%0A
-import net.minecraft.entity.EntityLivingBase;%0A
 impo
@@ -407,42 +407,53 @@
 ort 
-totemic_commons.pokefenn.ModBlocks
+net.minecraftforge.common.util.ForgeDirection
 ;%0Aim
@@ -801,57 +801,8 @@
 me;%0A
-import totemic_commons.pokefenn.util.EntityUtil;%0A
 impo
@@ -1283,38 +1283,38 @@
 breakStuffs(
+world, 
 x, y, z
-, world
 );%0A    %7D%0A%0A  
@@ -1340,28 +1340,31 @@
 lic 
-void onBlockPlacedBy
+boolean canPlaceBlockAt
 (Wor
@@ -1396,60 +1396,8 @@
 nt z
-, EntityLivingBase entityLiving, ItemStack itemStack
 )%0A  
@@ -1412,34 +1412,94 @@
     
-b
 re
-akStuffs
+turn world.isAirBlock
 (x, y
+-1
 , z
-,
+) &&
  world
+.isSideSolid(x, y+1, z, ForgeDirection.DOWN
 );%0A 
@@ -1519,15 +1519,8 @@
 lic 
-static 
 void
@@ -1532,16 +1532,29 @@
 kStuffs(
+World world, 
 int x, i
@@ -1568,21 +1568,8 @@
 nt z
-, World world
 )%0A  
@@ -1629,155 +1629,36 @@
  if(
-world.getTileEntity(x, y, z) instanceof TileWindChime)%0A            %7B%0A                if(!world.isAirBlock(x, y - 1, z) %7C%7C world.isAirBlock(x, y + 1
+!canPlaceBlockAt(world, x, y
 , z)
@@ -1667,34 +1667,26 @@
             
-    %7B%0A    
+%7B%0A
             
@@ -1739,27 +1739,19 @@
     
-    EntityUtil.drop
+dropBlockAs
 Item
@@ -1785,55 +1785,15 @@
 ack(
-ModBlocks.windChime, 1 , 0));%0A                %7D
+this));
 %0A   

Make wind chime behave better when it can't be placed