-
Notifications
You must be signed in to change notification settings - Fork 4
How to use ForgePerms
This will explain how to use the ForgePerms API within your mod, without making it a required dependency.
We will be using Java reflect to get all the methods needed.
First you want to get the 2 classes (ForgePerms and PermissionsBase) like this:
Class ForgePerms = Class.forName("com.sperion.forgeperms.ForgePerms"); //Used only to get the actual permission handler
Class PermissionsBase = Class.forName("com.sperion.forgeperms.PermissionsBase"); //Is the base of all permission managers in ForgePerms
Using those two classes, you would get the methods needed like this
Method getPermissionHandler = ForgePerms.getMethod("getPermissionHandler");
Method canAccess = PermissionsBase.getMethod("canAccess", String, String, String);
Method getPrefix = PermissionsBase.getMethod("getPrefix", String, String);
Method getPostfix = PermissionsBase.getMethod("getPostfix", String, String);
Method getOption = PermissionsBase.getMethod("getOption", String, String, String, String); //Not supported except with PEX Forge edition
Once you have Classes and Methods, all that is left is to get the handler and call the methods you need.
Class permHandler = getPermissionHandler.invoke(null);
canAccess.invoke(permHandler, username, world, node); //Will return a bool
getPrefix.invoke(permHandler, username, world); //Will return a string of the users prefix
getPostfix.invoke(permHandler, username, world); //Will return a string of the users postfix/suffix
getOption.invoke(permHandler, username, world, node, default); //Will return a string with the users option
Hopefully I didn't make any mistakes. Always remember to use try catch when dealing with Java Reflect and always check if something is null before using!
If you have any questions, please ask.