Package dev.majek.pc.data.storage
Class JsonConfig
- java.lang.Object
-
- dev.majek.pc.data.storage.JsonConfig
-
public class JsonConfig extends Object
Used for JSON configuration files.
-
-
Constructor Summary
Constructors Constructor Description JsonConfig(File pluginDataFolder, String name)
Please notice that the constructor does not yet create the JSON configuration file.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
createConfig()
This creates the configuration file.void
createSubDirectory(String name)
This will create a sub-directory in the plugin's data folder, which can be accessed withgetDirectory()
.void
deleteFile()
This deletes the config file.void
deleteParentDir()
This deletes the config file's directory and all it's contents.File
getDirectory()
File
getFile()
This returns the actual File object of the config file.String
getName()
This returns the name of the configuration file with the .json extension.void
putInJsonObject(String k, int v)
void
putInJsonObject(String k, com.google.gson.JsonElement v)
void
putInJsonObject(String k, String v)
void
removeFromJsonObject(String key)
Remove a key-value from the JSONObject insidegetFile()
.void
reset()
This deletes and recreates the file, wiping all its contents.com.google.gson.JsonObject
toJSONObject()
void
wipeDirectory()
Wipe the config file's directory, including the file itself.
-
-
-
Constructor Detail
-
JsonConfig
public JsonConfig(File pluginDataFolder, String name)
Please notice that the constructor does not yet create the JSON configuration file. To create the file on the disk, usecreateConfig()
.- Parameters:
pluginDataFolder
- The plugin's data directory, accessible with JavaPlugin#getDataFolder();name
- The name of the config file excluding file extensions.
-
-
Method Detail
-
createConfig
public void createConfig() throws FileNotFoundException
This creates the configuration file. If the data folder is invalid, it will be created along with the config file.- Throws:
FileNotFoundException
- if the file is not found
-
getDirectory
public File getDirectory()
- Returns:
- The configuration file's directory. To get its name, use
getName()
instead.
-
getName
public String getName()
This returns the name of the configuration file with the .json extension. To get the file's directory, usegetDirectory()
.- Returns:
- The name of the configuration file, including file extensions.
-
getFile
public File getFile()
This returns the actual File object of the config file.- Returns:
- The config file.
-
deleteFile
public void deleteFile()
This deletes the config file.
-
deleteParentDir
public void deleteParentDir()
This deletes the config file's directory and all it's contents.
-
reset
public void reset()
This deletes and recreates the file, wiping all its contents.
-
wipeDirectory
public void wipeDirectory()
Wipe the config file's directory, including the file itself.
-
createSubDirectory
public void createSubDirectory(String name) throws IOException
This will create a sub-directory in the plugin's data folder, which can be accessed withgetDirectory()
. If the entered name is not a valid name for a directory or the sub-directory already exists or the data folder does not exist, an IOException will be thrown.- Parameters:
name
- The sub directory's name.- Throws:
IOException
- If the entered string has a file extension or already exists.
-
toJSONObject
public com.google.gson.JsonObject toJSONObject() throws IOException
- Throws:
IOException
-
putInJsonObject
public void putInJsonObject(String k, com.google.gson.JsonElement v) throws IOException, com.google.gson.JsonParseException
- Throws:
IOException
com.google.gson.JsonParseException
-
putInJsonObject
public void putInJsonObject(String k, String v) throws IOException, com.google.gson.JsonParseException
- Throws:
IOException
com.google.gson.JsonParseException
-
putInJsonObject
public void putInJsonObject(String k, int v) throws IOException, com.google.gson.JsonParseException
- Throws:
IOException
com.google.gson.JsonParseException
-
removeFromJsonObject
public void removeFromJsonObject(String key) throws IOException
Remove a key-value from the JSONObject insidegetFile()
.- Parameters:
key
- The key.- Throws:
IOException
- Thrown when the file cannot be found.
-
-