bring back raw text component creation (#1612)

This commit is contained in:
Pasqual Koschmieder 2022-06-08 19:33:07 +02:00 committed by GitHub
parent a0a5469988
commit 4cc3957723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 17 deletions

View File

@ -1,8 +1,8 @@
package com.comphenix.protocol.wrappers;
import com.google.gson.JsonObject;
import java.io.StringReader;
import net.minecraft.network.chat.IChatBaseComponent;
import org.bukkit.ChatColor;
import com.comphenix.protocol.reflect.FieldUtils;
@ -28,6 +28,8 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra
private static MethodAccessor SERIALIZE_COMPONENT = null;
private static MethodAccessor CONSTRUCT_COMPONENT = null;
private static ConstructorAccessor CONSTRUCT_TEXT_COMPONENT = null;
static {
FuzzyReflection fuzzy = FuzzyReflection.fromClass(SERIALIZER, true);
@ -51,6 +53,13 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra
// Get a component from a standard Minecraft message
CONSTRUCT_COMPONENT = Accessors.getMethodAccessor(MinecraftReflection.getCraftChatMessage(), "fromString", String.class, boolean.class);
try {
// And the component text constructor
CONSTRUCT_TEXT_COMPONENT = Accessors.getConstructorAccessor(MinecraftReflection.getChatComponentTextClass(), String.class);
} catch (Exception ignored) {
// We don't need it
}
}
private static Object deserialize(String json) {
@ -100,7 +109,15 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra
*/
public static WrappedChatComponent fromText(String text) {
Preconditions.checkNotNull(text, "text cannot be NULL.");
return fromLegacyText(text); // TODO: CraftChatMessage.fromString now takes a "plain" boolean parameter
if (CONSTRUCT_TEXT_COMPONENT != null) {
return fromHandle(CONSTRUCT_TEXT_COMPONENT.invoke(text));
}
// this is a bit hacky, but it works good enough and has no need for additional magic
JsonObject object = new JsonObject();
object.addProperty("text", text);
return fromJson(object.toString());
}
/**

View File

@ -8,7 +8,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import com.comphenix.protocol.BukkitInitialization;
import java.util.List;
import net.minecraft.advancements.AdvancementDisplay;
import net.minecraft.advancements.AdvancementFrameType;
import net.minecraft.network.chat.IChatBaseComponent;
@ -45,7 +44,7 @@ public class AutoWrapperTest {
assertTrue(nms.h());
assertTrue(nms.j());
assertFalse(nms.i());
assertEquals(nms.d().a(), "test");
assertEquals("test", nms.d().a());
validateRawText(nms.a(), "Test123");
validateRawText(nms.b(), "Test567");
assertSame(AdvancementFrameType.b, nms.e());
@ -94,12 +93,8 @@ public class AutoWrapperTest {
}
private void validateRawText(IChatBaseComponent component, String expected) {
List<IChatBaseComponent> siblings = component.c();
assertEquals(1, siblings.size());
IChatBaseComponent sibling = siblings.get(0);
assertInstanceOf(LiteralContents.class, sibling.b());
assertEquals(expected, ((LiteralContents) sibling.b()).a());
LiteralContents content = assertInstanceOf(LiteralContents.class, component.b());
assertEquals(expected, content.a());
}
public enum WrappedFrameType {