Skip to content
Merged
Show file tree
Hide file tree
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
@@ -1,9 +1,10 @@
package vakiliner.chatcomponentapi.common;

import java.util.Map;
import java.util.Objects;
import com.google.common.collect.Maps;

public class ChatNamedColor extends ChatTextColor {
public final class ChatNamedColor extends ChatTextColor {
private static final Map<ChatTextFormat, ChatNamedColor> BY_FORMAT = Maps.newHashMap();
private static final Map<Integer, ChatNamedColor> BY_VALUE = Maps.newHashMap();
public static final ChatNamedColor BLACK = new ChatNamedColor(ChatTextFormat.BLACK, 0);
Expand All @@ -24,7 +25,7 @@ public class ChatNamedColor extends ChatTextColor {
public static final ChatNamedColor WHITE = new ChatNamedColor(ChatTextFormat.WHITE, 16777215);

private ChatNamedColor(ChatTextFormat format, int color) {
super(color, format);
super(color, Objects.requireNonNull(format));
if (this.asFormat.isFormat()) throw new IllegalArgumentException("ChatTextFormat cannot be a format");
BY_FORMAT.put(this.asFormat, this);
BY_VALUE.put(this.value, this);
Expand All @@ -34,6 +35,10 @@ public String getName() {
return this.asFormat.getName();
}

public ChatTextFormat asFormat(ChatTextFormat orElse) {
return this.asFormat;
}

public String toString() {
return this.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,26 @@ public int blue() {
return this.value & 0xFF;
}

public ChatTextFormat asFormat() {
return this.asFormat;
}

public ChatTextFormat asFormat(ChatTextFormat orElse) {
ChatTextFormat format = this.asFormat();
return format != null ? format : orElse;
}

public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (!(obj instanceof ChatTextColor)) {
return false;
} else {
ChatTextColor other = (ChatTextColor) obj;
return this.value == other.value;
return this.value == other.value && this.asFormat == other.asFormat;
}
}

public ChatTextFormat asFormat() {
return this.asFormat;
}

public String toString() {
return String.format("#%06X", this.value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public static ChatTextFormat getByName(String name) {
return BY_NAME.get(name);
}

public static ChatTextFormat getFromColor(ChatTextColor color) {
return getFromColor(color, RESET);
}

public static ChatTextFormat getFromColor(ChatTextColor color, ChatTextFormat def) {
return color != null ? color.asFormat(def) : def;
}

static {
for (ChatTextFormat color : values()) {
BY_CHAR.put(color.code, color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ protected String toLegacyText(final ChatTextColor parentColor, final Set<ChatCom
}
formats = Collections.unmodifiableSet(formats);
if (reset) {
ChatTextFormat textColor = color != null ? color.asFormat() : ChatTextFormat.RESET;
text.append(textColor != null ? textColor : ChatTextFormat.RESET);
text.append(ChatTextFormat.getFromColor(color));
for (ChatComponentFormat format : formats) {
text.append(format.asTextFormat());
}
Expand All @@ -102,7 +101,7 @@ protected String toLegacyText(final ChatTextColor parentColor, final Set<ChatCom
}
}
if (reset) {
text.append(parentColor != null ? parentColor.asFormat() : ChatTextFormat.RESET);
text.append(ChatTextFormat.getFromColor(color));
for (ChatComponentFormat format : parentFormats) {
text.append(format);
}
Expand Down
Loading