Java Code Examples for org.bukkit.configuration.serialization.ConfigurationSerializable

The following are top voted examples for showing how to use org.bukkit.configuration.serialization.ConfigurationSerializable. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: MCLibrary   File: ConfigurationSerializableAdapter.java   View source code 6 votes vote down vote up
public static ConfigurationSerializable deserialize(Map<String, Object> map) {
    if (map == null)
        return null;

    for (Map.Entry<String, Object> entry : map.entrySet()) {
        String key = entry.getKey();
        Object val = entry.getValue();

        if (!(val instanceof Map))
            continue;

        Map<String, Object> subMap = (Map<String, Object>) val;
        if (subMap.containsKey(SERIALIZED_TYPE_KEY)) {
            map.put(key, deserialize(subMap));
        }
    }
    return ConfigurationSerialization.deserializeObject(map);
}
 
Example 2
Project: MCLibrary   File: BukkitTypeAdapterFactory.java   View source code 6 votes vote down vote up
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<?> rawType = type.getRawType();
    TypeAdapter<Map> mapAdapter = gson.getAdapter(Map.class);
    TypeAdapter<Collection> collectionAdapter = gson.getAdapter(Collection.class);
    if (Location.class.isAssignableFrom(rawType)) {
        return (TypeAdapter<T>) new LocationTypeAdapter();
    } else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) {
        return (TypeAdapter<T>) new ConfigurationSerializableAdapter(mapAdapter);
    } else if (GUISignature.class.isAssignableFrom(rawType)) {
        return (TypeAdapter<T>) new GUISignatureTypeAdapter(mapAdapter, collectionAdapter);
    } else if (MaterialAndData.class.isAssignableFrom(rawType)) {
        return (TypeAdapter<T>) new MaterialAndDataAdapter();
    }
    return null;
}
 
Example 3
Project: GameDispenser   File: PrimitiveComponent.java   View source code 6 votes vote down vote up
public PrimitiveComponent() {
    super(  Integer.class,
            Integer.TYPE,
            String.class,
            Float.class,
            Float.TYPE,
            Boolean.class,
            Boolean.TYPE,
            Double.class,
            Double.TYPE,
            Long.class,
            Long.TYPE,
            Short.class,
            Short.TYPE,
            Byte.class,
            Byte.TYPE,
            Character.class,
            Character.TYPE,
            ConfigurationSerializable.class);
}
 
Example 4
Project: SwornAPI   File: FileSerialization.java   View source code 6 votes vote down vote up
/**
 * Loads a previously serialized object from a given file using YAML.
 * 
 * @param file File to load from
 * @param clazz Class the object should be of
 * @param exists Whether or not the file exists and the expensive
 *        {@link File#exists()} operation can be skipped
 * @return The deserialized object, or null if the file does not exist
 * @throws IllegalArgumentException If the file or class is null
 * @throws IOException If the file cannot be read
 * @throws InvalidConfigurationException If the given file is not a valid
 *         YAML configuration
 * @see #save(ConfigurationSerializable, File)
 */
@SuppressWarnings("unchecked")
public static <T extends ConfigurationSerializable> T load(File file, Class<T> clazz, boolean exists) throws IOException, InvalidConfigurationException
{
	Validate.notNull(file, "file cannot be null!");
	Validate.notNull(clazz, "clazz cannot be null!");

	if (! exists && ! file.exists())
		return null;

	YamlConfiguration config = new YamlConfiguration();
	config.load(file);

	Map<String, Object> map = config.getValues(true);
	return (T) ConfigurationSerialization.deserializeObject(map, clazz);
}
 
Example 5
Project: SwornAPI   File: FileSerialization.java   View source code 6 votes vote down vote up
/**
 * Saves a serializable object to a given file.
 * 
 * @param instance Object to seriaize
 * @param file File to save to
 * @throws IllegalArgumentException If the instance or file is null
 * @throws IOException If the file cannot be written to
 * @see #load(File, Class, boolean)
 */
public static void save(ConfigurationSerializable instance, File file) throws IOException
{
	Validate.notNull(instance, "instance cannot be null!");
	Validate.notNull(file, "file cannot be null!");

	file.delete();
	file.createNewFile();

	YamlConfiguration config = new YamlConfiguration();

	for (Entry<String, Object> entry : instance.serialize().entrySet())
	{
		config.set(entry.getKey(), entry.getValue());
	}

	config.save(file);
}
 
Example 6
Project: HeavySpleef   File: ItemStackFlag.java   View source code 6 votes vote down vote up
static void marshalSerializeable(Element baseElement, ConfigurationSerializable serializeable) {
	Map<String, Object> serialized = serializeable.serialize();
	if (serializeable instanceof ItemMeta) {
		baseElement.addAttribute("itemmeta", String.valueOf(true));
	}
	
	for (Entry<String, Object> entry : serialized.entrySet()) {
		Element entryElement = baseElement.addElement(entry.getKey());
		Object value = entry.getValue();
		
		if (value instanceof ItemMeta) {
			marshalSerializeable(entryElement, (ItemMeta) value);
		} else {
			serializeObject(value, entryElement);
		}
	}
}
 
Example 7
Project: PlayerVaults   File: Serialization.java   View source code 6 votes vote down vote up
@Deprecated
private static Map<String, Object> handleSerialization(Map<String, Object> map) {
    Map<String, Object> serialized = recreateMap(map);
    for (Entry<String, Object> entry : serialized.entrySet()) {
        if (entry.getValue() instanceof ConfigurationSerializable) {
            entry.setValue(serialize((ConfigurationSerializable) entry.getValue()));
        } else if (entry.getValue() instanceof Iterable<?>) {
            List<Object> newList = new ArrayList<>();
            for (Object object : ((Iterable) entry.getValue())) {
                if (object instanceof ConfigurationSerializable) {
                    object = serialize((ConfigurationSerializable) object);
                }
                newList.add(object);
            }
            entry.setValue(newList);
        } else if (entry.getValue() instanceof Map<?, ?>) {
            // unchecked cast here.  If you're serializing to a non-standard Map you deserve ClassCastExceptions
            entry.setValue(handleSerialization((Map<String, Object>) entry.getValue()));
        }
    }
    return serialized;
}
 
Example 8
Project: helper   File: BukkitSerializableAdapterFactory.java   View source code 5 votes vote down vote up
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<? super T> clazz = type.getRawType();

    if (!ConfigurationSerializable.class.isAssignableFrom(clazz)) {
        return null;
    }

    //noinspection unchecked
    return (TypeAdapter<T>) new Adapter(gson);
}
 
Example 9
Project: helper   File: BukkitSerializableAdapterFactory.java   View source code 5 votes vote down vote up
@Override
public void write(JsonWriter out, ConfigurationSerializable value) {
    Map<String, Object> serialized = value.serialize();

    Map<String, Object> map = new LinkedHashMap<>(serialized.size() + 1);
    map.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(value.getClass()));
    map.putAll(serialized);

    gson.toJson(map, RAW_OUTPUT_TYPE, out);
}
 
Example 10
Project: MCLibrary   File: ConfigurationSerializableAdapter.java   View source code 5 votes vote down vote up
public static Map<String, Object> serialize(ConfigurationSerializable serializable) {
    Map<String, Object> serialized = new HashMap<>();
    serialized.putAll(serializable.serialize());
    for (Map.Entry<String, Object> entry : serialized.entrySet()) {
        String key = entry.getKey();
        Object val = entry.getValue();

        if (val instanceof ConfigurationSerializable) {
            serialized.put(key, serialize((ConfigurationSerializable) val));
        }
    }
    serialized.put(SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(serializable.getClass()));
    return serialized;
}
 
Example 11
Project: MCLibrary   File: ConfigurationSerializableAdapter.java   View source code 5 votes vote down vote up
@Override
public void write(JsonWriter out, ConfigurationSerializable value) throws IOException {
    if (value != null)
        mapAdapter.write(out, serialize(value));
    else
        out.nullValue();
}
 
Example 12
Project: Skript   File: ConfigurationSerializer.java   View source code 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
public final static <T extends ConfigurationSerializable> T deserializeCS(final String s, final Class<T> c) {
	final YamlConfiguration y = new YamlConfiguration();
	try {
		y.loadFromString(s);
	} catch (final InvalidConfigurationException e) {
		return null;
	}
	final Object o = y.get("value");
	if (!c.isInstance(o))
		return null;
	return (T) o;
}
 
Example 13
Project: Skript   File: ConfigurationSerializer.java   View source code 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Deprecated
@Nullable
public final static <T extends ConfigurationSerializable> T deserializeCSOld(final String s, final Class<T> c) {
	final YamlConfiguration y = new YamlConfiguration();
	try {
		y.loadFromString(s.replace("\uFEFF", "\n"));
	} catch (final InvalidConfigurationException e) {
		return null;
	}
	final Object o = y.get("value");
	if (!c.isInstance(o))
		return null;
	return (T) o;
}
 
Example 14
Project: Skript   File: Variables.java   View source code 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
@Override
@Nullable
public String getID(final @NonNull Class<?> c) {
	if (ConfigurationSerializable.class.isAssignableFrom(c) && Classes.getSuperClassInfo(c) == Classes.getExactClassInfo(Object.class))
		return configurationSerializablePrefix + ConfigurationSerialization.getAlias((Class<? extends ConfigurationSerializable>) c);
	return null;
}
 
Example 15
Project: Skript   File: Variables.java   View source code 5 votes vote down vote up
@Override
@Nullable
public Class<? extends ConfigurationSerializable> getClass(final @NonNull String id) {
	if (id.startsWith(configurationSerializablePrefix))
		return ConfigurationSerialization.getClassByAlias(id.substring(configurationSerializablePrefix.length()));
	return null;
}
 
Example 16
Project: Thermos-Bukkit   File: JavaPluginLoader.java   View source code 5 votes vote down vote up
void setClass(final String name, final Class<?> clazz) {
    if (!classes.containsKey(name)) {
        classes.put(name, clazz);

        if (ConfigurationSerializable.class.isAssignableFrom(clazz)) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.registerClass(serializable);
        }
    }
}
 
Example 17
Project: Thermos-Bukkit   File: JavaPluginLoader.java   View source code 5 votes vote down vote up
private void removeClass(String name) {
    Class<?> clazz = classes.remove(name);

    try {
        if ((clazz != null) && (ConfigurationSerializable.class.isAssignableFrom(clazz))) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.unregisterClass(serializable);
        }
    } catch (NullPointerException ex) {
        // Boggle!
        // (Native methods throwing NPEs is not fun when you can't stop it before-hand)
    }
}
 
Example 18
Project: Thermos-Bukkit   File: FireworkEffect.java   View source code 5 votes vote down vote up
/**
 * @see ConfigurationSerializable
 */
public static ConfigurationSerializable deserialize(Map<String, Object> map) {
    Type type = Type.valueOf((String) map.get(TYPE));
    if (type == null) {
        throw new IllegalArgumentException(map.get(TYPE) + " is not a valid Type");
    }

    return builder()
        .flicker((Boolean) map.get(FLICKER))
        .trail((Boolean) map.get(TRAIL))
        .withColor((Iterable<?>) map.get(COLORS))
        .withFade((Iterable<?>) map.get(FADE_COLORS))
        .with(type)
        .build();
}
 
Example 19
Project: Thermos-Bukkit   File: YamlRepresenter.java   View source code 5 votes vote down vote up
@Override
public Node representData(Object data) {
    ConfigurationSerializable serializable = (ConfigurationSerializable) data;
    Map<String, Object> values = new LinkedHashMap<String, Object>();
    values.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(serializable.getClass()));
    values.putAll(serializable.serialize());

    return super.representData(values);
}
 
Example 20
Project: Thermos-Bukkit   File: BukkitObjectOutputStream.java   View source code 5 votes vote down vote up
@Override
protected Object replaceObject(Object obj) throws IOException {
    if (!(obj instanceof Serializable) && (obj instanceof ConfigurationSerializable)) {
        obj = Wrapper.newWrapper((ConfigurationSerializable) obj);
    }

    return super.replaceObject(obj);
}
 
Example 21
Project: CauldronGit   File: JavaPluginLoader.java   View source code 5 votes vote down vote up
void setClass(final String name, final Class<?> clazz) {
    if (!classes.containsKey(name)) {
        classes.put(name, clazz);

        if (ConfigurationSerializable.class.isAssignableFrom(clazz)) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.registerClass(serializable);
        }
    }
}
 
Example 22
Project: CauldronGit   File: JavaPluginLoader.java   View source code 5 votes vote down vote up
private void removeClass(String name) {
    Class<?> clazz = classes.remove(name);

    try {
        if ((clazz != null) && (ConfigurationSerializable.class.isAssignableFrom(clazz))) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.unregisterClass(serializable);
        }
    } catch (NullPointerException ex) {
        // Boggle!
        // (Native methods throwing NPEs is not fun when you can't stop it before-hand)
    }
}
 
Example 23
Project: CauldronGit   File: FireworkEffect.java   View source code 5 votes vote down vote up
/**
 * @see ConfigurationSerializable
 */
public static ConfigurationSerializable deserialize(Map<String, Object> map) {
    Type type = Type.valueOf((String) map.get(TYPE));
    if (type == null) {
        throw new IllegalArgumentException(map.get(TYPE) + " is not a valid Type");
    }

    return builder()
        .flicker((Boolean) map.get(FLICKER))
        .trail((Boolean) map.get(TRAIL))
        .withColor((Iterable<?>) map.get(COLORS))
        .withFade((Iterable<?>) map.get(FADE_COLORS))
        .with(type)
        .build();
}
 
