From 004cb65ad7231469d38a0eeacd3a320a736e5fdb Mon Sep 17 00:00:00 2001 From: zml Date: Sun, 25 Feb 2024 09:06:31 -0800 Subject: [PATCH] fix(text-minimessage): Handle larger raninbow phases correctly Fixes GH-1040 --- .../minimessage/tag/standard/RainbowTag.java | 12 +++++------ .../tag/standard/RainbowTagTest.java | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/RainbowTag.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/RainbowTag.java index 101f01f63..769db1b75 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/RainbowTag.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/RainbowTag.java @@ -47,7 +47,7 @@ final class RainbowTag extends AbstractColorChangingTag { static final TagResolver RESOLVER = TagResolver.resolver(RAINBOW, RainbowTag::create); private final boolean reversed; - private final int phase; + private final double dividedPhase; private int colorIndex = 0; @@ -75,7 +75,7 @@ static Tag create(final ArgumentQueue args, final Context ctx) { private RainbowTag(final boolean reversed, final int phase) { this.reversed = reversed; - this.phase = phase; + this.dividedPhase = ((double) phase) / 10d; } @Override @@ -101,13 +101,13 @@ protected void advanceColor() { @Override protected TextColor color() { final float index = this.colorIndex; - final float hue = (index / this.size() + this.phase / 10f) % 1; + final float hue = (float) ((index / this.size() + this.dividedPhase) % 1f); return TextColor.color(HSVLike.hsvLike(hue, 1f, 1f)); } @Override public @NotNull Stream examinableProperties() { - return Stream.of(ExaminableProperty.of("phase", this.phase)); + return Stream.of(ExaminableProperty.of("phase", this.dividedPhase)); } @Override @@ -115,11 +115,11 @@ public boolean equals(final @Nullable Object other) { if (this == other) return true; if (other == null || this.getClass() != other.getClass()) return false; final RainbowTag that = (RainbowTag) other; - return this.colorIndex == that.colorIndex && this.phase == that.phase; + return this.colorIndex == that.colorIndex && this.dividedPhase == that.dividedPhase; } @Override public int hashCode() { - return Objects.hash(this.colorIndex, this.phase); + return Objects.hash(this.colorIndex, this.dividedPhase); } } diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/RainbowTagTest.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/RainbowTagTest.java index 36657bed0..9f304b749 100644 --- a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/RainbowTagTest.java +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/RainbowTagTest.java @@ -30,6 +30,7 @@ import static net.kyori.adventure.text.Component.empty; import static net.kyori.adventure.text.Component.text; +import static net.kyori.adventure.text.Component.textOfChildren; import static net.kyori.adventure.text.event.ClickEvent.openUrl; import static net.kyori.adventure.text.format.NamedTextColor.BLACK; import static net.kyori.adventure.text.format.NamedTextColor.GREEN; @@ -352,4 +353,24 @@ void gh147() { final Component expected1 = text().append(text("y", color(0xff0000)), text("o", color(0x00ffff))).build(); this.assertParsedEquals(expected1, input, component("msg", text("yo"))); } + + // https://github.com/KyoriPowered/adventure/issues/1040 + @Test + void gh1040() { + final String input = "||||||||||"; + final Component expected = textOfChildren( + text("|", color(0x00ffff)), + text("|", color(0x0065ff)), + text("|", color(0x3200ff)), + text("|", color(0xcc00ff)), + text("|", color(0xff0099)), + text("|", color(0xff0000)), + text("|", color(0xff9900)), + text("|", color(0xccff00)), + text("|", color(0x32ff00)), + text("|", color(0x00ff65)) + ); + + this.assertParsedEquals(expected, input); + } }