Made BSBDatabase much easier to use for addons

Now obtaining the database handler is easy and understandable.
This commit is contained in:
Tastybento 2018-04-15 17:30:42 -07:00
parent a5cc4c20ea
commit 2e1c055cd7
10 changed files with 162 additions and 81 deletions

View File

@ -7,7 +7,7 @@ import us.tastybento.bskyblock.api.placeholders.PlaceholderHandler;
import us.tastybento.bskyblock.api.user.Notifier;
import us.tastybento.bskyblock.commands.AdminCommand;
import us.tastybento.bskyblock.commands.IslandCommand;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.BSBDbSetup;
import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.listeners.JoinLeaveListener;
import us.tastybento.bskyblock.listeners.NetherPortals;
@ -195,7 +195,7 @@ public class BSkyBlock extends JavaPlugin {
@Override
public String getValue() {
return BSBDatabase.getDatabase().toString();
return BSBDbSetup.getDatabase().toString();
}
});
}

View File

@ -18,7 +18,7 @@ import us.tastybento.bskyblock.api.configuration.ConfigEntry;
import us.tastybento.bskyblock.api.configuration.ISettings;
import us.tastybento.bskyblock.api.configuration.StoreAt;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.database.BSBDatabase.DatabaseType;
import us.tastybento.bskyblock.database.BSBDbSetup.DatabaseType;
import us.tastybento.bskyblock.database.objects.adapters.Adapter;
import us.tastybento.bskyblock.database.objects.adapters.PotionEffectListAdapter;

View File