Example 24
Project: CauldronGit   File: YamlRepresenter.java   View source code 5 votes vote down vote up
@Override
public Node representData(Object data) {
    ConfigurationSerializable serializable = (ConfigurationSerializable) data;
    Map<String, Object> values = new LinkedHashMap<String, Object>();
    values.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(serializable.getClass()));
    values.putAll(serializable.serialize());

    return super.representData(values);
}
 
Example 25
Project: CauldronGit   File: BukkitObjectOutputStream.java   View source code 5 votes vote down vote up
@Override
protected Object replaceObject(Object obj) throws IOException {
    if (!(obj instanceof Serializable) && (obj instanceof ConfigurationSerializable)) {
        obj = Wrapper.newWrapper((ConfigurationSerializable) obj);
    }

    return super.replaceObject(obj);
}
 
Example 26
Project: Peacecraft   File: YamlRepresenter.java   View source code 5 votes vote down vote up
@Override
public Node representData(Object data) {
    ConfigurationSerializable serializable = (ConfigurationSerializable) data;
    Map<String, Object> values = new LinkedHashMap<String, Object>();
    values.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(serializable.getClass()));
    values.putAll(serializable.serialize());
    return super.representData(values);
}
 
Example 27
Project: GameDispenser   File: GameLoader.java   View source code 5 votes vote down vote up
void setClass(final String name, final Class<?> clazz) {
    if (!classes.containsKey(name)) {
        classes.put(name, clazz);
        
        if (ConfigurationSerializable.class.isAssignableFrom(clazz)) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.registerClass(serializable);
        }
    }
}
 
Example 28
Project: Tweakkit-Server   File: NBTMetadataStore.java   View source code 5 votes vote down vote up
/**
 * A helper method to convert a tag directly to a MetadataValue.
 *
 * @param plugin The owning plugin
 * @param tag The tag to convert
 * @return A new MetadataValue object, or null if the tag could not be converted
 */
public static PersistentMetadataValue convertToMetadata(Plugin plugin, NBTBase tag) {
    if (plugin == null || tag == null) return null;

    Object converted = convert(tag);
    // Note that we don't currently export arrays as metadata
    if (converted instanceof String) {
        return new PersistentMetadataValue(plugin, (String)converted);
    } else if (converted instanceof Integer) {
        return new PersistentMetadataValue(plugin, (Integer)converted);
    } else if (converted instanceof Short) {
        return new PersistentMetadataValue(plugin, (Short)converted);
    } else if (converted instanceof Byte) {
        return new PersistentMetadataValue(plugin, (Byte)converted);
    } else if (converted instanceof Long) {
        return new PersistentMetadataValue(plugin, (Long)converted);
    } else if (converted instanceof Float) {
        return new PersistentMetadataValue(plugin, (Float)converted);
    } else if (converted instanceof Double) {
        return new PersistentMetadataValue(plugin, (Double)converted);
    } else if (converted instanceof Map) {
        return new PersistentMetadataValue(plugin, (Map<String, ?>)converted);
    } else if (converted instanceof List) {
        return new PersistentMetadataValue(plugin, (List<?>)converted);
    } else if (converted instanceof ConfigurationSerializable) {
        return new PersistentMetadataValue(plugin, (ConfigurationSerializable)converted);
    }

    return null;
}
 
Example 29
Project: Cauldron   File: JavaPluginLoader.java   View source code 5 votes vote down vote up
void setClass(final String name, final Class<?> clazz) {
    if (!classes.containsKey(name)) {
        classes.put(name, clazz);

        if (ConfigurationSerializable.class.isAssignableFrom(clazz)) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.registerClass(serializable);
        }
    }
}
 
Example 30
Project: Cauldron   File: JavaPluginLoader.java   View source code 5 votes vote down vote up
private void removeClass(String name) {
    Class<?> clazz = classes.remove(name);

    try {
        if ((clazz != null) && (ConfigurationSerializable.class.isAssignableFrom(clazz))) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.unregisterClass(serializable);
        }
    } catch (NullPointerException ex) {
        // Boggle!
        // (Native methods throwing NPEs is not fun when you can't stop it before-hand)
    }
}
 
Example 31
Project: Cauldron   File: FireworkEffect.java   View source code 5 votes vote down vote up
/**
 * @see ConfigurationSerializable
 */
