diff --git a/src/main/java/net/minestom/server/color/Color.java b/src/main/java/net/minestom/server/color/Color.java index fcee18a31..f22620db6 100644 --- a/src/main/java/net/minestom/server/color/Color.java +++ b/src/main/java/net/minestom/server/color/Color.java @@ -1,8 +1,10 @@ package net.minestom.server.color; import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.util.RGBLike; import net.minestom.server.chat.ChatColor; import org.apache.commons.lang3.Validate; +import org.checkerframework.common.value.qual.IntRange; import org.jetbrains.annotations.NotNull; import java.util.Objects; @@ -10,7 +12,7 @@ import java.util.Objects; /** * A general purpose class for representing colors. */ -public class Color { +public class Color implements RGBLike { private static final int BIT_MASK = 0xff; private int red, green, blue; @@ -202,4 +204,19 @@ public class Color { public int hashCode() { return Objects.hash(red, green, blue); } + + @Override + public int red() { + return this.red; + } + + @Override + public int green() { + return this.green; + } + + @Override + public int blue() { + return this.blue; + } } diff --git a/src/main/java/net/minestom/server/color/DyeColor.java b/src/main/java/net/minestom/server/color/DyeColor.java index 835326837..56e7cd51c 100644 --- a/src/main/java/net/minestom/server/color/DyeColor.java +++ b/src/main/java/net/minestom/server/color/DyeColor.java @@ -1,11 +1,13 @@ package net.minestom.server.color; +import net.kyori.adventure.util.RGBLike; +import org.checkerframework.common.value.qual.IntRange; import org.jetbrains.annotations.NotNull; /** * Color values for dyes, wool and cloth items. */ -public enum DyeColor { +public enum DyeColor implements RGBLike { WHITE(new Color(0xF9FFFE), new Color(0xF0F0F0)), ORANGE(new Color(0xF9801D), new Color(0xEB8844)), MAGENTA(new Color(0xC74EBD), new Color(0xC354CD)), @@ -50,4 +52,19 @@ public enum DyeColor { public Color getFireworkColor() { return this.firework; } + + @Override + public int red() { + return this.color.red(); + } + + @Override + public int green() { + return this.color.green(); + } + + @Override + public int blue() { + return this.color.blue(); + } } diff --git a/src/main/java/net/minestom/server/color/TeamFormat.java b/src/main/java/net/minestom/server/color/TeamFormat.java new file mode 100644 index 000000000..c8991c494 --- /dev/null +++ b/src/main/java/net/minestom/server/color/TeamFormat.java @@ -0,0 +1,133 @@ +package net.minestom.server.color; + +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.text.format.TextDecoration; +import net.kyori.adventure.util.RGBLike; +import org.jetbrains.annotations.Nullable; + +/** + * The format used for teams. Note that this is often referred to as "team color". This + * is misleading as teams can also use text decoration like bold, italics, etc. + */ +public enum TeamFormat implements RGBLike { + BLACK(NamedTextColor.BLACK), + DARK_BLUE(NamedTextColor.DARK_BLUE), + DARK_GREEN(NamedTextColor.DARK_GREEN), + DARK_AQUA(NamedTextColor.DARK_AQUA), + DARK_RED(NamedTextColor.DARK_RED), + DARK_PURPLE(NamedTextColor.DARK_PURPLE), + GOLD(NamedTextColor.GOLD), + GRAY(NamedTextColor.GRAY), + DARK_GRAY(NamedTextColor.DARK_GRAY), + BLUE(NamedTextColor.BLUE), + GREEN(NamedTextColor.GREEN), + AQUA(NamedTextColor.AQUA), + RED(NamedTextColor.RED), + LIGHT_PURPLE(NamedTextColor.LIGHT_PURPLE), + YELLOW(NamedTextColor.YELLOW), + WHITE(NamedTextColor.WHITE), + OBFUSCATED(TextDecoration.OBFUSCATED), + BOLD(TextDecoration.BOLD), + STRIKETHROUGH(TextDecoration.STRIKETHROUGH), + UNDERLINE(TextDecoration.UNDERLINED), + ITALIC(TextDecoration.ITALIC), + RESET(null, null); + + private final TextDecoration decoration; + private final TextColor color; + + TeamFormat(TextDecoration decoration) { + this(decoration, null); + } + + TeamFormat(TextColor color) { + this(null, color); + } + + TeamFormat(TextDecoration decoration, TextColor color) { + this.decoration = decoration; + this.color = color; + } + + /** + * Checks if this team color is a color. + * + * @return if it is a color + */ + public boolean isColor() { + return this.color != null; + } + + /** + * Gets the text color equivalent to this team color, if any. + * + * @return the text color + */ + public @Nullable TextColor getTextColor() { + return this.color; + } + + /** + * Gets the color equivalent to this team color, if any. + * + * @return the color + */ + public @Nullable Color getColor() { + return this.color == null ? null : new Color(this.color); + } + + /** + * Checks if this team color is a text decoration. + * + * @return if it is a decoration + */ + public boolean isDecoration() { + return this.decoration != null; + } + + /** + * Gets the text decoration equivalent to this team color, if any. + * + * @return the decoration + */ + public @Nullable TextDecoration getDecoration() { + return this.decoration; + } + + /** + * @throws IllegalStateException if this team format is not a color + */ + @Override + public int red() { + if (!this.isColor()) { + throw new IllegalStateException("This TeamFormat does not represent a color"); + } + + return this.color.red(); + } + + /** + * @throws IllegalStateException if this team format is not a color + */ + @Override + public int green() { + if (!this.isColor()) { + throw new IllegalStateException("This TeamFormat does not represent a color"); + } + + return this.color.green(); + } + + /** + * @throws IllegalStateException if this team format is not a color + */ + @Override + public int blue() { + if (!this.isColor()) { + throw new IllegalStateException("This TeamFormat does not represent a color"); + } + + return this.color.blue(); + } +}