package net.minecraft.world.level.block;

import com.google.common.collect.Maps;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.monster.Silverfish;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import java.util.Map;

public class InfestedBlock extends Block {
    private final Block hostBlock;
    private static final Map<Block, Block> BLOCK_BY_HOST_BLOCK;
    
    public InfestedBlock(final Block bvr, final Properties c) {
        super(c);
        this.hostBlock = bvr;
        InfestedBlock.BLOCK_BY_HOST_BLOCK.put(bvr, this);
    }
    
    public Block getHostBlock() {
        return this.hostBlock;
    }
    
    public static boolean isCompatibleHostBlock(final BlockState cfj) {
        return InfestedBlock.BLOCK_BY_HOST_BLOCK.containsKey(cfj.getBlock());
    }
    
    private void spawnInfestation(final Level bqb, final BlockPos fu) {
        final Silverfish bci4 = EntityType.SILVERFISH.create(bqb);
        bci4.moveTo(fu.getX() + 0.5, fu.getY(), fu.getZ() + 0.5, 0.0f, 0.0f);
        bqb.addFreshEntity(bci4);
        bci4.spawnAnim();
    }
    
    @Override
    public void spawnAfterBreak(final BlockState cfj, final Level bqb, final BlockPos fu, final ItemStack bki) {
        super.spawnAfterBreak(cfj, bqb, fu, bki);
        if (!bqb.isClientSide && bqb.getGameRules().getBoolean(GameRules.RULE_DOBLOCKDROPS) && EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, bki) == 0) {
            this.spawnInfestation(bqb, fu);
        }
    }
    
    @Override
    public void wasExploded(final Level bqb, final BlockPos fu, final Explosion bpt) {
        if (!bqb.isClientSide) {
            this.spawnInfestation(bqb, fu);
        }
    }
    
    public static BlockState stateByHostBlock(final Block bvr) {
        return InfestedBlock.BLOCK_BY_HOST_BLOCK.get(bvr).defaultBlockState();
    }
    
    static {
        BLOCK_BY_HOST_BLOCK = Maps.newIdentityHashMap();
    }
}
