Added a specific config class to store objects as YAML

This commit is contained in:
Tastybento 2018-02-25 18:19:52 -08:00
parent b043e641a1
commit 00121e7e37

View File

@ -0,0 +1,93 @@
package us.tastybento.bskyblock.api.configuration;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.addons.Addon;
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
/**
* Handy config class to store and load Java POJOs as YAML configs
* @author tastybento
*
* @param <T>
*/
public class BSBConfig<T> {
private AbstractDatabaseHandler<T> handler;
private Logger logger;
@SuppressWarnings("unchecked")
public BSBConfig(BSkyBlock plugin, Class<T> type) {
this.logger = plugin.getLogger();
handler = (AbstractDatabaseHandler<T>) new FlatFileDatabase().getHandler(type);
}
@SuppressWarnings("unchecked")
public BSBConfig(Addon addon, Class<T> type) {
this.logger = addon.getLogger();
handler = (AbstractDatabaseHandler<T>) new FlatFileDatabase().getHandler(type);
}
/**
* Load all the config objects and supply them as a list
* @return list of config objects or an empty list if they cannot be loaded
*/
public List<T> loadConfigObjects() {
List<T> result = new ArrayList<>();
try {
result = handler.loadObjects();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException | ClassNotFoundException | IntrospectionException | SQLException e) {
logger.severe(() -> "Could not load config! Error: " + e.getMessage());
}
return result;
}
/**
* Loads the config object
* @param uniqueId - unique id of the object
* @return the object or null if it cannot be loaded
*/
public T loadConfigObject(String uniqueId) {
try {
return handler.loadObject(uniqueId);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| SecurityException | ClassNotFoundException | IntrospectionException | SQLException e) {
logger.severe(() -> "Could not load config object! " + e.getMessage());
}
return null;
}
/**
* Save config object
* @param instance to save
*/
public boolean saveConfigObject(T instance) {
try {
handler.saveObject(instance);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
| InstantiationException | NoSuchMethodException | IntrospectionException | SQLException e) {
logger.severe(() -> "Could not save config! Error: " + e.getMessage());
return false;
}
return true;
}
/**
* Checks if a config object exists or not
* @param name - unique name of the config object
* @return true if it exists
*/
public boolean configObjectExists(String name) {
return handler.objectExists(name);
}
}