Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ClassCastException for Color conversion in newer Java versions #161

Merged
merged 3 commits into from
Aug 28, 2024

Conversation

Rhodless
Copy link

@Rhodless Rhodless commented Aug 8, 2024

This commit corrects the issue by converting the Color object to its RGB hex string representation. The problem was that attempting to cast a java.awt.Color object to a String caused a ClassCastException in newer Java versions. The fix ensures that color values can be set using either HEX strings or Color objects without causing exceptions.

Overview

Description:

This pull request resolves the issue where attempting to cast a java.awt.Color object to a String caused a ClassCastException in newer Java versions. The problem arose when handling color values that could be either HEX strings or Color objects.

Changes:

The code has been updated to properly handle both String and Color inputs when setting color values. Specifically:

  • If the current value is a String, it is used directly.
  • If the current value is a Color object, it is converted to its RGB hex string representation using Integer.toHexString(currentColor.getRGB()) before being set.

This change ensures compatibility with both HEX string and Color object inputs, allowing the system to handle either type without causing exceptions.

Code Example:

The new implementation:

if (current instanceof String) {
    String string = (String) current;
    return valueBuilder.setStringValue(string).build();
} else {
    Color currentColor = (Color) current;
    return valueBuilder.setStringValue(Integer.toHexString(currentColor.getRGB())).build();
}

This logic ensures that color values are consistently stored as strings, whether they originate as HEX strings or Color objects.

Related Issue (If applicable):

Screenshots and/or Videos (If applicable):


Review Request Checklist

  • Your code follows the style guidelines of this project.
  • I have performed a self-review of my code.
  • 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.

This commit corrects the issue by converting the Color object to its RGB hex string representation.
@TrentinTheKid TrentinTheKid added type: Bug Something is broken :( status: Pending Issue is waiting to be verified as still active labels Aug 13, 2024
@ItsNature ItsNature changed the base branch from master to version/1.1.5 August 16, 2024 11:46
@ItsNature ItsNature added status: Accepted An issue that is going to be fixed and removed status: Pending Issue is waiting to be verified as still active labels Aug 16, 2024
@ItsNature
Copy link
Collaborator

ItsNature commented Aug 16, 2024

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

@AndyReckt
Copy link

AndyReckt commented Aug 16, 2024

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

…pport

This commit addresses the regression introduced in the previous fix that enabled Color object usage but broke HEX string compatibility. The code has been adjusted to ensure that both HEX strings (e.g., "#FFFFAA00") and Color objects (e.g., Color.RED) can be used interchangeably when setting color values in options.set(). This fix ensures backward compatibility with the previous behavior while retaining the ability to use Color objects without causing a ClassCastException.
@Rhodless
Copy link
Author

The commit 75df5dd fixes the issue. It has been tested for Java 17 and 8.

@ItsNature ItsNature merged commit 2324727 into LunarClient:version/1.1.5 Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: Accepted An issue that is going to be fixed type: Bug Something is broken :(
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants