Added preliminary JSON support

This commit is contained in:
Auxilor
2021-06-14 16:56:10 +01:00
parent 0ce9119a94
commit 1e3e54b8c5
7 changed files with 589 additions and 20 deletions

View File

@@ -0,0 +1,21 @@
package com.willfp.eco.core.config;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.internal.config.LoadableJsonConfig;
import com.willfp.eco.internal.config.LoadableYamlConfig;
import org.jetbrains.annotations.NotNull;
public abstract class JsonStaticBaseConfig extends LoadableJsonConfig {
/**
* Config implementation for configs present in the plugin's base directory (eg config.json, lang.json).
* <p>
* Does not automatically update.
*
* @param configName The name of the config
* @param plugin The plugin.
*/
protected JsonStaticBaseConfig(@NotNull final String configName,
@NotNull final EcoPlugin plugin) {
super(configName, plugin, "", plugin.getClass());
}
}

View File

@@ -7,7 +7,7 @@ public class ConfigSection extends ConfigWrapper<ConfigurationSection> {
/**
* Config section.
*
* @param section The section.
* @param section The section.
*/
public ConfigSection(@NotNull final ConfigurationSection section) {
this.init(section);

View File

@@ -0,0 +1,17 @@
package com.willfp.eco.internal.config;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
public class JSONConfigSection extends JSONConfigWrapper {
/**
* Config section.
*
* @param values The values.
*/
public JSONConfigSection(@NotNull final Map<String, Object> values) {
this.init(values);
}
}

View File

@@ -0,0 +1,278 @@
package com.willfp.eco.internal.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.config.Config;
import com.willfp.eco.util.StringUtils;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@SuppressWarnings({"unchecked", "unused"})
public abstract class JSONConfigWrapper implements Config {
/**
* The linked {@link ConfigurationSection} where values are physically stored.
*/
@Getter
private final Gson handle = new GsonBuilder().setPrettyPrinting().create();
/**
* All stored values.
*/
@Getter
private final Map<String, Object> values = new HashMap<>();
/**
* Abstract config.
*/
protected JSONConfigWrapper() {
}
public void init(@NotNull final Map<String, Object> values) {
this.values.clear();
this.values.putAll(values);
}
@Override
public final void clearCache() {
// Do nothing.
}
@Override
public boolean has(@NotNull final String path) {
return values.containsKey(path);
}
@NotNull
@Override
public List<String> getKeys(final boolean deep) {
return new ArrayList<>(values.keySet());
}
@Override
@Nullable
public Object get(@NotNull final String path) {
return values.get(path);
}
@Override
public void set(@NotNull final String path,
@Nullable final Object object) {
values.put(path, object);
}
@Override
@NotNull
public Config getSubsection(@NotNull final String path) {
Config subsection = getSubsectionOrNull(path);
Validate.notNull(subsection);
return subsection;
}
@Override
@Nullable
public Config getSubsectionOrNull(@NotNull final String path) {
}
@Override
public int getInt(@NotNull final String path) {
if (values.containsKey(path)) {
return (int) values.get(path);
} else {
values.put(path, 0);
return getInt(path);
}
}
@Override
@Nullable
public Integer getIntOrNull(@NotNull final String path) {
if (has(path)) {
return getInt(path);
} else {
return null;
}
}
@Override
public int getInt(@NotNull final String path,
final int def) {
if (values.containsKey(path)) {
return (int) values.get(path);
} else {
values.put(path, def);
return getInt(path);
}
}
@Override
@NotNull
public List<Integer> getInts(@NotNull final String path) {
if (values.containsKey(path)) {
return (List<Integer>) values.get(path);
} else {
values.put(path, 0);
return getInts(path);
}
}
@Override
@Nullable
public List<Integer> getIntsOrNull(@NotNull final String path) {
if (has(path)) {
return getInts(path);
} else {
return null;
}
}
@Override
public boolean getBool(@NotNull final String path) {
if (values.containsKey(path)) {
return (Boolean) values.get(path);
} else {
values.put(path, 0);
return getBool(path);
}
}
@Override
@Nullable
public Boolean getBoolOrNull(@NotNull final String path) {
if (has(path)) {
return getBool(path);
} else {
return null;
}
}
@Override
@NotNull
public List<Boolean> getBools(@NotNull final String path) {
if (values.containsKey(path)) {
return (List<Boolean>) values.get(path);
} else {
values.put(path, 0);
return getBools(path);
}
}
@Override
@Nullable
public List<Boolean> getBoolsOrNull(@NotNull final String path) {
if (has(path)) {
return getBools(path);
} else {
return null;
}
}
@Override
@NotNull
public String getString(@NotNull final String path) {
if (values.containsKey(path)) {
return (String) values.get(path);
} else {
values.put(path, 0);
return getString(path);
}
}
@Override
@Nullable
public String getStringOrNull(@NotNull final String path) {
if (has(path)) {
return getString(path);
} else {
return null;
}
}
@Override
@NotNull
public List<String> getStrings(@NotNull final String path) {
if (values.containsKey(path)) {
return (List<String>) values.get(path);
} else {
values.put(path, 0);
return getStrings(path);
}
}
@Override
@Nullable
public List<String> getStringsOrNull(@NotNull final String path) {
if (has(path)) {
return getStrings(path);
} else {
return null;
}
}
@Override
public double getDouble(@NotNull final String path) {
if (values.containsKey(path)) {
return (double) values.get(path);
} else {
values.put(path, 0);
return getDouble(path);
}
}
@Override
@Nullable
public Double getDoubleOrNull(@NotNull final String path) {
if (has(path)) {
return getDouble(path);
} else {
return null;
}
}
@Override
@NotNull
public List<Double> getDoubles(@NotNull final String path) {
if (values.containsKey(path)) {
return (List<Double>) values.get(path);
} else {
values.put(path, 0);
return getDoubles(path);
}
}
@Override
@Nullable
public List<Double> getDoublesOrNull(@NotNull final String path) {
if (has(path)) {
return getDoubles(path);
} else {
return null;
}
}
}

View File

@@ -0,0 +1,70 @@
package com.willfp.eco.internal.config;
import com.willfp.eco.core.EcoPlugin;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
public interface LoadableConfig {
/**
* Create the file.
*/
void createFile();
/**
* Get resource path as relative to base directory.
*
* @return The resource path.
*/
String getResourcePath();
/**
* Get YamlConfiguration as found in jar.
*
* @return The YamlConfiguration.
*/
YamlConfiguration getConfigInJar();
/**
* Save the config.
*
* @throws IOException If error in saving.
*/
void save() throws IOException;
/**
* Get the config file.
*
* @return The file.
*/
File getConfigFile();
/**
* Get the plugin.
*
* @return The plugin.
*/
EcoPlugin getPlugin();
/**
* Get the config name (including extension)
*
* @return The name.
*/
String getName();
/**
* Get the subdirectory path.
*
* @return The path.
*/
String getSubDirectoryPath();
/**
* Get the source/provider.
*
* @return The source.
*/
Class<?> getSource();
}

View File

@@ -0,0 +1,194 @@
package com.willfp.eco.internal.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.config.Config;
import com.willfp.eco.util.StringUtils;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.commons.lang.Validate;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@SuppressWarnings({"unchecked", "unused"})
public abstract class LoadableJsonConfig extends JSONConfigWrapper implements LoadableConfig {
/**
* The physical config file, as stored on disk.
*/
@Getter(AccessLevel.PROTECTED)
private final File configFile;
/**
* Plugin handle.
*/
@Getter(AccessLevel.PROTECTED)
private final EcoPlugin plugin;
/**
* The full name of the config file (eg config.json).
*/
@Getter
private final String name;
/**
* The subdirectory path.
*/
@Getter(AccessLevel.PROTECTED)
private final String subDirectoryPath;
/**
* The provider of the config.
*/
@Getter(AccessLevel.PROTECTED)
private final Class<?> source;
/**
* Abstract config.
*
* @param configName The name of the config
* @param plugin The plugin.
* @param subDirectoryPath The subdirectory path.
* @param source The class that owns the resource.
*/
protected LoadableJsonConfig(@NotNull final String configName,
@NotNull final EcoPlugin plugin,
@NotNull final String subDirectoryPath,
@NotNull final Class<?> source) {
this.plugin = plugin;
this.name = configName + ".json";
this.source = source;
this.subDirectoryPath = subDirectoryPath;
File directory = new File(this.getPlugin().getDataFolder(), subDirectoryPath);
if (!directory.exists()) {
directory.mkdirs();
}
if (!new File(directory, this.name).exists()) {
createFile();
}
this.configFile = new File(directory, this.name);
try {
init(this.configFile);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void createFile() {
String resourcePath = getResourcePath();
InputStream in = source.getResourceAsStream(resourcePath);
File outFile = new File(this.getPlugin().getDataFolder(), resourcePath);
int lastIndex = resourcePath.lastIndexOf('/');
File outDir = new File(this.getPlugin().getDataFolder(), resourcePath.substring(0, Math.max(lastIndex, 0)));
if (!outDir.exists()) {
outDir.mkdirs();
}
try {
if (!outFile.exists()) {
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
}
} catch (IOException ignored) {
}
}
/**
* Get resource path as relative to base directory.
*
* @return The resource path.
*/
@Override
public String getResourcePath() {
String resourcePath;
if (subDirectoryPath.isEmpty()) {
resourcePath = name;
} else {
resourcePath = subDirectoryPath + name;
}
return "/" + resourcePath;
}
/**
* Get YamlConfiguration as found in jar.
*
* @return The YamlConfiguration.
*/
@Override
public YamlConfiguration getConfigInJar() {
InputStream newIn = source.getResourceAsStream(getResourcePath());
if (newIn == null) {
throw new NullPointerException(name + " is null?");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(newIn, StandardCharsets.UTF_8));
YamlConfiguration newConfig = new YamlConfiguration();
try {
newConfig.load(reader);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
return newConfig;
}
/**
* Save the config.
*
* @throws IOException If error in saving.
*/
@Override
public void save() throws IOException {
String json = this.getHandle().toJson(this.getValues());
configFile.delete();
Files.write(configFile.toPath(), json.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
}
/**
* Initialize the config.
*
* @param file The config file.
* @throws FileNotFoundException If the file doesn't exist.
*/
public void init(@NotNull final File file) throws FileNotFoundException {
super.init(this.getHandle().fromJson(new FileReader(file), HashMap.class));
}
}

View File

@@ -16,7 +16,7 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public abstract class LoadableYamlConfig extends ConfigWrapper<YamlConfiguration> {
public abstract class LoadableYamlConfig extends ConfigWrapper<YamlConfiguration> implements LoadableConfig {
/**
* The physical config file, as stored on disk.
*/
@@ -77,7 +77,8 @@ public abstract class LoadableYamlConfig extends ConfigWrapper<YamlConfiguration
init(YamlConfiguration.loadConfiguration(configFile));
}
private void createFile() {
@Override
public void createFile() {
String resourcePath = getResourcePath();
InputStream in = source.getResourceAsStream(resourcePath);
@@ -104,12 +105,8 @@ public abstract class LoadableYamlConfig extends ConfigWrapper<YamlConfiguration
}
}
/**
* Get resource path as relative to base directory.
*
* @return The resource path.
*/
protected String getResourcePath() {
@Override
public String getResourcePath() {
String resourcePath;
if (subDirectoryPath.isEmpty()) {
@@ -121,12 +118,8 @@ public abstract class LoadableYamlConfig extends ConfigWrapper<YamlConfiguration
return "/" + resourcePath;
}
/**
* Get YamlConfiguration as found in jar.
*
* @return The YamlConfiguration.
*/
protected YamlConfiguration getConfigInJar() {
@Override
public YamlConfiguration getConfigInJar() {
InputStream newIn = source.getResourceAsStream(getResourcePath());
if (newIn == null) {
@@ -145,11 +138,7 @@ public abstract class LoadableYamlConfig extends ConfigWrapper<YamlConfiguration
return newConfig;
}
/**
* Save the config.
*
* @throws IOException If error in saving.
*/
@Override
public void save() throws IOException {
this.getHandle().save(this.getConfigFile());
}