mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-23 00:42:05 +01:00
Expand on ConfigurationSerializable methods in ConfigurationSection
By: Senmori <thesenmori@gmail.com>
This commit is contained in:
parent
e57e2489b5
commit
2496f119f4
3 changed files with 62 additions and 26 deletions
|
@ -71,7 +71,7 @@ public class AttributeModifier implements ConfigurationSerializable {
|
|||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
Map<String, Object> data = new HashMap<String, Object>();
|
||||
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<String, Object> 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"))]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<Map<?, ?>> 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 <T> 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 extends ConfigurationSerializable> T getSerializable(String path, Class<T> 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 <T> 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 extends ConfigurationSerializable> T getSerializable(String path, Class<T> clazz, T def);
|
||||
|
||||
/**
|
||||
* Gets the requested Vector by path.
|
||||
* <p>
|
||||
|
|
|
@ -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 extends ConfigurationSerializable> T getSerializable(String path, Class<T> 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 extends ConfigurationSerializable> T getSerializable(String path, Class<T> 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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue