From bf61ffc26b4845816da86e9ce53c29b7263fb7a7 Mon Sep 17 00:00:00 2001 From: Senmori Date: Thu, 15 Mar 2018 20:53:18 +1100 Subject: Expand on ConfigurationSerializable methods in ConfigurationSection --- .../org/bukkit/attribute/AttributeModifier.java | 4 +- .../bukkit/configuration/ConfigurationSection.java | 33 ++++++++++++++ .../org/bukkit/configuration/MemorySection.java | 51 ++++++++++++---------- 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/bukkit/attribute/AttributeModifier.java b/src/main/java/org/bukkit/attribute/AttributeModifier.java index ade7bf08..3c0c4bc5 100644 --- a/src/main/java/org/bukkit/attribute/AttributeModifier.java +++ b/src/main/java/org/bukkit/attribute/AttributeModifier.java @@ -71,7 +71,7 @@ public class AttributeModifier implements ConfigurationSerializable { @Override public Map serialize() { Map data = new HashMap(); - data.put("uuid", uuid); + data.put("uuid", uuid.toString()); data.put("name", name); data.put("operation", operation.ordinal()); data.put("amount", amount); @@ -79,7 +79,7 @@ public class AttributeModifier implements ConfigurationSerializable { } public static AttributeModifier deserialize(Map args) { - return new AttributeModifier((UUID) args.get("uuid"), (String) args.get("name"), NumberConversions.toDouble(args.get("amount")), Operation.values()[NumberConversions.toInt(args.get("operation"))]); + return new AttributeModifier(UUID.fromString((String) args.get("uuid")), (String) args.get("name"), NumberConversions.toDouble(args.get("amount")), Operation.values()[NumberConversions.toInt(args.get("operation"))]); } /** diff --git a/src/main/java/org/bukkit/configuration/ConfigurationSection.java b/src/main/java/org/bukkit/configuration/ConfigurationSection.java index 166f2a65..d8575d1b 100644 --- a/src/main/java/org/bukkit/configuration/ConfigurationSection.java +++ b/src/main/java/org/bukkit/configuration/ConfigurationSection.java @@ -6,6 +6,7 @@ import java.util.List; import org.bukkit.Color; import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.util.Vector; import org.bukkit.inventory.ItemStack; @@ -606,6 +607,38 @@ public interface ConfigurationSection { public List> getMapList(String path); // Bukkit + /** + * Gets the requested {@link ConfigurationSerializable} 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. + * + * @param the type of {@link ConfigurationSerializable} + * @param path the path to the object. + * @param clazz the type of {@link ConfigurationSerializable} + * @return Requested {@link ConfigurationSerializable} object + */ + public T getSerializable(String path, Class clazz); + + /** + * Gets the requested {@link ConfigurationSerializable} 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}. + * + * @param the type of {@link ConfigurationSerializable} + * @param path the path to the object. + * @param clazz the type of {@link ConfigurationSerializable} + * @param def the default object to return if the object is not present at + * the path + * @return Requested {@link ConfigurationSerializable} object + */ + public T getSerializable(String path, Class clazz, T def); + /** * Gets the requested Vector by path. *

diff --git a/src/main/java/org/bukkit/configuration/MemorySection.java b/src/main/java/org/bukkit/configuration/MemorySection.java index 6e5ba586..fe7762b9 100644 --- a/src/main/java/org/bukkit/configuration/MemorySection.java +++ b/src/main/java/org/bukkit/configuration/MemorySection.java @@ -12,6 +12,7 @@ import java.util.Set; import org.apache.commons.lang.Validate; import org.bukkit.Color; import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; @@ -623,64 +624,66 @@ public class MemorySection implements ConfigurationSection { } // Bukkit - public Vector getVector(String path) { + @Override + public T getSerializable(String path, Class clazz) { + Validate.notNull(clazz, "ConfigurationSerializable class cannot be null"); Object def = getDefault(path); - return getVector(path, (def instanceof Vector) ? (Vector) def : null); + return getSerializable(path, clazz, (def != null && clazz.isInstance(def)) ? clazz.cast(def) : null); + } + + @Override + public T getSerializable(String path, Class clazz, T def) { + Validate.notNull(clazz, "ConfigurationSerializable class cannot be null"); + Object val = get(path); + return (val != null && clazz.isInstance(val)) ? clazz.cast(val) : def; + } + + public Vector getVector(String path) { + return getSerializable(path, Vector.class); } public Vector getVector(String path, Vector def) { - Object val = get(path, def); - return (val instanceof Vector) ? (Vector) val : def; + return getSerializable(path, Vector.class, def); } public boolean isVector(String path) { - Object val = get(path); - return val instanceof Vector; + return getSerializable(path, Vector.class) != null; } public OfflinePlayer getOfflinePlayer(String path) { - Object def = getDefault(path); - return getOfflinePlayer(path, (def instanceof OfflinePlayer) ? (OfflinePlayer) def : null); + return getSerializable(path, OfflinePlayer.class); } public OfflinePlayer getOfflinePlayer(String path, OfflinePlayer def) { - Object val = get(path, def); - return (val instanceof OfflinePlayer) ? (OfflinePlayer) val : def; + return getSerializable(path, OfflinePlayer.class, def); } public boolean isOfflinePlayer(String path) { - Object val = get(path); - return val instanceof OfflinePlayer; + return getSerializable(path, OfflinePlayer.class) != null; } public ItemStack getItemStack(String path) { - Object def = getDefault(path); - return getItemStack(path, (def instanceof ItemStack) ? (ItemStack) def : null); + return getSerializable(path, ItemStack.class); } public ItemStack getItemStack(String path, ItemStack def) { - Object val = get(path, def); - return (val instanceof ItemStack) ? (ItemStack) val : def; + return getSerializable(path, ItemStack.class, def); } public boolean isItemStack(String path) { - Object val = get(path); - return val instanceof ItemStack; + return getSerializable(path, ItemStack.class) != null; } public Color getColor(String path) { - Object def = getDefault(path); - return getColor(path, (def instanceof Color) ? (Color) def : null); + return getSerializable(path, Color.class); } public Color getColor(String path, Color def) { - Object val = get(path, def); - return (val instanceof Color) ? (Color) val : def; + return getSerializable(path, Color.class, def); } public boolean isColor(String path) { - Object val = get(path); - return val instanceof Color; + return getSerializable(path, Color.class) != null; } public ConfigurationSection getConfigurationSection(String path) { -- cgit v1.2.3