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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
testImplementation(libs.cyano)
testImplementation(libs.adventure)
testImplementation(libs.junit.api)
testImplementation(libs.junit.params)
testImplementation(libs.junit.platform.launcher)
testImplementation(libs.mockito.core)
testImplementation(libs.mockito.junit)
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencyResolutionManagement {
library("mockito.core", "org.mockito", "mockito-core").withoutVersion()
library("mockito.junit", "org.mockito", "mockito-junit-jupiter").withoutVersion()
library("junit.platform.launcher", "org.junit.platform", "junit-platform-launcher").withoutVersion()
library("junit.params", "org.junit.jupiter", "junit-jupiter-params").withoutVersion()
}
}
}
43 changes: 36 additions & 7 deletions src/main/java/net/theevilreaper/xerus/api/ColorData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.color.Color;
import net.minestom.server.color.DyeColor;
import net.minestom.server.color.TeamColor;
import net.minestom.server.item.Material;

import java.util.EnumMap;
import java.util.Map;

/**
* Represents a mapping of visual color variants using text color, dye color, block material, and a string identifier.
* Each constant aligns with a specific base color and can be used to harmonize visual elements across different systems.
*
* @author theEvilReaper
* @version 1.6.0
* @version 1.7.0
* @since 1.0.0
*/
@SuppressWarnings("java:S3252")
Expand Down Expand Up @@ -40,14 +44,14 @@ public enum ColorData {
/** Aligns with the dark gray color. */
DARK_GRAY(NamedTextColor.DARK_GRAY, DyeColor.GRAY, Material.GRAY_WOOL, "colorDarkGray"),

/** Aligns with the green color. */
GREEN(NamedTextColor.DARK_GREEN, DyeColor.GREEN, Material.GREEN_WOOL, "colorGreen"),
/** Aligns with the dark green color. */
DARK_GREEN(NamedTextColor.DARK_GREEN, DyeColor.GREEN, Material.GREEN_WOOL, "colorGreen"),

/** Aligns with the light green color. */
LIGHT_GREEN(NamedTextColor.GREEN, DyeColor.LIME, Material.LIME_WOOL, "colorLime"),
/** Aligns with the green color. */
GREEN(NamedTextColor.GREEN, DyeColor.LIME, Material.LIME_WOOL, "colorLime"),

/** Aligns with the purple color. */
PURPLE(NamedTextColor.DARK_PURPLE, DyeColor.PURPLE, Material.PURPLE_WOOL, "colorPurple"),
/** Aligns with the dark purple color. */
DARK_PURPLE(NamedTextColor.DARK_PURPLE, DyeColor.PURPLE, Material.PURPLE_WOOL, "colorPurple"),

/** Aligns with the light purple color. */
LIGHT_PURPLE(NamedTextColor.LIGHT_PURPLE, DyeColor.MAGENTA, Material.MAGENTA_WOOL, "colorMagenta"),
Expand All @@ -67,6 +71,20 @@ public enum ColorData {
//Reduce defensive copies from the array, because values() returns each call a new array!
private static final ColorData[] VALUES = values();

private static final Map<ColorData, TeamColor> TEAM_COLOR_CACHE = new EnumMap<>(ColorData.class);

static {
for (ColorData colorData : VALUES) {
TeamColor mapped = TeamColor.fromName(colorData.chatColor.toString());
if (mapped == null) {
throw new ExceptionInInitializerError(
"No TeamColor mapping found for ColorData " + colorData.name()
);
}
TEAM_COLOR_CACHE.put(colorData, mapped);
}
}

private final NamedTextColor chatColor;
private final DyeColor dyeColor;
private final Material material;
Expand Down Expand Up @@ -132,6 +150,17 @@ public Color getColor() {
return this.dyeColor.color();
}

/**
* Returns the matching {@link TeamColor} for this entry, resolved via the underlying
* {@link NamedTextColor} rather than the enum name to avoid mismatches
* (e.g. this enum's {@code DARK_GREEN} maps to {@link NamedTextColor#DARK_GREEN}).
*
* @return the underlying teamColor
*/
public TeamColor toTeamColor() {
return TEAM_COLOR_CACHE.get(this);
}

/**
* Returns an array which contains all given color data values.
* Please use this method because it reduces defensive copies from the array,
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/net/theevilreaper/xerus/api/ColorDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.color.Color;
import net.minestom.server.color.DyeColor;
import net.minestom.server.color.TeamColor;
import net.minestom.server.item.Material;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -26,4 +29,16 @@ void testAquaData() {
void testLength() {
assertSame(16, ColorData.getValues().length);
}

@ParameterizedTest
@EnumSource(ColorData.class)
void testToTeamColorMatchesChatColor(ColorData colorData) {
TeamColor teamColor = colorData.toTeamColor();
NamedTextColor chatColor = colorData.getChatColor();

assertNotNull(teamColor);
assertEquals(chatColor.red(), teamColor.red());
assertEquals(chatColor.green(), teamColor.green());
assertEquals(chatColor.blue(), teamColor.blue());
}
}
Loading