Skip to content

Commit

Permalink
ColorManager: Fix colours being parsed as ints and not hex strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Aug 6, 2024
1 parent 77cde5b commit b56ae6d
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected void apply(Map<Identifier, List<JsonObject>> prepared, ResourceManager
for (var json : jsons) {
var partialPalette = PALETTE_CODEC.parse(JsonOps.INSTANCE, json).get()
.map(Function.identity(), partial -> {
LOGGER.error("[Adorn] Could not parse color palette {}", id);
LOGGER.error("[Adorn] Could not parse color palette {}: {}", id, partial.message());
return null;
});
if (partialPalette == null) continue;
Expand Down Expand Up @@ -106,13 +106,14 @@ private static DataResult<Integer> parseHexColor(String str) {
public record ColorPair(int bg, int fg) {
private static final int DEFAULT_FG = Colors.SCREEN_TEXT;

private static final Codec<Integer> COLOR_CODEC =
Codec.STRING.comapFlatMap(ColorManager::parseHexColor, color -> HexFormat.of().withUpperCase().toHexDigits(color));
public static final Codec<ColorPair> CODEC = Codecs.alternatively(
RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("bg").forGetter(ColorPair::bg),
Codec.INT.optionalFieldOf("fg", DEFAULT_FG).forGetter(ColorPair::bg)
COLOR_CODEC.fieldOf("bg").forGetter(ColorPair::bg),
COLOR_CODEC.optionalFieldOf("fg", DEFAULT_FG).forGetter(ColorPair::bg)
).apply(instance, ColorPair::new)),
Codec.STRING.comapFlatMap(ColorManager::parseHexColor, color -> HexFormat.of().withUpperCase().toHexDigits(color))
.xmap(bg -> new ColorPair(bg, DEFAULT_FG), ColorPair::bg)
COLOR_CODEC.xmap(bg -> new ColorPair(bg, DEFAULT_FG), ColorPair::bg)
);
}
}

0 comments on commit b56ae6d

Please sign in to comment.