Created
July 18, 2019 23:39
-
-
Save MrMaurice211/a651b6f990d988f339e509f2ff930efa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.nio.file.CopyOption; | |
import java.nio.file.Files; | |
import java.util.List; | |
import org.bukkit.configuration.Configuration; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import com.google.common.collect.Lists; | |
import lombok.Getter; | |
public class ConfigManager { | |
private static File dataFolder = #YourDataFolderHere; | |
private static List<Config> configs = Lists.newArrayList(); | |
public static Config of(String name) { | |
Config cfg = configs.stream().filter(c -> c.getConfigName().equals(name + ".yml")).findFirst().orElse(null); | |
if (cfg != null) | |
return cfg; | |
cfg = new Config(name); | |
configs.add(cfg.reload()); | |
return cfg; | |
} | |
public static class Config { | |
@Getter //Lombok or just create a getter method | |
private String configName; | |
private File configFile; | |
private YamlConfiguration config; | |
public Config(String name) { | |
configName = name + ".yml"; | |
configFile = new File(dataFolder, configName); | |
} | |
public Config save() { | |
try { | |
config.save(configFile); | |
} catch (Exception e) { | |
} | |
return this; | |
} | |
public Configuration get() { | |
if (config == null) | |
reload(); | |
return config; | |
} | |
public Config reload() { | |
try (InputStream is = getClass().getResourceAsStream("/" + configName)) { | |
if (!configFile.exists()) { | |
Files.copy(is, configFile.toPath(), new CopyOption[0]); | |
} | |
config = YamlConfiguration.loadConfiguration(configFile); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return this; | |
} | |
public Config set(String key, Object value) { | |
get().set(key, value); | |
return this; | |
} | |
@SuppressWarnings("unchecked") | |
public <T> T get(String key, T def) { | |
if (key == null || key.isEmpty()) | |
return def; | |
Object result = get().get(key); | |
if (result == null) | |
return def; | |
return (T) result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment