LunarClient / Apollo

Next-generation Lunar Client server API
https://lunarclient.dev
MIT License
92 stars 18 forks source link

[Bug Report] ClassCastException When Using Color in Configuration with OptionsImpl #168

Closed 5RoD closed 2 months ago

5RoD commented 2 months ago

Checklist before creating an issue:

Issue Description

I'm encountering a ClassCastException when attempting to use Java's Color class within the configuration system provided by Apollo. The error occurs specifically when trying to serialize and deserialize Color objects.

When you try to use any rgba colors like this: background-color: "#FFFFCC99" border-color: "#FF99FFFF"

Reproduction steps

Steps to Reproduce:

Configure an option to use java.awt.Color in the Apollo configuration.
Attempt to serialize or deserialize this configuration.
Expected Behavior:
Color objects should be serialized to a String format (e.g., "#AARRGGBB") and deserialized back to a Color object without issues.

Actual Behavior:
A ClassCastException is thrown with the following stack trace:

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')
    at com.lunarclient.apollo.option.OptionsImpl.wrapValue(OptionsImpl.java:276)
    at com.lunarclient.apollo.option.OptionsImpl.postPacket(OptionsImpl.java:342)
    ...

Details:
The issue seems to stem from the OptionsImpl class's wrapValue method, where Color objects are being cast incorrectly to String. The relevant part of the code is:

else if (Color.class.isAssignableFrom(clazz)) {
    return valueBuilder.setStringValue((String) current).build(); // This causes ClassCastException
}

Suggested Fix:
Modify the wrapValue and unwrapValue methods to correctly handle Color serialization and deserialization by converting Color to and from its string representation using the format #AARRGGBB.

Code Example:

else if (Color.class.isAssignableFrom(clazz)) {
    String colorString = String.format("#%08X", ((Color) current).getRGB());
    return valueBuilder.setStringValue(colorString).build();
}
and similarly for unwrapValue:

else if (Color.class.isAssignableFrom(clazz) && wrapper.hasStringValue()) {
    String colorString = wrapper.getStringValue();
    // Logic to convert the string to Color
}

Code Snippet (If applicable, if not write N/A)

N/A

Versions

1.8

Platforms

BungeeCord

Server Version

1.8

Modules

Optifine

Screenshots

https://prnt.sc/-1gqeUR_t9tL
https://prnt.sc/9AzGT-w2wp_3

Contact Information

Email: 5rod@gravemc.net

Additional Context (Examples, Links, ETC)

No response

ItsNature commented 2 months ago

Hello, this was fixed a few days ago with #161, the 1.1.5 update should be out soon.

5RoD commented 2 months ago

Hello, this was fixed a few days ago with #161, the 1.1.5 update should be out soon.

alrighty thank you!