package net.minecraft.world.item;

import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockSource;
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.state.properties.RailShape;
import net.minecraft.world.level.block.BaseRailBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.tags.Tag;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.DispenserBlock;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.core.dispenser.DispenseItemBehavior;

public class MinecartItem extends Item {
    private static final DispenseItemBehavior DISPENSE_ITEM_BEHAVIOR;
    private final AbstractMinecart.Type type;
    
    public MinecartItem(final AbstractMinecart.Type a, final Properties a) {
        super(a);
        this.type = a;
        DispenserBlock.registerBehavior(this, MinecartItem.DISPENSE_ITEM_BEHAVIOR);
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext blv) {
        final Level bqb3 = blv.getLevel();
        final BlockPos fu4 = blv.getClickedPos();
        final BlockState cfj5 = bqb3.getBlockState(fu4);
        if (!cfj5.is(BlockTags.RAILS)) {
            return InteractionResult.FAIL;
        }
        final ItemStack bki6 = blv.getItemInHand();
        if (!bqb3.isClientSide) {
            final RailShape cgm7 = (cfj5.getBlock() instanceof BaseRailBlock) ? cfj5.<RailShape>getValue(((BaseRailBlock)cfj5.getBlock()).getShapeProperty()) : RailShape.NORTH_SOUTH;
            double double8 = 0.0;
            if (cgm7.isAscending()) {
                double8 = 0.5;
            }
            final AbstractMinecart bfr10 = AbstractMinecart.createMinecart(bqb3, fu4.getX() + 0.5, fu4.getY() + 0.0625 + double8, fu4.getZ() + 0.5, this.type);
            if (bki6.hasCustomHoverName()) {
                bfr10.setCustomName(bki6.getHoverName());
            }
            bqb3.addFreshEntity(bfr10);
        }
        bki6.shrink(1);
        return InteractionResult.sidedSuccess(bqb3.isClientSide);
    }
    
    static {
        DISPENSE_ITEM_BEHAVIOR = new DefaultDispenseItemBehavior() {
            private final DefaultDispenseItemBehavior defaultDispenseItemBehavior = new DefaultDispenseItemBehavior();
            
            public ItemStack execute(final BlockSource fv, final ItemStack bki) {
                final Direction fz4 = fv.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING);
                final Level bqb5 = fv.getLevel();
                final double double6 = fv.x() + fz4.getStepX() * 1.125;
                final double double7 = Math.floor(fv.y()) + fz4.getStepY();
                final double double8 = fv.z() + fz4.getStepZ() * 1.125;
                final BlockPos fu12 = fv.getPos().relative(fz4);
                final BlockState cfj13 = bqb5.getBlockState(fu12);
                final RailShape cgm14 = (cfj13.getBlock() instanceof BaseRailBlock) ? cfj13.<RailShape>getValue(((BaseRailBlock)cfj13.getBlock()).getShapeProperty()) : RailShape.NORTH_SOUTH;
                double double9;
                if (cfj13.is(BlockTags.RAILS)) {
                    if (cgm14.isAscending()) {
                        double9 = 0.6;
                    }
                    else {
                        double9 = 0.1;
                    }
                }
                else {
                    if (!cfj13.isAir() || !bqb5.getBlockState(fu12.below()).is(BlockTags.RAILS)) {
                        return this.defaultDispenseItemBehavior.dispense(fv, bki);
                    }
                    final BlockState cfj14 = bqb5.getBlockState(fu12.below());
                    final RailShape cgm15 = (cfj14.getBlock() instanceof BaseRailBlock) ? cfj14.<RailShape>getValue(((BaseRailBlock)cfj14.getBlock()).getShapeProperty()) : RailShape.NORTH_SOUTH;
                    if (fz4 == Direction.DOWN || !cgm15.isAscending()) {
                        double9 = -0.9;
                    }
                    else {
                        double9 = -0.4;
                    }
                }
                final AbstractMinecart bfr17 = AbstractMinecart.createMinecart(bqb5, double6, double7 + double9, double8, ((MinecartItem)bki.getItem()).type);
                if (bki.hasCustomHoverName()) {
                    bfr17.setCustomName(bki.getHoverName());
                }
                bqb5.addFreshEntity(bfr17);
                bki.shrink(1);
                return bki;
            }
            
            @Override
            protected void playSound(final BlockSource fv) {
                fv.getLevel().levelEvent(1000, fv.getPos(), 0);
            }
        };
    }
}
