2022-04-02 05:32:04 +00:00
|
|
|
package dev.plex.config;
|
|
|
|
|
|
|
|
import dev.plex.module.PlexModule;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a custom Config object
|
|
|
|
*/
|
|
|
|
public class ModuleConfig extends YamlConfiguration
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The plugin instance
|
|
|
|
*/
|
|
|
|
private PlexModule module;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The File instance
|
|
|
|
*/
|
|
|
|
private File file;
|
|
|
|
|
|
|
|
/**
|
2022-04-14 04:20:11 +00:00
|
|
|
* Where the file is in the module JAR
|
2022-04-02 05:32:04 +00:00
|
|
|
*/
|
2022-04-14 04:20:11 +00:00
|
|
|
private String from;
|
2022-04-02 05:32:04 +00:00
|
|
|
|
2022-04-14 04:12:24 +00:00
|
|
|
/**
|
2022-04-14 04:20:11 +00:00
|
|
|
* Where it should be copied to in the module folder
|
2022-04-14 04:12:24 +00:00
|
|
|
*/
|
2022-04-14 04:20:11 +00:00
|
|
|
private String to;
|
2022-04-14 04:12:24 +00:00
|
|
|
|
2022-04-02 05:32:04 +00:00
|
|
|
/**
|
|
|
|
* Creates a config object
|
|
|
|
*
|
|
|
|
* @param module The module instance
|
2022-04-14 04:20:11 +00:00
|
|
|
* @param to The file name
|
2022-04-02 05:32:04 +00:00
|
|
|
*/
|
2022-04-14 04:20:11 +00:00
|
|
|
public ModuleConfig(PlexModule module, String from, String to)
|
2022-04-02 05:32:04 +00:00
|
|
|
{
|
|
|
|
this.module = module;
|
2022-04-14 04:20:11 +00:00
|
|
|
this.file = new File(module.getDataFolder(), to);
|
|
|
|
this.to = to;
|
|
|
|
this.from = from;
|
2022-04-02 05:32:04 +00:00
|
|
|
|
|
|
|
if (!file.exists())
|
|
|
|
{
|
|
|
|
saveDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void load()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
super.load(file);
|
|
|
|
}
|
|
|
|
catch (IOException | InvalidConfigurationException ex)
|
|
|
|
{
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the configuration file
|
|
|
|
*/
|
|
|
|
public void save()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
super.save(file);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves the configuration file from the plugin's resources folder to the data folder (plugins/Plex/)
|
|
|
|
*/
|
|
|
|
private void saveDefault()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-04-14 04:20:11 +00:00
|
|
|
Files.copy(module.getClass().getResourceAsStream("/" + from), this.file.toPath());
|
2022-04-02 05:32:04 +00:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|