package net.minecraft.world.item;

import com.google.common.collect.Maps;
import com.google.common.collect.Iterables;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.BaseSpawner;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.core.Direction;
import java.util.Objects;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EntityType;
import java.util.Map;

public class SpawnEggItem extends Item {
    private static final Map<EntityType<?>, SpawnEggItem> BY_ID;
    private final int color1;
    private final int color2;
    private final EntityType<?> defaultType;
    
    public SpawnEggItem(final EntityType<?> aku, final int integer2, final int integer3, final Properties a) {
        super(a);
        this.defaultType = aku;
        this.color1 = integer2;
        this.color2 = integer3;
        SpawnEggItem.BY_ID.put(aku, this);
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext bfz) {
        final Level bjw3 = bfz.getLevel();
        if (bjw3.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final ItemStack ben4 = bfz.getItemInHand();
        final BlockPos fk5 = bfz.getClickedPos();
        final Direction fp6 = bfz.getClickedFace();
        final BlockState byj7 = bjw3.getBlockState(fk5);
        final Block bph8 = byj7.getBlock();
        if (bph8 == Blocks.SPAWNER) {
            final BlockEntity bwl9 = bjw3.getBlockEntity(fk5);
            if (bwl9 instanceof SpawnerBlockEntity) {
                final BaseSpawner bjd10 = ((SpawnerBlockEntity)bwl9).getSpawner();
                final EntityType<?> aku11 = this.getType(ben4.getTag());
                bjd10.setEntityId(aku11);
                bwl9.setChanged();
                bjw3.sendBlockUpdated(fk5, byj7, byj7, 3);
                ben4.shrink(1);
                return InteractionResult.SUCCESS;
            }
        }
        BlockPos fk6;
        if (byj7.getCollisionShape(bjw3, fk5).isEmpty()) {
            fk6 = fk5;
        }
        else {
            fk6 = fk5.relative(fp6);
        }
        final EntityType<?> aku12 = this.getType(ben4.getTag());
        if (aku12.spawn(bjw3, ben4, bfz.getPlayer(), fk6, MobSpawnType.SPAWN_EGG, true, !Objects.equals(fk5, fk6) && fp6 == Direction.UP) != null) {
            ben4.shrink(1);
        }
        return InteractionResult.SUCCESS;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bjw, final Player ayj, final InteractionHand ajk) {
        final ItemStack ben5 = ayj.getItemInHand(ajk);
        final HitResult cvi6 = Item.getPlayerPOVHitResult(bjw, ayj, ClipContext.Fluid.SOURCE_ONLY);
        if (cvi6.getType() != HitResult.Type.BLOCK) {
            return InteractionResultHolder.<ItemStack>pass(ben5);
        }
        if (bjw.isClientSide) {
            return InteractionResultHolder.<ItemStack>success(ben5);
        }
        final BlockHitResult cvg7 = (BlockHitResult)cvi6;
        final BlockPos fk8 = cvg7.getBlockPos();
        if (!(bjw.getBlockState(fk8).getBlock() instanceof LiquidBlock)) {
            return InteractionResultHolder.<ItemStack>pass(ben5);
        }
        if (!bjw.mayInteract(ayj, fk8) || !ayj.mayUseItemAt(fk8, cvg7.getDirection(), ben5)) {
            return InteractionResultHolder.<ItemStack>fail(ben5);
        }
        final EntityType<?> aku9 = this.getType(ben5.getTag());
        if (aku9.spawn(bjw, ben5, ayj, fk8, MobSpawnType.SPAWN_EGG, false, false) == null) {
            return InteractionResultHolder.<ItemStack>pass(ben5);
        }
        if (!ayj.abilities.instabuild) {
            ben5.shrink(1);
        }
        ayj.awardStat(Stats.ITEM_USED.get(this));
        return InteractionResultHolder.<ItemStack>success(ben5);
    }
    
    public boolean spawnsEntity(@Nullable final CompoundTag jt, final EntityType<?> aku) {
        return Objects.equals(this.getType(jt), aku);
    }
    
    public int getColor(final int integer) {
        return (integer == 0) ? this.color1 : this.color2;
    }
    
    @Nullable
    public static SpawnEggItem byId(@Nullable final EntityType<?> aku) {
        return SpawnEggItem.BY_ID.get(aku);
    }
    
    public static Iterable<SpawnEggItem> eggs() {
        return Iterables.<SpawnEggItem>unmodifiableIterable(SpawnEggItem.BY_ID.values());
    }
    
    public EntityType<?> getType(@Nullable final CompoundTag jt) {
        if (jt != null && jt.contains("EntityTag", 10)) {
            final CompoundTag jt2 = jt.getCompound("EntityTag");
            if (jt2.contains("id", 8)) {
                return EntityType.byString(jt2.getString("id")).orElse(this.defaultType);
            }
        }
        return this.defaultType;
    }
    
    static {
        BY_ID = Maps.newIdentityHashMap();
    }
}
