org.bukkit.configuration.serialization.ConfigurationSerializable Java Examples

The following examples show how to use org.bukkit.configuration.serialization.ConfigurationSerializable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Serialization.java    From PlayerVaults with GNU General Public License v3.0 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 #2
Source File: ItemStackFlag.java    From HeavySpleef with GNU General Public License v3.0 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 #3
Source File: YAMLProcessor.java    From skript-yaml with MIT License 6 votes vote down vote up
/**
 * Prepare a value for serialization, in case it's not a native type (and we
 * don't want to serialize objects as YAML represented objects).
 * 
 * @param value
 *            the value to serialize
 * @return the new object
 */
@SuppressWarnings("unchecked")
private Object serialize(Object value) {
	if (value instanceof Map) {
		for(Entry<String, Object> entry : ((Map<String, Object>) value).entrySet())
			((Map<String, Object>) value).replace(entry.getKey(), entry.getValue(), serialize(entry.getValue()));
		return value;
	} else if (value instanceof List) {
		for (int i = 0; i < ((List<Object>) value).size(); i++)
			((List<Object>) value).set(i, serialize(((List<Object>) value).get(i)));
		return value;
	} else if (!(SkriptYamlRepresenter.contains(value) || value instanceof ConfigurationSerializable || value instanceof Number || value instanceof Map || value instanceof List)) {
		SerializedVariable.Value val = Classes.serialize(value);
		if (val == null)
			return null;

		// workaround for class 'ch.njol.skript.expressions.ExprTool$1$2'
		if (val.type.equals("itemstack"))	
			return Classes.deserialize(val.type, val.data);	// returns ItemStack instead of SkriptClass

		return new SkriptClass(val.type, val.data);
	}
	return value;
}
 
Example #4
Source File: SerializationHelper.java    From QuickShop-Reremake with GNU General Public License v3.0 6 votes vote down vote up
public static Object serialize(@NotNull Object value) {
    if (value instanceof Object[]) {
        value = new ArrayList<>(Arrays.asList((Object[]) value));
    }
    if (value instanceof Set && !(value instanceof SerializableSet)) {
        value = new SerializableSet((Set) value);
    }
    if (value instanceof ConfigurationSection) {
        return buildMap(((ConfigurationSection) value).getValues(false));
    } else if (value instanceof Map) {
        return buildMap((Map) value);
    } else if (value instanceof List) {
        return buildList((List) value);
    } else if (value instanceof ConfigurationSerializable) {
        ConfigurationSerializable serializable = (ConfigurationSerializable) value;
        Map<String, Object> values = new LinkedHashMap<>();
        values.put(
                ConfigurationSerialization.SERIALIZED_TYPE_KEY,
                ConfigurationSerialization.getAlias(serializable.getClass()));
        values.putAll(serializable.serialize());
        return buildMap(values);
    } else {
        return value;
    }
}
 
Example #5
Source File: BukkitSerializableAdapterFactory.java    From helper with MIT License 5 votes vote down vote up
@Override
public ConfigurationSerializable read(JsonReader in) {
    Map<String, Object> map = this.gson.fromJson(in, RAW_OUTPUT_TYPE);
    if (map == null) {
        return null;
    }
    deserializeChildren(map);
    return ConfigurationSerialization.deserializeObject(map);
}
 
Example #6
Source File: Serialization.java    From PlayerVaults with GNU General Public License v3.0 5 votes vote down vote up
@Deprecated
public static List<String> toString(Inventory inv) {
    List<String> result = new ArrayList<>();
    List<ConfigurationSerializable> items = new ArrayList<>();
    Collections.addAll(items, inv.getContents());
    for (ConfigurationSerializable cs : items) {
        if (cs == null) {
            result.add("null");
        } else {
            result.add(new JSONObject(serialize(cs)).toString());
        }
    }
    return result;
}
 
