|
1 | 1 | package falseresync.vivatech.common.block; |
2 | 2 |
|
3 | | -import com.mojang.serialization.MapCodec; |
4 | | -import falseresync.vivatech.common.blockentity.ContactorBlockEntity; |
5 | | -import net.minecraft.block.BlockRenderType; |
| 3 | +import falseresync.vivatech.common.Vivatech; |
| 4 | +import falseresync.vivatech.common.item.VivatechItems; |
| 5 | +import falseresync.vivatech.common.power.GridEdge; |
| 6 | +import falseresync.vivatech.common.power.Wire; |
| 7 | +import net.minecraft.block.Block; |
6 | 8 | import net.minecraft.block.BlockState; |
7 | | -import net.minecraft.block.BlockWithEntity; |
8 | | -import net.minecraft.block.entity.BlockEntity; |
| 9 | +import net.minecraft.entity.player.PlayerEntity; |
| 10 | +import net.minecraft.item.ItemPlacementContext; |
| 11 | +import net.minecraft.item.ItemStack; |
| 12 | +import net.minecraft.server.world.ServerWorld; |
| 13 | +import net.minecraft.state.StateManager; |
| 14 | +import net.minecraft.state.property.BooleanProperty; |
| 15 | +import net.minecraft.state.property.DirectionProperty; |
| 16 | +import net.minecraft.state.property.Properties; |
| 17 | +import net.minecraft.util.*; |
| 18 | +import net.minecraft.util.hit.BlockHitResult; |
9 | 19 | import net.minecraft.util.math.BlockPos; |
10 | | -import org.jetbrains.annotations.Nullable; |
| 20 | +import net.minecraft.util.math.Direction; |
| 21 | +import net.minecraft.util.math.random.Random; |
| 22 | +import net.minecraft.world.World; |
11 | 23 |
|
12 | | -public class ContactorBlock extends BlockWithEntity { |
13 | | - public static final MapCodec<ContactorBlock> CODEC = createCodec(ContactorBlock::new); |
| 24 | +public class ContactorBlock extends Block { |
| 25 | + public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING; |
| 26 | + public static final BooleanProperty POWERED = Properties.POWERED; |
| 27 | + public static final BooleanProperty NORMALLY_OPEN = BooleanProperty.of("normally_open"); |
14 | 28 |
|
15 | 29 | protected ContactorBlock(Settings settings) { |
16 | 30 | super(settings); |
| 31 | + setDefaultState(stateManager.getDefaultState() |
| 32 | + .with(FACING, Direction.NORTH) |
| 33 | + .with(POWERED, false) |
| 34 | + .with(NORMALLY_OPEN, true)); |
17 | 35 | } |
18 | 36 |
|
19 | 37 | @Override |
20 | | - protected MapCodec<ContactorBlock> getCodec() { |
21 | | - return CODEC; |
| 38 | + protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { |
| 39 | + builder.add(FACING, POWERED, NORMALLY_OPEN); |
22 | 40 | } |
23 | 41 |
|
24 | | - @Nullable |
25 | 42 | @Override |
26 | | - public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { |
27 | | - return new ContactorBlockEntity(pos, state); |
| 43 | + public BlockState getPlacementState(ItemPlacementContext ctx) { |
| 44 | + return getDefaultState().with(FACING, ctx.getHorizontalPlayerFacing()); |
28 | 45 | } |
29 | 46 |
|
30 | 47 | @Override |
31 | | - protected BlockRenderType getRenderType(BlockState state) { |
32 | | - return BlockRenderType.MODEL; |
| 48 | + protected BlockState rotate(BlockState state, BlockRotation rotation) { |
| 49 | + return state.with(FACING, rotation.rotate(state.get(FACING))); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected BlockState mirror(BlockState state, BlockMirror mirror) { |
| 54 | + return state.rotate(mirror.getRotation(state.get(FACING))); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { |
| 59 | + if (stack.isOf(VivatechItems.SCREWDRIVER)) { |
| 60 | + if (!world.isClient) { |
| 61 | + world.setBlockState(pos, state.cycle(NORMALLY_OPEN), Block.NOTIFY_LISTENERS); |
| 62 | + } |
| 63 | + |
| 64 | + return ItemActionResult.SUCCESS; |
| 65 | + } |
| 66 | + |
| 67 | + return super.onUseWithItem(stack, state, world, pos, player, hand, hit); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected void neighborUpdate(BlockState state, World world, BlockPos pos, Block sourceBlock, BlockPos sourcePos, boolean notify) { |
| 72 | + if (!world.isClient) { |
| 73 | + var powered = state.get(POWERED); |
| 74 | + if (powered != world.isReceivingRedstonePower(pos)) { |
| 75 | + if (powered) { |
| 76 | + world.scheduleBlockTick(pos, this, 4); |
| 77 | + } else { |
| 78 | + world.setBlockState(pos, state.cycle(POWERED), Block.NOTIFY_LISTENERS); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + super.neighborUpdate(state, world, pos, sourceBlock, sourcePos, notify); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { |
| 87 | + if (state.isOf(newState.getBlock())) { |
| 88 | + manageGridConnection(state, world, pos); |
| 89 | + } |
| 90 | + super.onStateReplaced(state, world, pos, newState, moved); |
| 91 | + } |
| 92 | + |
| 93 | + private static void manageGridConnection(BlockState state, World world, BlockPos pos) { |
| 94 | + var facing = state.get(FACING); |
| 95 | + var gridsLookup = Vivatech.getServerGridsLoader().getGridsManager(world).getGridLookup(); |
| 96 | + |
| 97 | + var posU = pos.offset(facing.rotateYClockwise()); |
| 98 | + var gridU = gridsLookup.get(posU); |
| 99 | + if (gridU == null) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + var posV = pos.offset(facing.rotateYCounterclockwise()); |
| 104 | + var gridV = gridsLookup.get(posV); |
| 105 | + if (gridV == null) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + var powered = state.get(POWERED); |
| 110 | + if (!state.get(NORMALLY_OPEN)) { // I have no idea why this needs to be inverted... I am probably just dumb |
| 111 | + if (powered && gridU != gridV) { |
| 112 | + gridU.connect(new GridEdge(posU, posV), true); |
| 113 | + } else if (!powered && gridU == gridV) { |
| 114 | + gridU.disconnect(new GridEdge(posU, posV), Wire.DropRule.NO_DROP); |
| 115 | + } |
| 116 | + } else { |
| 117 | + if (powered && gridU == gridV) { |
| 118 | + gridU.disconnect(new GridEdge(posU, posV), Wire.DropRule.NO_DROP); |
| 119 | + } else if (!powered && gridU != gridV) { |
| 120 | + gridU.connect(new GridEdge(posU, posV), true); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + protected void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { |
| 127 | + if (state.get(POWERED) && !world.isReceivingRedstonePower(pos)) { |
| 128 | + world.setBlockState(pos, state.cycle(POWERED), Block.NOTIFY_LISTENERS); |
| 129 | + } |
33 | 130 | } |
34 | 131 | } |
0 commit comments