Inject permissions system into Vault on plugin load

This commit is contained in:
Marc Baloup 2023-06-25 16:32:46 +02:00
parent 736e0f0c23
commit 79e4bb90f7
2 changed files with 44 additions and 8 deletions

View File

@ -27,7 +27,7 @@ import fr.pandacube.lib.util.Log;
/** /**
* Class that integrates the {@code pandalib-permissions} system into a Bukkit/Spigot/Paper instance. * 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)}. * The permission system must be initialized first, using {@link Permissions#init(Function)}.
* Dont forget that the permission system also needs a connection to a database, so dont forget to call * Dont forget that the permission system also needs a connection to a database, so dont forget to call
* {@link DB#init(DBConnection, String)} with the appropriate parameters before anything. * {@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 String serverName;
/* package */ static final Map<String, String> permissionMap = new HashMap<>(); /* package */ static final Map<String, String> 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 plugin a Bukkit plugin.
* @param serverName the name of the current server, used to fetch server specific permissions. Cannot be null. * @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 * If this server in not in a multiserver configuration, use a dummy server name, like
* {@code ""} (empty string). * {@code ""} (empty string).
*/ */
public static void init(JavaPlugin plugin, String serverName) { public static void onLoad(JavaPlugin plugin, String serverName) {
PandalibPaperPermissions.plugin = plugin; PandalibPaperPermissions.plugin = plugin;
PandalibPaperPermissions.serverName = serverName; 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()); PermissionsInjectorBukkit.inject(Bukkit.getConsoleSender());
PermissionsInjectorVault.inject(); PermissionsInjectorVault.onEnable();
PermissionsInjectorWEPIF.inject(); PermissionsInjectorWEPIF.inject();
Bukkit.getPluginManager().registerEvents(new PandalibPaperPermissions(), plugin); Bukkit.getPluginManager().registerEvents(new PandalibPaperPermissions(), plugin);

View File

@ -17,7 +17,10 @@ import java.util.List;
public static PandaVaultPermission permInstance; 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 { try {
permInstance = new PandaVaultPermission(); permInstance = new PandaVaultPermission();
PandaVaultChat chat = new PandaVaultChat(permInstance); PandaVaultChat chat = new PandaVaultChat(permInstance);
@ -26,13 +29,16 @@ import java.util.List;
Bukkit.getServicesManager().register(Chat.class, chat, Bukkit.getServicesManager().register(Chat.class, chat,
PandalibPaperPermissions.plugin, servicePriority); PandalibPaperPermissions.plugin, servicePriority);
Log.info("Providing permissions and chat prefix/suffix through Vault API."); Log.info("Providing permissions and chat prefix/suffix through Vault API.");
Bukkit.getScheduler().runTaskLater(PandalibPaperPermissions.plugin,
PermissionsInjectorVault::checkServicesRegistration, 1);
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
Log.warning("Vault plugin not detected. Not using it to provide permissions and prefix/suffix." + e.getMessage()); 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() { private static void checkServicesRegistration() {
Permission permService = Bukkit.getServicesManager().load(Permission.class); Permission permService = Bukkit.getServicesManager().load(Permission.class);
@ -68,6 +74,11 @@ import java.util.List;
return PandalibPaperPermissions.plugin != null && PandalibPaperPermissions.plugin.isEnabled(); return PandalibPaperPermissions.plugin != null && PandalibPaperPermissions.plugin.isEnabled();
} }
private void checkEnabled() {
if (!isEnabled())
throw new IllegalStateException("Cannot provide permission service because plugin is disabled.");
}
@Override @Override
public boolean hasSuperPermsCompat() { public boolean hasSuperPermsCompat() {
return true; return true;
@ -81,6 +92,7 @@ import java.util.List;
@Override @Override
public boolean playerHas(String world, OfflinePlayer player, String permission) { public boolean playerHas(String world, OfflinePlayer player, String permission) {
checkEnabled();
Boolean res = Permissions.getPlayer(player.getUniqueId()).hasPermission(permission, PandalibPaperPermissions.serverName, world); Boolean res = Permissions.getPlayer(player.getUniqueId()).hasPermission(permission, PandalibPaperPermissions.serverName, world);
if (res != null) if (res != null)
return res; return res;
@ -100,6 +112,7 @@ import java.util.List;
@Override @Override
public boolean playerAdd(String world, OfflinePlayer player, String permission) { public boolean playerAdd(String world, OfflinePlayer player, String permission) {
checkEnabled();
String server = PandalibPaperPermissions.serverName; String server = PandalibPaperPermissions.serverName;
Permissions.getPlayer(player.getUniqueId()).addSelfPermission(permission, server, world); 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."); 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 @Override
public boolean playerRemove(String world, OfflinePlayer player, String permission) { public boolean playerRemove(String world, OfflinePlayer player, String permission) {
checkEnabled();
String server = PandalibPaperPermissions.serverName; String server = PandalibPaperPermissions.serverName;
Permissions.getPlayer(player.getUniqueId()).removeSelfPermission(permission, server, world); 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."); 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 @Override
public boolean groupHas(String world, String group, String permission) { public boolean groupHas(String world, String group, String permission) {
checkEnabled();
Boolean res = Permissions.getGroup(group).hasPermission(permission, PandalibPaperPermissions.serverName, world); Boolean res = Permissions.getGroup(group).hasPermission(permission, PandalibPaperPermissions.serverName, world);
if (res != null) if (res != null)
return res; return res;
@ -155,6 +170,7 @@ import java.util.List;
@Override @Override
public boolean playerInGroup(String world, OfflinePlayer player, String group) { public boolean playerInGroup(String world, OfflinePlayer player, String group) {
checkEnabled();
return Permissions.getPlayer(player.getUniqueId()).isInGroup(group); return Permissions.getPlayer(player.getUniqueId()).isInGroup(group);
} }
@ -182,6 +198,7 @@ import java.util.List;
@Override @Override
public String[] getPlayerGroups(String world, OfflinePlayer player) { public String[] getPlayerGroups(String world, OfflinePlayer player) {
checkEnabled();
List<String> groups = Permissions.getPlayer(player.getUniqueId()).getGroupsString(); List<String> groups = Permissions.getPlayer(player.getUniqueId()).getGroupsString();
return groups.toArray(new String[0]); return groups.toArray(new String[0]);
} }
@ -194,12 +211,14 @@ import java.util.List;
@Override @Override
public String getPrimaryGroup(String world, OfflinePlayer player) { public String getPrimaryGroup(String world, OfflinePlayer player) {
checkEnabled();
return Permissions.getPlayer(player.getUniqueId()).getGroupsString().stream() return Permissions.getPlayer(player.getUniqueId()).getGroupsString().stream()
.findFirst().orElse(null); .findFirst().orElse(null);
} }
@Override @Override
public String[] getGroups() { public String[] getGroups() {
checkEnabled();
return Permissions.getGroups().stream() return Permissions.getGroups().stream()
.map(PermGroup::getName).toArray(String[]::new); .map(PermGroup::getName).toArray(String[]::new);
} }
@ -228,6 +247,11 @@ import java.util.List;
return PandalibPaperPermissions.plugin != null && PandalibPaperPermissions.plugin.isEnabled(); return PandalibPaperPermissions.plugin != null && PandalibPaperPermissions.plugin.isEnabled();
} }
private void checkEnabled() {
if (!isEnabled())
throw new IllegalStateException("Cannot provide permission service because plugin is disabled.");
}
@Deprecated @Deprecated
@Override @Override
public String getPlayerPrefix(String world, String player) { public String getPlayerPrefix(String world, String player) {
@ -236,6 +260,7 @@ import java.util.List;
@Override @Override
public String getPlayerPrefix(String world, OfflinePlayer player) { public String getPlayerPrefix(String world, OfflinePlayer player) {
checkEnabled();
return Permissions.getPlayer(player.getUniqueId()).getPrefix(); return Permissions.getPlayer(player.getUniqueId()).getPrefix();
} }
@ -247,16 +272,19 @@ import java.util.List;
@Override @Override
public String getPlayerSuffix(String world, OfflinePlayer player) { public String getPlayerSuffix(String world, OfflinePlayer player) {
checkEnabled();
return Permissions.getPlayer(player.getUniqueId()).getSuffix(); return Permissions.getPlayer(player.getUniqueId()).getSuffix();
} }
@Override @Override
public String getGroupPrefix(String world, String group) { public String getGroupPrefix(String world, String group) {
checkEnabled();
return Permissions.getGroup(group).getPrefix(); return Permissions.getGroup(group).getPrefix();
} }
@Override @Override
public String getGroupSuffix(String world, String group) { public String getGroupSuffix(String world, String group) {
checkEnabled();
return Permissions.getGroup(group).getSuffix(); return Permissions.getGroup(group).getSuffix();
} }