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.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 bfz) {
        final Level bjw3 = bfz.getLevel();
        final BlockPos fk4 = bfz.getClickedPos();
        final BlockState byj5 = bjw3.getBlockState(fk4);
        if (!byj5.is(BlockTags.RAILS)) {
            return InteractionResult.FAIL;
        }
        final ItemStack ben6 = bfz.getItemInHand();
        if (!bjw3.isClientSide) {
            final RailShape bzn7 = (byj5.getBlock() instanceof BaseRailBlock) ? byj5.<RailShape>getValue(((BaseRailBlock)byj5.getBlock()).getShapeProperty()) : RailShape.NORTH_SOUTH;
            double double8 = 0.0;
            if (bzn7.isAscending()) {
                double8 = 0.5;
            }
            final AbstractMinecart azx10 = AbstractMinecart.createMinecart(bjw3, fk4.getX() + 0.5, fk4.getY() + 0.0625 + double8, fk4.getZ() + 0.5, this.type);
            if (ben6.hasCustomHoverName()) {
                azx10.setCustomName(ben6.getHoverName());
            }
            bjw3.addFreshEntity(azx10);
        }
        ben6.shrink(1);
        return InteractionResult.SUCCESS;
    }
    
    static {
        DISPENSE_ITEM_BEHAVIOR = new DefaultDispenseItemBehavior() {
            private final DefaultDispenseItemBehavior defaultDispenseItemBehavior = new DefaultDispenseItemBehavior();
            
            public ItemStack execute(final BlockSource fl, final ItemStack ben) {
                final Direction fp4 = fl.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING);
                final Level bjw5 = fl.getLevel();
                final double double6 = fl.x() + fp4.getStepX() * 1.125;
                final double double7 = Math.floor(fl.y()) + fp4.getStepY();
                final double double8 = fl.z() + fp4.getStepZ() * 1.125;
                final BlockPos fk12 = fl.getPos().relative(fp4);
                final BlockState byj13 = bjw5.getBlockState(fk12);
                final RailShape bzn14 = (byj13.getBlock() instanceof BaseRailBlock) ? byj13.<RailShape>getValue(((BaseRailBlock)byj13.getBlock()).getShapeProperty()) : RailShape.NORTH_SOUTH;
                double double9;
                if (byj13.is(BlockTags.RAILS)) {
                    if (bzn14.isAscending()) {
                        double9 = 0.6;
                    }
                    else {
                        double9 = 0.1;
                    }
                }
                else {
                    if (!byj13.isAir() || !bjw5.getBlockState(fk12.below()).is(BlockTags.RAILS)) {
                        return this.defaultDispenseItemBehavior.dispense(fl, ben);
                    }
                    final BlockState byj14 = bjw5.getBlockState(fk12.below());
                    final RailShape bzn15 = (byj14.getBlock() instanceof BaseRailBlock) ? byj14.<RailShape>getValue(((BaseRailBlock)byj14.getBlock()).getShapeProperty()) : RailShape.NORTH_SOUTH;
                    if (fp4 == Direction.DOWN || !bzn15.isAscending()) {
                        double9 = -0.9;
                    }
                    else {
                        double9 = -0.4;
                    }
                }
                final AbstractMinecart azx17 = AbstractMinecart.createMinecart(bjw5, double6, double7 + double9, double8, ((MinecartItem)ben.getItem()).type);
                if (ben.hasCustomHoverName()) {
                    azx17.setCustomName(ben.getHoverName());
                }
                bjw5.addFreshEntity(azx17);
                ben.shrink(1);
                return ben;
            }
            
            @Override
            protected void playSound(final BlockSource fl) {
                fl.getLevel().levelEvent(1000, fl.getPos(), 0);
            }
        };
    }
}
