2020-07-13 20:37:21 +00:00
|
|
|
package com.thebrokenrail.energonrelics.block;
|
|
|
|
|
|
|
|
import com.thebrokenrail.energonrelics.block.entity.SwitchBlockEntity;
|
2020-07-26 23:05:59 +00:00
|
|
|
import com.thebrokenrail.energonrelics.block.util.energy.EnergyBlock;
|
2020-07-13 20:37:21 +00:00
|
|
|
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.entity.BlockEntity;
|
|
|
|
import net.minecraft.block.entity.BlockEntityType;
|
2020-07-28 03:10:16 +00:00
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.entity.ItemEntity;
|
|
|
|
import net.minecraft.fluid.WaterFluid;
|
2020-07-13 20:37:21 +00:00
|
|
|
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.Random;
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
2020-07-26 23:05:59 +00:00
|
|
|
public class SwitchBlock extends EnergyBlock {
|
2020-07-13 20:37:21 +00:00
|
|
|
public static final BooleanProperty POWERED = Properties.POWERED;
|
|
|
|
|
|
|
|
public SwitchBlock() {
|
|
|
|
super(FabricBlockSettings.of(Material.STONE).requiresTool().strength(1.5f, 6.0f));
|
|
|
|
setDefaultState(getDefaultState().with(POWERED, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockState getPlacementState(ItemPlacementContext ctx) {
|
|
|
|
return getDefaultState().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) {
|
|
|
|
builder.add(POWERED);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Function<BlockEntityType<BlockEntity>, BlockEntity> getFactory() {
|
|
|
|
return SwitchBlockEntity::new;
|
|
|
|
}
|
|
|
|
}
|