fix: ClassCastException for Color conversion in newer Java versions
This commit corrects the issue by converting the Color object to its RGB hex string representation.
Overview
Description: The previous implementation attempted to cast a java.awt.Color to a String, causing a ClassCastException in new Java versions (or even older ones, I didn't test them out)
[WARN]: [Server Thread] java.lang.ClassCastException: class java.awt.Color
cannot be cast to class java.lang.String (java.awt.Color is in module java.desktop of loader 'bootstrap';
java.lang.String is in module java.base of loader 'bootstrap')
Changes: Updated the code to correctly convert the Color object to its RGB hex string representation.
Code Example (If applicable):
} else if (Color.class.isAssignableFrom(clazz)) {
return valueBuilder.setStringValue((String) current).build();
}
Changed to:
} else if (Color.class.isAssignableFrom(clazz)) {
Color currentColor = (Color) current;
return valueBuilder.setStringValue(Integer.toHexString(currentColor.getRGB())).build();
}
Related Issue (If applicable):
Screenshots and/or Videos (If applicable):
Review Request Checklist
- [x] Your code follows the style guidelines of this project.
- [x] I have performed a self-review of my code.
- [x] I have tested this change myself. (If applicable)
- [ ] I have made corresponding changes to the documentation. (If applicable)
- [ ] The branch name follows the projects naming conventions. (e.g.
feature/add-module&bugfix/fix-issue)
Thanks to @AndyReckt for his help.
Your code fixes one issue but creates a new one. The code must support the Color object's usage and a String HEX.
This is the current master behavior:
options.set(ModMemory.LOW_MEM_COLOR, "#FFFFAA00"); // Works
options.set(ModMemory.LOW_MEM_COLOR, Color.RED); // Broken
After your changes:
options.set(ModMemory.LOW_MEM_COLOR, "#FFFFAA00"); // Broken
options.set(ModMemory.LOW_MEM_COLOR, Color.RED); // Works
~~Commit 6bcb0ef is untested, as I currently do not have an environment set up with java 8 to test it, I will test it this weekend.~~
Irrelevant due to recent changes
The commit 75df5dd fixes the issue. It has been tested for Java 17 and 8.