@ -5,7 +5,7 @@ import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.BSBDbSetup;
import us.tastybento.bskyblock.database.flatfile.ConfigHandler;
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
@ -38,7 +38,7 @@ public interface ISettings<T> {
@SuppressWarnings("unchecked")
default T loadSettings() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, SQLException {
// See if this settings object already exists in the database
AbstractDatabaseHandler<T> dbhandler = (AbstractDatabaseHandler<T>) BSBDatabase.getDatabase().getHandler(getClass());
AbstractDatabaseHandler<T> dbhandler = (AbstractDatabaseHandler<T>) BSBDbSetup.getDatabase().getHandler(getClass());
T dbConfig = null;
if (dbhandler.objectExists(this.getUniqueId())) {
// Load it

View File

@ -1,43 +1,114 @@
package us.tastybento.bskyblock.database;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
import us.tastybento.bskyblock.database.mongodb.MongoDBDatabase;
import us.tastybento.bskyblock.database.mysql.MySQLDatabase;
import us.tastybento.bskyblock.api.addons.Addon;
public abstract class BSBDatabase {
/**
* Handy class to store and load Java POJOs in the BSkyBlock Database
* @author tastybento
*
* @param <T>
*/
public class BSBDatabase<T> {
/**
* Gets the type of database being used. Currently supported options are
* FLATFILE and MYSQL. Default is FLATFILE
* @return Database type
*/
public static BSBDatabase getDatabase(){
for(DatabaseType type : DatabaseType.values()){
if(type == BSkyBlock.getInstance().getSettings().getDatabaseType()) {
return type.database;
}
}
return DatabaseType.FLATFILE.database;
private AbstractDatabaseHandler<T> handler;
private Logger logger;
@SuppressWarnings("unchecked")
public BSBDatabase(BSkyBlock plugin, Class<T> type) {
this.logger = plugin.getLogger();
handler = (AbstractDatabaseHandler<T>) BSBDbSetup.getDatabase().getHandler(type);
}
public enum DatabaseType{
FLATFILE(new FlatFileDatabase()),
MYSQL(new MySQLDatabase()),
MONGO(new MongoDBDatabase());
BSBDatabase database;
DatabaseType(BSBDatabase database){
this.database = database;
}
@SuppressWarnings("unchecked")
public BSBDatabase(Addon addon, Class<T> type) {
this.logger = addon.getLogger();
handler = (AbstractDatabaseHandler<T>) BSBDbSetup.getDatabase().getHandler(type);
}
/**
* Gets a database handler that will store and retrieve classes of type dataObjectClass
* @param dataObjectClass - class of the object to be stored in the database
* @return handler for this database object
* 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 abstract AbstractDatabaseHandler<?> getHandler(Class<?> dataObjectClass);
public List<T> loadObjects() {
List<T> result = new ArrayList<>();
try {
result = handler.loadObjects();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | ClassNotFoundException | IntrospectionException e) {
logger.severe(() -> "Could not load objects from database! 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 loadObject(String uniqueId) {
try {
return handler.loadObject(uniqueId);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| ClassNotFoundException | IntrospectionException e) {
logger.severe(() -> "Could not load object from database! " + e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* Save config object
* @param instance to save
*/
public boolean saveObject(T instance) {
try {
handler.saveObject(instance);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException
| IntrospectionException e) {
logger.severe(() -> "Could not save object to database! 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 objectExists(String name) {
return handler.objectExists(name);
}
/**
* Delete object from database
* @param object - object to delete
*/
public void deleteObject(T object) {
try {
handler.deleteObject(object);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| IntrospectionException e) {
logger.severe(() -> "Could not delete config! Error: " + e.getMessage());
}
}
/**
* Close the database
*/
public void close() {
handler.close();
}
}

View File

@ -0,0 +1,43 @@
package us.tastybento.bskyblock.database;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
import us.tastybento.bskyblock.database.mongodb.MongoDBDatabase;
import us.tastybento.bskyblock.database.mysql.MySQLDatabase;
public abstract class BSBDbSetup {
/**
* Gets the type of database being used. Currently supported options are
* FLATFILE and MYSQL. Default is FLATFILE
* @return Database type
*/
public static BSBDbSetup getDatabase(){
for(DatabaseType type : DatabaseType.values()){
if(type == BSkyBlock.getInstance().getSettings().getDatabaseType()) {
return type.database;
}
}
return DatabaseType.FLATFILE.database;
}
public enum DatabaseType{
FLATFILE(new FlatFileDatabase()),
MYSQL(new MySQLDatabase()),
MONGO(new MongoDBDatabase());
BSBDbSetup database;
DatabaseType(BSBDbSetup database){
this.database = database;
}
}
/**
* Gets a database handler that will store and retrieve classes of type dataObjectClass
* @param dataObjectClass - class of the object to be stored in the database
* @return handler for this database object
*/
public abstract AbstractDatabaseHandler<?> getHandler(Class<?> dataObjectClass);
}

View File

@ -2,9 +2,9 @@ package us.tastybento.bskyblock.database.flatfile;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.BSBDbSetup;
public class FlatFileDatabase extends BSBDatabase{
public class FlatFileDatabase extends BSBDbSetup{
@Override
public AbstractDatabaseHandler<?> getHandler(Class<?> type) {

View File

@ -2,10 +2,10 @@ package us.tastybento.bskyblock.database.mongodb;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.BSBDbSetup;
import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
public class MongoDBDatabase extends BSBDatabase{
public class MongoDBDatabase extends BSBDbSetup{
@Override
public AbstractDatabaseHandler<?> getHandler(Class<?> type) {

View File

@ -2,10 +2,10 @@ package us.tastybento.bskyblock.database.mysql;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.BSBDbSetup;
import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
public class MySQLDatabase extends BSBDatabase{
public class MySQLDatabase extends BSBDbSetup{
@Override
public AbstractDatabaseHandler<?> getHandler(Class<?> type) {

View File

@ -23,7 +23,6 @@ import org.bukkit.util.Vector;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.managers.island.IslandCache;
@ -114,13 +113,12 @@ public class IslandsManager {
private BSkyBlock plugin;
private BSBDatabase database;
/**
* One island can be spawn, this is the one - otherwise, this value is null
*/
private Island spawn;
private AbstractDatabaseHandler<Island> handler;
private BSBDatabase<Island> handler;
private Location last;
// Metrics data
@ -129,12 +127,10 @@ public class IslandsManager {
// Island Cache
private IslandCache islandCache;
@SuppressWarnings("unchecked")
public IslandsManager(BSkyBlock plugin){
this.plugin = plugin;
database = BSBDatabase.getDatabase();
// Set up the database handler to store and retrieve Island classes
handler = (AbstractDatabaseHandler<Island>) database.getHandler(Island.class);
handler = new BSBDatabase<>(plugin, Island.class);
islandCache = new IslandCache();
spawn = null;
}
@ -253,11 +249,7 @@ public class IslandsManager {
// Remove island from the cache
islandCache.deleteIslandFromCache(island);
// Remove the island from the database
try {
handler.deleteObject(island);
} catch (Exception e) {
plugin.getLogger().severe(()->"Could not delete island from database! " + e.getMessage());
}
handler.deleteObject(island);
// Remove blocks from world
new DeleteIslandChunks(plugin, island);
}
@ -296,10 +288,6 @@ public class IslandsManager {
return islandCache.size();
}
public AbstractDatabaseHandler<Island> getHandler() {
return handler;
}
/**
* Gets the island for this player. If they are in a team, the team island is returned
* @param uuid - UUID

View File

@ -1,7 +1,5 @@
package us.tastybento.bskyblock.managers;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@ -18,7 +16,6 @@ import org.bukkit.entity.Player;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.database.objects.Players;
@ -27,8 +24,7 @@ public class PlayersManager{
private static final boolean DEBUG = false;
private BSkyBlock plugin;
private BSBDatabase database;
private AbstractDatabaseHandler<Players> handler;
private BSBDatabase<Players> handler;
private Map<UUID, Players> playerCache;
private Set<UUID> inTeleport;
@ -40,12 +36,10 @@ public class PlayersManager{
*
* @param plugin - BSkyBlock plugin object
*/
@SuppressWarnings("unchecked")
public PlayersManager(BSkyBlock plugin){
this.plugin = plugin;
database = BSBDatabase.getDatabase();
// Set up the database handler to store and retrieve Players classes
handler = (AbstractDatabaseHandler<Players>) database.getHandler(Players.class);
handler = new BSBDatabase<>(plugin, Players.class);
playerCache = new HashMap<>();
inTeleport = new HashSet<>();
}
@ -576,22 +570,7 @@ public class PlayersManager{
*/
public void save(UUID playerUUID) {
if (playerCache.containsKey(playerUUID)) {
final Players player = playerCache.get(playerUUID);
try {
if (DEBUG) {
plugin.getLogger().info("DEBUG: saving player by uuid " + player.getPlayerName() + " " + playerUUID + " saved");
}
handler.saveObject(player);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | SecurityException
| IntrospectionException e) {
plugin.getLogger().severe("Could not save player to database: " + playerUUID + " " + e.getMessage());
}
} else {
if (DEBUG) {
plugin.getLogger().info("DEBUG: " + playerUUID + " is not in the cache to save");
}
handler.saveObject(playerCache.get(playerUUID));
}
}