Skip to content

Commit d1ef761

Browse files
committed
Added .clear(), .delete() and .exists() to YamlConfig
1 parent 51afbae commit d1ef761

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

buildnumber.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#maven.buildNumber.plugin properties file
2-
#Wed Apr 09 22:46:14 CEST 2014
3-
buildNumber=104
2+
#Sun May 11 21:36:29 CEST 2014
3+
buildNumber=105

src/main/java/net/pravian/bukkitlib/config/YamlConfig.java

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public YamlConfig(Plugin plugin, File file, boolean copyDefaults) {
8282
this.COPY_DEFAULTS = copyDefaults;
8383
}
8484

85+
/**
86+
* Verifies if the config file exists.
87+
*
88+
* @return True if the config exists.
89+
*/
90+
public boolean exists() {
91+
return CONFIG_FILE.exists();
92+
}
93+
8594
/**
8695
* @see #set(java.lang.String, java.lang.Object)
8796
*/
@@ -389,21 +398,6 @@ public <K, V> Map<K, V> getMap(String path) {
389398
return keyMap;
390399
}
391400

392-
/**
393-
* Saves the configuration to the predefined file.
394-
*
395-
* @see #YamlConfig(Plugin, String, boolean)
396-
*/
397-
@Override
398-
public void save() {
399-
try {
400-
super.save(CONFIG_FILE);
401-
} catch (Exception ex) {
402-
InternalExceptionHandler.handle(PLUGIN, "Could not save configuration file: " + CONFIG_FILE.getName());
403-
InternalExceptionHandler.handle(PLUGIN, ex);
404-
}
405-
}
406-
407401
/**
408402
* Loads the configuration from the predefined file.
409403
*
@@ -415,7 +409,7 @@ public void save() {
415409
public void load() {
416410
try {
417411
if (COPY_DEFAULTS) {
418-
if (!CONFIG_FILE.exists()) {
412+
if (!exists()) {
419413
CONFIG_FILE.getParentFile().mkdirs();
420414
try {
421415
FileUtils.copy(PLUGIN.getResource(CONFIG_FILE.getName()), CONFIG_FILE);
@@ -436,6 +430,39 @@ public void load() {
436430
}
437431
}
438432

433+
/**
434+
* Saves the configuration to the predefined file.
435+
*
436+
* @see #YamlConfig(Plugin, String, boolean)
437+
*/
438+
@Override
439+
public void save() {
440+
try {
441+
super.save(CONFIG_FILE);
442+
} catch (Exception ex) {
443+
InternalExceptionHandler.handle(PLUGIN, "Could not save configuration file: " + CONFIG_FILE.getName());
444+
InternalExceptionHandler.handle(PLUGIN, ex);
445+
}
446+
}
447+
448+
/**
449+
* Deletes all the values in the config.
450+
*/
451+
public void clear() {
452+
for (String key : super.getKeys(false)) {
453+
super.set(key, null);
454+
}
455+
}
456+
457+
/**
458+
* Deletes the config if it exists.
459+
*/
460+
public void delete() {
461+
if (exists()) {
462+
CONFIG_FILE.delete();
463+
}
464+
}
465+
439466
/**
440467
* Returns the raw YamlConfiguration this config is based on.
441468
*

0 commit comments

Comments
 (0)