Example #7
Source File: Variables.java    From Skript with GNU General Public License v3.0 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 #8
Source File: Variables.java    From Skript with GNU General Public License v3.0 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 #9
Source File: ConfigurationSerializer.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Deprecated
@Nullable
public 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 #10
Source File: ConfigurationSerializer.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
public 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 #11
Source File: SkriptYamlRepresenter.java    From skript-yaml with MIT License 5 votes vote down vote up
private Node representConfigurationSerializable(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 #12
Source File: SkriptYamlRepresenter.java    From skript-yaml with MIT License 5 votes vote down vote up
public SkriptYamlRepresenter() {
	this.nullRepresenter = new Represent() {
		@Override
		public Node representData(Object o) {
			return representScalar(Tag.NULL, "");
		}
	};

	this.representers.put(SkriptClass.class, new RepresentSkriptClass());
	this.representers.put(ItemType.class, new RepresentSkriptItemType());
	this.representers.put(Slot.class, new RepresentSkriptSlot());
	this.representers.put(Date.class, new RepresentSkriptDate());
	this.representers.put(Time.class, new RepresentSkriptTime());
	this.representers.put(Timespan.class, new RepresentSkriptTimespan());
	this.representers.put(Color.class, new RepresentSkriptColor());
	this.representers.put(WeatherType.class, new RepresentSkriptWeather());

	this.representers.put(Vector.class, new RepresentVector());
	this.representers.put(Location.class, new RepresentLocation());

	this.multiRepresenters.put(ConfigurationSerializable.class, new RepresentConfigurationSerializable());

	for (Class<?> c : representers.keySet()) {
		if (c != null) {
			String name = c.getSimpleName();
			if (!representedClasses.contains(name))
				representedClasses.add(name);
		}
	}
}
 
Example #13
Source File: BukkitTypeSerializer.java    From helper with MIT License 5 votes vote down vote up
@Override
public void serialize(TypeToken<?> type, ConfigurationSerializable from, ConfigurationNode to) throws ObjectMappingException {
    Map<String, Object> serialized = from.serialize();

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

    to.setValue(map);
}
 
Example #14
Source File: BukkitSerializableAdapterFactory.java    From helper with MIT License 5 votes vote down vote up
@Override
public void write(JsonWriter out, ConfigurationSerializable value) {
    if (value == null) {
        this.gson.toJson(null, RAW_OUTPUT_TYPE, out);
        return;
    }
    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);

    this.gson.toJson(map, RAW_OUTPUT_TYPE, out);
}
 
Example #15
Source File: TSerializer.java    From TabooLib with MIT License 5 votes vote down vote up
public static <T extends ConfigurationSerializable> T deserializeCS(String s, Class<T> c) {
    YamlConfiguration y = new YamlConfiguration();
    try {
        y.loadFromString(s);
    } catch (InvalidConfigurationException var4) {
        return null;
    }
    Object o = y.get("value");
    return !c.isInstance(o) ? null : (T) o;
}
 
Example #16
Source File: JavaPluginLoader.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
void setClass(String name, 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
Source File: JavaPluginLoader.java    From Kettle with GNU General Public License v3.0 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
Source File: FireworkEffect.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param map the map to deserialize
 * @return the resulting serializable
 * @see ConfigurationSerializable
 */
public static ConfigurationSerializable deserialize(Map<String, Object> map) {
    Type type = Type.valueOf((String) map.get(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
Source File: YamlRepresenter.java    From Kettle with GNU General Public License v3.0 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
Source File: BukkitObjectOutputStream.java    From Kettle with GNU General Public License v3.0 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
Source File: BukkitSerializableAdapterFactory.java    From helper with MIT License 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 #22
Source File: Wrapper.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
static Wrapper<ImmutableMap<String, ?>> newWrapper(ConfigurationSerializable obj) {
    return new Wrapper<ImmutableMap<String, ?>>(ImmutableMap.<String, Object>builder().put(ConfigurationSerialization.SERIALIZED_TYPE_KEY, ConfigurationSerialization.getAlias(obj.getClass())).putAll(obj.serialize()).build());
}
 
Example #23
Source File: YamlRepresenter.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
public YamlRepresenter() {
    this.multiRepresenters.put(ConfigurationSection.class, new RepresentConfigurationSection());
    this.multiRepresenters.put(ConfigurationSerializable.class, new RepresentConfigurationSerializable());
}
 
Example #24
Source File: ConfigurationSerializer.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
public static String serializeCS(final ConfigurationSerializable o) {
	final YamlConfiguration y = new YamlConfiguration();
	y.set("value", o);
	return "" + y.saveToString();
}
 
Example #25
Source File: MemorySection.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@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;
}
 
Example #26
Source File: MemorySection.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@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 getSerializable(path, clazz, (def != null && clazz.isInstance(def)) ? clazz.cast(def) : null);
}
 
Example #27
Source File: Variables.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private final void init() {
	// used by asserts
	info = (ClassInfo<? extends ConfigurationSerializable>) (ClassInfo) Classes.getExactClassInfo(Object.class);
}
 
Example #28
Source File: BannerMeta.java    From Carbon with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static ConfigurationSerializable deserialize(Map<String, Object> map) {
	return new BannerPattern(((String) map.get("Pattern")), (int) map.get("Color"));
}
 
Example #29
Source File: BukkitTypeSerializer.java    From helper with MIT License 4 votes vote down vote up
@Override
public ConfigurationSerializable deserialize(TypeToken<?> type, ConfigurationNode from) throws ObjectMappingException {
    Map<String, Object> map = from.getValue(TYPE);
    deserializeChildren(map);
    return ConfigurationSerialization.deserializeObject(map);
}
 
Example #30
Source File: TSerializer.java    From TabooLib with MIT License 4 votes vote down vote up
public static String serializeCS(ConfigurationSerializable o) {
    YamlConfiguration y = new YamlConfiguration();
    y.set("value", o);
    return y.saveToString();
}