Skip to content

Commit 93aa63a

Browse files
committed
Clean up and fix version logic
1 parent 95bbed2 commit 93aa63a

1 file changed

Lines changed: 32 additions & 32 deletions

File tree

src/main/java/net/countercraft/movecraft/combat/features/BlockBehaviorOverride.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
@@ -385,7 +386,6 @@ protected static <T> T getFieldValue(@NotNull Object instance, String fieldName)
385386
}
386387

387388
private static class NMSSpigotMappings extends NMSHelper {
388-
389389
// Tested in 1.19.4
390390
private static final String FIELD_NAME_IGNITE_ODDS = "O";
391391
private static final String FIELD_NAME_BURN_ODDS = "P";
@@ -399,6 +399,7 @@ protected NMSSpigotMappings(String blastResField, String burnOddsField, String i
399399
super(blastResField, burnOddsField, igniteOddsField);
400400
}
401401

402+
@NotNull
402403
@Override
403404
Class<?> getCraftMagicNumbersClass() throws ClassNotFoundException {
404405
String packageName = Bukkit.getServer().getClass().getPackage().getName();
@@ -408,7 +409,6 @@ Class<?> getCraftMagicNumbersClass() throws ClassNotFoundException {
408409
}
409410

410411
private static class NMSMojangMappings extends NMSHelper {
411-
412412
// Tested on 1.20.4 and 1.21
413413
private static final String FIELD_NAME_IGNITE_ODDS = "igniteOdds";
414414
private static final String FIELD_NAME_BURN_ODDS = "burnOdds";
@@ -422,10 +422,10 @@ protected NMSMojangMappings(String blastResField, String burnOddsField, String i
422422
super(blastResField, burnOddsField, igniteOddsField);
423423
}
424424

425+
@NotNull
425426
@Override
426427
Class<?> getCraftMagicNumbersClass() throws ClassNotFoundException {
427428
return Class.forName(Bukkit.getServer().getClass().getPackage().getName() + ".util.CraftMagicNumbers");
428429
}
429430
}
430-
431431
}

0 commit comments

Comments
 (0)