Data and language files are stored in separate folders now.

The old save file is automatically moved. Existing language files need
to be moved manually. Existing trading logs are not moved.
master
blablubbabc 2020-11-14 01:20:36 +01:00
parent d6aef5193d
commit a92a6e7d7d
3 changed files with 88 additions and 7 deletions

View File

@ -4,6 +4,12 @@ Date format: (YYYY-MM-DD)
## v2.12.1 (TBA)
### Supported MC versions: 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.15.2, 1.14.4
Migration notes:
* The folder structure has changed:
* The save file and trading logs (if enabled) are stored inside a new 'data' folder now.
* If no save file exists at the new location and a previous save file is found at the old location, it is automatically moved.
* Custom language files need to be located inside a new 'lang' folder now.
* Existing custom language files need to be manually moved!
## v2.12.0 (2020-11-04)
### Supported MC versions: 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.15.2, 1.14.4

View File

@ -86,6 +86,8 @@ import com.nisovin.shopkeepers.villagers.VillagerInteractionListener;
public class SKShopkeepersPlugin extends JavaPlugin implements ShopkeepersPlugin {
private static final int ASYNC_TASKS_TIMEOUT_SECONDS = 10;
private static final String dataFolder = "data";
private static final String langFolder = "lang";
private static SKShopkeepersPlugin plugin;
@ -197,7 +199,7 @@ public class SKShopkeepersPlugin extends JavaPlugin implements ShopkeepersPlugin
// Load language config:
String lang = Settings.language;
String langFileName = "language-" + lang + ".yml";
File langFile = new File(this.getDataFolder(), langFileName);
File langFile = new File(this.getSKLangFolder(), langFileName);
if (!langFile.exists() && this.getResource(langFileName) != null) {
this.saveResource(langFileName, false);
}
@ -258,6 +260,9 @@ public class SKShopkeepersPlugin extends JavaPlugin implements ShopkeepersPlugin
return;
}
// Create folder structure:
this.createFolderStructure();
// Load language file:
this.loadLanguageFile();
@ -308,6 +313,11 @@ public class SKShopkeepersPlugin extends JavaPlugin implements ShopkeepersPlugin
return;
}
// Create folder structure (if not already done during onLoad):
if (!alreadySetup) {
this.createFolderStructure();
}
// Load language file (if not already loaded during onLoad):
if (!alreadySetup) {
this.loadLanguageFile();
@ -354,7 +364,7 @@ public class SKShopkeepersPlugin extends JavaPlugin implements ShopkeepersPlugin
PluginManager pm = Bukkit.getPluginManager();
pm.registerEvents(new PlayerJoinQuitListener(this), this);
pm.registerEvents(new TradingCountListener(this), this);
pm.registerEvents(new TradeFileLogger(this.getDataFolder()), this);
pm.registerEvents(new TradeFileLogger(this.getSKDataFolder()), this);
// DEFAULT SHOP OBJECT TYPES
@ -544,6 +554,21 @@ public class SKShopkeepersPlugin extends JavaPlugin implements ShopkeepersPlugin
commands.onPlayerQuit(player);
}
// FOLDER STRUCTURE
public File getSKDataFolder() {
return new File(this.getDataFolder(), dataFolder);
}
public File getSKLangFolder() {
return new File(this.getDataFolder(), langFolder);
}
private void createFolderStructure() {
this.getSKDataFolder().mkdirs();
this.getSKLangFolder().mkdirs();
}
// SHOPKEEPER REGISTRY
@Override

View File

@ -2,8 +2,10 @@ package com.nisovin.shopkeepers.storage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -180,7 +182,7 @@ public class SKShopkeeperStorage implements ShopkeeperStorage {
}
private File getSaveFile() {
return new File(plugin.getDataFolder(), "save.yml");
return new File(plugin.getSKDataFolder(), "save.yml");
}
private File getTempSaveFile() {
@ -265,6 +267,50 @@ public class SKShopkeeperStorage implements ShopkeeperStorage {
// LOADING
// We previously stored the save file within the plugin's root folder. If no save file exist at the expected
// location, we check the old save file location and migrate the save file if it is found.
private File getOldSaveFile() {
return new File(plugin.getDataFolder(), "save.yml");
}
private File getOldTempSaveFile() {
File saveFile = this.getOldSaveFile();
return new File(saveFile.getParentFile(), saveFile.getName() + ".temp");
}
// Returns false if the migration failed.
// Returns true if the migration succeeded or there is no old save file to migrate.
private boolean migrateOldSaveFile() {
File oldSaveFile = this.getOldSaveFile();
if (!oldSaveFile.exists()) {
File oldTempSaveFile = this.getOldTempSaveFile();
if (oldTempSaveFile.exists()) {
// Load from old temporary save file instead:
Log.warning("Found no old save file, but an existing old temporary save file! (" + oldTempSaveFile.getName() + ")");
Log.warning("This might indicate an issue during a previous saving attempt!");
Log.warning("We try to migrate this temporary save file instead!");
oldSaveFile = oldTempSaveFile;
} else {
// No save file found that needs to be migrated.
return true;
}
}
// Move old save file to new location:
File saveFile = this.getSaveFile();
Log.info("Migrating old save file (" + oldSaveFile.getName() + ") to new location ("
+ saveFile.getParentFile().getName() + "/" + saveFile.getName() + ")!");
try {
Files.move(oldSaveFile.toPath(), saveFile.toPath());
} catch (IOException e) {
Log.severe("Failed to migrate old save file! (" + oldSaveFile.getName() + ")", e);
return false;
}
// Migration succeeded:
return true;
}
// Returns true on success, and false if there was some severe issue during loading.
public boolean reload() {
if (currentlyLoading) {
@ -300,12 +346,16 @@ public class SKShopkeeperStorage implements ShopkeeperStorage {
// Load from temporary save file instead:
Log.warning("Found no save file, but an existing temporary save file! (" + tempSaveFile.getName() + ")");
Log.warning("This might indicate an issue during a previous saving attempt!");
Log.warning("Trying to load the shopkeepers data from this temporary save file instead!");
Log.warning("We try to load the Shopkeepers data from this temporary save file instead!");
saveFile = tempSaveFile;
} else {
// The save file does not exist yet -> no shopkeeper data available.
// Silently the setup data version and abort:
} else if (!this.migrateOldSaveFile()) {
// Migration of old save file failed:
return false; // Disable without save
} else if (!saveFile.exists()) {
// No save file exists yet (even after checking for it again, after the migration) -> No shopkeeper data
// available.
// We silently setup the data version and abort:
saveData.set(DATA_VERSION_KEY, currentDataVersion.getCombinded());
return true;
}