-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add shadow tag to minimessage, closes #1120
- Loading branch information
1 parent
df01801
commit bc0ef7a
Showing
3 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...c/main/java/net/kyori/adventure/text/minimessage/tag/standard/ShadowColorTagResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...essage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/ShadowTagTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |