Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.internal.Constants;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent;
import com.sk89q.worldedit.util.formatting.text.event.HoverEvent;
import com.sk89q.worldedit.util.formatting.text.format.Style;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
import com.sk89q.worldedit.util.formatting.text.format.TextDecoration;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Type;

Expand All @@ -49,6 +51,8 @@
public class StyleSerializer implements JsonDeserializer<Style>, JsonSerializer<Style> {
public static final StyleSerializer INSTANCE = new StyleSerializer();

private static final Logger LOGGER = LogManagerCompat.getLogger();

private static final TextDecoration[] DECORATIONS = TextDecoration.values();

static final boolean PRE_1215;
Expand Down Expand Up @@ -87,12 +91,21 @@ private Style deserialize(final JsonObject json, final JsonDeserializationContex
final Style.Builder style = Style.builder();

if (json.has(COLOR)) {
final TextColorWrapper color = context.deserialize(json.get(COLOR), TextColorWrapper.class);
if (color.color != null) {
style.color(color.color);
} else if (color.decoration != null) {
// I know. Setting a decoration from the color is weird. This is, unfortunately, something we need to support.
style.decoration(color.decoration, true);
TextColorWrapper color = null;
try {
color = context.deserialize(json.get(COLOR), TextColorWrapper.class);
} catch (JsonParseException e) {
// Vanilla text components may carry hex colors (#RRGGBB) since 1.16, text3 cannot represent them.
// Drop the color instead of failing to parse the whole component.
Comment thread
octylFractal marked this conversation as resolved.
LOGGER.debug("Failed to parse component color, dropping it", e);
}
if (color != null) {
if (color.color != null) {
style.color(color.color);
} else if (color.decoration != null) {
// I know. Setting a decoration from the color is weird. This is, unfortunately, something we need to support.
style.decoration(color.decoration, true);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.util.formatting.text.serializer.gson;
Comment thread
octylFractal marked this conversation as resolved.

import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class StyleSerializerTest {

@Test
void hexColorIsDroppedInsteadOfFailing() {
// Vanilla item names may carry hex colors ("#RRGGBB") since 1.16, text3 cannot represent them.
Component hexOnly = GsonComponentSerializer.INSTANCE.deserialize(
"{\"text\":\"Gold item\",\"color\":\"#FFD700\"}"
);
assertEquals("Gold item", ((TextComponent) hexOnly).content());
assertNull(hexOnly.color());
}

@Test
void namedColorStillParsed() {
Component component = GsonComponentSerializer.INSTANCE.deserialize(
"{\"text\":\"Gold item\",\"color\":\"gold\"}"
);
assertEquals(TextColor.GOLD, component.color());
}
}
Loading