5
5
import net .minecraft .block .entity .BlockEntity ;
6
6
import net .minecraft .block .entity .BlockEntityTicker ;
7
7
import net .minecraft .block .entity .BlockEntityType ;
8
+ import net .minecraft .entity .player .PlayerEntity ;
8
9
import net .minecraft .fluid .FluidState ;
9
10
import net .minecraft .state .StateManager ;
11
+ import net .minecraft .state .property .BooleanProperty ;
10
12
import net .minecraft .state .property .IntProperty ;
11
13
import net .minecraft .state .property .Properties ;
14
+ import net .minecraft .util .ActionResult ;
15
+ import net .minecraft .util .hit .BlockHitResult ;
12
16
import net .minecraft .util .math .BlockPos ;
13
17
import net .minecraft .util .math .Direction ;
14
18
import net .minecraft .util .math .MathHelper ;
15
19
import net .minecraft .world .BlockView ;
16
20
import net .minecraft .world .World ;
21
+ import net .minecraft .world .event .GameEvent ;
17
22
import org .jetbrains .annotations .Nullable ;
18
23
19
24
import static com .lumiscosity .rounded .blocks .RegisterBlocks .MOISTURE_DETECTOR_BE ;
20
25
21
26
public class MoistureDetectorBlock extends BlockWithEntity {
22
27
public static final MapCodec <MoistureDetectorBlock > CODEC = createCodec (MoistureDetectorBlock ::new );
23
28
public static final IntProperty POWER = Properties .POWER ;
29
+ public static final BooleanProperty CHECK_LEVEL = BooleanProperty .of ("check_level" );
24
30
25
31
@ Override
26
32
public MapCodec <MoistureDetectorBlock > getCodec () {
@@ -29,7 +35,7 @@ public MapCodec<MoistureDetectorBlock> getCodec() {
29
35
30
36
public MoistureDetectorBlock (AbstractBlock .Settings settings ) {
31
37
super (settings );
32
- this .setDefaultState (this .stateManager .getDefaultState ().with (POWER , 0 ));
38
+ this .setDefaultState (this .stateManager .getDefaultState ().with (POWER , 0 ). with ( CHECK_LEVEL , false ) );
33
39
}
34
40
35
41
@ Override
@@ -42,9 +48,31 @@ protected int getWeakRedstonePower(BlockState state, BlockView world, BlockPos p
42
48
return state .get (POWER );
43
49
}
44
50
51
+ public static void updateState (BlockState state , World world , BlockPos pos ) {
52
+ BlockPos check_pos = pos .up ();
53
+ if (state .get (CHECK_LEVEL )) {
54
+ if (world .getBlockState (check_pos ).isOf (Blocks .WATER )) {
55
+ getWaterLevel (state , world , pos , world .getFluidState (check_pos ));
56
+ } else if (world .getBlockState (check_pos ).contains (Properties .WATERLOGGED ) ? world .getBlockState (check_pos ).get (Properties .WATERLOGGED ) : false ) {
57
+ if (state .get (POWER ) != 9 ) {
58
+ world .setBlockState (pos , state .with (POWER , 9 ), Block .NOTIFY_ALL );
59
+ }
60
+ } else {
61
+ if (state .get (POWER ) != 0 ) {
62
+ world .setBlockState (pos , state .with (POWER , 0 ), Block .NOTIFY_ALL );
63
+ }
64
+ }
65
+ } else if (world .getBlockState (check_pos ).isOf (Blocks .AIR ) && world .hasRain (check_pos )) {
66
+ getRainLevel (state , world , pos );
67
+ } else {
68
+ if (state .get (POWER ) != 0 ) {
69
+ world .setBlockState (pos , state .with (POWER , 0 ), Block .NOTIFY_ALL );
70
+ }
71
+ }
72
+ }
73
+
45
74
public static void getWaterLevel (BlockState state , World world , BlockPos pos , FluidState water ) {
46
75
int i = water .getLevel ();
47
- i = MathHelper .clamp (i , 0 , 15 );
48
76
if (state .get (POWER ) != i ) {
49
77
world .setBlockState (pos , state .with (POWER , i ), Block .NOTIFY_ALL );
50
78
}
@@ -57,6 +85,23 @@ public static void getRainLevel(BlockState state, World world, BlockPos pos) {
57
85
}
58
86
}
59
87
88
+ @ Override
89
+ protected ActionResult onUse (BlockState state , World world , BlockPos pos , PlayerEntity player , BlockHitResult hit ) {
90
+ if (player .canModifyBlocks ()) {
91
+ if (world .isClient ) {
92
+ return ActionResult .SUCCESS ;
93
+ } else {
94
+ BlockState blockState = state .cycle (CHECK_LEVEL );
95
+ world .setBlockState (pos , blockState , Block .NOTIFY_LISTENERS );
96
+ world .emitGameEvent (GameEvent .BLOCK_CHANGE , pos , GameEvent .Emitter .of (player , blockState ));
97
+ updateState (blockState , world , pos );
98
+ return ActionResult .CONSUME ;
99
+ }
100
+ } else {
101
+ return super .onUse (state , world , pos , player , hit );
102
+ }
103
+ }
104
+
60
105
@ Override
61
106
protected boolean emitsRedstonePower (BlockState state ) {
62
107
return true ;
@@ -81,25 +126,12 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, Block
81
126
82
127
private static void tick (World world , BlockPos pos , BlockState state , MoistureDetectorBlockEntity blockEntity ) {
83
128
if (world .getTime () % 2L == 0L ) {
84
- BlockPos check_pos = pos .up ();
85
- if (world .getBlockState (check_pos ).isOf (Blocks .WATER )) {
86
- getWaterLevel (state , world , pos , world .getFluidState (check_pos ));
87
- } else if (world .getBlockState (check_pos ).contains (Properties .WATERLOGGED ) ? world .getBlockState (check_pos ).get (Properties .WATERLOGGED ) : false ) {
88
- if (state .get (POWER ) != 9 ) {
89
- world .setBlockState (pos , state .with (POWER , 9 ), Block .NOTIFY_ALL );
90
- }
91
- } else if (world .getBlockState (check_pos ).isOf (Blocks .AIR ) && world .hasRain (check_pos )) {
92
- getRainLevel (state , world , pos );
93
- } else {
94
- if (state .get (POWER ) != 0 ) {
95
- world .setBlockState (pos , state .with (POWER , 0 ), Block .NOTIFY_ALL );
96
- }
97
- }
129
+ updateState (state , world , pos );
98
130
}
99
131
}
100
132
101
133
@ Override
102
134
protected void appendProperties (StateManager .Builder <Block , BlockState > builder ) {
103
- builder .add (POWER );
135
+ builder .add (POWER , MoistureDetectorBlock . CHECK_LEVEL );
104
136
}
105
137
}
0 commit comments