Skip to content

Commit

Permalink
Added data folder for all data of the plugin (#1165)
Browse files Browse the repository at this point in the history
* Moved the database file into its own folder

* Added support for the data folder for modules

* Changed missions module to work with the new data folder

* Fixed old missions data folder not moved correctly

* Renamed data folder to datastore
  • Loading branch information
OmerBenGera committed May 28, 2022
1 parent 6a65f4a commit ba11477
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 40 deletions.
Expand Up @@ -16,19 +16,19 @@
public final class ModuleResources {

private final File moduleFile;
private final File dataFolder;
private final File moduleFolder;
private final ClassLoader classLoader;

/**
* Constructor for the class.
*
* @param moduleFile The file of the module.
* @param dataFolder The data folder of the module.
* @param classLoader The class loader of the module.
* @param moduleFile The file of the module.
* @param moduleFolder The data folder of the module.
* @param classLoader The class loader of the module.
*/
public ModuleResources(File moduleFile, File dataFolder, ClassLoader classLoader) {
public ModuleResources(File moduleFile, File moduleFolder, ClassLoader classLoader) {
this.moduleFile = moduleFile;
this.dataFolder = dataFolder;
this.moduleFolder = moduleFolder;
this.classLoader = classLoader;
}

Expand All @@ -44,9 +44,9 @@ public void saveResource(String resourcePath) {
resourcePath = resourcePath.replace('\\', '/');

try (InputStream resourceInput = getResource(resourcePath)) {
File outFile = new File(this.dataFolder, resourcePath);
File outFile = new File(this.moduleFolder, resourcePath);
int lastIndex = resourcePath.lastIndexOf(47);
File outDir = new File(this.dataFolder, resourcePath.substring(0, Math.max(lastIndex, 0)));
File outDir = new File(this.moduleFolder, resourcePath.substring(0, Math.max(lastIndex, 0)));

if (!outDir.exists()) {
outDir.mkdirs();
Expand Down
Expand Up @@ -23,6 +23,7 @@ public abstract class PluginModule {

private File dataFolder;
private File moduleFile;
private File moduleFolder;
private ClassLoader classLoader;
private Logger logger;
private ModuleResources moduleResources;
Expand Down Expand Up @@ -147,9 +148,20 @@ public final String getAuthor() {
/**
* Get the data folder of the module.
* The path for the folder is always plugins/SuperiorSkyblock2/modules/{module-name}/
*
* @deprecated Misleading name; check out {@link #getModuleFolder()}
*/
@Deprecated
public final File getDataFolder() {
return dataFolder;
return this.getModuleFolder();
}

/**
* Get the folder of the module.
* The path for the folder is always plugins/SuperiorSkyblock2/modules/{module-name}/
*/
public final File getModuleFolder() {
return this.moduleFolder;
}

/**
Expand All @@ -162,6 +174,14 @@ public final File getModuleFile() {
return moduleFile;
}

/**
* Get the folder where data of the module can be stored at.
* The path for the folder is always plugins/SuperiorSkyblock2/datastore/modules/{module-name}/
*/
public final File getDataStoreFolder() {
return this.dataFolder;
}

/**
* Get the class loader of the module.
* May be null if the module was registered without calling {@link #initModuleLoader(File, ClassLoader)}
Expand All @@ -181,7 +201,7 @@ public final Logger getLogger() {

/**
* Check whether the module was initialized or not.
* Modules will be initialized after calling to {@link #initModule(SuperiorSkyblock, File)}
* Modules will be initialized after calling to {@link #initModule(SuperiorSkyblock, File, File)}
*/
public final boolean isInitialized() {
return initialized;
Expand Down Expand Up @@ -216,22 +236,37 @@ public final InputStream getResource(String resourceName) {
* This method cannot be called twice - do not call it unless you know what you are doing.
*
* @param plugin An instance to the plugin.
* @param dataFolder The folder of the module.
* @param dataFolder The database folder of the module.
*/
@Deprecated
public final void initModule(SuperiorSkyblock plugin, File dataFolder) {
this.initModule(plugin, new File(moduleFile.getParentFile(), moduleName), dataFolder);
}

/**
* Initialize the module.
* This method cannot be called twice - do not call it unless you know what you are doing.
*
* @param plugin An instance to the plugin.
* @param dataFolder The database folder of the module.
* @param moduleFolder The folder of the module.
*/
public final void initModule(SuperiorSkyblock plugin, File moduleFolder, File dataFolder) {
if (initialized)
throw new RuntimeException("The module " + moduleName + " was already initialized.");

initialized = true;

this.dataFolder = dataFolder;
this.moduleFolder = moduleFolder;

if (!moduleFolder.exists() && !moduleFolder.mkdirs())
throw new RuntimeException("Cannot create module folder for " + moduleName + ".");

this.logger = new ModuleLogger(this);

if (moduleFile != null && classLoader != null)
this.moduleResources = new ModuleResources(this.moduleFile, this.dataFolder, this.classLoader);

if (!dataFolder.exists() && !dataFolder.mkdirs())
throw new RuntimeException("Cannot create module folder for " + moduleName + ".");
this.moduleResources = new ModuleResources(this.moduleFile, this.moduleFolder, this.classLoader);

onPluginInit(plugin);
}
Expand Down
Expand Up @@ -38,14 +38,21 @@ public void setLogging(boolean logging) {
this.logging = logging;
}

private void moveOldDatabaseFile(File newDataFile) {
File oldDataFile = new File(plugin.getDataFolder(), "database.db");
if (oldDataFile.exists())
oldDataFile.renameTo(newDataFile);
}

@Override
public boolean createConnection() {
log("Trying to connect to local database (SQLite)...");

File file = new File(plugin.getDataFolder(), "database.db");
File file = new File(plugin.getDataFolder(), "datastore/database.db");

if (!file.exists()) {
file.getParentFile().mkdirs();
moveOldDatabaseFile(file);
try {
if (!file.createNewFile()) {
log("&cFailed to create database file.");
Expand Down
Expand Up @@ -301,14 +301,32 @@ public void rewardMission(Mission<?> mission, SuperiorPlayer superiorPlayer, boo
}
}

private boolean moveOldDataFolder(File newDataFolder) {
File oldDataFolder = new File(BuiltinModules.MISSIONS.getModuleFolder(), "data");

if (!oldDataFolder.exists())
return true;

newDataFolder.mkdirs();

for (File file : oldDataFolder.listFiles()) {
File targetFile = new File(newDataFolder, file.getName());
if (!file.renameTo(targetFile))
return false;
}

FileUtils.deleteDirectory(oldDataFolder);

return true;
}

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void saveMissionsData() {
File dataFolder = new File(BuiltinModules.MISSIONS.getDataFolder(), "data");
File dataFolder = BuiltinModules.MISSIONS.getDataStoreFolder();

if (!dataFolder.exists()) {
if (!dataFolder.exists())
dataFolder.mkdirs();
}

for (Mission<?> mission : getAllMissions()) {
YamlConfiguration data = new YamlConfiguration();
Expand Down Expand Up @@ -352,7 +370,10 @@ public void loadMissionsData(List<Mission<?>> missionsList) {
// Convert old data file to new format.
convertOldMissionsData();

File dataFolder = new File(BuiltinModules.MISSIONS.getDataFolder(), "data");
File dataFolder = BuiltinModules.MISSIONS.getDataStoreFolder();

if (!moveOldDataFolder(dataFolder))
throw new IllegalStateException("Failed moving old missions folder.");

if (!dataFolder.exists())
return;
Expand Down Expand Up @@ -497,12 +518,12 @@ private static String getIslandPlaceholder(@Nullable IMissionsHolder missionsHol
}

private void convertOldMissionsData() {
File file = new File(BuiltinModules.MISSIONS.getDataFolder(), "_data.yml");
File file = new File(BuiltinModules.MISSIONS.getModuleFolder(), "_data.yml");

if (!file.exists())
return;

File dataFolder = new File(BuiltinModules.MISSIONS.getDataFolder(), "data");
File dataFolder = BuiltinModules.MISSIONS.getDataStoreFolder();

YamlConfiguration oldData = YamlConfiguration.loadConfiguration(file);

Expand Down
Expand Up @@ -75,7 +75,7 @@ protected void onPluginInit(SuperiorSkyblockPlugin plugin) {
}

public File createConfig() {
File configFile = new File(getDataFolder(), "config.yml");
File configFile = new File(getModuleFolder(), "config.yml");

if (!configFile.exists())
FileUtils.saveResource("modules/" + getName() + "/config.yml");
Expand Down
Expand Up @@ -27,11 +27,13 @@ public final class ModulesHandler extends AbstractHandler implements ModulesMana

private final ModulesContainer modulesContainer;
private final File modulesFolder;
private final File dataFolder;

public ModulesHandler(SuperiorSkyblockPlugin plugin, ModulesContainer modulesContainer) {
super(plugin);
this.modulesContainer = modulesContainer;
modulesFolder = new File(plugin.getDataFolder(), "modules");
this.modulesFolder = new File(plugin.getDataFolder(), "modules");
this.dataFolder = new File(plugin.getDataFolder(), "datastore/modules");
}

@Override
Expand All @@ -50,7 +52,7 @@ public void loadData() {
@Override
public void registerModule(PluginModule pluginModule) {
Preconditions.checkNotNull(pluginModule, "pluginModule parameter cannot be null.");
this.modulesContainer.registerModule(pluginModule, modulesFolder, plugin);
this.modulesContainer.registerModule(pluginModule, modulesFolder, dataFolder, plugin);
}

@Override
Expand Down
Expand Up @@ -57,7 +57,7 @@ protected void onPluginInit(SuperiorSkyblockPlugin plugin) {
updatedConfig = true;

if (updatedConfig) {
File moduleConfigFile = new File(getDataFolder(), "config.yml");
File moduleConfigFile = new File(getModuleFolder(), "config.yml");

try {
super.config.save(moduleConfigFile);
Expand Down
Expand Up @@ -22,15 +22,16 @@ public final class DefaultModulesContainer implements ModulesContainer {
private final Map<PluginModule, ModuleData> modulesData = new HashMap<>();

@Override
public void registerModule(PluginModule pluginModule, File modulesFolder, SuperiorSkyblockPlugin plugin) {
public void registerModule(PluginModule pluginModule, File modulesFolder, File modulesDataFolder, SuperiorSkyblockPlugin plugin) {
String moduleName = pluginModule.getName().toLowerCase(Locale.ENGLISH);

Preconditions.checkState(!modulesMap.containsKey(moduleName), "PluginModule with the name " + moduleName + " already exists.");

File dataFolder = new File(modulesFolder, pluginModule.getName());
File dataFolder = new File(modulesDataFolder, pluginModule.getName());
File moduleFolder = new File(modulesFolder, pluginModule.getName());

try {
pluginModule.initModule(plugin, dataFolder);
pluginModule.initModule(plugin, moduleFolder, dataFolder);
} catch (Throwable error) {
SuperiorSkyblockPlugin.log("&cAn error occurred while initializing the module " + pluginModule.getName() + ":");
SuperiorSkyblockPlugin.log("&cContact " + pluginModule.getAuthor() + " regarding this, this has nothing to do with the plugin.");
Expand Down
Expand Up @@ -10,7 +10,7 @@

public interface ModulesContainer {

void registerModule(PluginModule pluginModule, File modulesFolder, SuperiorSkyblockPlugin plugin);
void registerModule(PluginModule pluginModule, File modulesFolder, File modulesDataFolder, SuperiorSkyblockPlugin plugin);

void unregisterModule(PluginModule pluginModule, SuperiorSkyblockPlugin plugin);

Expand Down
Expand Up @@ -33,7 +33,7 @@ protected void onPluginInit(SuperiorSkyblockPlugin plugin) {
super.config.set("enabled", config.getBoolean("generators"));
config.set("generators", null);

File moduleConfigFile = new File(getDataFolder(), "config.yml");
File moduleConfigFile = new File(getModuleFolder(), "config.yml");

try {
super.config.save(moduleConfigFile);
Expand Down
Expand Up @@ -54,7 +54,7 @@ private void generateDefaultFiles() {
FileUtils.copyResource("modules/missions/KillsMissions");
FileUtils.copyResource("modules/missions/StatisticsMissions");

File categoriesFolder = new File(getDataFolder(), "categories");
File categoriesFolder = new File(getModuleFolder(), "categories");

if ((!categoriesFolder.exists() || !categoriesFolder.isDirectory()) && categoriesFolder.mkdirs()) {
FileUtils.saveResource("modules/missions/categories/farmer/farmer_1.yml");
Expand All @@ -81,7 +81,7 @@ private void generateDefaultFiles() {

@Override
protected void onPluginInit(SuperiorSkyblockPlugin plugin) {
File file = new File(getDataFolder(), "config.yml");
File file = new File(getModuleFolder(), "config.yml");

if (!file.exists())
FileUtils.saveResource("modules/missions/config.yml");
Expand Down Expand Up @@ -177,7 +177,7 @@ protected String[] getIgnoredSections() {
}

private boolean canLoadCategory(SuperiorSkyblockPlugin plugin, String categoryName, List<Mission<?>> categoryMissions) {
File categoryFolder = new File(getDataFolder(), "categories/" + categoryName);
File categoryFolder = new File(getModuleFolder(), "categories/" + categoryName);

if (!categoryFolder.exists()) {
SuperiorSkyblockPlugin.log("&cThe directory of the mission category " + categoryName + " doesn't exist, skipping...");
Expand Down Expand Up @@ -223,7 +223,7 @@ private boolean canLoadCategory(SuperiorSkyblockPlugin plugin, String categoryNa

ConfigurationSection missionSection = missionConfigFile.getConfigurationSection("");

Mission<?> mission = plugin.getMissions().loadMission(missionName, getDataFolder(), missionSection);
Mission<?> mission = plugin.getMissions().loadMission(missionName, getModuleFolder(), missionSection);

if (mission != null) {
categoryMissions.add(mission);
Expand Down Expand Up @@ -272,8 +272,8 @@ private void convertNonCategorizedMissions(SuperiorSkyblockPlugin plugin, File f
categoriesSection.set("players.slot", playersCategorySlot);
}

File islandsCategoryFile = new File(getDataFolder(), "categories/islands");
File playersCategoryFile = new File(getDataFolder(), "categories/players");
File islandsCategoryFile = new File(getModuleFolder(), "categories/islands");
File playersCategoryFile = new File(getModuleFolder(), "categories/players");

islandsCategoryFile.mkdirs();
playersCategoryFile.mkdirs();
Expand Down Expand Up @@ -344,13 +344,13 @@ private void convertOldMissions(SuperiorSkyblockPlugin plugin, File file, YamlCo
if (oldMissionsFiles != null) {
for (File jarFile : oldMissionsFiles) {
if (jarFile.getName().endsWith(".jar"))
jarFile.renameTo(new File(getDataFolder(), jarFile.getName()));
jarFile.renameTo(new File(getModuleFolder(), jarFile.getName()));
}
}

File oldDataFile = new File(oldMissionsFolder, "_data.yml");
if (oldDataFile.exists())
oldDataFile.renameTo(new File(getDataFolder(), "_data.yml"));
oldDataFile.renameTo(new File(getModuleFolder(), "_data.yml"));

FileUtils.deleteDirectory(oldMissionsFolder);
}
Expand Down
Expand Up @@ -265,7 +265,7 @@ private void convertOldUpgradesFile(SuperiorSkyblockPlugin plugin) {

super.config.set("upgrades", config.get("upgrades"));

File moduleConfigFile = new File(getDataFolder(), "config.yml");
File moduleConfigFile = new File(getModuleFolder(), "config.yml");

try {
super.config.save(moduleConfigFile);
Expand Down

0 comments on commit ba11477

Please sign in to comment.