From f6e3f1ca0b7587c39085ef05e192297c790483bc Mon Sep 17 00:00:00 2001 From: Luck Date: Wed, 29 Jun 2016 20:09:34 +0100 Subject: [PATCH] Fix NPE and plugin not loading when Vault missing --- .../me/lucko/luckperms/LPBukkitPlugin.java | 23 ++++-------- .../luckperms/vaulthooks/VaultChatHook.java | 4 +-- .../lucko/luckperms/vaulthooks/VaultHook.java | 36 +++++++++++++++++++ .../vaulthooks/VaultPermissionHook.java | 9 ++--- .../me/lucko/luckperms/data/Datastore.java | 2 +- .../data/methods/FlatfileDatastore.java | 2 +- .../data/methods/MySQLDatastore.java | 8 ++++- .../luckperms/data/methods/SQLDatastore.java | 8 ++--- .../data/methods/SQLiteDatastore.java | 16 +++++++-- 9 files changed, 74 insertions(+), 34 deletions(-) create mode 100644 bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultHook.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java index ff523f889..a35c9059b 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java @@ -12,14 +12,10 @@ import me.lucko.luckperms.runnables.UpdateTask; import me.lucko.luckperms.users.BukkitUserManager; import me.lucko.luckperms.users.UserManager; import me.lucko.luckperms.utils.LPConfiguration; -import me.lucko.luckperms.vaulthooks.VaultChatHook; -import me.lucko.luckperms.vaulthooks.VaultPermissionHook; -import net.milkbowl.vault.chat.Chat; -import net.milkbowl.vault.permission.Permission; +import me.lucko.luckperms.vaulthooks.VaultHook; import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; @@ -85,18 +81,11 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin { } // Provide vault support - try { - if (getServer().getPluginManager().isPluginEnabled("Vault")) { - final VaultPermissionHook permsHook = new VaultPermissionHook(this); - getServer().getServicesManager().register(Permission.class, permsHook, this, ServicePriority.High); - getServer().getServicesManager().register(Chat.class, new VaultChatHook(permsHook), this, ServicePriority.Lowest); - getLogger().info("Registered Vault permission & chat hook."); - } else { - getLogger().info("Vault not found."); - } - } catch (Exception e) { - getLogger().warning("Error whilst hooking into Vault."); - e.printStackTrace(); + if (getServer().getPluginManager().isPluginEnabled("Vault")) { + VaultHook.hook(this); + getLogger().info("Registered Vault permission & chat hook."); + } else { + getLogger().info("Vault not found."); } } diff --git a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultChatHook.java b/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultChatHook.java index fbba67b83..56dcdd889 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultChatHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultChatHook.java @@ -9,9 +9,9 @@ import net.milkbowl.vault.permission.Permission; * * Registered on the lowest priority so other plugins can override */ -public class VaultChatHook extends Chat { +class VaultChatHook extends Chat { - public VaultChatHook(Permission perms) { + VaultChatHook(Permission perms) { super(perms); } diff --git a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultHook.java b/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultHook.java new file mode 100644 index 000000000..d35286c09 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultHook.java @@ -0,0 +1,36 @@ +package me.lucko.luckperms.vaulthooks; + +import me.lucko.luckperms.LPBukkitPlugin; +import net.milkbowl.vault.chat.Chat; +import net.milkbowl.vault.permission.Permission; +import org.bukkit.plugin.ServicePriority; +import org.bukkit.plugin.ServicesManager; + +public class VaultHook { + + private static VaultChatHook chatHook = null; + private static VaultPermissionHook permissionHook = null; + + public static void hook(LPBukkitPlugin plugin) { + try { + if (permissionHook == null) { + permissionHook = new VaultPermissionHook(); + } + permissionHook.setPlugin(plugin); + + if (chatHook == null) { + chatHook = new VaultChatHook(permissionHook); + } + + final ServicesManager sm = plugin.getServer().getServicesManager(); + sm.unregisterAll(plugin); + sm.register(Permission.class, permissionHook, plugin, ServicePriority.High); + sm.register(Chat.class, chatHook, plugin, ServicePriority.Lowest); + + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultPermissionHook.java b/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultPermissionHook.java index 3cd8b07d9..7ee1033e0 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultPermissionHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultPermissionHook.java @@ -1,6 +1,6 @@ package me.lucko.luckperms.vaulthooks; -import lombok.AllArgsConstructor; +import lombok.Setter; import me.lucko.luckperms.LPBukkitPlugin; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectLacksPermissionException; @@ -8,9 +8,10 @@ import me.lucko.luckperms.groups.Group; import me.lucko.luckperms.users.User; import net.milkbowl.vault.permission.Permission; -@AllArgsConstructor -public class VaultPermissionHook extends Permission { - private final LPBukkitPlugin plugin; +class VaultPermissionHook extends Permission { + + @Setter + private LPBukkitPlugin plugin; @Override public String getName() { diff --git a/common/src/main/java/me/lucko/luckperms/data/Datastore.java b/common/src/main/java/me/lucko/luckperms/data/Datastore.java index b70d73ff2..78a69e4f5 100644 --- a/common/src/main/java/me/lucko/luckperms/data/Datastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/Datastore.java @@ -21,7 +21,7 @@ public abstract class Datastore { protected Datastore(LuckPermsPlugin plugin, String name) { this.plugin = plugin; this.name = name; - this.acceptingLogins = true; + this.acceptingLogins = false; } /** diff --git a/common/src/main/java/me/lucko/luckperms/data/methods/FlatfileDatastore.java b/common/src/main/java/me/lucko/luckperms/data/methods/FlatfileDatastore.java index 6476662ad..eb442e907 100644 --- a/common/src/main/java/me/lucko/luckperms/data/methods/FlatfileDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/methods/FlatfileDatastore.java @@ -89,12 +89,12 @@ public class FlatfileDatastore extends Datastore { try { makeFiles(); } catch (IOException e) { - // TODO catch here or something e.printStackTrace(); return; } uuidCache.putAll(getUUIDCache()); + setAcceptingLogins(true); } private void makeFiles() throws IOException { diff --git a/common/src/main/java/me/lucko/luckperms/data/methods/MySQLDatastore.java b/common/src/main/java/me/lucko/luckperms/data/methods/MySQLDatastore.java index c163acabe..3be5cbf9b 100644 --- a/common/src/main/java/me/lucko/luckperms/data/methods/MySQLDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/methods/MySQLDatastore.java @@ -6,6 +6,7 @@ import me.lucko.luckperms.data.MySQLConfiguration; import java.sql.Connection; import java.sql.SQLException; +import java.util.logging.Level; public class MySQLDatastore extends SQLDatastore { @@ -38,7 +39,12 @@ public class MySQLDatastore extends SQLDatastore { hikari.addDataSourceProperty("user", username); hikari.addDataSourceProperty("password", password); - setupTables(CREATETABLE_UUID, CREATETABLE_USERS, CREATETABLE_GROUPS); + if (!setupTables(CREATETABLE_UUID, CREATETABLE_USERS, CREATETABLE_GROUPS)) { + plugin.getLogger().log(Level.SEVERE, "Error occurred whilst initialising the database. All connections are disallowed."); + shutdown(); + } else { + setAcceptingLogins(true); + } } @Override diff --git a/common/src/main/java/me/lucko/luckperms/data/methods/SQLDatastore.java b/common/src/main/java/me/lucko/luckperms/data/methods/SQLDatastore.java index 78a1ebf37..edbb1875a 100644 --- a/common/src/main/java/me/lucko/luckperms/data/methods/SQLDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/methods/SQLDatastore.java @@ -113,17 +113,13 @@ abstract class SQLDatastore extends Datastore { return success; } - void setupTables(String... tableQueries) { + boolean setupTables(String... tableQueries) { boolean success = true; for (String q : tableQueries) { if (!runQuery(new Query(q))) success = false; } - if (!success) { - plugin.getLogger().log(Level.SEVERE, "Error occurred whilst initialising the database. All connections are disallowed."); - shutdown(); - setAcceptingLogins(false); - } + return success; } @Override diff --git a/common/src/main/java/me/lucko/luckperms/data/methods/SQLiteDatastore.java b/common/src/main/java/me/lucko/luckperms/data/methods/SQLiteDatastore.java index 97c0152dd..6a5b951f8 100644 --- a/common/src/main/java/me/lucko/luckperms/data/methods/SQLiteDatastore.java +++ b/common/src/main/java/me/lucko/luckperms/data/methods/SQLiteDatastore.java @@ -6,6 +6,7 @@ import java.io.File; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.util.logging.Level; public class SQLiteDatastore extends SQLDatastore { @@ -23,12 +24,23 @@ public class SQLiteDatastore extends SQLDatastore { @Override public void init() { - setupTables(CREATETABLE_UUID, CREATETABLE_USERS, CREATETABLE_GROUPS); + if (!setupTables(CREATETABLE_UUID, CREATETABLE_USERS, CREATETABLE_GROUPS)) { + plugin.getLogger().log(Level.SEVERE, "Error occurred whilst initialising the database. All connections are disallowed."); + shutdown(); + } else { + setAcceptingLogins(true); + } } @Override public void shutdown() { - + try { + if (connection != null && !connection.isClosed()) { + connection.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } } @Override