From 01b7b04dfcdc1c998b9909989c169c40dcae448a Mon Sep 17 00:00:00 2001 From: Tastybento Date: Fri, 24 Nov 2017 13:04:13 -0800 Subject: [PATCH] Made database classes available to add-ons. --- plugin.yml | 6 +--- .../api/commands/AbstractCommand.java | 5 ++- .../bskyblock/commands/AdminCommand.java | 2 +- .../bskyblock/commands/IslandCommand.java | 2 +- .../bskyblock/database/BSBDatabase.java | 5 +-- .../database/flatfile/FlatFileDatabase.java | 5 +-- .../flatfile/FlatFileDatabaseConnecter.java | 6 ++-- .../flatfile/FlatFileDatabaseHandler.java | 35 +++++++++++-------- .../managers/AbstractDatabaseHandler.java | 6 ++-- .../database/mongodb/MongoDBDatabase.java | 5 +-- .../database/mysql/MySQLDatabase.java | 5 +-- .../database/mysql/MySQLDatabaseHandler.java | 4 +-- .../database/sqlite/SQLiteDatabase.java | 5 +-- 13 files changed, 49 insertions(+), 42 deletions(-) diff --git a/plugin.yml b/plugin.yml index 528463257..edd203089 100755 --- a/plugin.yml +++ b/plugin.yml @@ -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 diff --git a/src/main/java/us/tastybento/bskyblock/api/commands/AbstractCommand.java b/src/main/java/us/tastybento/bskyblock/api/commands/AbstractCommand.java index 7ba85e4d2..83a71bca1 100644 --- a/src/main/java/us/tastybento/bskyblock/api/commands/AbstractCommand.java +++ b/src/main/java/us/tastybento/bskyblock/api/commands/AbstractCommand.java @@ -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 argumentsMap; public final Set 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; diff --git a/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java b/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java index e122265ff..c637f2777 100755 --- a/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java @@ -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; diff --git a/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java b/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java index a1c9d6c3d..fb1890f54 100755 --- a/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java @@ -53,7 +53,7 @@ public class IslandCommand extends AbstractCommand { protected Set 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; diff --git a/src/main/java/us/tastybento/bskyblock/database/BSBDatabase.java b/src/main/java/us/tastybento/bskyblock/database/BSBDatabase.java index 239185b42..01183c90d 100755 --- a/src/main/java/us/tastybento/bskyblock/database/BSBDatabase.java +++ b/src/main/java/us/tastybento/bskyblock/database/BSBDatabase.java @@ -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); } \ No newline at end of file diff --git a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabase.java b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabase.java index 23a61d8b0..c20f15112 100755 --- a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabase.java +++ b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabase.java @@ -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)); } diff --git a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseConnecter.java b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseConnecter.java index b1df81e24..14358b2da 100644 --- a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseConnecter.java +++ b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseConnecter.java @@ -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); } diff --git a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseHandler.java b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseHandler.java index f06e8c66b..eaa5e2faf 100644 --- a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseHandler.java +++ b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseHandler.java @@ -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 extends AbstractDatabaseHandler { private static final String DATABASE_FOLDER_NAME = "database"; private static final boolean DEBUG = false; - public FlatFileDatabaseHandler(BSkyBlock plugin, Class type, DatabaseConnecter databaseConnecter) { + public FlatFileDatabaseHandler(Plugin plugin, Class type, DatabaseConnecter databaseConnecter) { super(plugin, type, databaseConnecter); } @@ -269,23 +269,28 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { 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 result = new HashMap(); - for (Entry object : ((Map)value).entrySet()) { - // Serialize all key types - // TODO: also need to serialize values? - result.put(serialize(object.getKey()), object.getValue()); + if (value != null) { + Map result = new HashMap(); + for (Entry object : ((Map)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 list = new ArrayList(); - for (Object object : (Set)value) { - list.add(serialize(object)); + if (DEBUG) + plugin.getLogger().info("DEBUG: Set for " + field.getName()); + if (value != null) { + List list = new ArrayList(); + for (Object object : (Set)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)); diff --git a/src/main/java/us/tastybento/bskyblock/database/managers/AbstractDatabaseHandler.java b/src/main/java/us/tastybento/bskyblock/database/managers/AbstractDatabaseHandler.java index dbd06f0ae..bfa546a81 100644 --- a/src/main/java/us/tastybento/bskyblock/database/managers/AbstractDatabaseHandler.java +++ b/src/main/java/us/tastybento/bskyblock/database/managers/AbstractDatabaseHandler.java @@ -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 { protected final String deleteQuery; - protected BSkyBlock plugin; + protected Plugin plugin; /** @@ -48,7 +50,7 @@ public abstract class AbstractDatabaseHandler { * Contains the settings to create a connection to the database * like host/port/database/user/password */ - protected AbstractDatabaseHandler(BSkyBlock plugin, Class type, DatabaseConnecter databaseConnecter) { + protected AbstractDatabaseHandler(Plugin plugin, Class type, DatabaseConnecter databaseConnecter) { this.plugin = plugin; this.databaseConnecter = databaseConnecter; this.type = type; diff --git a/src/main/java/us/tastybento/bskyblock/database/mongodb/MongoDBDatabase.java b/src/main/java/us/tastybento/bskyblock/database/mongodb/MongoDBDatabase.java index 789271b30..3ec28773d 100644 --- a/src/main/java/us/tastybento/bskyblock/database/mongodb/MongoDBDatabase.java +++ b/src/main/java/us/tastybento/bskyblock/database/mongodb/MongoDBDatabase.java @@ -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 } diff --git a/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabase.java b/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabase.java index b65d6235b..d126bee51 100755 --- a/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabase.java +++ b/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabase.java @@ -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())); } diff --git a/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java b/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java index 761258e7b..114935dd7 100644 --- a/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java +++ b/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java @@ -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 extends AbstractDatabaseHandler { * @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 type, DatabaseConnecter databaseConnecter) { + public MySQLDatabaseHandler(Plugin plugin, Class type, DatabaseConnecter databaseConnecter) { super(plugin, type, databaseConnecter); try { connection = databaseConnecter.createConnection(); diff --git a/src/main/java/us/tastybento/bskyblock/database/sqlite/SQLiteDatabase.java b/src/main/java/us/tastybento/bskyblock/database/sqlite/SQLiteDatabase.java index caa6f3827..056153e7d 100755 --- a/src/main/java/us/tastybento/bskyblock/database/sqlite/SQLiteDatabase.java +++ b/src/main/java/us/tastybento/bskyblock/database/sqlite/SQLiteDatabase.java @@ -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(plugin, Island.class, new FlatFileDatabaseConnecter(plugin, null)); return null; }