Fixed json config and moved to 1.14.4
This commit is contained in:
parent
c10ea55d7e
commit
9e38c52f73
4 changed files with 136 additions and 121 deletions
|
@ -1,6 +1,5 @@
|
|||
package nl.iobyte.themepark.api.config.objects;
|
||||
|
||||
import com.dumptruckman.bukkit.configuration.json.JsonConfiguration;
|
||||
import nl.iobyte.themepark.logger.ThemeParkLogger;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
package nl.iobyte.themepark.api.config.objects;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.yaml.snakeyaml.error.YAMLException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class JsonConfiguration extends FileConfiguration {
|
||||
|
||||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String saveToString() {
|
||||
return gson.toJson(this.map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFromString(@NotNull String contents) throws InvalidConfigurationException {
|
||||
Validate.notNull(contents, "Contents cannot be null");
|
||||
|
||||
Map input;
|
||||
try {
|
||||
input = this.gson.fromJson(contents, Map.class);
|
||||
} catch (YAMLException var4) {
|
||||
throw new InvalidConfigurationException(var4);
|
||||
} catch (ClassCastException var5) {
|
||||
throw new InvalidConfigurationException("Top level is not a Map.");
|
||||
}
|
||||
|
||||
if (input != null) {
|
||||
this.convertMapsToSections(input, this);
|
||||
}
|
||||
}
|
||||
|
||||
protected void convertMapsToSections(@NotNull Map<?, ?> input, @NotNull ConfigurationSection section) {
|
||||
for (Map.Entry<?, ?> entry : input.entrySet()) {
|
||||
String key = entry.getKey().toString();
|
||||
Object value = entry.getValue();
|
||||
if (value instanceof Map) {
|
||||
this.convertMapsToSections((Map) value, section.createSection(key));
|
||||
} else {
|
||||
section.set(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String buildHeader() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static YamlConfiguration loadConfiguration(@NotNull File file) {
|
||||
Validate.notNull(file, "File cannot be null");
|
||||
YamlConfiguration config = new YamlConfiguration();
|
||||
|
||||
try {
|
||||
config.load(file);
|
||||
} catch (IOException | InvalidConfigurationException var4) {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, var4);
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue