EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/reactor/ReactorControllerBlock.java

70 lines
2.6 KiB
Java
Raw Normal View History

2020-07-13 20:37:21 +00:00
package com.thebrokenrail.energonrelics.block.reactor;
import com.thebrokenrail.energonrelics.block.entity.reactor.ReactorControllerBlockEntity;
2020-08-04 17:06:11 +00:00
import com.thebrokenrail.energonrelics.api.block.energy.FacingEnergyBlock;
2020-07-22 20:17:01 +00:00
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
2020-07-13 20:37:21 +00:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
2020-07-22 20:17:01 +00:00
import net.minecraft.block.Material;
import net.minecraft.block.MaterialColor;
2020-07-13 20:37:21 +00:00
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")
2020-07-26 23:05:59 +00:00
public class ReactorControllerBlock extends FacingEnergyBlock {
2020-07-13 20:37:21 +00:00
public static final BooleanProperty POWERED = Properties.POWERED;
public ReactorControllerBlock() {
2020-07-22 20:17:01 +00:00
super(FabricBlockSettings.of(Material.STONE, MaterialColor.PURPLE_TERRACOTTA).requiresTool().strength(1.5f, 6.0f));
2020-07-13 20:37:21 +00:00
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<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(POWERED);
}
@Override
protected Function<BlockEntityType<BlockEntity>, BlockEntity> getFactory() {
return ReactorControllerBlockEntity::new;
}
}