Tired of flipping between your code and permissions/config options?

Discussion in 'Resources' started by mdcollins05, Jan 18, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    mdcollins05

    Well, I've got some code for you!

    Here's what it does:

    • Allows you to add permissions/configuration options easily with only an additional line per option
    • Allows you to use your IDE's auto completion for permissions and config options
    Here's the classes:
    PermNodes.java (open)

    PHP:
    public enum PermNodes {
        
    ADMIN("uws.admin.all"PermissionDefault.FALSE),
        
    ADMIN_CREATE("uws.admin.create"PermissionDefault.FALSE"uws.admin.all"true);
     
        private 
    String node;
        private 
    PermissionDefault defValue;
        private 
    String parentNode;
        private 
    Boolean childEnabled;
     
        private 
    PermNodes(String nodePermissionDefault defValue) {
            
    this.node node;
            
    this.defValue defValue;
        }
     
        private 
    PermNodes(String nodePermissionDefault defValueString parentNodeBoolean childEnabled) {
            
    this.node node;
            
    this.defValue defValue;
            
    this.parentNode parentNode;
            
    this.childEnabled childEnabled;
        }
     
        public 
    String getNode() {
            return 
    this.node;
        }
     
        public 
    PermissionDefault getDefault() {
            return 
    this.defValue;
        }
     
        public 
    String getParent() {
            return 
    this.parentNode;
        }
     
        public 
    Boolean isChildEnabled() {
            return 
    this.childEnabled;
        }

    Perms.java (open)

    PHP:
    public class Perms {
     
        
    YourPluginName plugin null;
     
        public 
    Perms(YourPluginName plugin) {
            
    this.plugin plugin;
        }
     
        public 
    void setPermissions() {
            
    PluginManager pm this.plugin.getServer().getPluginManager();
            for (
    PermNodes node PermNodes.values()) {
                
    Permission perm = new Permission(node.getNode());
                
    perm.setDefault(node.getDefault());
                if (
    node.getParent() != null) {
                    
    perm.addParent(node.getParent(), node.isChildEnabled());
                }
                
    pm.addPermission(perm);
            }
        }
     
        public 
    boolean hasServerPerm(CommandSender csboolean allowConsoleString perm) {
            if (
    cs instanceof Player) {
                if (
    cs.hasPermission(perm)) {
                    return 
    true;
                }
            } else {
                if (
    allowConsole) {
                    return 
    true;
                } else {
                    return 
    false;
                }
            }
            return 
    false;
        }
    }

    ConfigOptions.java (open)

    PHP:
    public enum ConfigOptions {
        
    ENABLE_TP_CMD("enable.tp.command"false),
        
    ENABLE_HOME_CMD("enable.home.command"false),
        
    ENABLE_WARP_CMD("enable.warp.command"false),
        
    IM_A_STRING("testing.string""Default value!"),
        
    INTEGER_THIS("testing.int"123);
       
        private 
    String node;
        private 
    Object defValue;
       
        private 
    ConfigOptions(String nodeObject defaultValue) {
            
    this.node node;
            
    this.defValue defaultValue;
        }
       
        public 
    String getNode() {
            return 
    this.node;
        }
       
        public 
    Object getDefault() {
            return 
    this.defValue;
        }
    }

    Config.java (open)

    PHP:
    public class Config {
     
        
    YourPluginName plugin null;
        public 
    String seperator ".";
     
        public 
    Config(YourPluginName plugin) {
            
    this.plugin plugin;
        }
     
        public 
    void loadConfiguration() {
            for (
    ConfigOptions c ConfigOptions.values()) {
                
    this.plugin.getConfig().addDefault(c.getNode(), c.getDefault());
            }
     
            
    plugin.getConfig().options().copyDefaults(true);
            
    //Save the config whenever you manipulate it
            
    plugin.saveConfig();
        }
     
        public 
    Object getOption(String node) {
            return 
    this.plugin.getConfig().get(node);
        }
    }


    How it works:

    The PermNodes and ConfigOptions files are ENUMs (just like the familiar Material ENUM) and that's where you define your options and their default values. The ConfigOptions should support many different types with the exception of lists and things like that. (Any suggestions to fix this are appreciated!)

    How to use:

    In your main class, you'll want to add something similar to:

    PHP:
    public Config config = new Config(this);
    public 
    Perms perms = new Perms(this);
    And, in your onEnable() you can do the following.

    PHP:
    this.config.loadConfiguration();
    this.perms.setPermissions();
    After doing so, you can get the value of, say the "testing.string" option by doing:
    PHP:
    String myString this.getConfig().getString(ConfigOptions.IM_A_STRING.getNode());
    Or, for a permission:
    PHP:
    if (player.hasPermission(PermNodes.ADMIN.getNode())) {
    ...
    }

    The code in this post is released under the WTFPL:
    License (open)

    DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
    Version 2, December 2004

    Copyright (C) 2004 Sam Hocevar <[email protected]>

    Everyone is permitted to copy and distribute verbatim or modified
    copies of this license document, and changing it is allowed as long
    as the name is changed.

    DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

    0. You just DO WHAT THE FUCK YOU WANT TO.


    Let me know if you have any questions and I'll try to answer 'em! Hope someone can get some use out of these, I know I will.

    Edit: Fixed some example code.
     
  2. Offline

    RyanTheLeach

    does this prevent the permissions needing to be defined in the plugin.yml at all?

    some example usage to show why this is good, would make this thread better.
     
Thread Status:
Not open for further replies.

Share This Page