Nice stuff :)

This commit is contained in:
Paldiu 2021-02-08 22:33:11 -06:00
parent 9fe9f8e2ae
commit d645e818e9
8 changed files with 194 additions and 37 deletions

View File

@ -33,6 +33,8 @@ public final class SimplexCore extends SimplexAddon<SimplexCore> {
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) {

View File

@ -0,0 +1,5 @@
package io.github.paldiu.simplexcore.config;
public class Json {
// TODO: Write actual JSON implementations.
}

View File

@ -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<String, File, String> pathways() {
return new Trio<>(resourcePath, directory, fileName);
}
}
// TODO: Write a JSON Object Factory.
}

View File

@ -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<String> 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!");
}
}

View File

@ -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");
}

View File

@ -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;
}
}

View File

@ -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 {

View File

@ -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 <K, V> void mapFE(Map<K, V> map, BiConsumer<K, V> actions) {
map.forEach(actions);
}
public static boolean isValid(Validate validate) {
return validate.isValid();
}
}