diff --git a/paper-api/src/main/java/org/bukkit/configuration/ConfigurationSection.java b/paper-api/src/main/java/org/bukkit/configuration/ConfigurationSection.java index 5dac53691..e83de68ac 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/ConfigurationSection.java +++ b/paper-api/src/main/java/org/bukkit/configuration/ConfigurationSection.java @@ -633,6 +633,53 @@ public interface ConfigurationSection { public List> getMapList(@NotNull String path); // Bukkit + /** + * Gets the requested object at the given path. + * + * If the Object does not exist but a default value has been specified, this + * will return the default value. If the Object does not exist and no + * default value was specified, this will return null. + * + * Note: For example #getObject(path, String.class) is not + * equivalent to {@link #getString(String) #getString(path)} because + * {@link #getString(String) #getString(path)} converts internally all + * Objects to Strings. However, #getObject(path, Boolean.class) is + * equivalent to {@link #getBoolean(String) #getBoolean(path)} for example. + * + * @param the type of the requested object + * @param path the path to the object. + * @param clazz the type of the requested object + * @return Requested object + */ + @Nullable + public T getObject(@NotNull String path, @NotNull Class clazz); + + /** + * Gets the requested object at the given path, returning a default value if + * not found + * + * If the Object does not exist then the specified default value will + * returned regardless of if a default has been identified in the root + * {@link Configuration}. + * + * Note: For example #getObject(path, String.class, def) is + * not equivalent to + * {@link #getString(String, String) #getString(path, def)} because + * {@link #getString(String, String) #getString(path, def)} converts + * internally all Objects to Strings. However, #getObject(path, + * Boolean.class, def) is equivalent to {@link #getBoolean(String, boolean) #getBoolean(path, + * def)} for example. + * + * @param the type of the requested object + * @param path the path to the object. + * @param clazz the type of the requested object + * @param def the default object to return if the object is not present at + * the path + * @return Requested object + */ + @Nullable + public T getObject(@NotNull String path, @NotNull Class clazz, @Nullable T def); + /** * Gets the requested {@link ConfigurationSerializable} object at the given * path. diff --git a/paper-api/src/main/java/org/bukkit/configuration/MemorySection.java b/paper-api/src/main/java/org/bukkit/configuration/MemorySection.java index 9c7fc51ae..37bb1c67a 100644 --- a/paper-api/src/main/java/org/bukkit/configuration/MemorySection.java +++ b/paper-api/src/main/java/org/bukkit/configuration/MemorySection.java @@ -653,18 +653,30 @@ public class MemorySection implements ConfigurationSection { // Bukkit @Nullable @Override - public T getSerializable(@NotNull String path, @NotNull Class clazz) { - Validate.notNull(clazz, "ConfigurationSerializable class cannot be null"); + public T getObject(@NotNull String path, @NotNull Class clazz) { + Validate.notNull(clazz, "Class cannot be null"); Object def = getDefault(path); - return getSerializable(path, clazz, (def != null && clazz.isInstance(def)) ? clazz.cast(def) : null); + return getObject(path, clazz, (def != null && clazz.isInstance(def)) ? clazz.cast(def) : null); + } + + @Nullable + @Override + public T getObject(@NotNull String path, @NotNull Class clazz, @Nullable T def) { + Validate.notNull(clazz, "Class cannot be null"); + Object val = get(path, def); + return (val != null && clazz.isInstance(val)) ? clazz.cast(val) : def; + } + + @Nullable + @Override + public T getSerializable(@NotNull String path, @NotNull Class clazz) { + return getObject(path, clazz); } @Nullable @Override public T getSerializable(@NotNull String path, @NotNull Class clazz, @Nullable T def) { - Validate.notNull(clazz, "ConfigurationSerializable class cannot be null"); - Object val = get(path); - return (val != null && clazz.isInstance(val)) ? clazz.cast(val) : def; + return getObject(path, clazz, def); } @Nullable diff --git a/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java b/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java index 462fc0a71..4e91b1a27 100644 --- a/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java +++ b/paper-api/src/test/java/org/bukkit/configuration/ConfigurationSectionTest.java @@ -460,6 +460,43 @@ public abstract class ConfigurationSectionTest { assertFalse(section.isList("doesntExist")); } + @Test + public void testGetObject_String_Class() { + ConfigurationSection section = getConfigurationSection(); + + section.set("set", Integer.valueOf(1)); + section.addDefault("default", Integer.valueOf(2)); + section.addDefault("defaultAndSet", Boolean.TRUE); + section.set("defaultAndSet", Integer.valueOf(3)); + + assertEquals(Integer.valueOf(1), section.getObject("set", Integer.class)); + assertNull(section.getObject("set", Boolean.class)); + assertEquals(Integer.valueOf(2), section.getObject("default", Number.class)); + assertNull(section.getObject("default", Boolean.class)); + assertEquals(Integer.valueOf(3), section.getObject("defaultAndSet", Integer.class)); + assertEquals(Boolean.TRUE, section.getObject("defaultAndSet", Boolean.class)); + assertEquals(Integer.valueOf(3), section.getObject("defaultAndSet", Object.class)); + assertNull(section.getObject("defaultAndSet", String.class)); + assertNull(section.getObject("doesntExist", Boolean.class)); + assertNull(section.getString("doesntExist")); + } + + @Test + public void testGetObject_String_Class_T() { + ConfigurationSection section = getConfigurationSection(); + + section.set("set", Integer.valueOf(1)); + section.addDefault("default", Integer.valueOf(2)); + + assertEquals(Integer.valueOf(1), section.getObject("set", Integer.class, null)); + assertEquals(Integer.valueOf(1), section.getObject("set", Integer.class, Integer.valueOf(4))); + assertNull(section.getObject("set", Boolean.class, null)); + assertNull(section.getObject("default", Integer.class, null)); + assertNull(section.getObject("doesntExist", Boolean.class, null)); + assertEquals(Boolean.TRUE, section.getObject("doesntExist", Boolean.class, Boolean.TRUE)); + assertNull(section.getString("doesntExist")); + } + @Test public void testGetVector_String() { ConfigurationSection section = getConfigurationSection();