Junix03

Switch.java

Jan 20th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. package com.junix.logicboard.blocks;
  2.  
  3. import com.junix.logicboard.setup.Registration;
  4. import com.junix.logicboard.setup.Component;
  5. import net.minecraft.core.BlockPos;
  6. import net.minecraft.core.Direction;
  7. import net.minecraft.world.InteractionHand;
  8. import net.minecraft.world.InteractionResult;
  9. import net.minecraft.world.entity.player.Player;
  10. import net.minecraft.world.item.context.BlockPlaceContext;
  11. import net.minecraft.world.level.Level;
  12. import net.minecraft.world.level.block.Block;
  13. import net.minecraft.world.level.block.state.BlockState;
  14. import net.minecraft.world.level.block.state.StateDefinition;
  15. import net.minecraft.world.level.block.state.properties.BlockStateProperties;
  16. import net.minecraft.world.level.block.state.properties.BooleanProperty;
  17. import net.minecraft.world.level.block.state.properties.DirectionProperty;
  18. import net.minecraft.world.phys.BlockHitResult;
  19. import org.jetbrains.annotations.NotNull;
  20. import org.jetbrains.annotations.Nullable;
  21.  
  22. public class Switch extends Component {
  23.  
  24. public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
  25. public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
  26.  
  27. public Switch() {
  28. super(Registration.METAL_BLOCK_PROPERTIES);
  29.  
  30. registerDefaultState(defaultBlockState()
  31. .setValue(FACING, Direction.NORTH)
  32. .setValue(POWERED, Boolean.TRUE));
  33. }
  34.  
  35. @Nullable
  36. @Override
  37. public BlockState getStateForPlacement(BlockPlaceContext context) {
  38. return defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
  39. }
  40.  
  41. @Override
  42. protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
  43. builder.add(POWERED, FACING);
  44. }
  45.  
  46. @Override
  47. public @NotNull InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult) {
  48. if (!player.getAbilities().mayBuild)
  49. return InteractionResult.PASS;
  50. else {
  51. level.setBlock(blockPos, blockState.cycle(POWERED), Block.UPDATE_ALL);
  52. return InteractionResult.sidedSuccess(level.isClientSide);
  53. }
  54. }
  55.  
  56. @Override
  57. public int getSourceDistance(BlockState blockState) {
  58. return 0;
  59. }
  60.  
  61. @Override
  62. public boolean isSignalSource(Direction direction) {
  63. return true;
  64. }
  65.  
  66. @Override
  67. public boolean isConnectable(BlockState blockState, Direction direction) {
  68. return false;
  69. }
  70. }
  71.  
Add Comment
Please, Sign In to add comment