Now uses the official config.yml in the BSkyBlock data folder.

Saves a backup of the config after validation in the database so it can
be checked.

To Be Done: Validation of the config when it is loaded against the
database version.
This commit is contained in:
Tastybento 2018-01-07 14:43:37 -08:00
parent 16bcb90127
commit 09b9d4dd49
6 changed files with 48 additions and 29 deletions

View File

@ -249,6 +249,25 @@ world:
### Island Settings ### ### Island Settings ###
island: island:
# Default chest items
chest-items:
- ==: org.bukkit.inventory.ItemStack
type: LAVA_BUCKET
- ==: org.bukkit.inventory.ItemStack
type: ICE
amount: 2
- ==: org.bukkit.inventory.ItemStack
type: MELON_SEEDS
- ==: org.bukkit.inventory.ItemStack
type: BONE
amount: 2
- ==: org.bukkit.inventory.ItemStack
type: COBBLESTONE
amount: 5
- ==: org.bukkit.inventory.ItemStack
type: SAPLING
amount: 2
# Default max team size # Default max team size
# Use this permission to set for specific user groups: askyblock.team.maxsize.<number> # Use this permission to set for specific user groups: askyblock.team.maxsize.<number>
# Permission size cannot be less than the default below. # Permission size cannot be less than the default below.

View File

