1111import java .util .logging .Level ;
1212import java .util .logging .Logger ;
1313
14+ @ SuppressWarnings ("unused" )
1415public class ConfigFile {
1516
1617 private final File configFile ;
@@ -25,7 +26,9 @@ public class ConfigFile {
2526 *
2627 * Please notice that the constructor does not yet create the YAML-configuration file. To create the file on the disk, use {@link ConfigFile#createConfig()}.
2728 */
29+ @ SuppressWarnings ("unused" )
2830 public ConfigFile (CoreJavaPlugin plugin , String fileName ) {
31+ fileName = fileName .replace ('\\' , '/' );
2932 this .fileName = fileName ;
3033 configFile = new File (plugin .getDataFolder (), fileName );
3134 this .pluginDataFolder = plugin .getDataFolder ();
@@ -35,18 +38,27 @@ public ConfigFile(CoreJavaPlugin plugin, String fileName) {
3538
3639 /**
3740 * This creates the configuration file. If the data folder is invalid, it will be created along with the config file.
41+ *
42+ * @return true if the file was successfully created
3843 */
39- public void createConfig () {
44+ public boolean createConfig () {
45+ boolean success = false ;
4046 if (! configFile .exists ()) {
4147 if (! this .pluginDataFolder .exists ()) {
42- this .pluginDataFolder .mkdir ();
48+ if (this .pluginDataFolder .mkdir ()) {
49+ success = true ;
50+ }
4351 }
4452 try {
45- configFile .createNewFile ();
53+ if (configFile .createNewFile ()) {
54+ success = true ;
55+ }
4656 } catch (IOException e ) {
4757 e .printStackTrace ();
58+ success = false ;
4859 }
4960 }
61+ return success ;
5062 }
5163
5264 /**
@@ -151,24 +163,35 @@ public boolean deleteDir() {
151163
152164 /**
153165 * @since 1.0.0
166+ * @return true if the file was successfully reset
154167 * This deletes and recreates the file, wiping all its contents.
155168 */
156- public void reset () {
169+ public boolean reset () {
157170 this .deleteFile ();
171+ boolean success ;
158172 try {
159- configFile .createNewFile ();
173+ success = configFile .createNewFile ();
160174 } catch (IOException e ) {
161175 e .printStackTrace ();
176+ success = false ;
162177 }
178+ return success ;
163179 }
164180
165181 /**
166182 * @since 1.0.0
183+ * @return true if the directory was successfully wiped
167184 * Wipe the config file's directory, including the file itself.
168185 */
169- public void wipeDirectory () {
170- this .getDirectory ().delete ();
171- this .pluginDataFolder .mkdir ();
186+ public boolean wipeDirectory () {
187+ boolean success = true ;
188+ if (!getDirectory ().delete ()) {
189+ success = false ;
190+ }
191+ if (!pluginDataFolder .mkdir ()) {
192+ success = false ;
193+ }
194+ return success ;
172195 }
173196
174197 /**
@@ -230,24 +253,22 @@ public InputStream getResource(String filename) {
230253
231254 public void saveDefaultConfig () {
232255 if (!configFile .exists ()) {
233- saveResource (fileName , false );
256+ saveResource ();
234257 }
235258 }
236259
237- protected void saveResource (String resourcePath , boolean replace ) {
238- if (resourcePath == null || resourcePath .equals ("" )) {
260+ protected void saveResource () {
261+ if (fileName == null || fileName .equals ("" )) {
239262 throw new IllegalArgumentException ("ResourcePath cannot be null or empty" );
240263 }
241-
242- resourcePath = resourcePath .replace ('\\' , '/' );
243- InputStream in = getResource (resourcePath );
264+ InputStream in = getResource (fileName );
244265 if (in == null ) {
245- throw new IllegalArgumentException ("The embedded resource '" + resourcePath + "' cannot be found in " + fileName );
266+ throw new IllegalArgumentException ("The embedded resource '" + fileName + "' cannot be found in " + fileName );
246267 }
247268
248- File outFile = new File (pluginDataFolder , resourcePath );
249- int lastIndex = resourcePath .lastIndexOf ('/' );
250- File outDir = new File (pluginDataFolder , resourcePath .substring (0 , Math .max (lastIndex , 0 )));
269+ File outFile = new File (pluginDataFolder , fileName );
270+ int lastIndex = fileName .lastIndexOf ('/' );
271+ File outDir = new File (pluginDataFolder , fileName .substring (0 , Math .max (lastIndex , 0 )));
251272
252273 if (!outDir .exists ()) {
253274 //noinspection ResultOfMethodCallIgnored
@@ -256,7 +277,7 @@ protected void saveResource(String resourcePath, boolean replace) {
256277 Logger logger = Bukkit .getLogger ();
257278
258279 try {
259- if (!outFile .exists () || replace ) {
280+ if (!outFile .exists ()) {
260281 OutputStream out = new FileOutputStream (outFile );
261282 byte [] buf = new byte [1024 ];
262283 int len ;
0 commit comments