Move storage files to nested folder

This commit is contained in:
PikaMug 2023-12-06 02:14:20 -05:00
parent 5c5d4dd0b3
commit 45a64a05ed
9 changed files with 48 additions and 18 deletions

View File

@ -173,10 +173,13 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests {
// 6 - Load soft-depends // 6 - Load soft-depends
depends.init(); depends.init();
// 7 - Save resources from jar // 7 - Transfer resources from jar
saveResourceAs("quests.yml", "quests.yml", false); moveStorageResource("quests.yml");
saveResourceAs("actions.yml", "actions.yml", false); moveStorageResource("actions.yml");
saveResourceAs("conditions.yml", "conditions.yml", false); moveStorageResource("conditions.yml");
saveResourceAs("quests.yml", "storage/quests.yml", false);
saveResourceAs("actions.yml", "storage/actions.yml", false);
saveResourceAs("conditions.yml", "storage/conditions.yml", false);
// 8 - Save config with any new options // 8 - Save config with any new options
getConfig().options().copyDefaults(true); getConfig().options().copyDefaults(true);
@ -553,6 +556,33 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests {
return storage; return storage;
} }
/**
* Move a storage file from legacy location, if present
*
* @param fileName Name of file to attempt move
*/
private void moveStorageResource(String fileName) {
File storageFile = new File(getDataFolder(), fileName);
if (!storageFile.isFile()) {
return;
}
final File outFile = new File(getDataFolder(), "storage" + File.separatorChar + fileName);
final File outDir = new File(outFile.getPath().replace(fileName, ""));
if (!outDir.exists()) {
if (!outDir.mkdirs()) {
getLogger().log(Level.SEVERE, "Failed to make directories for " + fileName + " (canWrite= "
+ outDir.canWrite() + ")");
}
}
boolean q = storageFile.renameTo(outFile);
if (!q) {
getLogger().severe("Unable to move " + fileName + " file. Check folder permissions and restart.");
getServer().getPluginManager().disablePlugin(this);
setEnabled(false);
}
}
/** /**
* Save a Quests plugin resource to a specific path in the filesystem * Save a Quests plugin resource to a specific path in the filesystem
* *

View File

@ -268,7 +268,7 @@ public class BukkitActionFactory implements ActionFactory, ConversationAbandoned
public void deleteAction(final ConversationContext context) { public void deleteAction(final ConversationContext context) {
final YamlConfiguration data = new YamlConfiguration(); final YamlConfiguration data = new YamlConfiguration();
final File actionsFile = new File(plugin.getDataFolder(), "actions.yml"); final File actionsFile = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "actions.yml");
try { try {
data.load(actionsFile); data.load(actionsFile);
} catch (final IOException | InvalidConfigurationException e) { } catch (final IOException | InvalidConfigurationException e) {
@ -316,7 +316,7 @@ public class BukkitActionFactory implements ActionFactory, ConversationAbandoned
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void saveAction(final ConversationContext context) { public void saveAction(final ConversationContext context) {
final YamlConfiguration data = new YamlConfiguration(); final YamlConfiguration data = new YamlConfiguration();
final File actionsFile = new File(plugin.getDataFolder(), "actions.yml"); final File actionsFile = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "actions.yml");
try { try {
data.load(actionsFile); data.load(actionsFile);
} catch (final IOException | InvalidConfigurationException e) { } catch (final IOException | InvalidConfigurationException e) {

View File

@ -162,7 +162,7 @@ public class BukkitConditionFactory implements ConditionFactory, ConversationAba
public void deleteCondition(final ConversationContext context) { public void deleteCondition(final ConversationContext context) {
final YamlConfiguration data = new YamlConfiguration(); final YamlConfiguration data = new YamlConfiguration();
final File conditionsFile = new File(plugin.getDataFolder(), "conditions.yml"); final File conditionsFile = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "conditions.yml");
try { try {
data.load(conditionsFile); data.load(conditionsFile);
} catch (final IOException | InvalidConfigurationException e) { } catch (final IOException | InvalidConfigurationException e) {
@ -204,7 +204,7 @@ public class BukkitConditionFactory implements ConditionFactory, ConversationAba
public void saveCondition(final ConversationContext context) { public void saveCondition(final ConversationContext context) {
final YamlConfiguration data = new YamlConfiguration(); final YamlConfiguration data = new YamlConfiguration();
final File conditionsFile = new File(plugin.getDataFolder(), "conditions.yml"); final File conditionsFile = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "conditions.yml");
try { try {
data.load(conditionsFile); data.load(conditionsFile);
} catch (final IOException | InvalidConfigurationException e) { } catch (final IOException | InvalidConfigurationException e) {

View File

@ -858,7 +858,7 @@ public class QuestMainPrompt extends QuestsEditorNumericPrompt {
} }
final FileConfiguration data = new YamlConfiguration(); final FileConfiguration data = new YamlConfiguration();
try { try {
data.load(new File(plugin.getDataFolder(), "quests.yml")); data.load(new File(plugin.getDataFolder(), "storage" + File.separatorChar + "quests.yml"));
ConfigurationSection questSection = data.getConfigurationSection("quests"); ConfigurationSection questSection = data.getConfigurationSection("quests");
if (questSection == null) { if (questSection == null) {
questSection = data.createSection("quests"); questSection = data.createSection("quests");
@ -884,7 +884,7 @@ public class QuestMainPrompt extends QuestsEditorNumericPrompt {
} }
if (newSection != null) { if (newSection != null) {
plugin.getQuestFactory().saveQuest(context, newSection); plugin.getQuestFactory().saveQuest(context, newSection);
data.save(new File(plugin.getDataFolder(), "quests.yml")); data.save(new File(plugin.getDataFolder(), "storage" + File.separatorChar + "quests.yml"));
context.getForWhom().sendRawMessage(ChatColor.GREEN context.getForWhom().sendRawMessage(ChatColor.GREEN
+ BukkitLang.get("questEditorSaved").replace("<command>", "/questadmin " + BukkitLang.get("questEditorSaved").replace("<command>", "/questadmin "
+ BukkitLang.get("COMMAND_QUESTADMIN_RELOAD"))); + BukkitLang.get("COMMAND_QUESTADMIN_RELOAD")));

View File

@ -539,7 +539,7 @@ public class BukkitQuestFactory implements QuestFactory, ConversationAbandonedLi
public void deleteQuest(final ConversationContext context) { public void deleteQuest(final ConversationContext context) {
final FileConfiguration data = new YamlConfiguration(); final FileConfiguration data = new YamlConfiguration();
final File questsFile = new File(plugin.getDataFolder(), "quests.yml"); final File questsFile = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "quests.yml");
try { try {
data.load(questsFile); data.load(questsFile);
} catch (final IOException | InvalidConfigurationException e) { } catch (final IOException | InvalidConfigurationException e) {

View File

@ -54,7 +54,7 @@ public class BukkitActionYamlStorage implements ActionStorageImpl {
public void init() { public void init() {
final YamlConfiguration config = new YamlConfiguration(); final YamlConfiguration config = new YamlConfiguration();
final File legacyFile = new File(plugin.getDataFolder(), "events.yml"); final File legacyFile = new File(plugin.getDataFolder(), "events.yml");
final File actionsFile = new File(plugin.getDataFolder(), "actions.yml"); final File actionsFile = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "actions.yml");
// Using #isFile because #exists and #renameTo can return false positives // Using #isFile because #exists and #renameTo can return false positives
if (legacyFile.isFile()) { if (legacyFile.isFile()) {
try { try {
@ -123,7 +123,7 @@ public class BukkitActionYamlStorage implements ActionStorageImpl {
return null; return null;
} }
final File legacy = new File(plugin.getDataFolder(), "events.yml"); final File legacy = new File(plugin.getDataFolder(), "events.yml");
final File actions = new File(plugin.getDataFolder(), "actions.yml"); final File actions = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "actions.yml");
final FileConfiguration data = new YamlConfiguration(); final FileConfiguration data = new YamlConfiguration();
try { try {
if (actions.isFile()) { if (actions.isFile()) {

View File

@ -48,7 +48,7 @@ public class BukkitConditionYamlStorage implements ConditionStorageImpl {
@Override @Override
public void init() { public void init() {
final YamlConfiguration config = new YamlConfiguration(); final YamlConfiguration config = new YamlConfiguration();
final File conditionsFile = new File(plugin.getDataFolder(), "conditions.yml"); final File conditionsFile = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "conditions.yml");
// Using #isFile because #exists and #renameTo can return false positives // Using #isFile because #exists and #renameTo can return false positives
if (conditionsFile.length() != 0) { if (conditionsFile.length() != 0) {
try { try {
@ -92,7 +92,7 @@ public class BukkitConditionYamlStorage implements ConditionStorageImpl {
if (name == null) { if (name == null) {
return null; return null;
} }
final File conditions = new File(plugin.getDataFolder(), "conditions.yml"); final File conditions = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "conditions.yml");
final FileConfiguration data = new YamlConfiguration(); final FileConfiguration data = new YamlConfiguration();
try { try {
if (conditions.isFile()) { if (conditions.isFile()) {

View File

@ -73,7 +73,7 @@ public class BukkitQuestYamlStorage implements QuestStorageImpl {
public void init() { public void init() {
boolean needsSaving = false; boolean needsSaving = false;
FileConfiguration config = null; FileConfiguration config = null;
final File file = new File(plugin.getDataFolder(), "quests.yml"); final File file = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "quests.yml");
try { try {
config = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), config = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file),
StandardCharsets.UTF_8)); StandardCharsets.UTF_8));
@ -139,7 +139,7 @@ public class BukkitQuestYamlStorage implements QuestStorageImpl {
return null; return null;
} }
FileConfiguration config = null; FileConfiguration config = null;
final File file = new File(plugin.getDataFolder(), "quests.yml"); final File file = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "quests.yml");
try { try {
config = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), config = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file),
StandardCharsets.UTF_8)); StandardCharsets.UTF_8));

View File

@ -70,7 +70,7 @@ public class BukkitModuleJarStorage implements ModuleStorageImpl {
plugin.getLogger().warning("Unable to create module directory"); plugin.getLogger().warning("Unable to create module directory");
} }
FileConfiguration config = null; FileConfiguration config = null;
final File file = new File(plugin.getDataFolder(), "quests.yml"); final File file = new File(plugin.getDataFolder(), "storage" + File.separatorChar + "quests.yml");
try { try {
config = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), config = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file),
StandardCharsets.UTF_8)); StandardCharsets.UTF_8));