package musician101.common.java.minecraft.sponge.config;

import musician101.common.java.config.JSONConfig;
import org.json.simple.parser.ParseException;
import org.spongepowered.api.data.DataQuery;
import org.spongepowered.api.data.MemoryDataContainer;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.service.persistence.DataBuilder;
import org.spongepowered.api.service.persistence.SerializationService;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@SuppressWarnings({"unchecked", "unused"})
public class SpongeJSONConfig extends JSONConfig
{
    public SpongeJSONConfig()
    {
        super();
    }

    public static SpongeJSONConfig load(File file) throws IOException, ParseException
    {
        return (SpongeJSONConfig) JSONConfig.load(file);
    }

    public static SpongeJSONConfig load(String string) throws ParseException
    {
        return (SpongeJSONConfig) JSONConfig.load(string);
    }

    public SpongeJSONConfig getSpongeJSONConfig(String key)
    {
        return (SpongeJSONConfig) getJSONConfig(key);
    }

    public ItemStack getItemStack(SerializationService ss, String key)
    {
        DataBuilder<ItemStack> db = ss.getBuilder(ItemStack.class).get();
        return db.build(new MemoryDataContainer().set(DataQuery.of(), get(key))).get();
    }

    public void setItemStack(String key, ItemStack itemStack)
    {
        put(key, itemStack.toContainer().toString());
    }

    public List<ItemStack> getItemStackList(SerializationService ss, String key)
    {
        return getList(key).stream().map(object -> ss.getBuilder(ItemStack.class).get().build(new MemoryDataContainer().set(DataQuery.of(), object)).get()).collect(Collectors.toList());
    }

    public void setItemStackList(String key, List<ItemStack> list)
    {
        put(key, list.stream().map(itemStack -> itemStack.toContainer().toString()));
    }
}

@@ -192,16 +192,68 @@
 aQuery;%0A
+import org.spongepowered.api.data.DataSerializable;%0A
 import o
@@ -387,39 +387,27 @@
 api.
-service.persistence.DataBuilder
+potion.PotionEffect
 ;%0Aim
@@ -548,16 +548,43 @@
 l.List;%0A
+import java.util.Optional;%0A
 import j
@@ -706,16 +706,46 @@
 onfig%0A%7B%0A
+    SerializationService ss;%0A%0A
     publ
@@ -788,32 +788,136 @@
 super();%0A    %7D%0A%0A
+    public SpongeJSONConfig(SerializationService ss)%0A    %7B%0A        this();%0A        this.ss = ss;%0A    %7D%0A%0A
     public stati
@@ -1371,203 +1371,72 @@
 ck(S
-erializationService ss, String key)%0A    %7B%0A        DataBuilder%3CItemStack%3E db = ss.getBuilder(ItemStack.class).get();%0A        return db.build(new MemoryDataContainer().set(DataQuery.of(), get(key))
+tring key)%0A    %7B%0A        return getDataView(key, ItemStack.class
 ).ge
@@ -1624,33 +1624,8 @@
 ist(
-SerializationService ss, 
 Stri
@@ -1693,22 +1693,28 @@
  -%3E 
-ss.getBuilder(
+getDataView(object, 
 Item
@@ -1735,68 +1735,371 @@
 et()
-.build(new MemoryDataContainer().set(DataQuery.of(), object)
+).collect(Collectors.toList());%0A    %7D%0A%0A    public void setItemStackList(String key, List%3CItemStack%3E list)%0A    %7B%0A        put(key, list.stream().map(itemStack -%3E itemStack.toContainer().toString()));%0A    %7D%0A%0A    public List%3CPotionEffect%3E getPotionEffectsList(String key)%0A    %7B%0A        return getList(key).stream().map(object -%3E getDataView(object, PotionEffect.class
 ).ge
@@ -2155,33 +2155,37 @@
 lic void set
-ItemStack
+PotionEffects
 List(String 
@@ -2190,33 +2190,36 @@
 g key, List%3C
-ItemStack
+PotionEffect
 %3E list)%0A    
@@ -2259,54 +2259,602 @@
 map(
-itemStack -%3E itemStack.toContainer().toString(
+effect -%3E effect.toContainer().toString()));%0A    %7D%0A%0A    public PotionEffect getPotionEffect(String key)%0A    %7B%0A        return getDataView(key, PotionEffect.class).get();%0A    %7D%0A%0A    private %3CT extends DataSerializable%3E Optional%3CT%3E getDataView(Object object, Class%3CT%3E clazz)%0A    %7B%0A        return ss.getBuilder(clazz).get().build(new MemoryDataContainer().set(DataQuery.of(), get(object)));%0A    %7D%0A%0A    private %3CT extends DataSerializable%3E Optional%3CT%3E getDataView(String key, Class%3CT%3E clazz)%0A    %7B%0A        return ss.getBuilder(clazz).get().build(new MemoryDataContainer().set(DataQuery.of(), get(key
 )));

Add new constructor to SpongeJSONConfig that accepts an instance of Sponge's SerializationService as a parameter. Added a base method that gets an instance of Optional<T extends DataSerializable> for cleaner code.