mirror of
				https://github.com/mcMMO-Dev/mcMMO.git
				synced 2025-11-04 11:03:43 +01:00 
			
		
		
		
	Add "volatile API" for a custom DatabaseManager
Reflection is fun! Catch-alls abound! Also added method createCustomDatabaseManager(), intended for conversion - this lets the command specify the class name of the custom manager
This commit is contained in:
		@@ -1,12 +1,51 @@
 | 
			
		||||
package com.gmail.nossr50.database;
 | 
			
		||||
 | 
			
		||||
import com.gmail.nossr50.mcMMO;
 | 
			
		||||
import com.gmail.nossr50.config.Config;
 | 
			
		||||
 | 
			
		||||
public class DatabaseManagerFactory {
 | 
			
		||||
    private static Class<? extends DatabaseManager> customManager = null;
 | 
			
		||||
 | 
			
		||||
    public static DatabaseManager getDatabaseManager() {
 | 
			
		||||
        if (customManager != null) {
 | 
			
		||||
            try {
 | 
			
		||||
                return createCustomDatabaseManager(customManager);
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
                mcMMO.p.debug("Could not create custom database manager");
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
            } catch (Throwable e) {
 | 
			
		||||
                mcMMO.p.debug("Failed to create custom database manager");
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
            mcMMO.p.debug("Falling back on " + (Config.getInstance().getUseMySQL() ? "SQL" : "Flatfile") + " database");
 | 
			
		||||
        }
 | 
			
		||||
        return Config.getInstance().getUseMySQL() ? new SQLDatabaseManager() : new FlatfileDatabaseManager();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sets the custom DatabaseManager class for McMMO to use. This should be
 | 
			
		||||
     * called prior to mcMMO enabling.
 | 
			
		||||
     * <p>
 | 
			
		||||
     * The provided class must have an empty constructor, which is the one
 | 
			
		||||
     * that will be used.
 | 
			
		||||
     * <p>
 | 
			
		||||
     * This method is intended for API use, but it should not be considered
 | 
			
		||||
     * stable. This method is subject to change and/or removal in future
 | 
			
		||||
     * versions.
 | 
			
		||||
     *
 | 
			
		||||
     * @param man the DatabaseManager class to use
 | 
			
		||||
     * @throws IllegalArgumentException if the provided class does not have
 | 
			
		||||
     *             an empty constructor
 | 
			
		||||
     */
 | 
			
		||||
    public static void setCustomDatabaseManagerClass(Class<? extends DatabaseManager> clazz) {
 | 
			
		||||
        try {
 | 
			
		||||
            clazz.getConstructor((Class<?>) null);
 | 
			
		||||
        } catch (Throwable e) {
 | 
			
		||||
            throw new IllegalArgumentException("Provided database manager class must have an empty constructor", e);
 | 
			
		||||
        }
 | 
			
		||||
        customManager = clazz;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // For data conversion purposes
 | 
			
		||||
 | 
			
		||||
    public static FlatfileDatabaseManager createFlatfileDatabaseManager() {
 | 
			
		||||
@@ -16,4 +55,8 @@ public class DatabaseManagerFactory {
 | 
			
		||||
    public static SQLDatabaseManager createSQLDatabaseManager() {
 | 
			
		||||
        return new SQLDatabaseManager();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static DatabaseManager createCustomDatabaseManager(Class<? extends DatabaseManager> clazz) throws Throwable {
 | 
			
		||||
        return customManager.getConstructor((Class<?>) null).newInstance((Object[]) null);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user