Skip to content

Add support for other permissions plugins

Guilherme Penedo edited this page Jul 6, 2016 · 6 revisions

Introduction

The following are needed to add support for a permissions plugin (if you do add support, please PR your changes). To illustrate the procedure I'll use PermissionsEx as an example. You need to choose a short word to represent the permissions system. In this case I've chosen "PEX".

1. Implementing the PermissionsSystem interface

Create a class under the permsystems package named after the permissions plugin, in this case, PermissionsEx and let it implement PermissionSystem. Implement PermissionSystem's methods. For an example, here's my class for PermissionsEx:

public class PermissionsEx implements PermissionSystem {

    @Override
    public boolean requiresMySQL() {
        return true;
    }

    @Override
    public List<String> getGroups() throws SQLException {
        Connection c = BungeePexBridge.getDB().getConnection();
        ResultSet res = c.createStatement().executeQuery("SELECT DISTINCT(name) as name FROM `" + BungeePexBridge.getConfig().pex_tables_permissions + "` WHERE name NOT LIKE 'system' AND name NOT LIKE '%-%' AND type='0'");
        return BungeePexBridge.getDB().resultSetToList(res, "name");
    }

    @Override
    public List<String> getGroupPermissions(String group) {
        List<String> perms = new ArrayList<String>();
        Connection c = BungeePexBridge.getDB().getConnection();
        try {
            ResultSet res = c.createStatement().executeQuery("SELECT * FROM `" + BungeePexBridge.getConfig().pex_tables_permissions + "` WHERE name = '" + group + "'");

            while (res.next()) {
                if (Arrays.asList("rank", "prefix", "default").contains(res.getString("permission")))
                    continue;
                perms.add(res.getString("permission"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return perms;
    }

    @Override
    public List<String> getInheritance(String group) throws SQLException {
        Connection c = BungeePexBridge.getDB().getConnection();
        try {
            ResultSet res = c.createStatement().executeQuery("SELECT parent FROM `" + BungeePexBridge.getConfig().pex_tables_permissionsInheritance + "` WHERE child = '" + group + "' AND type='0'");
            return BungeePexBridge.getDB().resultSetToList(res, "parent");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<String>();
    }

    @Override
    public long getRank(String group) {
        Connection c = BungeePexBridge.getDB().getConnection();
        try {
            ResultSet res = c.createStatement().executeQuery("SELECT value FROM `" + BungeePexBridge.getConfig().pex_tables_permissions + "` WHERE name = '" + group + "' AND permission = 'rank'");
            if (res.next())
                return Long.parseLong(res.getString("value"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<String> getPlayerPermissions(ProxiedPlayer player) throws SQLException {
        Connection c = BungeePexBridge.getDB().getConnection();
        List<String> permissions = new ArrayList<String>();
        try {
            ResultSet res = c.createStatement().executeQuery("SELECT * FROM `" + BungeePexBridge.getConfig().pex_tables_permissions + "` WHERE name = '" + player.getUniqueId().toString() + "'");
            while (res.next()) {
                if (Arrays.asList("rank", "prefix", "name").contains(res.getString("permission")))
                    continue;
                permissions.add(res.getString("permission"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return permissions;
    }

    @Override
    public List<String> getPlayerGroups(ProxiedPlayer player) throws SQLException {
        Connection c = BungeePexBridge.getDB().getConnection();
        List<String> groups = new ArrayList<String>();
        ResultSet res = c.createStatement().executeQuery("SELECT parent FROM `" + BungeePexBridge.getConfig().pex_tables_permissionsInheritance + "` WHERE child ='" + player.getUniqueId().toString() + "' AND type='1'");
        while (res.next())
            groups.add(res.getString("parent"));
        return groups;
    }

    @Override
    public String getDefaultGroup() {
        Connection c = BungeePexBridge.getDB().getConnection();
        try {
            ResultSet res = c.createStatement().executeQuery("SELECT name FROM `" + BungeePexBridge.getConfig().pex_tables_permissions + "` WHERE permission = 'default' AND value = 'true'");
            if (res.next())
                return res.getString("name");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Please note that you can get a MySQL Connection using

BungeePexBridge.getDB().getConnection()

Any settings particular to your permissions system need to be accessed through the main Config instance using

BungeePexBridge.getConfig()

See below for info on adding your own config options.

2. Main plugin class

Assuming the class you created on 1. is called PermissionsPlugin, and the short word you chose earlier is "PEPLUGIN", you'll need to add the following if clause to the loadPermissionsSystem() method on the main plugin class.

private PermissionSystem loadPermissionsSystem() {
        if (config.permissionsSystem.equals("PEX"))
            return new PermissionsEx();
        else if (config.permissionsSystem.equals("SEXYPEX"))
            return new SexyPex();
        else if (config.permissionsSystem.equals("PEPLUGIN"))
            return new PermissionsPlugin();
        else return null;
    }

3. Configuration file

Firstly, you should add a comment to the config.yml file under "# Supported systems:" like "# SHORTWORD - YourPermissionsPlugin" so users know your permissionsplugin is available Like so:

# Choose your permissions system. (ALL UPPERCASE AS ON THE LEFT LIST)
# Supported systems:
# PEX - PermissionsEx
# SEXYPEX - SexyPex
# SHORTWORD - YourPermissionsPlugin

permissionsSystem: PEX

Settings

Optionally add settings specific to that permissions plugin on the config.yml file. Add a section to the end of the file named after your permissions plugin and add your settings there. Here's an example for PEX:

# PermissionsEx settings
pex:
  tables:
    # PEX MySQL table names
    # If you didn't change the default values on pex's config for 'aliases' leave as it is
    permissions: permissions
    permissionsInheritance: permissions_inheritance

If you added any settings, you'll need to add them to helpers/Config.java like so:

//PEX
public String pex_tables_permissions = "permissions";
public String pex_tables_permissionsInheritance = "permissions_inheritance";

Here, the variable name needs to match the settings path. _ are replaced with . to load settings. This means that the path of "pex_tables_permissions" is "pex: tables: permissions:". The value assigned to the variable is the default value.