diff --git a/.idea/misc.xml b/.idea/misc.xml index fe0b0da..f937d01 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/build.gradle b/build.gradle index 6d6450a..eabc049 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'dev.jsinco.abstractjavafilelib' -version = '2.2' +version = '2.4' repositories { mavenCentral() diff --git a/src/main/java/dev/jsinco/abstractjavafilelib/AbstractFileManager.java b/src/main/java/dev/jsinco/abstractjavafilelib/AbstractFileManager.java index 21da8a2..136be31 100644 --- a/src/main/java/dev/jsinco/abstractjavafilelib/AbstractFileManager.java +++ b/src/main/java/dev/jsinco/abstractjavafilelib/AbstractFileManager.java @@ -11,13 +11,21 @@ public abstract class AbstractFileManager extends ConfigurationSection { protected final static File dataFolder = FileLibSettings.getDataFolder(); protected final static LibLogger logger = FileLibSettings.getLogger(); - protected final File file; + protected File file; public AbstractFileManager(File file) { this.file = file; } + public void setFile(File file) { + this.file = file; + } + + public File getFile() { + return file; + } + public AbstractFileManager generateFile() { if (!file.exists()) { diff --git a/src/main/java/dev/jsinco/abstractjavafilelib/ConfigurationSection.java b/src/main/java/dev/jsinco/abstractjavafilelib/ConfigurationSection.java index 5dd541c..a397f0c 100644 --- a/src/main/java/dev/jsinco/abstractjavafilelib/ConfigurationSection.java +++ b/src/main/java/dev/jsinco/abstractjavafilelib/ConfigurationSection.java @@ -29,7 +29,7 @@ public List getKeys() { } public boolean contains(String key) { - return data.containsKey(key); + return getLastFromSection(key) != null; } @Nullable @@ -151,6 +151,75 @@ public short getShort(String path) { } + // + + + public String getString(String path, String defaultTo) { + final Object object = getLastFromSection(path); + if (object instanceof String) { + return (String) object; + } + return defaultTo; + } + + public int getInt(String path, int defaultTo) { + final Object object = getLastFromSection(path); + if (object instanceof Integer) { + return (int) object; + } + return defaultTo; + } + + public boolean getBoolean(String path, boolean defaultTo) { + final Object object = getLastFromSection(path); + if (object instanceof Boolean) { + return (boolean) object; + } + return defaultTo; + } + + public double getDouble(String path, double defaultTo) { + final Object object = getLastFromSection(path); + if (object instanceof Double) { + return (double) object; + } + return defaultTo; + } + + public long getLong(String path, long defaultTo) { + final Object object = getLastFromSection(path); + if (object instanceof Long) { + return (long) object; + } + return defaultTo; + } + + public float getFloat(String path, float defaultTo) { + final Object object = getLastFromSection(path); + if (object instanceof Float) { + return (float) object; + } + return defaultTo; + } + + public byte getByte(String path, byte defaultTo) { + final Object object = getLastFromSection(path); + if (object instanceof Byte) { + return (byte) object; + } + return defaultTo; + } + + public short getShort(String path, short defaultTo) { + final Object object = getLastFromSection(path); + if (object instanceof Short) { + return (short) object; + } + return defaultTo; + } + + // + public List getStringList(String path) { final Object object = getLastFromSection(path); if (object instanceof AbstractList) { diff --git a/src/main/java/dev/jsinco/abstractjavafilelib/schemas/JsonSavingSchema.java b/src/main/java/dev/jsinco/abstractjavafilelib/schemas/JsonSavingSchema.java index 58c7a9d..22613a0 100644 --- a/src/main/java/dev/jsinco/abstractjavafilelib/schemas/JsonSavingSchema.java +++ b/src/main/java/dev/jsinco/abstractjavafilelib/schemas/JsonSavingSchema.java @@ -75,26 +75,7 @@ public JsonSavingSchema loadFile() { @Override public JsonSavingSchema generateFile() { - if (!file.exists()) { - try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(file.getName())) { - file.getParentFile().mkdirs(); - file.createNewFile(); - - if (inputStream != null) { - OutputStream outputStream = Files.newOutputStream(file.toPath()); - byte[] buffer = new byte[1024]; - int bytesRead; - while ((bytesRead = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, bytesRead); - } - outputStream.flush(); - outputStream.close(); - } - - } catch (IOException e) { - logger.error("Error creating file: " + e.getMessage()); - } - } + super.generateFile(); return loadFile(); } diff --git a/src/main/java/dev/jsinco/abstractjavafilelib/schemas/SnakeYamlConfig.java b/src/main/java/dev/jsinco/abstractjavafilelib/schemas/SnakeYamlConfig.java index 2803fdd..2815623 100644 --- a/src/main/java/dev/jsinco/abstractjavafilelib/schemas/SnakeYamlConfig.java +++ b/src/main/java/dev/jsinco/abstractjavafilelib/schemas/SnakeYamlConfig.java @@ -78,26 +78,7 @@ public SnakeYamlConfig loadFile() { @Override public SnakeYamlConfig generateFile() { - if (!file.exists()) { - try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(file.getName())) { - file.getParentFile().mkdirs(); - file.createNewFile(); - - if (inputStream != null) { - OutputStream outputStream = Files.newOutputStream(file.toPath()); - byte[] buffer = new byte[1024]; - int bytesRead; - while ((bytesRead = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, bytesRead); - } - outputStream.flush(); - outputStream.close(); - } - - } catch (IOException e) { - logger.error("Error creating file: " + e.getMessage()); - } - } + super.generateFile(); return loadFile(); }