diff --git a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/GsonComponentSerializerImpl.java b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/GsonComponentSerializerImpl.java index 3cc4dea58..d31f97d83 100644 --- a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/GsonComponentSerializerImpl.java +++ b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/GsonComponentSerializerImpl.java @@ -66,7 +66,10 @@ static final class Instances { builder.registerTypeAdapterFactory(new SerializerFactory(downsampleColor, legacyHoverSerializer, emitLegacyHover)); return builder; }; - this.serializer = this.populator.apply(new GsonBuilder()).create(); + this.serializer = this.populator.apply( + new GsonBuilder() + .disableHtmlEscaping() // to be consistent with vanilla + ).create(); } @Override diff --git a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextColorSerializer.java b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextColorSerializer.java index e75ad21f9..f6e9407b4 100644 --- a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextColorSerializer.java +++ b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextColorSerializer.java @@ -27,6 +27,7 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import java.io.IOException; +import java.util.Locale; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextColor; import org.jetbrains.annotations.NotNull; @@ -49,10 +50,14 @@ public void write(final JsonWriter out, final TextColor value) throws IOExceptio } else if (this.downsampleColor) { out.value(NamedTextColor.NAMES.key(NamedTextColor.nearestTo(value))); } else { - out.value(value.asHexString()); + out.value(asUpperCaseHexString(value)); } } + private static String asUpperCaseHexString(final TextColor color) { + return String.format(Locale.ROOT, "#%06X", color.value()); // to be consistent with vanilla + } + @Override public @Nullable TextColor read(final JsonReader in) throws IOException { final @Nullable TextColor color = fromString(in.nextString()); diff --git a/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java b/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java new file mode 100644 index 000000000..42ed4436d --- /dev/null +++ b/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java @@ -0,0 +1,39 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.serializer.gson; + +import net.kyori.adventure.text.Component; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class Issue788Test { + @Test + void test() { + final String expected = "{\"extra\":[{\"color\":\"#FF00FF\",\"text\":\"PREPEND>\"},{\"text\":\"/sign test\"},{\"color\":\"#FF00FF\",\"text\":\" json.addProperty(StyleSerializer.COLOR, "#0a1ab9"))); + this.test(Style.style(TextColor.color(0x0a1ab9)), object(json -> json.addProperty(StyleSerializer.COLOR, "#0A1AB9"))); } @Test @@ -159,7 +159,7 @@ void testShowEntityHoverEvent() { contents.addProperty(ShowEntitySerializer.ID, dolores.toString()); contents.add(ShowEntitySerializer.NAME, object(name -> { name.addProperty(ComponentSerializerImpl.TEXT, "Dolores"); - name.addProperty(StyleSerializer.COLOR, "#0a1ab9"); + name.addProperty(StyleSerializer.COLOR, "#0A1AB9"); })); })); }));