package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.entity.StructureBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.StructureMode;
import net.minecraft.world.level.block.state.properties.EnumProperty;

public class StructureBlock extends BaseEntityBlock {
    public static final EnumProperty<StructureMode> MODE;
    
    protected StructureBlock(final Properties c) {
        super(c);
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bpg) {
        return new StructureBlockEntity();
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        final BlockEntity cdl8 = bqb.getBlockEntity(fu);
        if (cdl8 instanceof StructureBlockEntity) {
            return ((StructureBlockEntity)cdl8).usedBy(bec) ? InteractionResult.sidedSuccess(bqb.isClientSide) : InteractionResult.PASS;
        }
        return InteractionResult.PASS;
    }
    
    @Override
    public void setPlacedBy(final Level bqb, final BlockPos fu, final BlockState cfj, @Nullable final LivingEntity aoy, final ItemStack bki) {
        if (bqb.isClientSide) {
            return;
        }
        if (aoy != null) {
            final BlockEntity cdl7 = bqb.getBlockEntity(fu);
            if (cdl7 instanceof StructureBlockEntity) {
                ((StructureBlockEntity)cdl7).createdBy(aoy);
            }
        }
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState cfj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        return ((StateHolder<O, BlockState>)this.defaultBlockState()).<StructureMode, StructureMode>setValue(StructureBlock.MODE, StructureMode.DATA);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(StructureBlock.MODE);
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        if (bqb.isClientSide) {
            return;
        }
        final BlockEntity cdl8 = bqb.getBlockEntity(fu3);
        if (!(cdl8 instanceof StructureBlockEntity)) {
            return;
        }
        final StructureBlockEntity cel9 = (StructureBlockEntity)cdl8;
        final boolean boolean7 = bqb.hasNeighborSignal(fu3);
        final boolean boolean8 = cel9.isPowered();
        if (boolean7 && !boolean8) {
            cel9.setPowered(true);
            this.trigger(cel9);
        }
        else if (!boolean7 && boolean8) {
            cel9.setPowered(false);
        }
    }
    
    private void trigger(final StructureBlockEntity cel) {
        switch (cel.getMode()) {
            case SAVE: {
                cel.saveStructure(false);
                break;
            }
            case LOAD: {
                cel.loadStructure(false);
                break;
            }
            case CORNER: {
                cel.unloadStructure();
            }
        }
    }
    
    static {
        MODE = BlockStateProperties.STRUCTUREBLOCK_MODE;
    }
}
