/** * Gets the first serializer associated with a given class. * * <p><b>Note</b>: If {@link Serializer#isOptional() the serializer is optional}, * values <i>must</i> be wrapped in an {@link Optional}.</p> * * <p>If there are multiple serializers for a given class (i.e. BlockPosition), * you should use {@link #get(Class, boolean)} for more precision.</p> * * @param clazz Class to find serializer for * @return The serializer, or null if none exists */ public static Serializer get(Class<?> clazz) { Validate.notNull(clazz,"Class cannot be null!"); initialize(); for (Serializer serializer : REGISTRY) { if (serializer.getType().equals(clazz)) { return serializer; } } throw new IllegalArgumentException("No serializer found for " + clazz); }
/** * Gets the serializer associated with a given NMS handle. * @param handle The handle * @return The serializer, or null if none exists */ public static Serializer fromHandle(Object handle) { Validate.notNull(handle, "handle cannot be null!"); initialize(); for (Serializer serializer : REGISTRY) { if (serializer.getHandle().equals(handle)) { return serializer; } } return null; }
/** * Gets the first serializer associated with a given class and optional state. * * <p><b>Note</b>: If the serializer is optional, values <i>must<i> be * wrapped in an {@link Optional} * * @param clazz Class to find serializer for * @param optional Optional state * @return The serializer, or null if none exists */ public static Serializer get(Class<?> clazz, boolean optional) { Validate.notNull(clazz, "Class cannot be null!"); initialize(); Validate.notEmpty(REGISTRY, "Registry has no elements!"); for (Serializer serializer : REGISTRY) { if (serializer.getType().equals(clazz) && serializer.isOptional() == optional) { return serializer; } } throw new IllegalArgumentException("No serializer found for " + (optional ? "Optional<" + clazz + ">" : clazz)); }