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; package com.comphenix.protocol.wrappers;
import com.google.gson.JsonObject;
import java.io.StringReader; import java.io.StringReader;
import net.minecraft.network.chat.IChatBaseComponent;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import com.comphenix.protocol.reflect.FieldUtils; 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 SERIALIZE_COMPONENT = null;
private static MethodAccessor CONSTRUCT_COMPONENT = null; private static MethodAccessor CONSTRUCT_COMPONENT = null;
private static ConstructorAccessor CONSTRUCT_TEXT_COMPONENT = null;
static { static {
FuzzyReflection fuzzy = FuzzyReflection.fromClass(SERIALIZER, true); 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 // Get a component from a standard Minecraft message
CONSTRUCT_COMPONENT = Accessors.getMethodAccessor(MinecraftReflection.getCraftChatMessage(), "fromString", String.class, boolean.class); 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) { private static Object deserialize(String json) {
@ -65,13 +74,13 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra
} }
private transient String cache; private transient String cache;
private WrappedChatComponent(Object handle, String cache) { private WrappedChatComponent(Object handle, String cache) {
super(MinecraftReflection.getIChatBaseComponentClass()); super(MinecraftReflection.getIChatBaseComponentClass());
setHandle(handle); setHandle(handle);
this.cache = cache; this.cache = cache;
} }
/** /**
* Construct a new chat component wrapper around the given NMS object. * Construct a new chat component wrapper around the given NMS object.
* @param handle - the NMS object. * @param handle - the NMS object.
@ -80,7 +89,7 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra
public static WrappedChatComponent fromHandle(Object handle) { public static WrappedChatComponent fromHandle(Object handle) {
return new WrappedChatComponent(handle, null); return new WrappedChatComponent(handle, null);
} }
/** /**
* Construct a new chat component wrapper from the given JSON string. * Construct a new chat component wrapper from the given JSON string.
* @param json - the json. * @param json - the json.
@ -89,7 +98,7 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra
public static WrappedChatComponent fromJson(String json) { public static WrappedChatComponent fromJson(String json) {
return new WrappedChatComponent(deserialize(json), json); return new WrappedChatComponent(deserialize(json), json);
} }
/** /**
* Construct a wrapper around a new text chat component with the given text. * Construct a wrapper around a new text chat component with the given text.
* <p> * <p>
@ -100,9 +109,17 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra
*/ */
public static WrappedChatComponent fromText(String text) { public static WrappedChatComponent fromText(String text) {
Preconditions.checkNotNull(text, "text cannot be NULL."); 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());
} }
/** /**
* Construct an array of chat components from a standard Minecraft message. * Construct an array of chat components from a standard Minecraft message.
* <p> * <p>
@ -113,7 +130,7 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra
public static WrappedChatComponent[] fromChatMessage(String message) { public static WrappedChatComponent[] fromChatMessage(String message) {
Object[] components = (Object[]) CONSTRUCT_COMPONENT.invoke(null, message, false); Object[] components = (Object[]) CONSTRUCT_COMPONENT.invoke(null, message, false);
WrappedChatComponent[] result = new WrappedChatComponent[components.length]; WrappedChatComponent[] result = new WrappedChatComponent[components.length];
for (int i = 0; i < components.length; i++) { for (int i = 0; i < components.length; i++) {
result[i] = fromHandle(components[i]); result[i] = fromHandle(components[i]);
} }
@ -144,7 +161,7 @@ public class WrappedChatComponent extends AbstractWrapper implements ClonableWra
} }
return cache; return cache;
} }
/** /**
* Set the content of this component using a JSON object. * Set the content of this component using a JSON object.
* @param obj - the JSON that represents the new component. * @param obj - the JSON that represents the new component.

View File

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