Skip to content

Commit

Permalink
add defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsinco committed Jul 25, 2024
1 parent 3322803 commit fef1ac9
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 44 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'dev.jsinco.abstractjavafilelib'
version = '2.2'
version = '2.4'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public List<String> getKeys() {
}

public boolean contains(String key) {
return data.containsKey(key);
return getLastFromSection(key) != null;
}

@Nullable
Expand Down Expand Up @@ -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<String> getStringList(String path) {
final Object object = getLastFromSection(path);
if (object instanceof AbstractList<?>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit fef1ac9

Please sign in to comment.