11
11
12
12
import com .google .gson .JsonObject ;
13
13
import dev .lambdaurora .lambdynlights .LambDynLights ;
14
+ import net .minecraft .block .BlockState ;
14
15
import net .minecraft .block .Blocks ;
15
16
import net .minecraft .item .BlockItem ;
16
17
import net .minecraft .item .Item ;
17
18
import net .minecraft .item .ItemStack ;
18
19
import net .minecraft .item .Items ;
20
+ import net .minecraft .state .property .Property ;
19
21
import net .minecraft .util .Identifier ;
20
22
import net .minecraft .util .registry .Registry ;
21
23
import org .jetbrains .annotations .NotNull ;
26
28
* Represents an item light source.
27
29
*
28
30
* @author LambdAurora
29
- * @version 2.0.0
31
+ * @version 2.0.2
30
32
* @since 1.3.0
31
33
*/
32
- public record ItemLightSource (Identifier id , Item item , int luminance , boolean waterSensitive ) {
33
- public ItemLightSource (@ NotNull Identifier id , @ NotNull Item item , int luminance ) {
34
- this (id , item , luminance , false );
34
+ public abstract class ItemLightSource {
35
+ private final Identifier id ;
36
+ private final Item item ;
37
+ private final boolean waterSensitive ;
38
+
39
+ public ItemLightSource (Identifier id , Item item , boolean waterSensitive ) {
40
+ this .id = id ;
41
+ this .item = item ;
42
+ this .waterSensitive = waterSensitive ;
43
+ }
44
+
45
+ public ItemLightSource (Identifier id , Item item ) {
46
+ this (id , item , false );
47
+ }
48
+
49
+ public Identifier id () {
50
+ return this .id ;
51
+ }
52
+
53
+ public Item item () {
54
+ return this .item ;
55
+ }
56
+
57
+ public boolean waterSensitive () {
58
+ return this .waterSensitive ;
35
59
}
36
60
37
61
/**
38
62
* Gets the luminance of the item.
39
63
*
40
- * @param stack The item stack.
41
- * @param submergedInWater True if submerged in water, else false.
42
- * @return The luminance value between 0 and 15.
64
+ * @param stack the item stack
65
+ * @param submergedInWater {@code true} if submerged in water, else {@code false} .
66
+ * @return the luminance value between {@code 0} and {@code 15}
43
67
*/
44
- public int getLuminance (@ NotNull ItemStack stack , boolean submergedInWater ) {
45
- if (this .waterSensitive && LambDynLights .get ().config .hasWaterSensitiveCheck () && submergedInWater )
68
+ public int getLuminance (ItemStack stack , boolean submergedInWater ) {
69
+ if (this .waterSensitive () && LambDynLights .get ().config .hasWaterSensitiveCheck () && submergedInWater )
46
70
return 0 ; // Don't emit light with water sensitive items while submerged in water.
47
71
48
- return this .luminance ;
72
+ return this .getLuminance ( stack ) ;
49
73
}
50
74
75
+ /**
76
+ * Gets the luminance of the item.
77
+ *
78
+ * @param stack the item stack
79
+ * @return the luminance value between {@code 0} and {@code 15}
80
+ */
81
+ public abstract int getLuminance (ItemStack stack );
82
+
51
83
@ Override
52
84
public String toString () {
53
85
return "ItemLightSource{" +
54
- "item =" + item +
55
- ", luminance =" + luminance +
56
- ", water_sensitive=" + waterSensitive +
86
+ "id =" + this . id () +
87
+ "item =" + this . item () +
88
+ ", water_sensitive=" + this . waterSensitive () +
57
89
'}' ;
58
90
}
59
91
@@ -69,34 +101,85 @@ public String toString() {
69
101
if (item == Items .AIR )
70
102
return Optional .empty ();
71
103
72
- int luminance ;
104
+ boolean waterSensitive = false ;
105
+ if (json .has ("water_sensitive" ))
106
+ waterSensitive = json .get ("water_sensitive" ).getAsBoolean ();
107
+
73
108
var luminanceElement = json .get ("luminance" ).getAsJsonPrimitive ();
74
109
if (luminanceElement .isNumber ()) {
75
- luminance = luminanceElement .getAsInt ();
110
+ return Optional . of ( new StaticItemLightSource ( id , item , luminanceElement .getAsInt (), waterSensitive ) );
76
111
} else if (luminanceElement .isString ()) {
77
112
var luminanceStr = luminanceElement .getAsString ();
78
113
if (luminanceStr .equals ("block" )) {
79
114
if (item instanceof BlockItem blockItem ) {
80
- luminance = blockItem .getBlock ().getDefaultState ().getLuminance ();
81
- } else {
82
- return Optional .empty ();
115
+ return Optional .of (new BlockItemLightSource (id , item , blockItem .getBlock ().getDefaultState (), waterSensitive ));
83
116
}
84
117
} else {
85
- var block = Registry .BLOCK .get (new Identifier (luminanceStr ));
86
- if (block == Blocks .AIR )
87
- return Optional .empty ();
88
-
89
- luminance = block .getDefaultState ().getLuminance ();
118
+ var blockId = Identifier .tryParse (luminanceStr );
119
+ if (blockId != null ) {
120
+ var block = Registry .BLOCK .get (blockId );
121
+ if (block != Blocks .AIR )
122
+ return Optional .of (new BlockItemLightSource (id , item , block .getDefaultState (), waterSensitive ));
123
+ }
90
124
}
91
125
} else {
92
126
LambDynLights .get ().warn ("Failed to parse item light source \" " + id + "\" , invalid format: \" luminance\" field value isn't string or integer." );
93
- return Optional .empty ();
94
127
}
95
128
96
- boolean waterSensitive = false ;
97
- if (json .has ("water_sensitive" ))
98
- waterSensitive = json .get ("water_sensitive" ).getAsBoolean ();
129
+ return Optional .empty ();
130
+ }
131
+
132
+ public static class StaticItemLightSource extends ItemLightSource {
133
+ private final int luminance ;
134
+
135
+ public StaticItemLightSource (Identifier id , Item item , int luminance , boolean waterSensitive ) {
136
+ super (id , item , waterSensitive );
137
+ this .luminance = luminance ;
138
+ }
139
+
140
+ public StaticItemLightSource (Identifier id , Item item , int luminance ) {
141
+ super (id , item );
142
+ this .luminance = luminance ;
143
+ }
144
+
145
+ @ Override
146
+ public int getLuminance (ItemStack stack ) {
147
+ return this .luminance ;
148
+ }
149
+ }
99
150
100
- return Optional .of (new ItemLightSource (id , item , luminance , waterSensitive ));
151
+ public static class BlockItemLightSource extends ItemLightSource {
152
+ private final BlockState mimic ;
153
+
154
+ public BlockItemLightSource (Identifier id , Item item , BlockState block , boolean waterSensitive ) {
155
+ super (id , item , waterSensitive );
156
+ this .mimic = block ;
157
+ }
158
+
159
+ @ Override
160
+ public int getLuminance (ItemStack stack ) {
161
+ return getLuminance (stack , this .mimic );
162
+ }
163
+
164
+ static int getLuminance (ItemStack stack , BlockState state ) {
165
+ var nbt = stack .getNbt ();
166
+ if (nbt != null ) {
167
+ var blockStateTag = nbt .getCompound ("BlockStateTag" );
168
+ var stateManager = state .getBlock ().getStateManager ();
169
+
170
+ for (var key : blockStateTag .getKeys ()) {
171
+ var property = stateManager .getProperty (key );
172
+ if (property != null ) {
173
+ var value = blockStateTag .get (key ).asString ();
174
+ state = with (state , property , value );
175
+ }
176
+ }
177
+ }
178
+ return state .getLuminance ();
179
+ }
180
+
181
+ private static <T extends Comparable <T >> BlockState with (BlockState state , Property <T > property , String name ) {
182
+ return property .parse (name ).map (value -> state .with (property , value )).orElse (state );
183
+ }
101
184
}
102
185
}
0 commit comments