CraftBukkit/src/test/java/org/bukkit/craftbukkit/util/CraftChatMessageTest.java
Aikar 1b2830a3b3 SPIGOT-4441: Fix serializing Components to and from Legacy
While cfeef75cd97 might of semi helped being able to save black text
lore, it actually took a fundamental problem with the legacy serialization
code and expanded it to break even more aspects of the server when dealing
with Component to Legacy conversion.

This is causing data loss in Spigot with cases such as setting an item name
to white gets stripped resulting in it being italic.

Additionally, things such as book pages have been returning black formatting
codes for the end of the line even when the user doesn't have colors in the book.

The root issue is that the "Default Color" system is fundamentally wrong.

Components do not and should not care about what element of the game they
are being used by, and that's what the default color system did.

It results in components that if obtained from 1 source such as a Book
where the default / rendered color is black, is then copied to another
source such as an Entity name, the black is carried forward and shown
in the Entity name, when in reality it should have been white.

This commit reverts cfeef75cd97 and fixes the underlying serialization
issues when it comes to Legacy to and From conversions.

There was quite a number of issues with this code overall, in how
it handles inserting color codes, new line parsing and such.

Books was using mojangs own "getLegacyString" which doesn't match behavior.
We also do not want to use Mojangs method as there is no guarantee they don't
remove that in future.
Plus, everything about books uses the CB implementation anyways, and it should
be consistent (this was mandatory to avoid serialization format changes on old vs new)

These changes as is results in Item Stacks already serialized will not
change contents when they go to component and back, so this won't impact
any existing data.

Newly created books though for example will change behavior in that they
will no longer insert black color codes in the serialized data and will
only represent intentional color changes by the creator of the book.
This will result in cleaner data on them, and books are the only thing
I'm aware of that has a behavioral shift due to the likelyhood of the
default color system kicking in on other parts of the string.

A unit test has been added to verify integrity of serialization to
ensure that any legacy string that is converted into Components will
always re-encode back in the same way when going back to Legacy.
2020-06-01 19:19:42 +10:00

90 lines
3.8 KiB
Java

package org.bukkit.craftbukkit.util;
import net.minecraft.server.IChatBaseComponent;
import org.bukkit.support.AbstractTestingBase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CraftChatMessageTest extends AbstractTestingBase {
@Test
public void testSimpleStrings() {
// These should be able to go from legacy to comp to legacy back without data changing
testString("§fFoo");
testString("§fFoo§f§l"); // Keeps empty format at end
testString("Foo");
testString("§r§oFoo"); // Retains reset at start (item names can use this to get rid of italics)
testString("Foo§bBar");
testString("F§loo§b§oBa§b§lr"); // any non color formatting code implies previous color code.
// So §l at start has no inherited color code, so that's fine, but the one at the end,
// while Ba§l would work visually, serializing back will include the implied color
testString("F§loo§rBa§lr"); // But if reset was used before.... then it can be standalone
testString("§fFoo§bBar");
testString("§fFoo§bBar§rBaz");
}
@Test
public void testNewLineBehavior() {
// new line retain should stay as 1 comp
testString("Hello§0\n§rFoo\n§5Test", true);
testString("§0Foo!\n", true);
testString("§0Foo!§0\\n§0\\n§0Bar\n", true);
// dont retain line returns multiple components
IChatBaseComponent[] components = CraftChatMessage.fromString("Hello§0\n§rFoo\n§5Test");
assertEquals("Has 3 components", 3, components.length);
assertEquals("Hello§0", CraftChatMessage.fromComponent(components[0]));
assertEquals("§rFoo", CraftChatMessage.fromComponent(components[1]));
assertEquals("§5Test", CraftChatMessage.fromComponent(components[2]));
}
@Test
public void testComponents() {
testComponent("Foo§bBar§rBaz", create("Foo", "§bBar", "Baz"));
testComponent("§fFoo§bBar§rBaz", create("", "§fFoo", "§bBar", "Baz"));
testComponent("§fFoo§bBar§rBaz", create("", "§fFoo", "§bBar", "", "Baz"));
testComponent("§fFoo§bBar§rBaz", create("§fFoo", "§bBar", "Baz"));
testComponent("Foo§bBar§rBaz", create("", "Foo", "§bBar", "Baz"));
testComponent("§fFoo§bBar§rBaz", create("§fFoo", "§bBar", "Baz"));
testComponent("F§foo§bBar§rBaz", create("F§foo", "§bBar", "Baz"));
}
private IChatBaseComponent create(String txt, String ...rest) {
IChatBaseComponent cmp = CraftChatMessage.fromString(txt, false)[0];
for (String s : rest) {
cmp.addSibling(CraftChatMessage.fromString(s, true)[0]);
}
return cmp;
}
private void testString(String expected) {
testString(expected, false);
}
private void testString(String expected, boolean keepNewLines) {
testString(expected, expected, keepNewLines);
}
private void testString(String input, String expected) {
testString(input, expected, false);
}
private void testString(String input, String expected, boolean keepNewLines) {
IChatBaseComponent cmp = CraftChatMessage.fromString(input, keepNewLines)[0];
String actual = CraftChatMessage.fromComponent(cmp);
assertEquals("\nComponent: " + cmp + "\n", expected, actual);
}
private void testComponent(String expected, IChatBaseComponent cmp) {
String actual = CraftChatMessage.fromComponent(cmp);
assertEquals("\nComponent: " + cmp + "\n", expected, actual);
IChatBaseComponent expectedCmp = CraftChatMessage.fromString(expected, true)[0];
String actualExpectedCmp = CraftChatMessage.fromComponent(expectedCmp);
assertEquals("\nComponent: " + expectedCmp + "\n", expected, actualExpectedCmp);
}
}