package com.skcraft.plume.common.util.config;

import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.sk89q.worldedit.util.eventbus.EventHandler.Priority;
import com.sk89q.worldedit.util.eventbus.Subscribe;
import com.skcraft.plume.common.event.lifecycle.LoadConfigEvent;
import com.skcraft.plume.common.util.module.AutoRegister;

import java.io.File;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

@Singleton
@AutoRegister
public class ConfigFactory {

    private final List<Config<?>> configs = Lists.newArrayList();

    private final File dir;

    @Inject
    public ConfigFactory(@DataDir File dir) {
        checkNotNull(dir, "dir");
        this.dir = dir;
    }

    public <T> Config<T> create(String name, Class<T> type) {
        File file = new File(dir, name + ".cfg");
        file.getAbsoluteFile().getParentFile().mkdirs();
        Config<T> config = new HoconConfig<T>(file, type);
        configs.add(config);
        return config;
    }

    @Subscribe(priority = Priority.VERY_EARLY)
    public void onLoadConfig(LoadConfigEvent event) {
        configs.forEach(Config::load);
    }

    @Subscribe(priority = Priority.VERY_LATE)
    public void onLoadConfigLate(LoadConfigEvent event) {
        configs.forEach(Config::save);
    }

}

@@ -70,20 +70,114 @@
 on.c
-ollect.Lists
+ache.CacheBuilder;%0Aimport com.google.common.cache.CacheLoader;%0Aimport com.google.common.cache.LoadingCache
 ;%0Aim
@@ -481,16 +481,36 @@
 egister;
+%0Aimport lombok.Data;
 %0A%0Aimport
@@ -527,31 +527,8 @@
 ile;
-%0Aimport java.util.List;
 %0A%0Aim
@@ -669,12 +669,25 @@
 al L
-ist%3C
+oadingCache%3CKey, 
 Conf
@@ -707,27 +707,442 @@
 s = 
-Lists.newArrayList(
+CacheBuilder.newBuilder()%0A            .concurrencyLevel(1)%0A            .build(new CacheLoader%3CKey, Config%3C?%3E%3E() %7B%0A                @Override%0A                public Config%3C?%3E load(Key key) throws Exception %7B%0A                    File file = new File(dir, key.name + %22.cfg%22);%0A                    file.getAbsoluteFile().getParentFile().mkdirs();%0A                    return new HoconConfig%3C%3E(file, key.type);%0A                %7D%0A            %7D
 );%0A%0A
@@ -1289,24 +1289,59 @@
 dir;%0A    %7D%0A%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     public %3C
@@ -1402,115 +1402,16 @@
     
-File file = new File(dir, name + %22.cfg%22);%0A        file.getAbsoluteFile().getParentFile().mkdirs();%0A        
+return (
 Conf
@@ -1419,100 +1419,51 @@
 g%3CT%3E
+)
  config
- = new HoconConfig%3CT%3E(file, type);%0A        configs.add(config);%0A        return config
+s.getUnchecked(new Key(name, type))
 ;%0A  
@@ -1576,32 +1576,49 @@
         configs.
+asMap().values().
 forEach(Config::
@@ -1751,16 +1751,33 @@
 configs.
+asMap().values().
 forEach(
@@ -1798,10 +1798,130 @@
 %0A    %7D%0A%0A
+    @Data%0A    private static class Key %7B%0A        private final String name;%0A        private final Class%3C?%3E type;%0A    %7D%0A%0A
 %7D%0A

Make ConfigFactory cache multiple references to the same config.