Help with coding a MySQL Permissions Plugin

Discussion in 'Spigot Plugin Development' started by KristyPvP, Dec 30, 2019.

  1. Hey,

    I am trying to code a MySQL permissions plugin, however I am not sure how from a config side Permissions plugin to a MySQL side permissions plugin.

    I have already set up ranks, add permissions to the rank and a way to add players to the rank on MySQL, but i'm not sure how to make it search the database for the permission and then execute it. This is my code for the config version for doing permissions
    Code (Text):
    public class Permissions {
       
        static Main plugin = Main.getPlugin(Main.class);
        public static HashMap<UUID,PermissionAttachment> pPermissions = new HashMap<>();
       
        @EventHandler
        public static void permsSetup(Player p) {
            PermissionAttachment attachment = p.addAttachment(plugin);
            pPermissions.put(p.getUniqueId(), attachment);
            permsSet(p.getUniqueId());
        }
       
        public static void permsSet(UUID uuid) {
            PermissionAttachment attachment = pPermissions.get(uuid);
           
            for (String ranks : plugin.getConfig().getConfigurationSection("Ranks").getKeys(false)) {
                for (String perms : plugin.getConfig().getStringList("Ranks." + ranks + ".permissions")) {
                    attachment.setPermission(perms, true);
                }
            }
        }

    }
    I have done all the basic stuff (Commands, Set up the mysql database etc), I just need to know how to make it a mysql side Permissions plugin.

    Any help would be much appreciated!
     
  2. Another thread with this question, I will say what I said the previous time.
    It's your plugin! We can't say what you need to make the plugin support MySQL. A plugin that supports MySQL is a plugin that has the capabilty to save it's data into a MySQL database instead of a flat file (or any other database)
    If you want code examples, there are plenty of them on the internet.
     
  3. I've tried looking up examples online, I've got a bit of code but it doesn't work. It doesn't show any errors or nothing. My code is:
    Code (Text):
    public class Permissions {
       
        static Main plugin = Main.getPlugin(Main.class);
        public static HashMap<UUID,PermissionAttachment> pPermissions = new HashMap<>();
        public static int rankid;
       
        @EventHandler
        public static void permsSetup(Player p) {
            PermissionAttachment attachment = p.addAttachment(plugin);
            pPermissions.put(p.getUniqueId(), attachment);
            permsSet(p.getUniqueId());
        }
       
        public static void rankInformation(Player p) {
            try {
                PreparedStatement statement = plugin.getConnection()
                        .prepareStatement("SELECT * FROM `rank-users` WHERE userUUID=?");
                statement.setString(1, p.getUniqueId().toString());
                ResultSet results = statement.executeQuery();
                while (results.next()) {
                    rankid = results.getInt("rankid");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
       
        public static void permsSet(UUID uuid) {
            PermissionAttachment attachment = pPermissions.get(uuid);
            rankInformation(null);
            try {
                PreparedStatement statement = plugin.getConnection()
                        .prepareStatement("SELECT permission FROM `rank-permissions` WHERE rankid=?");
                statement.setInt(1, rankid);
                ResultSet results = statement.executeQuery();
                while (results.next()) {
                    String perm = results.getString("permission");
                    attachment.setPermission(perm, true);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
     
    • Create an interface for the backend with two implementations: One for YAML, the other one for MySQL.
    • Do your queries asynchronously.
    • Close your resources (e.g. PreparedStatement).