package net.minecraft.world.item;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.entity.decoration.Painting;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.decoration.HangingEntity;
import net.minecraft.world.entity.EntityType;

public class HangingEntityItem extends Item {
    private final EntityType<? extends HangingEntity> type;
    
    public HangingEntityItem(final EntityType<? extends HangingEntity> aku, final Properties a) {
        super(a);
        this.type = aku;
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext bfz) {
        final BlockPos fk3 = bfz.getClickedPos();
        final Direction fp4 = bfz.getClickedFace();
        final BlockPos fk4 = fk3.relative(fp4);
        final Player ayj6 = bfz.getPlayer();
        final ItemStack ben7 = bfz.getItemInHand();
        if (ayj6 != null && !this.mayPlace(ayj6, fp4, ben7, fk4)) {
            return InteractionResult.FAIL;
        }
        final Level bjw8 = bfz.getLevel();
        HangingEntity avq9;
        if (this.type == EntityType.PAINTING) {
            avq9 = new Painting(bjw8, fk4, fp4);
        }
        else {
            if (this.type != EntityType.ITEM_FRAME) {
                return InteractionResult.SUCCESS;
            }
            avq9 = new ItemFrame(bjw8, fk4, fp4);
        }
        final CompoundTag jt10 = ben7.getTag();
        if (jt10 != null) {
            EntityType.updateCustomEntityTag(bjw8, ayj6, avq9, jt10);
        }
        if (avq9.survives()) {
            if (!bjw8.isClientSide) {
                avq9.playPlacementSound();
                bjw8.addFreshEntity(avq9);
            }
            ben7.shrink(1);
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.CONSUME;
    }
    
    protected boolean mayPlace(final Player ayj, final Direction fp, final ItemStack ben, final BlockPos fk) {
        return !fp.getAxis().isVertical() && ayj.mayUseItemAt(fk, fp, ben);
    }
}
