package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.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 bjg) {
        return new StructureBlockEntity();
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        final BlockEntity bwl8 = bjw.getBlockEntity(fk);
        if (bwl8 instanceof StructureBlockEntity) {
            return ((StructureBlockEntity)bwl8).usedBy(ayj) ? InteractionResult.SUCCESS : InteractionResult.PASS;
        }
        return InteractionResult.PASS;
    }
    
    @Override
    public void setPlacedBy(final Level bjw, final BlockPos fk, final BlockState byj, @Nullable final LivingEntity akz, final ItemStack ben) {
        if (bjw.isClientSide) {
            return;
        }
        if (akz != null) {
            final BlockEntity bwl7 = bjw.getBlockEntity(fk);
            if (bwl7 instanceof StructureBlockEntity) {
                ((StructureBlockEntity)bwl7).createdBy(akz);
            }
        }
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState byj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        return ((AbstractStateHolder<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 byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        if (bjw.isClientSide) {
            return;
        }
        final BlockEntity bwl8 = bjw.getBlockEntity(fk3);
        if (!(bwl8 instanceof StructureBlockEntity)) {
            return;
        }
        final StructureBlockEntity bxl9 = (StructureBlockEntity)bwl8;
        final boolean boolean7 = bjw.hasNeighborSignal(fk3);
        final boolean boolean8 = bxl9.isPowered();
        if (boolean7 && !boolean8) {
            bxl9.setPowered(true);
            this.trigger(bxl9);
        }
        else if (!boolean7 && boolean8) {
            bxl9.setPowered(false);
        }
    }
    
    private void trigger(final StructureBlockEntity bxl) {
        switch (bxl.getMode()) {
            case SAVE: {
                bxl.saveStructure(false);
                break;
            }
            case LOAD: {
                bxl.loadStructure(false);
                break;
            }
            case CORNER: {
                bxl.unloadStructure();
            }
        }
    }
    
    static {
        MODE = BlockStateProperties.STRUCTUREBLOCK_MODE;
    }
}
