Skip to content

Commit

Permalink
Add optimized JPerms Support
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysB committed Jan 3, 2024
1 parent 8d89eee commit 3075692
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.sk89q.bukkit.migration;

import com.johnymuffin.jperms.beta.JohnyPerms;
import com.johnymuffin.jperms.beta.JohnyPermsAPI;
import com.johnymuffin.jperms.core.models.PermissionsGroup;
import com.projectposeidon.api.PoseidonUUID;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;

import java.util.UUID;

public class JPermsPermissionsResolver implements PermissionsResolver {
private Server server;
private JohnyPermsAPI api;

public JPermsPermissionsResolver(final Server server) throws MissingPluginException, PluginAccessException {
this.server = server;
if (Bukkit.getServer().getPluginManager().getPlugin("JPerms") == null) {
throw new MissingPluginException();
}
try {
this.api = JohnyPerms.getJPermsAPI();
} catch (Exception e) {
throw new PluginAccessException();
}

}

public void load() {

}

private UUID getPlayerUUID(final String name) {
for(Player player : Bukkit.getOnlinePlayers()) {
if(player.getName().equalsIgnoreCase(name)) {
return player.getUniqueId();
}
}
return PoseidonUUID.getPlayerGracefulUUID(name);
}

public boolean hasPermission(final String name, final String permission) {
UUID playerUUID = getPlayerUUID(name);

return api.getUser(playerUUID).hasPermissionSomehow(permission, true) || api.getUser(playerUUID).getGroup().hasPermission(permission, true);
}

@Override
public boolean hasPermission(String worldName, String name, String permission) {
//JPerms 1.0.0 does not support world permissions
return hasPermission(name, permission);
}

public boolean inGroup(final String name, final String group) {
String[] groups = getGroups(name);
for(String g : groups) {
if(g.equalsIgnoreCase(group)) {
return true;
}
}
return false;
}

public String[] getGroups(final String name) {
UUID playerUUID = getPlayerUUID(name);
PermissionsGroup group = api.getUser(playerUUID).getGroup();
String[] groups = new String[group.getInheritanceGroups().length + 1];
groups[0] = group.getName();
for(int i = 0; i < group.getInheritanceGroups().length; i++) {
groups[i + 1] = group.getInheritanceGroups()[i].getName();
}
return groups;
}

public static class PluginAccessException extends Exception {
private static final long serialVersionUID = 7044832912491608706L;
}

public static class MissingPluginException extends Exception {
private static final long serialVersionUID = 7044832912491608706L;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public PermissionsResolverManager(Plugin plugin, String name, Logger logger) {
}
public void findResolver() {
if (tryPluginPermissionsResolver()) return;
if (tryJPermsPermissions()) return;
if (tryNijiPermissions()) return;
if (tryPermissionsEx()) return;
if (tryDinnerPerms()) return;
Expand All @@ -96,6 +97,16 @@ private boolean tryNijiPermissions() {
return false;
}
}

private boolean tryJPermsPermissions() {
try {
perms = new JPermsPermissionsResolver(server);
logger.info(name + ": Permissions plugin detected! Using Permissions plugin for permissions.");
return true;
} catch (Throwable e) {
return false;
}
}

private boolean tryPermissionsEx() {
try {
Expand Down

0 comments on commit 3075692

Please sign in to comment.