Created
March 28, 2019 23:51
-
-
Save Crypnotic/a548b91d10b504f418d61ebdb92b5f96 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
package io.tagline.deejay.api.configuration; | |
import java.io.File; | |
import java.io.IOException; | |
import com.google.common.base.Preconditions; | |
import io.tagline.deejay.api.configuration.Configuration.Builder.ConfigType; | |
import io.tagline.deejay.util.FileIO; | |
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
import lombok.Setter; | |
import lombok.experimental.Accessors; | |
import ninja.leaping.configurate.ConfigurationNode; | |
import ninja.leaping.configurate.json.JSONConfigurationLoader; | |
import ninja.leaping.configurate.loader.ConfigurationLoader; | |
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; | |
@AllArgsConstructor | |
public class Configuration { | |
@Getter | |
private final File folder; | |
@Getter | |
private final File file; | |
@Getter | |
private final ConfigType type; | |
@Getter | |
private final ConfigurationLoader<?> loader; | |
@Getter | |
private ConfigurationNode root; | |
public boolean reload() { | |
ConfigurationNode fallback = root; | |
try { | |
this.root = loader.load(); | |
return true; | |
} catch (IOException exception) { | |
exception.printStackTrace(); | |
root = fallback; | |
return false; | |
} | |
} | |
public boolean save() { | |
try { | |
loader.save(root); | |
return true; | |
} catch (IOException exception) { | |
exception.printStackTrace(); | |
return false; | |
} | |
} | |
public static Builder builder() { | |
return new Builder(); | |
} | |
public static class Builder { | |
@Setter | |
@Accessors(fluent = true, chain = true) | |
private File folder = new File("").getAbsoluteFile(); | |
@Setter | |
@Accessors(fluent = true, chain = true) | |
private String name; | |
@Setter | |
@Accessors(fluent = true, chain = true) | |
private ConfigType type = ConfigType.JSON; | |
public Configuration build() { | |
Preconditions.checkNotNull(type); | |
Preconditions.checkNotNull(name); | |
try { | |
File file = FileIO.getOrCreate(folder, name); | |
ConfigurationLoader<?> loader; | |
if (type.isJson()) { | |
loader = JSONConfigurationLoader.builder().setFile(file).build(); | |
} else if (type.isYaml()) { | |
loader = YAMLConfigurationLoader.builder().setFile(file).build(); | |
} else { | |
throw new IllegalStateException("Undefined ConfigType"); | |
} | |
ConfigurationNode root = loader.load(); | |
return new Configuration(folder, file, type, loader, root); | |
} catch (IOException exception) { | |
exception.printStackTrace(); | |
} | |
return null; | |
} | |
public static enum ConfigType { | |
JSON, YAML; | |
public boolean isJson() { | |
return this == JSON; | |
} | |
public boolean isYaml() { | |
return this == YAML; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment