Made database classes available to add-ons.

This commit is contained in:
Tastybento 2017-11-24 13:04:13 -08:00
parent 4c1f7f24a5
commit 01b7b04dfc
13 changed files with 49 additions and 42 deletions

View File

@ -20,11 +20,7 @@ commands:
description: Admin commands
usage: |
/bsadmin help
bsc:
description: Game challenges
aliases: [c, challenge, bschallenge, challenges]
usage: |
/challenge
permissions:
bskyblock.*:
default: false

View File

@ -29,7 +29,7 @@ import us.tastybento.bskyblock.util.Util;
*/
public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
private BSkyBlock plugin;
private BSkyBlock plugin = BSkyBlock.getPlugin();
public final Map<String, ArgumentHandler> argumentsMap;
public final Set<ArgumentHandler> handlers;
@ -47,8 +47,7 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
private static final boolean DEBUG = false;
protected AbstractCommand(BSkyBlock plugin, String label, String[] aliases, boolean help) {
this.plugin = plugin;
protected AbstractCommand(String label, String[] aliases, boolean help) {
this.argumentsMap = new LinkedHashMap<>();
this.handlers = new HashSet<>();
this.label = label;

View File

@ -34,7 +34,7 @@ public class AdminCommand extends AbstractCommand {
* @param plugin
*/
public AdminCommand(BSkyBlock plugin) {
super(plugin, Settings.ADMINCOMMAND, new String[0], true);
super(Settings.ADMINCOMMAND, new String[0], true);
plugin.getCommand(Settings.ADMINCOMMAND).setExecutor(this);
plugin.getCommand(Settings.ADMINCOMMAND).setTabCompleter(this);
this.plugin = plugin;

View File

@ -53,7 +53,7 @@ public class IslandCommand extends AbstractCommand {
protected Set<UUID> kickingPlayers = new HashSet<>();
public IslandCommand(BSkyBlock plugin) {
super(plugin, Settings.ISLANDCOMMAND, new String[]{"is"}, true);
super(Settings.ISLANDCOMMAND, new String[]{"is"}, true);
plugin.getCommand(Settings.ISLANDCOMMAND).setExecutor(this);
plugin.getCommand(Settings.ISLANDCOMMAND).setTabCompleter(this);
this.plugin = plugin;

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.database;
import us.tastybento.bskyblock.BSkyBlock;
import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.config.Settings;
import us.tastybento.bskyblock.database.flatfile.FlatFileDatabase;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
@ -41,6 +42,6 @@ public abstract class BSBDatabase {
* @param dataObjectClass
* @return database handler
*/
public abstract AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> dataObjectClass);
public abstract AbstractDatabaseHandler<?> getHandler(Plugin plugin, Class<?> dataObjectClass);
}

View File

@ -1,13 +1,14 @@
package us.tastybento.bskyblock.database.flatfile;
import us.tastybento.bskyblock.BSkyBlock;
import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
public class FlatFileDatabase extends BSBDatabase{
@Override
public AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type) {
public AbstractDatabaseHandler<?> getHandler(Plugin plugin, Class<?> type) {
return new FlatFileDatabaseHandler<>(plugin, type, new FlatFileDatabaseConnecter(plugin, null));
}

View File

@ -6,8 +6,8 @@ import java.sql.SQLException;
import java.util.UUID;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
@ -15,11 +15,11 @@ public class FlatFileDatabaseConnecter implements DatabaseConnecter {
private static final int MAX_LOOPS = 100;
private static final String DATABASE_FOLDER_NAME = "database";
private BSkyBlock plugin;
private Plugin plugin;
private File dataFolder;
public FlatFileDatabaseConnecter(BSkyBlock plugin, DatabaseConnectionSettingsImpl databaseConnectionSettingsImpl) {
public FlatFileDatabaseConnecter(Plugin plugin, DatabaseConnectionSettingsImpl databaseConnectionSettingsImpl) {
this.plugin = plugin;
dataFolder = new File(plugin.getDataFolder(), DATABASE_FOLDER_NAME);
}

View File

@ -21,8 +21,8 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.util.Util;
@ -39,7 +39,7 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
private static final String DATABASE_FOLDER_NAME = "database";
private static final boolean DEBUG = false;
public FlatFileDatabaseHandler(BSkyBlock plugin, Class<T> type, DatabaseConnecter databaseConnecter) {
public FlatFileDatabaseHandler(Plugin plugin, Class<T> type, DatabaseConnecter databaseConnecter) {
super(plugin, type, databaseConnecter);
}
@ -269,23 +269,28 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
if (propertyDescriptor.getPropertyType().equals(HashMap.class) || propertyDescriptor.getPropertyType().equals(Map.class)) {
// Maps need to have keys serialized
//plugin.getLogger().info("DEBUG: Map for " + field.getName());
Map<Object, Object> result = new HashMap<Object, Object>();
for (Entry<Object, Object> object : ((Map<Object,Object>)value).entrySet()) {
// Serialize all key types
// TODO: also need to serialize values?
result.put(serialize(object.getKey()), object.getValue());
if (value != null) {
Map<Object, Object> result = new HashMap<Object, Object>();
for (Entry<Object, Object> object : ((Map<Object,Object>)value).entrySet()) {
// Serialize all key types
// TODO: also need to serialize values?
result.put(serialize(object.getKey()), object.getValue());
}
// Save the list in the config file
config.set(field.getName(), result);
}
// Save the list in the config file
config.set(field.getName(), result);
} else if (propertyDescriptor.getPropertyType().equals(Set.class)) {
// Sets need to be serialized as string lists
//plugin.getLogger().info("DEBUG: Set for " + field.getName());
List<Object> list = new ArrayList<Object>();
for (Object object : (Set<Object>)value) {
list.add(serialize(object));
if (DEBUG)
plugin.getLogger().info("DEBUG: Set for " + field.getName());
if (value != null) {
List<Object> list = new ArrayList<Object>();
for (Object object : (Set<Object>)value) {
list.add(serialize(object));
}
// Save the list in the config file
config.set(field.getName(), list);
}
// Save the list in the config file
config.set(field.getName(), list);
} else {
// For all other data that doesn't need special serialization
config.set(field.getName(), serialize(value));

View File

@ -5,6 +5,8 @@ import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.DatabaseConnecter;
@ -35,7 +37,7 @@ public abstract class AbstractDatabaseHandler<T> {
protected final String deleteQuery;
protected BSkyBlock plugin;
protected Plugin plugin;
/**
@ -48,7 +50,7 @@ public abstract class AbstractDatabaseHandler<T> {
* Contains the settings to create a connection to the database
* like host/port/database/user/password
*/
protected AbstractDatabaseHandler(BSkyBlock plugin, Class<T> type, DatabaseConnecter databaseConnecter) {
protected AbstractDatabaseHandler(Plugin plugin, Class<T> type, DatabaseConnecter databaseConnecter) {
this.plugin = plugin;
this.databaseConnecter = databaseConnecter;
this.type = type;

View File

@ -1,13 +1,14 @@
package us.tastybento.bskyblock.database.mongodb;
import us.tastybento.bskyblock.BSkyBlock;
import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
public class MongoDBDatabase extends BSBDatabase{
@Override
public AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type) {
public AbstractDatabaseHandler<?> getHandler(Plugin plugin, Class<?> type) {
return null; //TODO
}

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.database.mysql;
import us.tastybento.bskyblock.BSkyBlock;
import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.DatabaseConnectionSettingsImpl;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
@ -8,7 +9,7 @@ import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
public class MySQLDatabase extends BSBDatabase{
@Override
public AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type) {
public AbstractDatabaseHandler<?> getHandler(Plugin plugin, Class<?> type) {
return new MySQLDatabaseHandler<>(plugin, type, new MySQLDatabaseConnecter(new DatabaseConnectionSettingsImpl()));
}

View File

@ -30,8 +30,8 @@ import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.util.Util;
@ -99,7 +99,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
* @param type - the type of class to be stored in the database. Must inherit DataObject
* @param databaseConnecter - authentication details for the database
*/
public MySQLDatabaseHandler(BSkyBlock plugin, Class<T> type, DatabaseConnecter databaseConnecter) {
public MySQLDatabaseHandler(Plugin plugin, Class<T> type, DatabaseConnecter databaseConnecter) {
super(plugin, type, databaseConnecter);
try {
connection = databaseConnecter.createConnection();

View File

@ -1,13 +1,14 @@
package us.tastybento.bskyblock.database.sqlite;
import us.tastybento.bskyblock.BSkyBlock;
import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
public class SQLiteDatabase extends BSBDatabase{
@Override
public AbstractDatabaseHandler<?> getHandler(BSkyBlock plugin, Class<?> type) {
public AbstractDatabaseHandler<?> getHandler(Plugin plugin, Class<?> type) {
// return new SQLLiteDatabaseHandler<Island>(plugin, Island.class, new FlatFileDatabaseConnecter(plugin, null));
return null;
}