1
1
package cn .nukkit .block ;
2
2
3
3
import cn .nukkit .Player ;
4
+ import cn .nukkit .api .DeprecationDetails ;
4
5
import cn .nukkit .api .PowerNukkitDifference ;
6
+ import cn .nukkit .api .PowerNukkitOnly ;
7
+ import cn .nukkit .api .Since ;
8
+ import cn .nukkit .blockproperty .ArrayBlockProperty ;
9
+ import cn .nukkit .blockproperty .BlockProperties ;
10
+ import cn .nukkit .blockproperty .value .DoublePlantType ;
5
11
import cn .nukkit .item .Item ;
6
- import cn .nukkit .item .ItemSeedsWheat ;
12
+ import cn .nukkit .item .ItemID ;
7
13
import cn .nukkit .level .Level ;
8
14
import cn .nukkit .level .particle .BoneMealParticle ;
9
15
import cn .nukkit .math .BlockFace ;
12
18
import javax .annotation .Nonnull ;
13
19
import java .util .concurrent .ThreadLocalRandom ;
14
20
21
+ import static cn .nukkit .blockproperty .CommonBlockProperties .UPPER_BLOCK ;
22
+
15
23
/**
16
24
* @author xtypr
17
25
* @since 2015/11/23
18
26
*/
19
27
public class BlockDoublePlant extends BlockFlowable {
28
+ @ PowerNukkitOnly
29
+ @ Since ("1.4.0.0-PN" )
30
+ public static final ArrayBlockProperty <DoublePlantType > DOUBLE_PLANT_TYPE = new ArrayBlockProperty <>(
31
+ "double_plant_type" , true , DoublePlantType .class
32
+ );
33
+
34
+ @ PowerNukkitOnly
35
+ @ Since ("1.4.0.0-PN" )
36
+ public static final BlockProperties PROPERTIES = new BlockProperties (DOUBLE_PLANT_TYPE , UPPER_BLOCK );
37
+
38
+ @ Deprecated @ DeprecationDetails (since = "1.4.0.0-PN" , by = "PowerNukkit" , replaceWith = "DoublePlantType.SUNFLOWER" ,
39
+ reason = "Magic values may change in future without backward compatibility." )
20
40
public static final int SUNFLOWER = 0 ;
41
+ @ Deprecated @ DeprecationDetails (since = "1.4.0.0-PN" , by = "PowerNukkit" , replaceWith = "DoublePlantType.LILAC" ,
42
+ reason = "Magic values may change in future without backward compatibility." )
21
43
public static final int LILAC = 1 ;
44
+ @ Deprecated @ DeprecationDetails (since = "1.4.0.0-PN" , by = "PowerNukkit" , replaceWith = "DoublePlantType.TALL_GRASS" ,
45
+ reason = "Magic values may change in future without backward compatibility." )
22
46
public static final int TALL_GRASS = 2 ;
47
+ @ Deprecated @ DeprecationDetails (since = "1.4.0.0-PN" , by = "PowerNukkit" , replaceWith = "DoublePlantType.LARGE_FERN" ,
48
+ reason = "Magic values may change in future without backward compatibility." )
23
49
public static final int LARGE_FERN = 3 ;
50
+ @ Deprecated @ DeprecationDetails (since = "1.4.0.0-PN" , by = "PowerNukkit" , replaceWith = "DoublePlantType.ROSE_BUSH" ,
51
+ reason = "Magic values may change in future without backward compatibility." )
24
52
public static final int ROSE_BUSH = 4 ;
53
+ @ Deprecated @ DeprecationDetails (since = "1.4.0.0-PN" , by = "PowerNukkit" , replaceWith = "DoublePlantType.PEONY" ,
54
+ reason = "Magic values may change in future without backward compatibility." )
25
55
public static final int PEONY = 5 ;
56
+ @ Deprecated @ DeprecationDetails (since = "1.4.0.0-PN" , by = "PowerNukkit" , replaceWith = "CommonBlockProperties.UPPER_BLOCK" ,
57
+ reason = "Magic values may change in future without backward compatibility." )
26
58
public static final int TOP_HALF_BITMASK = 0x8 ;
27
59
28
- private static final String [] NAMES = new String []{
29
- "Sunflower" ,
30
- "Lilac" ,
31
- "Double Tallgrass" ,
32
- "Large Fern" ,
33
- "Rose Bush" ,
34
- "Peony"
35
- };
36
-
37
60
public BlockDoublePlant () {
38
61
this (0 );
39
62
}
@@ -47,22 +70,54 @@ public int getId() {
47
70
return DOUBLE_PLANT ;
48
71
}
49
72
73
+ @ Since ("1.4.0.0-PN" )
74
+ @ PowerNukkitOnly
75
+ @ Nonnull
76
+ @ Override
77
+ public BlockProperties getProperties () {
78
+ return PROPERTIES ;
79
+ }
80
+
81
+ @ PowerNukkitOnly
82
+ @ Since ("1.4.0.0-PN" )
83
+ @ Nonnull
84
+ public DoublePlantType getDoublePlantType () {
85
+ return getPropertyValue (DOUBLE_PLANT_TYPE );
86
+ }
87
+
88
+ @ PowerNukkitOnly
89
+ @ Since ("1.4.0.0-PN" )
90
+ public void setDoublePlantType (@ Nonnull DoublePlantType type ) {
91
+ setPropertyValue (DOUBLE_PLANT_TYPE , type );
92
+ }
93
+
94
+ @ PowerNukkitOnly
95
+ @ Since ("1.4.0.0-PN" )
96
+ public boolean isTopHalf () {
97
+ return getBooleanValue (UPPER_BLOCK );
98
+ }
99
+
100
+ @ PowerNukkitOnly
101
+ @ Since ("1.4.0.0-PN" )
102
+ public void setTopHalf (boolean topHalf ) {
103
+ setBooleanValue (UPPER_BLOCK , topHalf );
104
+ }
105
+
50
106
@ Override
51
107
public boolean canBeReplaced () {
52
- int damage = this .getDamage () & 0x7 ;
53
- return damage == TALL_GRASS || damage == LARGE_FERN ;
108
+ return getDoublePlantType ().isReplaceable ();
54
109
}
55
110
56
111
@ Override
57
112
public String getName () {
58
- return NAMES [ this . getDamage () > 5 ? 0 : this . getDamage ()] ;
113
+ return getDoublePlantType (). getEnglishName () ;
59
114
}
60
115
61
116
@ PowerNukkitDifference (since = "1.4.0.0-PN" , info = "Bottom part will break if the supporting block is invalid on normal update" )
62
117
@ Override
63
118
public int onUpdate (int type ) {
64
119
if (type == Level .BLOCK_UPDATE_NORMAL ) {
65
- if (( this . getDamage () & TOP_HALF_BITMASK ) == TOP_HALF_BITMASK ) {
120
+ if (isTopHalf () ) {
66
121
// Top
67
122
if (this .down ().getId () != DOUBLE_PLANT ) {
68
123
this .getLevel ().setBlock (this , Block .get (BlockID .AIR ), false , true );
@@ -84,8 +139,12 @@ public boolean place(@Nonnull Item item, @Nonnull Block block, @Nonnull Block ta
84
139
Block up = up ();
85
140
86
141
if (up .getId () == AIR && isSupportValid (down ())) {
142
+ setTopHalf (false );
87
143
this .getLevel ().setBlock (block , this , true , false ); // If we update the bottom half, it will drop the item because there isn't a flower block above
88
- this .getLevel ().setBlock (up , Block .get (BlockID .DOUBLE_PLANT , getDamage () ^ TOP_HALF_BITMASK ), true , true );
144
+
145
+ setTopHalf (true );
146
+ this .getLevel ().setBlock (up , this , true , true );
147
+ this .getLevel ().updateAround (this );
89
148
return true ;
90
149
}
91
150
@@ -106,7 +165,7 @@ private boolean isSupportValid(Block support) {
106
165
public boolean onBreak (Item item ) {
107
166
Block down = down ();
108
167
109
- if (( this . getDamage () & TOP_HALF_BITMASK ) == TOP_HALF_BITMASK ) { // Top half
168
+ if (isTopHalf () ) { // Top half
110
169
this .getLevel ().useBreakOn (down );
111
170
} else {
112
171
this .getLevel ().setBlock (this , Block .get (BlockID .AIR ), true , true );
@@ -117,38 +176,38 @@ public boolean onBreak(Item item) {
117
176
118
177
@ Override
119
178
public Item [] getDrops (Item item ) {
120
- if ((this .getDamage () & TOP_HALF_BITMASK ) != TOP_HALF_BITMASK ) {
121
- switch (this .getDamage () & 0x07 ) {
122
- case TALL_GRASS :
123
- case LARGE_FERN :
124
- boolean dropSeeds = ThreadLocalRandom .current ().nextInt (10 ) == 0 ;
125
- if (item .isShears ()) {
126
- //todo enchantment
127
- if (dropSeeds ) {
128
- return new Item []{
129
- new ItemSeedsWheat (0 , 1 ),
130
- toItem ()
131
- };
132
- } else {
133
- return new Item []{
134
- toItem ()
135
- };
136
- }
137
- }
179
+ if (isTopHalf ()){
180
+ return Item .EMPTY_ARRAY ;
181
+ }
138
182
183
+ switch (getDoublePlantType ()) {
184
+ case TALL_GRASS :
185
+ case LARGE_FERN :
186
+ boolean dropSeeds = ThreadLocalRandom .current ().nextInt (10 ) == 0 ;
187
+ if (item .isShears ()) {
188
+ //todo enchantment
139
189
if (dropSeeds ) {
140
190
return new Item []{
141
- new ItemSeedsWheat ()
191
+ Item .get (ItemID .WHEAT_SEEDS ),
192
+ toItem ()
142
193
};
143
194
} else {
144
- return Item .EMPTY_ARRAY ;
195
+ return new Item []{
196
+ toItem ()
197
+ };
145
198
}
146
- }
199
+ }
147
200
148
- return new Item []{toItem ()};
201
+ if (dropSeeds ) {
202
+ return new Item []{
203
+ Item .get (ItemID .WHEAT_SEEDS )
204
+ };
205
+ } else {
206
+ return Item .EMPTY_ARRAY ;
207
+ }
149
208
}
150
209
151
- return Item . EMPTY_ARRAY ;
210
+ return new Item []{ toItem ()} ;
152
211
}
153
212
154
213
@ Override
@@ -164,7 +223,7 @@ public boolean canBeActivated() {
164
223
@ Override
165
224
public boolean onActivate (@ Nonnull Item item , Player player ) {
166
225
if (item .isFertilizer ()) { //Bone meal
167
- switch (this . getDamage () & 0x07 ) {
226
+ switch (getDoublePlantType () ) {
168
227
case SUNFLOWER :
169
228
case LILAC :
170
229
case ROSE_BUSH :
0 commit comments