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 Rainbow, allow for more special named colors #23

Merged
merged 3 commits into from
Aug 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.teamresourceful.resourcefullib.common.utils.Scheduling;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextColor;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Locale;
Expand All @@ -24,16 +26,16 @@ public class Color {

protected static final Map<String, Color> colorsWithNames = new HashMap<>();

public static final Codec<Color> CODEC = Codec.PASSTHROUGH.comapFlatMap(Color::decodeColor, color -> new Dynamic<>(JsonOps.INSTANCE, new JsonPrimitive(color.value)));
public static final Codec<Color> CODEC = Codec.PASSTHROUGH.comapFlatMap(Color::decodeColor, color -> new Dynamic<>(JsonOps.INSTANCE, new JsonPrimitive(color.toString())));
public static final Color DEFAULT = defaultColor();
public static final Color RAINBOW = createRainbowColor();
public static final ByteCodec<Color> BYTE_CODEC = ByteCodec.BYTE.dispatch(aByte -> switch (aByte) {
case 0 -> ByteCodec.unit(DEFAULT);
case 1 -> ByteCodec.unit(RAINBOW);
case 1 -> ByteCodec.STRING.map(Color::parse, Color::toString);
default -> ByteCodec.INT.map(Color::new, Color::getValue);
}, color -> {
if (color.isDefault()) return (byte) 0;
if (color.isRainbow()) return (byte) 1;
if (color.isSpecial()) return (byte) 1;
return (byte) 2;
});

Expand All @@ -48,7 +50,9 @@ public class Color {
private int value;

private boolean defaultValue;
private boolean isRainbow;

@Nullable
private String specialName;

private float[] rgbaValue;

Expand Down Expand Up @@ -86,14 +90,15 @@ private static Color defaultColor() {

private static Color createRainbowColor() {
Color color = new Color(0xff0000);
color.isRainbow = true;
color.specialName = "rainbow";
colorsWithNames.put("rainbow", color);
return color;
}

public static Color createNamedColor(String name, int value) {
Color color = new Color(value);
colorsWithNames.putIfAbsent(name.toLowerCase(Locale.ENGLISH), color);
color.specialName = name.toLowerCase(Locale.ENGLISH);
colorsWithNames.putIfAbsent(color.specialName, color);
return color;
}

Expand Down Expand Up @@ -188,26 +193,35 @@ public boolean isDefault() {
return defaultValue;
}

/**
* @deprecated Use {@link #isSpecial()} instead.
*/
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "1.21.2")
public boolean isRainbow() {
return isRainbow;
return "rainbow".equals(specialName);
}

public boolean isSpecial() {
return specialName != null;
}

@Override
public String toString() {
if (this.isRainbow) return "rainbow";
if (specialName != null) return specialName;
return String.format("#%x", this.value);
}

@Override
public int hashCode() {
return Objects.hash(defaultValue, isRainbow, value);
return Objects.hash(defaultValue, specialName, value);
}

@Override
public boolean equals(Object obj) {
return obj instanceof Color color &&
color.value == this.value &&
color.isRainbow == this.isRainbow &&
color.specialName == this.specialName &&
color.defaultValue == this.defaultValue;
}

Expand Down
Loading