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> aoq, final Properties a) {
        super(a);
        this.type = aoq;
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext blv) {
        final BlockPos fu3 = blv.getClickedPos();
        final Direction fz4 = blv.getClickedFace();
        final BlockPos fu4 = fu3.relative(fz4);
        final Player bec6 = blv.getPlayer();
        final ItemStack bki7 = blv.getItemInHand();
        if (bec6 != null && !this.mayPlace(bec6, fz4, bki7, fu4)) {
            return InteractionResult.FAIL;
        }
        final Level bqb8 = blv.getLevel();
        HangingEntity baz9;
        if (this.type == EntityType.PAINTING) {
            baz9 = new Painting(bqb8, fu4, fz4);
        }
        else {
            if (this.type != EntityType.ITEM_FRAME) {
                return InteractionResult.sidedSuccess(bqb8.isClientSide);
            }
            baz9 = new ItemFrame(bqb8, fu4, fz4);
        }
        final CompoundTag le10 = bki7.getTag();
        if (le10 != null) {
            EntityType.updateCustomEntityTag(bqb8, bec6, baz9, le10);
        }
        if (baz9.survives()) {
            if (!bqb8.isClientSide) {
                baz9.playPlacementSound();
                bqb8.addFreshEntity(baz9);
            }
            bki7.shrink(1);
            return InteractionResult.sidedSuccess(bqb8.isClientSide);
        }
        return InteractionResult.CONSUME;
    }
    
    protected boolean mayPlace(final Player bec, final Direction fz, final ItemStack bki, final BlockPos fu) {
        return !fz.getAxis().isVertical() && bec.mayUseItemAt(fu, fz, bki);
    }
}
