package techreborn.blocks;

import me.modmuss50.jsonDestroyer.api.ITexturedBlock;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumWorldBlockLayer;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import reborncore.RebornCore;
import reborncore.common.BaseBlock;
import techreborn.client.TechRebornCreativeTabMisc;
import techreborn.entitys.EntityNukePrimed;

/**
 * Created by Mark on 13/03/2016.
 */
public class BlockNuke extends BaseBlock implements ITexturedBlock {
    public static final PropertyBool OVERLAY = PropertyBool.create("overlay");

    public BlockNuke() {
        super(Material.tnt);
        setUnlocalizedName("techreborn.nuke");
        setCreativeTab(TechRebornCreativeTabMisc.instance);
        RebornCore.jsonDestroyer.registerObject(this);
        this.setDefaultState(this.blockState.getBaseState().withProperty(OVERLAY, false));
    }

    public void explode(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase igniter) {
        if (!worldIn.isRemote) {
            EntityNukePrimed entitynukeprimed = new EntityNukePrimed(worldIn, (double) ((float) pos.getX() + 0.5F), (double) pos.getY(), (double) ((float) pos.getZ() + 0.5F), igniter);
            worldIn.spawnEntityInWorld(entitynukeprimed);
            worldIn.playSoundAtEntity(entitynukeprimed, "game.tnt.primed", 1.0F, 1.0F);
        }
    }

    @Override
    public void onBlockDestroyedByExplosion(World worldIn, BlockPos pos, Explosion explosionIn) {
        if (!worldIn.isRemote) {
            EntityNukePrimed entitynukeprimed = new EntityNukePrimed(worldIn, (double) ((float) pos.getX() + 0.5F), (double) pos.getY(), (double) ((float) pos.getZ() + 0.5F), explosionIn.getExplosivePlacedBy());
            entitynukeprimed.fuse = worldIn.rand.nextInt(entitynukeprimed.fuse / 4) + entitynukeprimed.fuse / 8;
            worldIn.spawnEntityInWorld(entitynukeprimed);
        }
    }

    public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) {
        if (!worldIn.isRemote && entityIn instanceof EntityArrow) {
            EntityArrow entityarrow = (EntityArrow) entityIn;

            if (entityarrow.isBurning()) {
                this.explode(worldIn, pos, state, entityarrow.shootingEntity instanceof EntityLivingBase ? (EntityLivingBase) entityarrow.shootingEntity : null);
                worldIn.setBlockToAir(pos);
            }
        }
    }

    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
        super.onBlockAdded(worldIn, pos, state);
        if (worldIn.isBlockPowered(pos)) {
            this.onBlockDestroyedByPlayer(worldIn, pos, state);
            worldIn.setBlockToAir(pos);
        }
    }

    /**
     * Called when a neighboring block changes.
     */
    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock) {
        if (worldIn.isBlockPowered(pos)) {
            this.onBlockDestroyedByPlayer(worldIn, pos, state);
            worldIn.setBlockToAir(pos);
        }
    }

    public void onBlockDestroyedByPlayer(World worldIn, BlockPos pos, IBlockState state)
    {
        this.explode(worldIn, pos, state, (EntityLivingBase)null);
    }

    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
        return false; //No flint and steel
    }

    @Override
    public String getTextureNameFromState(IBlockState iBlockState, EnumFacing enumFacing) {
        if (iBlockState.getValue(OVERLAY)) {
            return "techreborn:blocks/nuke_overlay";
        }
        return "techreborn:blocks/nuke";
    }

    @Override
    public int amountOfStates() {
        return 2;
    }

    public int getMetaFromState(IBlockState state) {
        return state.getValue(OVERLAY) ? 1 : 0;
    }

    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(OVERLAY, Boolean.valueOf((meta & 1) > 0));
    }

    protected BlockState createBlockState() {
        return new BlockState(this, OVERLAY);
    }

}

@@ -3178,32 +3178,15 @@
 his.
-onBlockDestroyedByPlayer
+explode
 (wor
@@ -3193,32 +3193,40 @@
 ldIn, pos, state
+, null);
 );%0A            w
@@ -3506,32 +3506,15 @@
 his.
-onBlockDestroyedByPlayer
+explode
 (wor
@@ -3521,32 +3521,38 @@
 ldIn, pos, state
+, null
 );%0A            w
@@ -3599,177 +3599,8 @@
  %7D%0A%0A
-    public void onBlockDestroyedByPlayer(World worldIn, BlockPos pos, IBlockState state)%0A    %7B%0A        this.explode(worldIn, pos, state, (EntityLivingBase)null);%0A    %7D%0A%0A
     

Stop nuke's exploding when broken