public static ConfigurationSerializable deserialize(Map<String, Object> map) {
    Type type = Type.valueOf((String) map.get(TYPE));
    if (type == null) {
        throw new IllegalArgumentException(map.get(TYPE) + " is not a valid Type");
    }

    return builder()
        .flicker((Boolean) map.get(FLICKER))
        .trail((Boolean) map.get(TRAIL))
        .withColor((Iterable<?>) map.get(COLORS))
        .withFade((Iterable<?>) map.get(FADE_COLORS))
        .with(type)
        .build();
}
 
Example 32
Project: Cauldron   File: YamlRepresenter.java   View source code 5 votes vote down vote up
@Override
public Node representData(Object data) {
    ConfigurationSerializable serializable = (ConfigurationSerializable) data;
    Map<String, Object> values = new LinkedHashMap<String, Object>();
    values.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(serializable.getClass()));
    values.putAll(serializable.serialize());

    return super.representData(values);
}
 
Example 33
Project: Cauldron   File: BukkitObjectOutputStream.java   View source code 5 votes vote down vote up
@Override
protected Object replaceObject(Object obj) throws IOException {
    if (!(obj instanceof Serializable) && (obj instanceof ConfigurationSerializable)) {
        obj = Wrapper.newWrapper((ConfigurationSerializable) obj);
    }

    return super.replaceObject(obj);
}
 
Example 34
Project: Cauldron   File: JavaPluginLoader.java   View source code 5 votes vote down vote up
void setClass(final String name, final Class<?> clazz) {
    if (!classes.containsKey(name)) {
        classes.put(name, clazz);

        if (ConfigurationSerializable.class.isAssignableFrom(clazz)) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.registerClass(serializable);
        }
    }
}
 
Example 35
Project: Cauldron   File: JavaPluginLoader.java   View source code 5 votes vote down vote up
private void removeClass(String name) {
    Class<?> clazz = classes.remove(name);

    try {
        if ((clazz != null) && (ConfigurationSerializable.class.isAssignableFrom(clazz))) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.unregisterClass(serializable);
        }
    } catch (NullPointerException ex) {
        // Boggle!
        // (Native methods throwing NPEs is not fun when you can't stop it before-hand)
    }
}
 
Example 36
Project: Cauldron   File: FireworkEffect.java   View source code 5 votes vote down vote up
/**
 * @see ConfigurationSerializable
 */
public static ConfigurationSerializable deserialize(Map<String, Object> map) {
    Type type = Type.valueOf((String) map.get(TYPE));
    if (type == null) {
        throw new IllegalArgumentException(map.get(TYPE) + " is not a valid Type");
    }

    return builder()
        .flicker((Boolean) map.get(FLICKER))
        .trail((Boolean) map.get(TRAIL))
        .withColor((Iterable<?>) map.get(COLORS))
        .withFade((Iterable<?>) map.get(FADE_COLORS))
        .with(type)
        .build();
}
 
Example 37
Project: Cauldron   File: YamlRepresenter.java   View source code 5 votes vote down vote up
@Override
public Node representData(Object data) {
    ConfigurationSerializable serializable = (ConfigurationSerializable) data;
    Map<String, Object> values = new LinkedHashMap<String, Object>();
    values.put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(serializable.getClass()));
    values.putAll(serializable.serialize());

    return super.representData(values);
}
 
Example 38
Project: Cauldron   File: BukkitObjectOutputStream.java   View source code 5 votes vote down vote up
@Override
protected Object replaceObject(Object obj) throws IOException {
    if (!(obj instanceof Serializable) && (obj instanceof ConfigurationSerializable)) {
        obj = Wrapper.newWrapper((ConfigurationSerializable) obj);
    }

    return super.replaceObject(obj);
}
 
Example 39
Project: Cauldron   File: JavaPluginLoader.java   View source code 5 votes vote down vote up
void setClass(final String name, final Class<?> clazz) {
    if (!classes.containsKey(name)) {
        classes.put(name, clazz);

        if (ConfigurationSerializable.class.isAssignableFrom(clazz)) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.registerClass(serializable);
        }
    }
}
 
Example 40
Project: Cauldron   File: JavaPluginLoader.java   View source code 5 votes vote down vote up
private void removeClass(String name) {
    Class<?> clazz = classes.remove(name);

    try {
        if ((clazz != null) && (ConfigurationSerializable.class.isAssignableFrom(clazz))) {
            Class<? extends ConfigurationSerializable> serializable = clazz.asSubclass(ConfigurationSerializable.class);
            ConfigurationSerialization.unregisterClass(serializable);
        }
    } catch (NullPointerException ex) {
        // Boggle!
        // (Native methods throwing NPEs is not fun when you can't stop it before-hand)
    }
}