Skip to content

Commit

Permalink
feat: add shadow tag to minimessage, closes #1120
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Nov 1, 2024
1 parent df01801 commit bc0ef7a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.kyori.adventure.text.minimessage.tag.standard;

import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.minimessage.Context;
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.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 {
private static final String SHADOW_COLOR = "shadow";

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

final String colorString = args.popOr("Expected to find a color parameter: #RRGGBBAA").lowerValue();
final ShadowColor color = ShadowColor.fromHexString(colorString);
if (color == null) {
throw ctx.newException(String.format("Unable to parse a shadow color from '%s'. Please use #RRGGBBAA formatting.", colorString));
}
return Tag.styling(color);
}

@Override
public boolean has(final @NotNull String name) {
return name.equals(SHADOW_COLOR);
}

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

Expand Down Expand Up @@ -275,6 +276,18 @@ public static TagResolver transition() {
return PrideTag.RESOLVER;
}

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

/**
* Get a resolver that handles all default standard tags.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.kyori.adventure.text.minimessage.tag.standard;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.minimessage.AbstractTest;
import org.junit.jupiter.api.Test;

class ShadowTagTest extends AbstractTest {

@Test
void testSerializeShadow() {
final String expected = "<shadow:#054D79FF>This is a test";

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

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

@Test
void testSerializeShadowClosing() {
final String expected = "<shadow:#054D79FF>This is a</shadow> test";

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

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

0 comments on commit bc0ef7a

Please sign in to comment.