package com.thebrokenrail.energonrelics.block.reactor; import com.thebrokenrail.energonrelics.block.entity.reactor.ReactorControllerBlockEntity; import com.thebrokenrail.energonrelics.api.block.energy.FacingEnergyBlock; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Material; import net.minecraft.block.MaterialColor; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.item.ItemPlacementContext; import net.minecraft.server.world.ServerWorld; import net.minecraft.state.StateManager; import net.minecraft.state.property.BooleanProperty; import net.minecraft.state.property.Properties; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import java.util.Objects; import java.util.Random; import java.util.function.Function; @SuppressWarnings("deprecation") public class ReactorControllerBlock extends FacingEnergyBlock { public static final BooleanProperty POWERED = Properties.POWERED; public ReactorControllerBlock() { super(FabricBlockSettings.of(Material.STONE, MaterialColor.PURPLE_TERRACOTTA).requiresTool().strength(1.5f, 6.0f)); setDefaultState(getDefaultState().with(POWERED, false)); } @Override public BlockState getPlacementState(ItemPlacementContext ctx) { return Objects.requireNonNull(super.getPlacementState(ctx)).with(POWERED, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos())); } @Override public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos fromPos, boolean notify) { if (!world.isClient()) { boolean bl = state.get(POWERED); if (bl != world.isReceivingRedstonePower(pos)) { if (bl) { world.getBlockTickScheduler().schedule(pos, this, 4); } else { world.setBlockState(pos, state.cycle(POWERED), 2); } } } } @Override public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { if (state.get(POWERED) && !world.isReceivingRedstonePower(pos)) { world.setBlockState(pos, state.cycle(POWERED), 2); } } @Override protected void appendProperties(StateManager.Builder builder) { super.appendProperties(builder); builder.add(POWERED); } @Override protected Function, BlockEntity> getFactory() { return ReactorControllerBlockEntity::new; } }