-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBlockBehaviorOverride.java
More file actions
431 lines (372 loc) · 18.6 KB
/
Copy pathBlockBehaviorOverride.java
File metadata and controls
431 lines (372 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package net.countercraft.movecraft.combat.features;
import it.unimi.dsi.fastutil.Pair;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import net.countercraft.movecraft.combat.MovecraftCombat;
import net.countercraft.movecraft.util.Tags;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
public class BlockBehaviorOverride {
public record BlockOverride(
Optional<Float> blastResistanceOverride,
Optional<Integer> burnOddity,
Optional<Integer> igniteOddity,
Optional<Integer> vanillaBurnOddity,
Optional<Integer> vanillaIgniteOddity
) {
}
private static final Map<Material, BlockOverride> BLOCK_OVERRIDES = new HashMap<>();
private static final NMSHelper NMS_HELPER = NMSHelper.createInstance();
public static void load(@NotNull FileConfiguration config) {
final Set<Material> materialList = new HashSet<>();
final Map<Material, Float> blastResMapping = new HashMap<>();
final Map<Material, Pair<Integer, Optional<Integer>>> burnOddsMapping = new HashMap<>();
final Map<Material, Pair<Integer, Optional<Integer>>> igniteOddsMapping = new HashMap<>();
// First: Collect them all
loadBlastResistanceValues(config, materialList::add, blastResMapping::put);
loadFlammabilityValues(config, materialList::add, burnOddsMapping::put, igniteOddsMapping::put);
// Second: Loop over and create the override objects
for(Material m : materialList) {
Optional<Float> blastResOverride = Optional.empty();
Optional<Integer> burnOddsOverride = Optional.empty();
Optional<Integer> burnOddsVanilla = Optional.empty();
Optional<Integer> igniteOddsOverride = Optional.empty();
Optional<Integer> igniteOddsVanilla = Optional.empty();
if (blastResMapping.containsKey(m)) {
blastResOverride = Optional.ofNullable(blastResMapping.getOrDefault(m, null));
}
if (burnOddsMapping.containsKey(m)) {
burnOddsOverride = Optional.ofNullable(burnOddsMapping.get(m).left());
}
if (igniteOddsMapping.containsKey(m)) {
igniteOddsOverride = Optional.ofNullable(igniteOddsMapping.get(m).left());
igniteOddsVanilla = igniteOddsMapping.get(m).right();
}
BlockOverride override = new BlockOverride(blastResOverride, burnOddsOverride, igniteOddsOverride, burnOddsVanilla, igniteOddsVanilla);
BLOCK_OVERRIDES.put(m, override);
}
}
protected static void loadFlammabilityValues(FileConfiguration config, Function<Material, Boolean> addToSet, BiConsumer<Material, Pair<Integer, Optional<Integer>>> putBurnOddsFunction, BiConsumer<Material, Pair<Integer, Optional<Integer>>> putIgniteOddsFunction) {
if (!config.contains("FlammabilityOverride"))
return;
var section = config.getConfigurationSection("FlammabilityOverride");
if (section == null)
return;
for (var entry : section.getValues(false).entrySet()) {
var elementSection = config.getConfigurationSection(section.getName() + "." + entry.getKey());
// For whatever reason this can be null. WTF YAML
if (elementSection == null) {
MovecraftCombat.getInstance().getLogger()
.warning("No section found within FlammabilitySection for key " + entry.getKey() + "!");
continue;
}
int burnOddOverride = elementSection.getInt("burnOddity", -1);
int igniteOddOverride = elementSection.getInt("igniteOddity", -1);
// Nothing set, continue
if (burnOddOverride < 0 && igniteOddOverride < 0)
continue;
EnumSet<Material> materials = Tags.parseMaterials(entry.getKey());
for (Material m : materials) {
Optional<Integer> burnOddVanilla = NMS_HELPER.getBurnOdds(m);
Optional<Integer> igniteOddVanilla = NMS_HELPER.getIgniteOdds(m);
addToSet.apply(m);
if (burnOddOverride >= 0) {
putBurnOddsFunction.accept(m, Pair.of(burnOddOverride, burnOddVanilla));
}
if (igniteOddOverride >= 0) {
putIgniteOddsFunction.accept(m, Pair.of(igniteOddOverride, igniteOddVanilla));
}
}
}
}
protected static void loadBlastResistanceValues(FileConfiguration config, Function<Material, Boolean> addToSet, BiConsumer<Material, Float> putFunction) {
if (!config.contains("BlastResistanceOverride"))
return;
var section = config.getConfigurationSection("BlastResistanceOverride");
if (section == null)
return;
for (var entry : section.getValues(false).entrySet()) {
EnumSet<Material> materials = Tags.parseMaterials(entry.getKey());
for (Material m : materials) {
float value;
String valStr = entry.getValue().toString();
try {
value = Float.parseFloat(valStr);
} catch (NumberFormatException | NullPointerException ex) {
MovecraftCombat.getInstance().getLogger()
.warning("Unable to load " + m.name() + ": " + entry.getValue());
continue;
}
addToSet.apply(m);
putFunction.accept(m, value);
}
}
}
public static void enable() {
processOverriddenEntry(BlockBehaviorOverride::set, "Failed to set block overrides!");
}
public static void disable() {
processOverriddenEntry(BlockBehaviorOverride::reset, "Failed to revert block overrides to vanilla!");
}
protected static void processOverriddenEntry(final BiFunction<Material, BlockOverride, Boolean> function, final String msgOnException) {
try {
for (Map.Entry<Material, BlockOverride> entry : BLOCK_OVERRIDES.entrySet()) {
if (!function.apply(entry.getKey(), entry.getValue()))
MovecraftCombat.getInstance().getLogger().warning("Unable to set " + entry.getKey().name());
}
} catch (Exception e) {
MovecraftCombat.getInstance().getLogger().info(msgOnException);
e.printStackTrace();
}
}
protected static boolean set(Material mat, BlockOverride override) {
boolean result = true;
// Blast resistance
if (override.blastResistanceOverride().isPresent()) {
result = result && NMS_HELPER.setBlastResistance(mat, override.blastResistanceOverride().get().floatValue());
}
// Burn oddity
if (override.burnOddity().isPresent()) {
result = result && NMS_HELPER.setBurnOdds(mat, override.burnOddity().get().intValue());
}
// Ignite oddity
if (override.igniteOddity().isPresent()) {
result = result && NMS_HELPER.setIgniteOdds(mat, override.igniteOddity().get().intValue());
}
return result;
}
protected static boolean reset(Material mat, BlockOverride override) {
boolean result = true;
// Blast resistance
if (override.blastResistanceOverride().isPresent()) {
result &= NMS_HELPER.setBlastResistance(mat, mat.getBlastResistance());
}
// Burn oddity
if (override.burnOddity().isPresent() && override.vanillaBurnOddity().isPresent()) {
result &= NMS_HELPER.setBurnOdds(mat, override.vanillaBurnOddity().get());
}
// Ignite oddity
if (override.igniteOddity().isPresent() && override.vanillaIgniteOddity().isPresent()) {
result &= NMS_HELPER.setIgniteOdds(mat, override.vanillaIgniteOddity().get());
}
return result;
}
private static abstract class NMSHelper {
public static final NMSHelper createInstance() {
String[] parts = Bukkit.getServer().getMinecraftVersion().split("\\.");
if (parts.length < 2)
throw new IllegalArgumentException();
int major_version = Integer.parseInt(parts[1]);
NMSHelper result;
if (major_version < 20) {
result = new NMSSpigotMappings();
} else {
result = new NMSMojangMappings();
}
return result;
}
@Nullable
private final String fieldNameBlastResistance;
@Nullable
private final String fieldNameBurnOdds;
@Nullable
private final String fieldNameIgniteOdds;
@Nullable
Class<?> magicNumbers;
protected NMSHelper(String blastResField, String burnOddsField, String igniteOddsField) {
this.fieldNameBlastResistance = blastResField;
this.fieldNameBurnOdds = burnOddsField;
this.fieldNameIgniteOdds = igniteOddsField;
}
public boolean setBlastResistance(Material m, float value) {
try {
Object block = this.getBlockClass(m);
writeField(block, value, this.fieldNameBlastResistance);
return true;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | NoSuchFieldException | ClassNotFoundException e) {
e.printStackTrace();
return false;
}
}
public Optional<Float> getBlastResistance(Material m) {
try {
Object block = this.getBlockClass(m);
return getFieldValueSafe(block, this.fieldNameBlastResistance);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | ClassNotFoundException e) {
e.printStackTrace();
}
return Optional.empty();
}
public boolean setBurnOdds(Material m, int value) {
try {
Object fireBlock = this.getBlockClass(Material.FIRE);
return setBurnOdds(m, value, fireBlock);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | ClassNotFoundException e) {
e.printStackTrace();
return false;
}
}
public Optional<Integer> getBurnOdds(Material m) {
try {
Object fireBlock = this.getBlockClass(Material.FIRE);
Object block = this.getBlockClass(m);
Optional<Object2IntMap> optMap = getFieldValueSafe(fireBlock, this.fieldNameBurnOdds);
if (optMap.isPresent()) {
if (optMap.get().containsKey(block)) {
return Optional.of(optMap.get().getInt(block));
}
return Optional.empty();
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | ClassNotFoundException e) {
e.printStackTrace();
}
return Optional.empty();
}
public boolean setIgniteOdds(Material m, int value) {
try {
Object fireBlock = this.getBlockClass(Material.FIRE);
return setIgniteOdds(m, value, fireBlock);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | ClassNotFoundException e) {
e.printStackTrace();
return false;
}
}
public Optional<Integer> getIgniteOdds(Material m) {
try {
Object fireBlock = this.getBlockClass(Material.FIRE);
Object block = this.getBlockClass(m);
Optional<Object2IntMap<Object>> optMap = getFieldValueSafe(fireBlock, this.fieldNameIgniteOdds);
if (optMap.isPresent()) {
if (optMap.get().containsKey(block)) {
return Optional.of(optMap.get().getInt(block));
}
return Optional.empty();
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException | ClassNotFoundException e) {
e.printStackTrace();
}
return Optional.empty();
}
protected boolean setBurnOdds(Material m, int value, Object fireBlock) {
try {
final Object block = getBlockClass(m);
// First Object2Identiy map in field list is the one for ignite odds, second one is for burn odds
NMSSpigotMappings.<Object2IntMap<Object>>writeField(fireBlock, (map) -> {
map.put(block, value);
}, this.fieldNameBurnOdds);
return true;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | NoSuchFieldException
| SecurityException | ClassNotFoundException e) {
e.printStackTrace();
return false;
}
}
protected boolean setIgniteOdds(Material m, int value, Object fireBlock) {
try {
final Object block = getBlockClass(m);
// First Object2Identiy map in field list is the one for ignite odds, second one is for burn odds
NMSSpigotMappings.<Object2IntMap<Object>>writeField(fireBlock, (map) -> {
map.put(block, value);
}, this.fieldNameIgniteOdds);
return true;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | NoSuchFieldException
| SecurityException | ClassNotFoundException e) {
e.printStackTrace();
return false;
}
}
abstract Class<?> getCraftMagicNumbersClass() throws ClassNotFoundException;
protected Object getBlockClass(Material m)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
ClassNotFoundException {
if (this.magicNumbers == null) {
this.magicNumbers = this.getCraftMagicNumbersClass();
}
Method method = magicNumbers.getMethod("getBlock", Material.class);
return method.invoke(null, m);
}
protected static <T> void writeField(@NotNull Object block, @NotNull Consumer<T> whatToDoWithField,
String fieldName) throws IllegalAccessException, NoSuchFieldException, ClassCastException,
InaccessibleObjectException, SecurityException {
Field field = block.getClass().getField(fieldName);
field.setAccessible(true);
T obj = (T) field.get(block);
whatToDoWithField.accept(obj);
}
protected static <T> void writeField(@NotNull Object block, T value, String fieldName)
throws IllegalAccessException, NoSuchFieldException, ClassCastException, InaccessibleObjectException,
SecurityException {
Field field = block.getClass().getField(fieldName);
field.setAccessible(true);
field.set(block, value);
}
protected static <T> Optional<T> getFieldValueSafe(@NotNull Object instance, String fieldName) {
try {
return Optional.ofNullable(getFieldValue(instance, fieldName));
} catch (Exception ex) {
return Optional.empty();
}
}
protected static <T> T getFieldValue(@NotNull Object instance, String fieldName)
throws IllegalAccessException, NoSuchFieldException, ClassCastException, InaccessibleObjectException,
SecurityException {
Field field = instance.getClass().getField(fieldName);
field.setAccessible(true);
T obj = (T) field.get(instance);
return obj;
}
}
private static class NMSSpigotMappings extends NMSHelper {
// Tested in 1.19.4
private static final String FIELD_NAME_IGNITE_ODDS = "O";
private static final String FIELD_NAME_BURN_ODDS = "P";
private static final String FIELD_NAME_BLAST_RESISTANCE = "aH";
public NMSSpigotMappings() {
this(FIELD_NAME_BLAST_RESISTANCE, FIELD_NAME_BURN_ODDS, FIELD_NAME_IGNITE_ODDS);
}
protected NMSSpigotMappings(String blastResField, String burnOddsField, String igniteOddsField) {
super(blastResField, burnOddsField, igniteOddsField);
}
@Override
Class<?> getCraftMagicNumbersClass() throws ClassNotFoundException {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
String version = packageName.substring(packageName.lastIndexOf('.') + 1);
return Class.forName("org.bukkit.craftbukkit." + version + ".util.CraftMagicNumbers");
}
}
private static class NMSMojangMappings extends NMSHelper {
// Tested on 1.20.4 and 1.21
private static final String FIELD_NAME_IGNITE_ODDS = "igniteOdds";
private static final String FIELD_NAME_BURN_ODDS = "burnOdds";
private static final String FIELD_NAME_BLAST_RESISTANCE = "explosionResistance";
public NMSMojangMappings() {
this(FIELD_NAME_BLAST_RESISTANCE, FIELD_NAME_BURN_ODDS, FIELD_NAME_IGNITE_ODDS);
}
protected NMSMojangMappings(String blastResField, String burnOddsField, String igniteOddsField) {
super(blastResField, burnOddsField, igniteOddsField);
}
@Override
Class<?> getCraftMagicNumbersClass() throws ClassNotFoundException {
return Class.forName(Bukkit.getServer().getClass().getPackage().getName() + ".util.CraftMagicNumbers");
}
}
}