From 79e4bb90f74f47e2b3c8b82a49e7baa717ab7561 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 25 Jun 2023 16:32:46 +0200 Subject: [PATCH] Inject permissions system into Vault on plugin load --- .../permissions/PandalibPaperPermissions.java | 16 ++++++--- .../permissions/PermissionsInjectorVault.java | 36 ++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/pandalib-paper-permissions/src/main/java/fr/pandacube/lib/paper/permissions/PandalibPaperPermissions.java b/pandalib-paper-permissions/src/main/java/fr/pandacube/lib/paper/permissions/PandalibPaperPermissions.java index 5db68d5..37d80d1 100644 --- a/pandalib-paper-permissions/src/main/java/fr/pandacube/lib/paper/permissions/PandalibPaperPermissions.java +++ b/pandalib-paper-permissions/src/main/java/fr/pandacube/lib/paper/permissions/PandalibPaperPermissions.java @@ -27,7 +27,7 @@ import fr.pandacube.lib.util.Log; /** * Class that integrates the {@code pandalib-permissions} system into a Bukkit/Spigot/Paper instance. - * The integration is made when calling {@link #init(JavaPlugin, String)}. + * The integration is made when calling {@link #onLoad(JavaPlugin, String)} and {@link #onEnable()}. * The permission system must be initialized first, using {@link Permissions#init(Function)}. * Don’t forget that the permission system also needs a connection to a database, so don’t forget to call * {@link DB#init(DBConnection, String)} with the appropriate parameters before anything. @@ -38,18 +38,26 @@ public class PandalibPaperPermissions implements Listener { /* package */ static String serverName; /* package */ static final Map permissionMap = new HashMap<>(); + /** - * Integrates the {@code pandalib-permissions} system into the Bukkit server. + * Integrates the {@code pandalib-permissions} system into the Bukkit server, during the loading phase of the plugin. * @param plugin a Bukkit plugin. * @param serverName the name of the current server, used to fetch server specific permissions. Cannot be null. * If this server in not in a multiserver configuration, use a dummy server name, like * {@code ""} (empty string). */ - public static void init(JavaPlugin plugin, String serverName) { + public static void onLoad(JavaPlugin plugin, String serverName) { PandalibPaperPermissions.plugin = plugin; PandalibPaperPermissions.serverName = serverName; + PermissionsInjectorVault.onLoad(); + } + + /** + * Integrates the {@code pandalib-permissions} system into the Bukkit server, during the enabling phase of the plugin. + */ + public static void onEnable() { PermissionsInjectorBukkit.inject(Bukkit.getConsoleSender()); - PermissionsInjectorVault.inject(); + PermissionsInjectorVault.onEnable(); PermissionsInjectorWEPIF.inject(); Bukkit.getPluginManager().registerEvents(new PandalibPaperPermissions(), plugin); diff --git a/pandalib-paper-permissions/src/main/java/fr/pandacube/lib/paper/permissions/PermissionsInjectorVault.java b/pandalib-paper-permissions/src/main/java/fr/pandacube/lib/paper/permissions/PermissionsInjectorVault.java index c408766..93e57f8 100644 --- a/pandalib-paper-permissions/src/main/java/fr/pandacube/lib/paper/permissions/PermissionsInjectorVault.java +++ b/pandalib-paper-permissions/src/main/java/fr/pandacube/lib/paper/permissions/PermissionsInjectorVault.java @@ -16,8 +16,11 @@ import java.util.List; private static final ServicePriority servicePriority = ServicePriority.Highest; public static PandaVaultPermission permInstance; - - public static void inject() { + + /** + * Vault injection needs to happen as soon as possible so other plugins detects it when they load. + */ + public static void onLoad() { try { permInstance = new PandaVaultPermission(); PandaVaultChat chat = new PandaVaultChat(permInstance); @@ -26,12 +29,15 @@ import java.util.List; Bukkit.getServicesManager().register(Chat.class, chat, PandalibPaperPermissions.plugin, servicePriority); Log.info("Providing permissions and chat prefix/suffix through Vault API."); - Bukkit.getScheduler().runTaskLater(PandalibPaperPermissions.plugin, - PermissionsInjectorVault::checkServicesRegistration, 1); } catch (NoClassDefFoundError e) { Log.warning("Vault plugin not detected. Not using it to provide permissions and prefix/suffix." + e.getMessage()); } } + + public static void onEnable() { + Bukkit.getScheduler().runTaskLater(PandalibPaperPermissions.plugin, + PermissionsInjectorVault::checkServicesRegistration, 1); + } private static void checkServicesRegistration() { @@ -68,6 +74,11 @@ import java.util.List; return PandalibPaperPermissions.plugin != null && PandalibPaperPermissions.plugin.isEnabled(); } + private void checkEnabled() { + if (!isEnabled()) + throw new IllegalStateException("Cannot provide permission service because plugin is disabled."); + } + @Override public boolean hasSuperPermsCompat() { return true; @@ -81,6 +92,7 @@ import java.util.List; @Override public boolean playerHas(String world, OfflinePlayer player, String permission) { + checkEnabled(); Boolean res = Permissions.getPlayer(player.getUniqueId()).hasPermission(permission, PandalibPaperPermissions.serverName, world); if (res != null) return res; @@ -100,6 +112,7 @@ import java.util.List; @Override public boolean playerAdd(String world, OfflinePlayer player, String permission) { + checkEnabled(); String server = PandalibPaperPermissions.serverName; Permissions.getPlayer(player.getUniqueId()).addSelfPermission(permission, server, world); Log.info("A plugin added permission " + permission + " (server=" + server + ",world=" + world + ") to player " + player.getName() + " through Vault."); @@ -114,6 +127,7 @@ import java.util.List; @Override public boolean playerRemove(String world, OfflinePlayer player, String permission) { + checkEnabled(); String server = PandalibPaperPermissions.serverName; Permissions.getPlayer(player.getUniqueId()).removeSelfPermission(permission, server, world); Log.info("A plugin removed permission " + permission + " (server=" + server + ",world=" + world + ") to player " + player.getName() + " through Vault."); @@ -122,6 +136,7 @@ import java.util.List; @Override public boolean groupHas(String world, String group, String permission) { + checkEnabled(); Boolean res = Permissions.getGroup(group).hasPermission(permission, PandalibPaperPermissions.serverName, world); if (res != null) return res; @@ -155,6 +170,7 @@ import java.util.List; @Override public boolean playerInGroup(String world, OfflinePlayer player, String group) { + checkEnabled(); return Permissions.getPlayer(player.getUniqueId()).isInGroup(group); } @@ -182,6 +198,7 @@ import java.util.List; @Override public String[] getPlayerGroups(String world, OfflinePlayer player) { + checkEnabled(); List groups = Permissions.getPlayer(player.getUniqueId()).getGroupsString(); return groups.toArray(new String[0]); } @@ -194,12 +211,14 @@ import java.util.List; @Override public String getPrimaryGroup(String world, OfflinePlayer player) { + checkEnabled(); return Permissions.getPlayer(player.getUniqueId()).getGroupsString().stream() .findFirst().orElse(null); } @Override public String[] getGroups() { + checkEnabled(); return Permissions.getGroups().stream() .map(PermGroup::getName).toArray(String[]::new); } @@ -228,6 +247,11 @@ import java.util.List; return PandalibPaperPermissions.plugin != null && PandalibPaperPermissions.plugin.isEnabled(); } + private void checkEnabled() { + if (!isEnabled()) + throw new IllegalStateException("Cannot provide permission service because plugin is disabled."); + } + @Deprecated @Override public String getPlayerPrefix(String world, String player) { @@ -236,6 +260,7 @@ import java.util.List; @Override public String getPlayerPrefix(String world, OfflinePlayer player) { + checkEnabled(); return Permissions.getPlayer(player.getUniqueId()).getPrefix(); } @@ -247,16 +272,19 @@ import java.util.List; @Override public String getPlayerSuffix(String world, OfflinePlayer player) { + checkEnabled(); return Permissions.getPlayer(player.getUniqueId()).getSuffix(); } @Override public String getGroupPrefix(String world, String group) { + checkEnabled(); return Permissions.getGroup(group).getPrefix(); } @Override public String getGroupSuffix(String world, String group) { + checkEnabled(); return Permissions.getGroup(group).getSuffix(); }