From fbc596d57bbe8acac5c69a44207e1765049f636f Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Tue, 23 Jun 2015 12:59:13 -0400 Subject: [PATCH] Add a generic enum converter and a set converter --- .../protocol/wrappers/BukkitConverters.java | 62 ++++++++++++++++++- .../protocol/wrappers/EnumWrappers.java | 9 +++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java index a128d2b5..a07436e0 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/BukkitConverters.java @@ -24,9 +24,11 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import org.bukkit.Material; import org.bukkit.World; @@ -320,7 +322,65 @@ public class BukkitConverters { } }; } - + + /** + * Retrieve an equivalent converter for a set of generic items. + * @param Type + * @param genericItemType - the generic item type. + * @param itemConverter - an equivalent converter for the generic type. + * @return An equivalent converter. + */ + @SuppressWarnings("unchecked") + public static EquivalentConverter> getSetConverter(final Class genericItemType, final EquivalentConverter itemConverter) { + // Convert to and from the wrapper + return new IgnoreNullConverter>() { + + @Override + protected Set getSpecificValue(Object generic) { + if (generic instanceof Collection) { + Set items = new HashSet(); + + // Copy everything to a new list + for (Object item : (Collection) generic) { + T result = itemConverter.getSpecific(item); + + if (item != null) + items.add(result); + } + + return items; + } + + // Not valid + return null; + } + + @Override + protected Object getGenericValue(Class genericType, Set specific) { + Collection newContainer = (Collection) DefaultInstances.DEFAULT.getDefault(genericType); + + // Convert each object + for (T position : specific) { + Object converted = itemConverter.getGeneric(genericItemType, position); + + if (position == null) + newContainer.add(null); + else if (converted != null) + newContainer.add(converted); + } + + return newContainer; + } + + @Override + public Class> getSpecificType() { + // Damn you Java + Class dummy = Set.class; + return (Class>) dummy; + } + }; + } + /** * Retrieve an equivalent converter for an array of generic items. * @param Type diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java index 41c5a24a..f891c41c 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/EnumWrappers.java @@ -489,6 +489,15 @@ public abstract class EnumWrappers { return new EnumConverter(Particle.class); } + /** + * Retrieve a generic enum converter for use with StructureModifiers. + * @param enumClass - Enum class + * @return A generic enum converter + */ + public static > EquivalentConverter getGenericConverter(Class enumClass) { + return new EnumConverter(enumClass); + } + // The common enum converter @SuppressWarnings({ "rawtypes", "unchecked" }) private static class EnumConverter> implements EquivalentConverter {