Solved how to color the permission-message?

Discussion in 'Plugin Development' started by tom1000o, Oct 30, 2012.

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

    tom1000o

    how do you add a color to the message that displays when you don't have the permission for the command your using? i set it to "" in the plugin.yml and threw this in my code:
    Code:java
    1. if (sender.hasPermission("withernerf.reload") == false){
    2. sender.sendMessage(ChatColor.RED + "You don't have permission to use that command.");
    3. }

    any help? :p
     
  2. Offline

    CorrieKay

    if you wanted it to be red, you could just put
    permission-message: §cYou don't have permission to use that command.
    For each of the commands in your plugin.yml
     
  3. Offline

    tom1000o

    i get an invaid plugin.yml error when i do that :(
     
  4. Offline

    sensus12

    just use:
    Code:
    if(!sender.hasPermissions("your.pex"){
    sender.sendMessage(ChatColor.RED + "xd");
    }
     
  5. Offline

    CorrieKay

    Oh, whoops. My bad, you cant include that character apparently.

    Try this

    for(String cmd : getConfig().getConfigurationSection("commands").getKeys(false)){
    getCommand(cmd).setPermissionMessage(ChatColor.RED+"You don't have permission to use that command");
    }

    Put that in onEnable()

    edit:
    That wont work, as the code wont even reach there if they dont have the permission.

    Oh gosh, i made a horrible mistake, im sorry, not the config, the plugin.yml. Hold on, and watch this post, ill edit it.

    Code:
    //gets the plugin.yml loaded as a configuration file, then iterates through all of the registered commands.
    FileConfiguration config = YamlConfiguration.loadConfiguration(getResource("plugin.yml"));
    for(String cmd : config.getConfigurationSection("commands").getKeys(false)){
      getCommand(cmd).setPermissionMessage(ChatColor.RED+"You dont't have permission to use that command");
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  6. Offline

    tom1000o

    wait a minute.... could you use this to make an extra yml file just used for storing data?? :eek: and thanks so much, ill go and see if it works :)
    edit : works perfectly :)
     
  7. Offline

    CorrieKay

    Yes, you can make different yml files. My server currently houses over five thousand five hundred yml, and thats just MY plugin alone :p

    Were you interested in learning how to manage different yml files too?
     
    ZeusAllMighty11 likes this.
  8. Offline

    tom1000o

    im very interested in that :D that would go great with a plugin im trying to make
     
  9. Offline

    ZeusAllMighty11 Retired Staff


    I amm
     
    tom1000o likes this.
  10. Offline

    CorrieKay

    Alright, well good news, its not hard at all :p

    Basics
    There are three basic methods used in creating new yml configuration files.

    First, your creating method.
    This will create a brand new empty configuration file at the specified location, returning the configuration object for you to utilize.
    Code:
    public FileConfiguration createConfig(String name, String subdirectory){
      File dir = new File(plugin.getDataFolder()+File.separator+subdirectory);
      //this is optional. Only needed if you want to put it into a subdirectory. If not, and you just wanna put your file in the plugin data folder, no need to add "+File.separator+subdirectory" to the constructor parameter. You can remove the second parameter even. If you wanna get fancy, check for null, or overload the method.
      if(!dir.isDirectory()){
        dir.mkdirs(); //creates the directory folders if they dont exist.
      }
      File file = new File(dir,name+".yml"); //initializes the file object
      if(!file.exists()){
        file.createNewFile(); //creates a new file if it doesnt exist
      }
      return YamlConfiguration.loadConfiguration(file);//returns the newly created configuration object.
    }
    
    Next, your setting method.
    This will save the file.
    Code:
    public void saveConfig(FileConfiguration config, String name, String subdirectory){
      //gets directory
      File dir = new File(plugin.getDataFolder()+File.separator+subdirectory);
      if(!dir.isDirectory()){
        dir.mkdirs();
      }
      //gets file
      File file = new File(dir,name+".yml");
      //saves to file
      config.save(file);
    }
    
    Lastly, you have the getter methods, for already existing config files.
    Code:
    public FileConfiguration getConfig(String name, String subdirectory){
      File dir = new File(plugin.getDataFolder()+File.separator+subdirectory);
      if(!dir.isDirectory()){
        return null; //File directory doesnt exist, file cant exist. Return null.
      }
      File file = new File(dir,name+".yml");
      if(!file.exists()){
        return null; //File doesnt exist, Return null.
      }
      return YamlConfiguration.loadConfiguration(file); //file found, load into config and return it.
    }
    
    Advanced
    Now, here are some fancy things you can do with this.

    Load a default config from resources
    Lets say you want a generated configuration system, and you dont want to have to run the code to generate all of the keys and values every time you want to generate it. This is where you put a "default configuration file" into the resources of your plugin. Create a new file, of the yml filetype (with whatever name you want) next to your plugin.yml. Then, do this!
    Code:
    public FileConfiguration createGeneratedConfiguration(String name, String subdirectory){
      File dir = new File(plugin.getDataFolder()+File.separator+subdirectory);
      if(!dir.isDirectory()){
        dir.mkdirs();
      }
      File file = new File(dir,name+".yml");
      if(!file.exists()){
        file.createNewFile();
      }
      //Standard stuff up till here. Now lets load that resource, which we will call "DefaultConfiguration.yml"
      InputStream is = plugin.getResource("DefaultConfiguration.yml");
      //okay, so we now have the input stream (basically the data) of the default configuration. time to load it into the config object.
      FileConfiguration config = YamlConfiguration.loadConfiguration(is);
      //Thats it! you can load an input stream directly into the configuration getter static method just like that!
      return config;
      //Now you have the config, you can save it!
    }
     
    
    Player config files
    Lets say you want to work player configuration files per player, just like mine and many other plugins do! One strategy that i have, is that i store them in a specific, unchanging folder. ("Players" as a subdirectory of the data folder for my plugin) Since this never changes, and that we specifically only need to work with player configurations (theyre always the same type of config) this allows us to cut a few corners.
    (here is the class i will be referencing)

    Right off the bat, you'll notice that the class is abstract. This is because it is completly static, and shouldnt be instantiated. Because of that, and that the players folder is unchanging, we hold the directory file object in the class field static/finally.

    The getter method is generally the same thing as i wrote up above, However the save method is self sufficient, given the config file. Every configuration file has the players name inside, a string under the key "name". I use this to know exactly which file to save it to.

    The create method is slightly different. As the only time a players configuration file will ever be generated is the first time they join, i use this method to set their "first join" time, their name, and their starting nickname (which is their name. heh). Now that the file has a player name, it is completly independant, and can be saved using the saveConfig method. I save it, and then return the config.

    Any questions?
     
    KyleParks, mydeblob, SumrioL and 4 others like this.
  11. Offline

    devilquak

    PHP:
    if(!player.hasPermission("some.permission")
        {
            
    player.sendMessage("You don't have permission!");
        }
    else
        {
            
    player.sendMessage("You have permission!");
        }
    No?
     
  12. Offline

    CorrieKay

    Nope.

    If the command has a permission, its checked at the door. It doesnt get to the onCommand method.

    The only way to do it this way, is to not give your commands permissions, and then manually check every single command for permission before executing. Much easier to give them a permission via the yml, and modify the permission denial message.
     
  13. Offline

    devilquak

    Is all you're saying is that this shouldn't be used because it's better to register permissions in the plugin.yml if it's a command?
     
  14. Offline

    CorrieKay

    Something like that.

    What im saying is that if you do it this way, you have to include that code for every single command check, instead of letting bukkit do it for you.

    In fact, doing it the way i showed up top, was four lines, covering an infinite number of commands, while checking manually is three lines PER command.

    While technically possible, less efficient codewise. pre, and post compile.
     
  15. Offline

    devilquak

    Oh, yes, efficiency-wise, your method probably would be better. I was just thinking about how I use it, for when I need most of the messages to be different and because I mostly use this in event functions. I was just unsure if you were saying it wouldn't work, because much earlier on you had said that just checking if the player doesn't have permissions wouldn't be able to run code if the player did turn out to have the permission. :p
     
  16. Offline

    tom1000o

    CorrieKay

    im going to buy a brain swapping machine, and steal your brain, so i can be awesome and know more about java than the people who made it (lol XD) i bookmarked this page so i can look back to your post, its extremely helpful! :)
     
Thread Status:
Not open for further replies.

Share This Page