Skip to content

Commit

Permalink
shadow color: tidy up tag implementation, add !shadow tag
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Dec 8, 2024
1 parent 2e7827f commit 0f7f2f8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,28 @@
import net.kyori.adventure.text.minimessage.ParsingException;
import net.kyori.adventure.text.minimessage.internal.serializer.SerializableResolver;
import net.kyori.adventure.text.minimessage.internal.serializer.StyleClaim;
import net.kyori.adventure.text.minimessage.internal.serializer.TokenEmitter;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

class ShadowColorTagResolver implements TagResolver, SerializableResolver.Single {
final class ShadowColorTag {
private static final String SHADOW_COLOR = "shadow";
private static final String SHADOW_NONE = "!" + SHADOW_COLOR;
private static final float DEFAULT_ALPHA = 0.25f;

static final TagResolver INSTANCE = new ShadowColorTagResolver();
private static final StyleClaim<ShadowColor> STYLE = StyleClaim.claim(SHADOW_COLOR, Style::shadowColor, (color, emitter) -> {
emitter.tag(SHADOW_COLOR);

final @Nullable NamedTextColor possibleMatch = NamedTextColor.namedColor(TextColor.color(color).value());
if (possibleMatch != null) {
emitter.argument(NamedTextColor.NAMES.key(possibleMatch)).argument(Float.toString((float) color.alpha() / 0xff));
} else {
emitter.argument(color.asHexString());
}
});

ShadowColorTagResolver() {
}

@Override
public @Nullable Tag resolve(final @NotNull String name, final @NotNull ArgumentQueue args, final @NotNull Context ctx) throws ParsingException {
if (!this.has(name)) {
return null;
}
static final TagResolver RESOLVER = TagResolver.resolver(
SerializableResolver.claimingStyle(
SHADOW_COLOR,
ShadowColorTag::create,
StyleClaim.claim(SHADOW_COLOR, Style::shadowColor, ShadowColorTag::emit)
),
TagResolver.resolver(SHADOW_NONE, Tag.styling(ShadowColor.none()))
);

static Tag create(final @NotNull ArgumentQueue args, final @NotNull Context ctx) throws ParsingException {
final String colorString = args.popOr("Expected to find a color parameter: #RRGGBBAA").lowerValue();
final ShadowColor color;
if (colorString.startsWith(TextColor.HEX_PREFIX) && colorString.length() == 9) {
Expand All @@ -78,13 +69,22 @@ class ShadowColorTagResolver implements TagResolver, SerializableResolver.Single
return Tag.styling(color);
}

@Override
public boolean has(final @NotNull String name) {
return name.equals(SHADOW_COLOR);
static void emit(final @NotNull ShadowColor color, final @NotNull TokenEmitter emitter) {
if (ShadowColor.none().equals(color)) {
emitter.tag(SHADOW_NONE);
return;
}

emitter.tag(SHADOW_COLOR);

final @Nullable NamedTextColor possibleMatch = NamedTextColor.namedColor(TextColor.color(color).value());
if (possibleMatch != null) {
emitter.argument(NamedTextColor.NAMES.key(possibleMatch)).argument(Float.toString((float) color.alpha() / 0xff));
} else {
emitter.argument(color.asHexString());
}
}

@Override
public @Nullable StyleClaim<?> claimStyle() {
return STYLE;
private ShadowColorTag() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private StandardTags() {
ScoreTag.RESOLVER,
NbtTag.RESOLVER,
PrideTag.RESOLVER,
ShadowColorTagResolver.INSTANCE
ShadowColorTag.RESOLVER
)
.build();

Expand Down Expand Up @@ -277,15 +277,15 @@ public static TagResolver transition() {
}

/**
* Get a resolver for the {@value ShadowColorTagResolver#SHADOW_COLOR} tags.
* Get a resolver for the {@value ShadowColorTag#SHADOW_COLOR} tags.
*
* <p>This tag support both hex string</p>
*
* @return a resolver for the {@value ShadowColorTagResolver#SHADOW_COLOR} tags
* @return a resolver for the {@value ShadowColorTag#SHADOW_COLOR} tags
* @since 4.10.0
*/
public static @NotNull TagResolver shadowColor() {
return ShadowColorTagResolver.INSTANCE;
return ShadowColorTag.RESOLVER;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,28 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.minimessage.AbstractTest;
import org.junit.jupiter.api.Test;

class ShadowTagTest extends AbstractTest {
import static net.kyori.adventure.text.format.ShadowColor.shadowColor;

class ShadowColorTagTest extends AbstractTest {
@Test
void testNoShadow() {
final String input = "now i'm here <!shadow>and now i'm not!";
final Component expected = Component.text("now i'm here ")
.append(Component.text("and now i'm not!", Style.style(ShadowColor.none())));

this.assertParsedEquals(expected, input);
this.assertSerializedEquals(input, expected);
}

@Test
void testRoundtripNamedShadow() {
final String input = "<shadow:red:0.8>i have a red shadow";
final Component expected = Component.text("i have a red shadow")
.shadowColor(ShadowColor.shadowColor(NamedTextColor.RED, 0xCC));
.shadowColor(shadowColor(NamedTextColor.RED, 0xCC));

this.assertParsedEquals(expected, input);
this.assertSerializedEquals(input, expected);
Expand All @@ -44,7 +57,7 @@ void testRoundtripNamedShadow() {
void testParseHexComponentShadow() {
final String input = "<shadow:#FF0000:0.8>i have a redder shadow";
final Component expected = Component.text("i have a redder shadow")
.shadowColor(ShadowColor.shadowColor(0xFF, 0, 0, 0xCC));
.shadowColor(shadowColor(0xFF, 0, 0, 0xCC));

this.assertParsedEquals(expected, input);
}
Expand All @@ -54,7 +67,7 @@ void testSerializeShadow() {
final String expected = "<shadow:#054D79FF>This is a test";

final Component builder = Component.text("This is a test")
.shadowColor(ShadowColor.shadowColor(0xff_05_4d_79));
.shadowColor(shadowColor(0xff_05_4d_79));

this.assertSerializedEquals(expected, builder);
this.assertParsedEquals(builder, expected);
Expand All @@ -66,7 +79,7 @@ void testSerializeShadowClosing() {

final Component builder = Component.text()
.append(Component.text("This is a")
.shadowColor(ShadowColor.shadowColor(0xff_05_4d_79)))
.shadowColor(shadowColor(0xff_05_4d_79)))
.append(Component.text(" test")).asComponent();

this.assertSerializedEquals(expected, builder);
Expand Down

0 comments on commit 0f7f2f8

Please sign in to comment.