Add support for accessing ChatComponent arrays

Fixes #7
This commit is contained in:
Dan Mulloy 2014-11-30 12:23:59 -05:00
parent 3ac9793553
commit 6abd0cfdaa

View File

@ -594,7 +594,7 @@ public class PacketContainer implements Serializable {
* Retrieves a read/write structure for chat components in Minecraft 1.7.2.
* <p>
* This modifier will automatically marshall between WrappedChatComponent and the
* internal Minecraft GameProfile.
* internal Minecraft IChatBaseComponent.
* @return A modifier for ChatComponent fields.
*/
public StructureModifier<WrappedChatComponent> getChatComponents() {
@ -603,6 +603,20 @@ public class PacketContainer implements Serializable {
MinecraftReflection.getIChatBaseComponentClass(), BukkitConverters.getWrappedChatComponentConverter());
}
/**
* Retrieves a read/write structure for arrays of chat components.
* <p>
* This modifier will automatically marshall between WrappedChatComponent and the
* internal Minecraft IChatBaseComponent.
* @return A modifier for ItemStack array fields.
*/
public StructureModifier<WrappedChatComponent[]> getChatComponentArrays() {
// Convert to and from the Bukkit wrapper
return structureModifier.<WrappedChatComponent[]>withType(
MinecraftReflection.getIChatBaseComponentClass(),
BukkitConverters.getIgnoreNull(new WrappedChatComponentArrayConverter()));
}
/**
* Retrieve a read/write structure for the ServerPing fields in the following packet: <br>
* <ul>
@ -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<WrappedChatComponent[]> {
final EquivalentConverter<WrappedChatComponent> componentConverter = BukkitConverters.getWrappedChatComponentConverter();
@Override
public Object getGeneric(Class<?>genericType, 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<WrappedChatComponent[]> getSpecificType() {
return WrappedChatComponent[].class;
}
}
}