/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.config;

import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMapper;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataSerializable;
import org.spongepowered.api.data.persistence.DataTranslators;

/**
 * An implementation of {@link TypeSerializer} so that DataSerializables can be
 * provided in {@link ObjectMapper}-using classes.
 */
public class DataSerializableTypeSerializer implements TypeSerializer<DataSerializable> {

    @Override
    public DataSerializable deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
        Class<?> clazz = type.getRawType();
        return Sponge.getDataManager()
                .deserialize(clazz.asSubclass(DataSerializable.class), DataTranslators.CONFIGURATION_NODE.translate(value))
                .orElseThrow(() -> new ObjectMappingException("Could not translate DataSerializable of type: " + clazz.getName()));
    }

    @Override
    public void serialize(TypeToken<?> type, DataSerializable obj, ConfigurationNode value) throws ObjectMappingException {
        value.setValue(DataTranslators.CONFIGURATION_NODE.translate(obj.toContainer()));
    }
}

@@ -1580,24 +1580,66 @@
 Serializer;%0A
+import org.spongepowered.api.CatalogType;%0A
 import org.s
@@ -1663,16 +1663,16 @@
 Sponge;%0A
-
 import o
@@ -2131,32 +2131,207 @@
 pingException %7B%0A
+        if (type.getRawType().isAssignableFrom(CatalogType.class)) %7B%0A            return (DataSerializable) new CatalogTypeTypeSerializer().deserialize(type, value);%0A        %7D%0A
         Class%3C?%3E
@@ -2790,32 +2790,182 @@
 pingException %7B%0A
+        if (obj instanceof CatalogType) %7B%0A            new CatalogTypeTypeSerializer().serialize(type, (CatalogType) obj, value);%0A        %7D else %7B%0A    
         value.se
@@ -3033,20 +3033,30 @@
 tainer()));%0A
+        %7D%0A
     %7D%0A%7D%0A

Implement workaround for Configurate using DataSerializableTypeSerializer instead of CatalogTypeTypeSerializer randomly. Fixes #1171. See zml2008/configurate#75.