@ -1,9 +1,5 @@
package us.tastybento.bskyblock; package us.tastybento.bskyblock;
import java.util.Arrays;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -49,13 +45,22 @@ public class BSkyBlock extends JavaPlugin {
@Override @Override
public void onEnable(){ public void onEnable(){
// Save the default config from config.yml
saveDefaultConfig();
plugin = this; plugin = this;
settings = new Settings(); settings = new Settings();
// Load config - EXPERIMENTAL // Load settings from config.yml. This will check if there are any issues with it too.
try { try {
settings.saveSettings(); //doesn't work completely yet //settings.saveSettings();
settings = settings.loadSettings(); settings = settings.loadSettings();
getLogger().info("DEBUG: island distance = " + settings.getIslandDistance()); } catch (Exception e) {
e.printStackTrace();
}
// Save a backup of settings to the database so it can be checked next time
try {
settings.saveBackup();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -89,18 +94,6 @@ public class BSkyBlock extends JavaPlugin {
// Load islands from database // Load islands from database
islandsManager.load(); islandsManager.load();
// TODO: load these from config.yml
getSettings().setChestItems(Arrays.asList(
new ItemStack(Material.LAVA_BUCKET,1),
new ItemStack(Material.ICE,2),
new ItemStack(Material.MELON_SEEDS,1),
new ItemStack(Material.BONE,2),
new ItemStack(Material.COBBLESTONE,5),
new ItemStack(Material.SAPLING,2)
));
//getSettings().setDefaultLanguage("en-US");
plugin.getLogger().info("DEBUG: ************************** Loading Locales **************************");
localesManager = new LocalesManager(plugin); localesManager = new LocalesManager(plugin);
//TODO localesManager.registerLocales(plugin); //TODO localesManager.registerLocales(plugin);

View File

@ -214,7 +214,10 @@ public class Settings implements ISettings<Settings> {
/* SCHEMATICS */ /* SCHEMATICS */
private List<String> companionNames = new ArrayList<>(); private List<String> companionNames = new ArrayList<>();
@ConfigEntry(path = "island.chest-items")
private List<ItemStack> chestItems = new ArrayList<>(); private List<ItemStack> chestItems = new ArrayList<>();
private EntityType companionType = EntityType.COW; private EntityType companionType = EntityType.COW;
private boolean useOwnGenerator; private boolean useOwnGenerator;

View File

@ -17,7 +17,7 @@ import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
* @param <T> * @param <T>
*/ */
public interface ISettings<T> { public interface ISettings<T> {
// ----------------Saver------------------- // ----------------Saver-------------------
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
default void saveSettings() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, IntrospectionException, SQLException { default void saveSettings() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, IntrospectionException, SQLException {
@ -27,10 +27,15 @@ public interface ISettings<T> {
Bukkit.getLogger().info("DEBUG: settingsHandler = " + settingsHandler); Bukkit.getLogger().info("DEBUG: settingsHandler = " + settingsHandler);
Bukkit.getLogger().info("DEBUG: instance = " + getInstance()); Bukkit.getLogger().info("DEBUG: instance = " + getInstance());
settingsHandler.saveSettings(getInstance()); settingsHandler.saveSettings(getInstance());
}
default void saveBackup() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, IntrospectionException, SQLException {
// Save backup in real database // Save backup in real database
@SuppressWarnings("unchecked")
AbstractDatabaseHandler<T> dbhandler = (AbstractDatabaseHandler<T>) BSBDatabase.getDatabase().getHandler(getInstance().getClass()); AbstractDatabaseHandler<T> dbhandler = (AbstractDatabaseHandler<T>) BSBDatabase.getDatabase().getHandler(getInstance().getClass());
dbhandler.saveObject(getInstance()); dbhandler.saveObject(getInstance());
} }
// --------------- Loader ------------------ // --------------- Loader ------------------
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
default T loadSettings() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, ClassNotFoundException, IntrospectionException, SQLException { default T loadSettings() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, ClassNotFoundException, IntrospectionException, SQLException {
@ -41,18 +46,17 @@ public interface ISettings<T> {
// Load it // Load it
dbConfig = dbhandler.loadObject(getUniqueId()); dbConfig = dbhandler.loadObject(getUniqueId());
} }
// Get the handler // Get the handler
AbstractDatabaseHandler<T> configHandler = (AbstractDatabaseHandler<T>) new FlatFileDatabase().getHandler(getInstance().getClass()); AbstractDatabaseHandler<T> configHandler = (AbstractDatabaseHandler<T>) new FlatFileDatabase().getHandler(getInstance().getClass());
// Load every field in the config class // Load every field in the config class
return configHandler.loadSettings(getUniqueId(), dbConfig); return configHandler.loadSettings(getUniqueId(), dbConfig);
} }
/** /**
* @return instance of the implementing class, i.e., return this. * @return instance of the implementing class, i.e., return this.
*/ */
T getInstance(); T getInstance();
/** /**
* @return the uniqueId * @return the uniqueId
*/ */
@ -62,5 +66,5 @@ public interface ISettings<T> {
* @param uniqueId the uniqueId to set * @param uniqueId the uniqueId to set
*/ */
void setUniqueId(String uniqueId); void setUniqueId(String uniqueId);
} }

View File

@ -44,7 +44,7 @@ public class FlatFileDatabaseConnecter implements DatabaseConnecter {
if (!fileName.endsWith(".yml")) { if (!fileName.endsWith(".yml")) {
fileName = fileName + ".yml"; fileName = fileName + ".yml";
} }
File yamlFile = new File(dataFolder, tableName + File.separator + fileName); File yamlFile = new File(plugin.getDataFolder(), tableName + File.separator + fileName);
YamlConfiguration config = null; YamlConfiguration config = null;
if (yamlFile.exists()) { if (yamlFile.exists()) {
@ -85,7 +85,7 @@ public class FlatFileDatabaseConnecter implements DatabaseConnecter {
if (!fileName.endsWith(".yml")) { if (!fileName.endsWith(".yml")) {
fileName = fileName + ".yml"; fileName = fileName + ".yml";
} }
File tableFolder = new File(dataFolder, tableName); File tableFolder = new File(plugin.getDataFolder(), tableName);
File file = new File(tableFolder, fileName); File file = new File(tableFolder, fileName);
if (!tableFolder.exists()) { if (!tableFolder.exists()) {
tableFolder.mkdirs(); tableFolder.mkdirs();

View File

@ -78,7 +78,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
*/ */
@Override @Override
public T loadObject(String key) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, ClassNotFoundException { public T loadObject(String key) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, ClassNotFoundException {
String path = dataObject.getSimpleName(); String path = DATABASE_FOLDER_NAME + File.separator + dataObject.getSimpleName();
String fileName = key; String fileName = key;
StoreAt storeAt = dataObject.getAnnotation(StoreAt.class); StoreAt storeAt = dataObject.getAnnotation(StoreAt.class);
if (storeAt != null) { if (storeAt != null) {
@ -134,7 +134,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
if (storeAt != null) { if (storeAt != null) {
fileName = storeAt.filename(); fileName = storeAt.filename();
} }
YamlConfiguration config = databaseConnecter.loadYamlFile(dataObject.getSimpleName(), fileName); YamlConfiguration config = databaseConnecter.loadYamlFile(DATABASE_FOLDER_NAME + File.separator + dataObject.getSimpleName(), fileName);
list.add(createObject(config)); list.add(createObject(config));
} }
return list; return list;
@ -314,7 +314,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// The file name of the Yaml file. // The file name of the Yaml file.
String filename = ""; String filename = "";
String path = dataObject.getSimpleName(); String path = DATABASE_FOLDER_NAME + File.separator + dataObject.getSimpleName();
// Only allow storing in an arbitrary place if it is a config object. Otherwise it is in the database // Only allow storing in an arbitrary place if it is a config object. Otherwise it is in the database
if (configFlag) { if (configFlag) {