@@ -72,7 +72,7 @@ public static void load(@NotNull FileConfiguration config) {
7272 }
7373 }
7474
75- protected static void loadFlammabilityValues (FileConfiguration config , Function <Material , Boolean > addToSet , BiConsumer <Material , Pair <Integer , Optional <Integer >>> putBurnOddsFunction , BiConsumer <Material , Pair <Integer , Optional <Integer >>> putIgniteOddsFunction ) {
75+ protected static void loadFlammabilityValues (@ NotNull FileConfiguration config , Function <Material , Boolean > addToSet , BiConsumer <Material , Pair <Integer , Optional <Integer >>> putBurnOddsFunction , BiConsumer <Material , Pair <Integer , Optional <Integer >>> putIgniteOddsFunction ) {
7676 if (!config .contains ("FlammabilityOverride" ))
7777 return ;
7878 var section = config .getConfigurationSection ("FlammabilityOverride" );
@@ -111,7 +111,7 @@ protected static void loadFlammabilityValues(FileConfiguration config, Function<
111111 }
112112 }
113113
114- protected static void loadBlastResistanceValues (FileConfiguration config , Function <Material , Boolean > addToSet , BiConsumer <Material , Float > putFunction ) {
114+ protected static void loadBlastResistanceValues (@ NotNull FileConfiguration config , Function <Material , Boolean > addToSet , BiConsumer <Material , Float > putFunction ) {
115115 if (!config .contains ("BlastResistanceOverride" ))
116116 return ;
117117 var section = config .getConfigurationSection ("BlastResistanceOverride" );
@@ -156,26 +156,26 @@ protected static void processOverriddenEntry(final BiFunction<Material, BlockOve
156156 }
157157 }
158158
159- protected static boolean set (Material mat , BlockOverride override ) {
159+ protected static boolean set (Material mat , @ NotNull BlockOverride override ) {
160160 boolean result = true ;
161161
162162 // Blast resistance
163163 if (override .blastResistanceOverride ().isPresent ()) {
164- result = result && NMS_HELPER .setBlastResistance (mat , override .blastResistanceOverride ().get (). floatValue ());
164+ result &= NMS_HELPER .setBlastResistance (mat , override .blastResistanceOverride ().get ());
165165 }
166166 // Burn oddity
167167 if (override .burnOddity ().isPresent ()) {
168- result = result && NMS_HELPER .setBurnOdds (mat , override .burnOddity ().get (). intValue ());
168+ result &= NMS_HELPER .setBurnOdds (mat , override .burnOddity ().get ());
169169 }
170170 // Ignite oddity
171171 if (override .igniteOddity ().isPresent ()) {
172- result = result && NMS_HELPER .setIgniteOdds (mat , override .igniteOddity ().get (). intValue ());
172+ result &= NMS_HELPER .setIgniteOdds (mat , override .igniteOddity ().get ());
173173 }
174174
175175 return result ;
176176 }
177177
178- protected static boolean reset (Material mat , BlockOverride override ) {
178+ protected static boolean reset (Material mat , @ NotNull BlockOverride override ) {
179179 boolean result = true ;
180180
181181 // Blast resistance
@@ -195,14 +195,15 @@ protected static boolean reset(Material mat, BlockOverride override) {
195195 }
196196
197197 private static abstract class NMSHelper {
198-
199- public static final NMSHelper createInstance () {
198+ @ NotNull
199+ public static NMSHelper createInstance () {
200200 String [] parts = Bukkit .getServer ().getMinecraftVersion ().split ("\\ ." );
201201 if (parts .length < 2 )
202202 throw new IllegalArgumentException ();
203- int major_version = Integer .parseInt (parts [1 ]);
203+ int major_version = Integer .parseInt (parts [0 ]);
204+ int minor_version = Integer .parseInt (parts [1 ]);
204205 NMSHelper result ;
205- if (major_version < 20 ) {
206+ if (( major_version < 2 ) && ( minor_version < 20 ) ) {
206207 result = new NMSSpigotMappings ();
207208 } else {
208209 result = new NMSMojangMappings ();
@@ -219,16 +220,16 @@ public static final NMSHelper createInstance() {
219220 @ Nullable
220221 Class <?> magicNumbers ;
221222
222- protected NMSHelper (String blastResField , String burnOddsField , String igniteOddsField ) {
223- this . fieldNameBlastResistance = blastResField ;
224- this . fieldNameBurnOdds = burnOddsField ;
225- this . fieldNameIgniteOdds = igniteOddsField ;
223+ protected NMSHelper (@ Nullable String blastResField , @ Nullable String burnOddsField , @ Nullable String igniteOddsField ) {
224+ fieldNameBlastResistance = blastResField ;
225+ fieldNameBurnOdds = burnOddsField ;
226+ fieldNameIgniteOdds = igniteOddsField ;
226227 }
227228
228229 public boolean setBlastResistance (Material m , float value ) {
229230 try {
230- Object block = this . getBlockClass (m );
231- writeField (block , value , this . fieldNameBlastResistance );
231+ Object block = getBlockClass (m );
232+ writeField (block , value , fieldNameBlastResistance );
232233 return true ;
233234 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
234235 | NoSuchMethodException | SecurityException | NoSuchFieldException | ClassNotFoundException e ) {
@@ -239,8 +240,8 @@ public boolean setBlastResistance(Material m, float value) {
239240
240241 public Optional <Float > getBlastResistance (Material m ) {
241242 try {
242- Object block = this . getBlockClass (m );
243- return getFieldValueSafe (block , this . fieldNameBlastResistance );
243+ Object block = getBlockClass (m );
244+ return getFieldValueSafe (block , fieldNameBlastResistance );
244245 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
245246 | NoSuchMethodException | SecurityException | ClassNotFoundException e ) {
246247 e .printStackTrace ();
@@ -250,7 +251,7 @@ public Optional<Float> getBlastResistance(Material m) {
250251
251252 public boolean setBurnOdds (Material m , int value ) {
252253 try {
253- Object fireBlock = this . getBlockClass (Material .FIRE );
254+ Object fireBlock = getBlockClass (Material .FIRE );
254255 return setBurnOdds (m , value , fireBlock );
255256 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
256257 | NoSuchMethodException | SecurityException | ClassNotFoundException e ) {
@@ -261,9 +262,9 @@ public boolean setBurnOdds(Material m, int value) {
261262
262263 public Optional <Integer > getBurnOdds (Material m ) {
263264 try {
264- Object fireBlock = this . getBlockClass (Material .FIRE );
265- Object block = this . getBlockClass (m );
266- Optional <Object2IntMap > optMap = getFieldValueSafe (fireBlock , this . fieldNameBurnOdds );
265+ Object fireBlock = getBlockClass (Material .FIRE );
266+ Object block = getBlockClass (m );
267+ Optional <Object2IntMap > optMap = getFieldValueSafe (fireBlock , fieldNameBurnOdds );
267268 if (optMap .isPresent ()) {
268269 if (optMap .get ().containsKey (block )) {
269270 return Optional .of (optMap .get ().getInt (block ));
@@ -279,7 +280,7 @@ public Optional<Integer> getBurnOdds(Material m) {
279280
280281 public boolean setIgniteOdds (Material m , int value ) {
281282 try {
282- Object fireBlock = this . getBlockClass (Material .FIRE );
283+ Object fireBlock = getBlockClass (Material .FIRE );
283284 return setIgniteOdds (m , value , fireBlock );
284285 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
285286 | NoSuchMethodException | SecurityException | ClassNotFoundException e ) {
@@ -290,9 +291,9 @@ public boolean setIgniteOdds(Material m, int value) {
290291
291292 public Optional <Integer > getIgniteOdds (Material m ) {
292293 try {
293- Object fireBlock = this . getBlockClass (Material .FIRE );
294- Object block = this . getBlockClass (m );
295- Optional <Object2IntMap <Object >> optMap = getFieldValueSafe (fireBlock , this . fieldNameIgniteOdds );
294+ Object fireBlock = getBlockClass (Material .FIRE );
295+ Object block = getBlockClass (m );
296+ Optional <Object2IntMap <Object >> optMap = getFieldValueSafe (fireBlock , fieldNameIgniteOdds );
296297 if (optMap .isPresent ()) {
297298 if (optMap .get ().containsKey (block )) {
298299 return Optional .of (optMap .get ().getInt (block ));
@@ -342,8 +343,8 @@ protected boolean setIgniteOdds(Material m, int value, Object fireBlock) {
342343 protected Object getBlockClass (Material m )
343344 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException ,
344345 ClassNotFoundException {
345- if (this . magicNumbers == null ) {
346- this . magicNumbers = this . getCraftMagicNumbersClass ();
346+ if (magicNumbers == null ) {
347+ magicNumbers = getCraftMagicNumbersClass ();
347348 }
348349 Method method = magicNumbers .getMethod ("getBlock" , Material .class );
349350 return method .invoke (null , m );
@@ -352,7 +353,17 @@ protected Object getBlockClass(Material m)
352353 protected static <T > void writeField (@ NotNull Object block , @ NotNull Consumer <T > whatToDoWithField ,
353354 String fieldName ) throws IllegalAccessException , NoSuchFieldException , ClassCastException ,
354355 InaccessibleObjectException , SecurityException {
355- Field field = block .getClass ().getField (fieldName );
356+ Class <?> clazz = block .getClass ();
357+ Field field = null ;
358+ while (clazz != null ) {
359+ try {
360+ field = clazz .getDeclaredField (fieldName );
361+ break ;
362+ } catch (NoSuchFieldException e ) {
363+ clazz = clazz .getSuperclass ();
364+ }
365+ }
366+ if (field == null ) throw new NoSuchFieldException (fieldName );
356367 field .setAccessible (true );
357368 T obj = (T ) field .get (block );
358369 whatToDoWithField .accept (obj );
@@ -361,7 +372,17 @@ protected static <T> void writeField(@NotNull Object block, @NotNull Consumer<T>
361372 protected static <T > void writeField (@ NotNull Object block , T value , String fieldName )
362373 throws IllegalAccessException , NoSuchFieldException , ClassCastException , InaccessibleObjectException ,
363374 SecurityException {
364- Field field = block .getClass ().getField (fieldName );
375+ Class <?> clazz = block .getClass ();
376+ Field field = null ;
377+ while (clazz != null ) {
378+ try {
379+ field = clazz .getDeclaredField (fieldName );
380+ break ;
381+ } catch (NoSuchFieldException e ) {
382+ clazz = clazz .getSuperclass ();
383+ }
384+ }
385+ if (field == null ) throw new NoSuchFieldException (fieldName );
365386 field .setAccessible (true );
366387 field .set (block , value );
367388 }
@@ -377,15 +398,14 @@ protected static <T> Optional<T> getFieldValueSafe(@NotNull Object instance, Str
377398 protected static <T > T getFieldValue (@ NotNull Object instance , String fieldName )
378399 throws IllegalAccessException , NoSuchFieldException , ClassCastException , InaccessibleObjectException ,
379400 SecurityException {
380- Field field = instance .getClass ().getField (fieldName );
401+ Field field = instance .getClass ().getDeclaredField (fieldName );
381402 field .setAccessible (true );
382403 T obj = (T ) field .get (instance );
383404 return obj ;
384405 }
385406 }
386407
387408 private static class NMSSpigotMappings extends NMSHelper {
388-
389409 // Tested in 1.19.4
390410 private static final String FIELD_NAME_IGNITE_ODDS = "O" ;
391411 private static final String FIELD_NAME_BURN_ODDS = "P" ;
@@ -399,6 +419,7 @@ protected NMSSpigotMappings(String blastResField, String burnOddsField, String i
399419 super (blastResField , burnOddsField , igniteOddsField );
400420 }
401421
422+ @ NotNull
402423 @ Override
403424 Class <?> getCraftMagicNumbersClass () throws ClassNotFoundException {
404425 String packageName = Bukkit .getServer ().getClass ().getPackage ().getName ();
@@ -408,7 +429,6 @@ Class<?> getCraftMagicNumbersClass() throws ClassNotFoundException {
408429 }
409430
410431 private static class NMSMojangMappings extends NMSHelper {
411-
412432 // Tested on 1.20.4 and 1.21
413433 private static final String FIELD_NAME_IGNITE_ODDS = "igniteOdds" ;
414434 private static final String FIELD_NAME_BURN_ODDS = "burnOdds" ;
@@ -422,10 +442,10 @@ protected NMSMojangMappings(String blastResField, String burnOddsField, String i
422442 super (blastResField , burnOddsField , igniteOddsField );
423443 }
424444
445+ @ NotNull
425446 @ Override
426447 Class <?> getCraftMagicNumbersClass () throws ClassNotFoundException {
427448 return Class .forName (Bukkit .getServer ().getClass ().getPackage ().getName () + ".util.CraftMagicNumbers" );
428449 }
429450 }
430-
431451}
0 commit comments