
Crashes due to trying to get a float value from config when it's actually double
Prunoideae opened this issue · 2 comments
Describe the bug
Crash due to config type casting
To Reproduce
Steps to reproduce the behavior:
- Have a duck with food
- Wait for a long time till it will eat food
- Crash
Expected behavior
Won't crash
Version
1.21.1 Neoforge 21.1.114
Error Log
https://gist.github.com/Prunoideae/5780a35a80de6ac3277d6ac9237d0245
Had the same issue and dug into the source code to potentially find a fix. My suggestion would be to edit this function at UntitledConfigImpl.java:
public static float foodHealingValue() {
try {
return FOOD_HEALING_VALUE.get().floatValue(); // Added explicit cast to float
} catch (Exception e) {
return 0.5F; // Default value to prevent crashes
}
}
This issue likely occurs because Forge's config system stores numerical values as Double by default, even when defining them as Float. Since defineInRange tries to enforce type safety but may still store the value as Double, the .get() method returns a Double instead of a Float, leading to a ClassCastException when used directly in foodHealingValue(). At least, that's my guess.
The issue originates from the Double parsing mechanism in Forge's configuration system.
Final solution:
ConfigValue<Double> → get().floatValue()
Thanks for providing the investigation direction.
Had the same issue and dug into the source code to potentially find a fix. My suggestion would be to edit this function at UntitledConfigImpl.java:
public static float foodHealingValue() { try { return FOOD_HEALING_VALUE.get().floatValue(); // Added explicit cast to float } catch (Exception e) { return 0.5F; // Default value to prevent crashes } }
This issue likely occurs because Forge's config system stores numerical values as Double by default, even when defining them as Float. Since defineInRange tries to enforce type safety but may still store the value as Double, the .get() method returns a Double instead of a Float, leading to a ClassCastException when used directly in foodHealingValue(). At least, that's my guess.