package net.minecraft.world.item;

import com.google.common.collect.Maps;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.AgableMob;
import java.util.Optional;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.Mob;
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.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<?> aoq, final int integer2, final int integer3, final Properties a) {
        super(a);
        this.defaultType = aoq;
        this.color1 = integer2;
        this.color2 = integer3;
        SpawnEggItem.BY_ID.put(aoq, this);
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext blv) {
        final Level bqb3 = blv.getLevel();
        if (bqb3.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final ItemStack bki4 = blv.getItemInHand();
        final BlockPos fu5 = blv.getClickedPos();
        final Direction fz6 = blv.getClickedFace();
        final BlockState cfj7 = bqb3.getBlockState(fu5);
        if (cfj7.is(Blocks.SPAWNER)) {
            final BlockEntity cdl8 = bqb3.getBlockEntity(fu5);
            if (cdl8 instanceof SpawnerBlockEntity) {
                final BaseSpawner bpd9 = ((SpawnerBlockEntity)cdl8).getSpawner();
                final EntityType<?> aoq10 = this.getType(bki4.getTag());
                bpd9.setEntityId(aoq10);
                cdl8.setChanged();
                bqb3.sendBlockUpdated(fu5, cfj7, cfj7, 3);
                bki4.shrink(1);
                return InteractionResult.CONSUME;
            }
        }
        BlockPos fu6;
        if (cfj7.getCollisionShape(bqb3, fu5).isEmpty()) {
            fu6 = fu5;
        }
        else {
            fu6 = fu5.relative(fz6);
        }
        final EntityType<?> aoq11 = this.getType(bki4.getTag());
        if (aoq11.spawn(bqb3, bki4, blv.getPlayer(), fu6, MobSpawnType.SPAWN_EGG, true, !Objects.equals(fu5, fu6) && fz6 == Direction.UP) != null) {
            bki4.shrink(1);
        }
        return InteractionResult.CONSUME;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bqb, final Player bec, final InteractionHand anf) {
        final ItemStack bki5 = bec.getItemInHand(anf);
        final HitResult dej6 = Item.getPlayerPOVHitResult(bqb, bec, ClipContext.Fluid.SOURCE_ONLY);
        if (dej6.getType() != HitResult.Type.BLOCK) {
            return InteractionResultHolder.<ItemStack>pass(bki5);
        }
        if (bqb.isClientSide) {
            return InteractionResultHolder.<ItemStack>success(bki5);
        }
        final BlockHitResult deh7 = (BlockHitResult)dej6;
        final BlockPos fu8 = deh7.getBlockPos();
        if (!(bqb.getBlockState(fu8).getBlock() instanceof LiquidBlock)) {
            return InteractionResultHolder.<ItemStack>pass(bki5);
        }
        if (!bqb.mayInteract(bec, fu8) || !bec.mayUseItemAt(fu8, deh7.getDirection(), bki5)) {
            return InteractionResultHolder.<ItemStack>fail(bki5);
        }
        final EntityType<?> aoq9 = this.getType(bki5.getTag());
        if (aoq9.spawn(bqb, bki5, bec, fu8, MobSpawnType.SPAWN_EGG, false, false) == null) {
            return InteractionResultHolder.<ItemStack>pass(bki5);
        }
        if (!bec.abilities.instabuild) {
            bki5.shrink(1);
        }
        bec.awardStat(Stats.ITEM_USED.get(this));
        return InteractionResultHolder.<ItemStack>consume(bki5);
    }
    
    public boolean spawnsEntity(@Nullable final CompoundTag le, final EntityType<?> aoq) {
        return Objects.equals(this.getType(le), aoq);
    }
    
    public int getColor(final int integer) {
        return (integer == 0) ? this.color1 : this.color2;
    }
    
    @Nullable
    public static SpawnEggItem byId(@Nullable final EntityType<?> aoq) {
        return SpawnEggItem.BY_ID.get(aoq);
    }
    
    public static Iterable<SpawnEggItem> eggs() {
        return Iterables.<SpawnEggItem>unmodifiableIterable(SpawnEggItem.BY_ID.values());
    }
    
    public EntityType<?> getType(@Nullable final CompoundTag le) {
        if (le != null && le.contains("EntityTag", 10)) {
            final CompoundTag le2 = le.getCompound("EntityTag");
            if (le2.contains("id", 8)) {
                return EntityType.byString(le2.getString("id")).orElse(this.defaultType);
            }
        }
        return this.defaultType;
    }
    
    public Optional<Mob> spawnOffspringFromSpawnEgg(final Player bec, final Mob aoz, final EntityType<? extends Mob> aoq, final Level bqb, final Vec3 dem, final ItemStack bki) {
        if (!this.spawnsEntity(bki.getTag(), aoq)) {
            return Optional.<Mob>empty();
        }
        Mob aoz2;
        if (aoz instanceof AgableMob) {
            aoz2 = ((AgableMob)aoz).getBreedOffspring((AgableMob)aoz);
        }
        else {
            aoz2 = (Mob)aoq.create(bqb);
        }
        if (aoz2 == null) {
            return Optional.<Mob>empty();
        }
        aoz2.setBaby(true);
        if (!aoz2.isBaby()) {
            return Optional.<Mob>empty();
        }
        aoz2.moveTo(dem.x(), dem.y(), dem.z(), 0.0f, 0.0f);
        bqb.addFreshEntity(aoz2);
        if (bki.hasCustomHoverName()) {
            aoz2.setCustomName(bki.getHoverName());
        }
        if (!bec.abilities.instabuild) {
            bki.shrink(1);
        }
        return Optional.<Mob>of(aoz2);
    }
    
    static {
        BY_ID = Maps.newIdentityHashMap();
    }
}
