diff --git a/src/main/java/io/github/paldiu/simplexcore/SimplexCore.java b/src/main/java/io/github/paldiu/simplexcore/SimplexCore.java index e8d9042..a16f4a1 100644 --- a/src/main/java/io/github/paldiu/simplexcore/SimplexCore.java +++ b/src/main/java/io/github/paldiu/simplexcore/SimplexCore.java @@ -33,6 +33,8 @@ public final class SimplexCore extends SimplexAddon { try { Constants.getRegistry().register(this); Constants.getCommandLoader().classpath(Command_info.class).load(); + Constants.getConfig().reload(); + // SimplexListener.register(new ServerPluginListener(), this); new Announcer(); } catch (Exception ex) { diff --git a/src/main/java/io/github/paldiu/simplexcore/config/Json.java b/src/main/java/io/github/paldiu/simplexcore/config/Json.java new file mode 100644 index 0000000..6a2944c --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/config/Json.java @@ -0,0 +1,5 @@ +package io.github.paldiu.simplexcore.config; + +public class Json { + // TODO: Write actual JSON implementations. +} diff --git a/src/main/java/io/github/paldiu/simplexcore/config/JsonFactory.java b/src/main/java/io/github/paldiu/simplexcore/config/JsonFactory.java index 2a0f387..8e52afa 100644 --- a/src/main/java/io/github/paldiu/simplexcore/config/JsonFactory.java +++ b/src/main/java/io/github/paldiu/simplexcore/config/JsonFactory.java @@ -1,32 +1,5 @@ package io.github.paldiu.simplexcore.config; -import io.github.paldiu.simplexcore.plugin.SimplexAddon; -import io.github.paldiu.simplexcore.utils.Trio; - -import java.io.File; - public final class JsonFactory { - private final SimplexAddon plugin; - private String resourcePath; - private File directory; - private String fileName; - - public JsonFactory(SimplexAddon plugin) { - this.plugin = plugin; - } - - public JsonFactory setPathways(String resourcePath, File directory, String fileName) { - this.resourcePath = resourcePath; - this.directory = directory; - this.fileName = fileName; - return this; - } - - public JsonFactory setDefaultPathways() { - return setPathways("data.json", plugin.getDataFolder(), "data.json"); - } - - public Trio pathways() { - return new Trio<>(resourcePath, directory, fileName); - } -} \ No newline at end of file + // TODO: Write a JSON Object Factory. +} diff --git a/src/main/java/io/github/paldiu/simplexcore/config/Yaml.java b/src/main/java/io/github/paldiu/simplexcore/config/Yaml.java new file mode 100644 index 0000000..56aacef --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/config/Yaml.java @@ -0,0 +1,161 @@ +package io.github.paldiu.simplexcore.config; + +import io.github.paldiu.simplexcore.functional.Guard; +import io.github.paldiu.simplexcore.plugin.SimplexAddon; +import io.github.paldiu.simplexcore.utils.Bean; +import io.github.paldiu.simplexcore.utils.Constants; +import io.github.paldiu.simplexcore.utils.Utilities; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.nio.file.NotDirectoryException; +import java.util.List; + +public final class Yaml { + private final SimplexAddon plugin; + private final String fileName; + private final File directory; + private final String resourcePath; + private FileConfiguration config; + private File file; + + Yaml(SimplexAddon plugin, String fileName, File directory, String resourcePath) { + if (!fileName.endsWith(".yml")) { + fileName+=".yml"; + } + + if (!resourcePath.endsWith(".yml")) { + resourcePath+=".yml"; + } + + this.plugin = plugin; + this.fileName = fileName; + this.directory = directory; + this.resourcePath = resourcePath; + } + + public final void set(String path, Object value) { + this.getConfig().set(path, value); + } + + public boolean contains(String path) { + return this.getConfig().contains(path); + } + + public ConfigurationSection getConfigurationSection(String path) { + return this.getConfig().getConfigurationSection(path); + } + + public List getStringList(String path) { + return this.getConfig().getStringList(path); + } + + public long getLong(String path) { + return this.getConfig().getLong(path); + } + + public List getList(String path) { + return this.getConfig().getList(path); + } + + public boolean getBoolean(String path) { + return this.getConfig().getBoolean(path); + } + + public int getInt(String path) { + return this.getConfig().getInt(path); + } + + public double getDouble(String path) { + return this.getConfig().getDouble(path); + } + + public String getString(String path) { + return this.getConfig().getString(path); + } + + public long getLong(String path, long def) { + return this.getConfig().getLong(path, def); + } + + public List getList(String path, List def) { + return this.getConfig().getList(path, def); + } + + public boolean getBoolean(String path, boolean def) { + return this.getConfig().getBoolean(path, def); + } + + public int getInt(String path, int def) { + return this.getConfig().getInt(path, def); + } + + public double getDouble(String path, double def) { + return this.getConfig().getDouble(path, def); + } + + public String getString(String path, String def) { + return this.getConfig().getString(path, def); + } + + public Object get(String path, Object def) { + return this.getConfig().get(path, def); + } + + public final void reload() { + if (this.directory.exists()) { // Directory exist? + if (!this.directory.isDirectory()) { // File is directory? + try { // Not directory may contain important info! + throw new NotDirectoryException("'" + this.directory.getName() + "' is not a directory."); + } catch (NotDirectoryException e) { + e.printStackTrace(); + } + } + } else { // Directory doesn't exist. + this.directory.mkdir(); // Make directory + } + this.file = new File(this.directory, this.fileName); + boolean created = false; + if (!file.exists()) { + if (this.resourcePath != null) { + if (plugin.getResource(resourcePath) != null) { + plugin.getLogger().info("Attempting to create '" + this.fileName + "' from resources."); + plugin.saveResource(resourcePath, false); + } + } + created = true; + } + this.config = YamlConfiguration.loadConfiguration(this.file); + if (created) create(); + onReload(); + } + + public final void save() throws IOException { + config.save(file); + } + + public final boolean exists() { + return this.file.exists(); + } + + public final FileConfiguration getConfig() { + return this.config; + } + + /** + * Called when a file is created. + */ + public void create() { + Constants.getLogger().info("File created!"); + } + + /** + * Called when then file is reloaded + */ + public void onReload() { + Constants.getLogger().info("The plugin configuration has been reloaded!"); + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/config/YamlFactory.java b/src/main/java/io/github/paldiu/simplexcore/config/YamlFactory.java index fce5905..7e890df 100644 --- a/src/main/java/io/github/paldiu/simplexcore/config/YamlFactory.java +++ b/src/main/java/io/github/paldiu/simplexcore/config/YamlFactory.java @@ -15,14 +15,14 @@ public final class YamlFactory { this.plugin = plugin; } - public YamlFactory setPathways(String resourcePath, File directory, String fileName) { + public Yaml setPathways(String resourcePath, File directory, String fileName) { this.resourcePath = resourcePath; this.directory = directory; this.fileName = fileName; - return this; + return new Yaml(plugin, resourcePath, directory, fileName); } - public YamlFactory setDefaultPathways() { + public Yaml setDefaultPathways() { return setPathways("config.yml", plugin.getDataFolder(), "config.yml"); } diff --git a/src/main/java/io/github/paldiu/simplexcore/functional/Guard.java b/src/main/java/io/github/paldiu/simplexcore/functional/Guard.java new file mode 100644 index 0000000..83e2fc6 --- /dev/null +++ b/src/main/java/io/github/paldiu/simplexcore/functional/Guard.java @@ -0,0 +1,11 @@ +package io.github.paldiu.simplexcore.functional; + +@FunctionalInterface +public interface Guard { + void verify(); + + default Guard after(Guard clause) { + clause.verify(); + return this; + } +} diff --git a/src/main/java/io/github/paldiu/simplexcore/utils/Constants.java b/src/main/java/io/github/paldiu/simplexcore/utils/Constants.java index ef34643..f63bb59 100644 --- a/src/main/java/io/github/paldiu/simplexcore/utils/Constants.java +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Constants.java @@ -2,6 +2,8 @@ package io.github.paldiu.simplexcore.utils; import io.github.paldiu.simplexcore.SimplexCore; import io.github.paldiu.simplexcore.command.CommandLoader; +import io.github.paldiu.simplexcore.config.Yaml; +import io.github.paldiu.simplexcore.config.YamlFactory; import io.github.paldiu.simplexcore.plugin.AddonRegistry; import io.github.paldiu.simplexcore.plugin.DependencyManagement; import org.bukkit.Server; @@ -18,6 +20,8 @@ public final class Constants { private static final PluginManager manager = server.getPluginManager(); private static final BukkitScheduler scheduler = server.getScheduler(); private static final DependencyManagement dpm = new DependencyManagement(); + private static final Yaml config = new YamlFactory(plugin).setDefaultPathways(); + private static final TimeValues time = new TimeValues(); public static SimplexCore getPlugin() { return plugin; @@ -52,7 +56,11 @@ public final class Constants { } public static TimeValues getTimeValues() { - return new TimeValues(); + return time; + } + + public static Yaml getConfig() { + return config; } public static class TimeValues { diff --git a/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java b/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java index 9a1605b..1819b7c 100644 --- a/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java +++ b/src/main/java/io/github/paldiu/simplexcore/utils/Utilities.java @@ -1,5 +1,6 @@ package io.github.paldiu.simplexcore.utils; +import io.github.paldiu.simplexcore.functional.Guard; import io.github.paldiu.simplexcore.functional.Validate; import java.util.Arrays; @@ -31,8 +32,4 @@ public final class Utilities { public static void mapFE(Map map, BiConsumer actions) { map.forEach(actions); } - - public static boolean isValid(Validate validate) { - return validate.isValid(); - } }