Refactor configurate dao

This commit is contained in:
Luck 2017-11-23 22:51:05 +00:00
parent d992d7f77c
commit ae8fe0fe1e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 39 additions and 91 deletions

View File

@ -57,10 +57,12 @@ import me.lucko.luckperms.common.utils.ImmutableCollectors;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.SimpleConfigurationNode;
import ninja.leaping.configurate.Types;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -104,13 +106,21 @@ public abstract class ConfigurateDao extends AbstractDao {
this.dataFolderName = dataFolderName;
}
protected abstract ConfigurationLoader<? extends ConfigurationNode> loader(Path path);
private ConfigurationNode readFile(StorageLocation location, String name) throws IOException {
File file = new File(getDirectory(location), name + fileExtension);
registerFileAction(location, file);
return readFile(file);
}
protected abstract ConfigurationNode readFile(File file) throws IOException;
private ConfigurationNode readFile(File file) throws IOException {
if (!file.exists()) {
return null;
}
return loader(file.toPath()).load();
}
private void saveFile(StorageLocation location, String name, ConfigurationNode node) throws IOException {
File file = new File(getDirectory(location), name + fileExtension);
@ -118,7 +128,16 @@ public abstract class ConfigurateDao extends AbstractDao {
saveFile(file, node);
}
protected abstract void saveFile(File file, ConfigurationNode node) throws IOException;
private void saveFile(File file, ConfigurationNode node) throws IOException {
if (node == null) {
if (file.exists()) {
file.delete();
}
return;
}
loader(file.toPath()).save(node);
}
private File getDirectory(StorageLocation location) {
switch (location) {

View File

@ -29,11 +29,11 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class HoconDao extends ConfigurateDao {
@ -42,33 +42,10 @@ public class HoconDao extends ConfigurateDao {
}
@Override
protected ConfigurationNode readFile(File file) throws IOException {
if (!file.exists()) {
return null;
}
HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
.setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8))
.setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8))
protected ConfigurationLoader<? extends ConfigurationNode> loader(Path path) {
return HoconConfigurationLoader.builder()
.setSource(() -> Files.newBufferedReader(path, StandardCharsets.UTF_8))
.setSink(() -> Files.newBufferedWriter(path, StandardCharsets.UTF_8))
.build();
return loader.load();
}
@Override
protected void saveFile(File file, ConfigurationNode node) throws IOException {
if (node == null) {
if (file.exists()) {
file.delete();
}
return;
}
HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
.setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8))
.setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8))
.build();
loader.save(node);
}
}

View File

@ -29,11 +29,11 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.gson.GsonConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class JsonDao extends ConfigurateDao {
@ -42,35 +42,11 @@ public class JsonDao extends ConfigurateDao {
}
@Override
protected ConfigurationNode readFile(File file) throws IOException {
if (!file.exists()) {
return null;
}
GsonConfigurationLoader loader = GsonConfigurationLoader.builder()
protected ConfigurationLoader<? extends ConfigurationNode> loader(Path path) {
return GsonConfigurationLoader.builder()
.setIndent(2)
.setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8))
.setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8))
.setSource(() -> Files.newBufferedReader(path, StandardCharsets.UTF_8))
.setSink(() -> Files.newBufferedWriter(path, StandardCharsets.UTF_8))
.build();
return loader.load();
}
@Override
protected void saveFile(File file, ConfigurationNode node) throws IOException {
if (node == null) {
if (file.exists()) {
file.delete();
}
return;
}
GsonConfigurationLoader loader = GsonConfigurationLoader.builder()
.setIndent(2)
.setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8))
.setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8))
.build();
loader.save(node);
}
}

View File

@ -30,12 +30,12 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import org.yaml.snakeyaml.DumperOptions;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class YamlDao extends ConfigurateDao {
@ -44,36 +44,12 @@ public class YamlDao extends ConfigurateDao {
}
@Override
protected ConfigurationNode readFile(File file) throws IOException {
if (!file.exists()) {
return null;
}
YAMLConfigurationLoader loader = YAMLConfigurationLoader.builder()
.setFlowStyle(DumperOptions.FlowStyle.BLOCK)
.setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8))
.setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8))
.build();
return loader.load();
}
@Override
protected void saveFile(File file, ConfigurationNode node) throws IOException {
if (node == null) {
if (file.exists()) {
file.delete();
}
return;
}
YAMLConfigurationLoader loader = YAMLConfigurationLoader.builder()
protected ConfigurationLoader<? extends ConfigurationNode> loader(Path path) {
return YAMLConfigurationLoader.builder()
.setFlowStyle(DumperOptions.FlowStyle.BLOCK)
.setIndent(2)
.setSource(() -> Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8))
.setSink(() -> Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8))
.setSource(() -> Files.newBufferedReader(path, StandardCharsets.UTF_8))
.setSink(() -> Files.newBufferedWriter(path, StandardCharsets.UTF_8))
.build();
loader.save(node);
}
}