From 6abd0cfdaa90231380972277e0f0ad5d9a6eb9a5 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sun, 30 Nov 2014 12:23:59 -0500 Subject: [PATCH] Add support for accessing ChatComponent arrays Fixes #7 --- .../protocol/events/PacketContainer.java | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java index 8f28ba3f..75696fa4 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketContainer.java @@ -594,7 +594,7 @@ public class PacketContainer implements Serializable { * Retrieves a read/write structure for chat components in Minecraft 1.7.2. *

* This modifier will automatically marshall between WrappedChatComponent and the - * internal Minecraft GameProfile. + * internal Minecraft IChatBaseComponent. * @return A modifier for ChatComponent fields. */ public StructureModifier getChatComponents() { @@ -602,6 +602,20 @@ public class PacketContainer implements Serializable { return structureModifier.withType( MinecraftReflection.getIChatBaseComponentClass(), BukkitConverters.getWrappedChatComponentConverter()); } + + /** + * Retrieves a read/write structure for arrays of chat components. + *

+ * This modifier will automatically marshall between WrappedChatComponent and the + * internal Minecraft IChatBaseComponent. + * @return A modifier for ItemStack array fields. + */ + public StructureModifier getChatComponentArrays() { + // Convert to and from the Bukkit wrapper + return structureModifier.withType( + MinecraftReflection.getIChatBaseComponentClass(), + BukkitConverters.getIgnoreNull(new WrappedChatComponentArrayConverter())); + } /** * Retrieve a read/write structure for the ServerPing fields in the following packet:
@@ -899,4 +913,41 @@ public class PacketContainer implements Serializable { return ItemStack[].class; } } + + /** + * Represents an equivalent converter for ChatComponent arrays. + * @author Kristian + */ + private static class WrappedChatComponentArrayConverter implements EquivalentConverter { + final EquivalentConverter componentConverter = BukkitConverters.getWrappedChatComponentConverter(); + + @Override + public Object getGeneric(ClassgenericType, WrappedChatComponent[] specific) { + Class nmsComponent = MinecraftReflection.getIChatBaseComponentClass(); + Object[] result = (Object[]) Array.newInstance(nmsComponent, specific.length); + + // Unwrap every item + for (int i = 0; i < result.length; i++) { + result[i] = componentConverter.getGeneric(nmsComponent, specific[i]); + } + return result; + } + + @Override + public WrappedChatComponent[] getSpecific(Object generic) { + Object[] input = (Object[]) generic; + WrappedChatComponent[] result = new WrappedChatComponent[input.length]; + + // Add the wrapper + for (int i = 0; i < result.length; i++) { + result[i] = componentConverter.getSpecific(input[i]); + } + return result; + } + + @Override + public Class getSpecificType() { + return WrappedChatComponent[].class; + } + } }