Reload Command for Plugin Only

Discussion in 'Plugin Development' started by mig4ng, Nov 16, 2012.

Thread Status:
Not open for further replies.
  1. Hi, i'm the developer of MoreThanFish, and i'm having a problem creating a command that when it's executed will reload the config.yml file.
    Can anyone help me with this?
     
  2. Offline

    Drkmaster83

    Which aspect? The command? The reloading of the config?
     
  3. It's very simple, i need a command like /mtf reload that reloads the MoreThanFish config.yml file. Just this.
    I don't know where to start, because this plugin is my first one, and i've never worked on java before.
    At this time my onEnable is like this:
    Code:
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info("[" + pdfFile.getName() + "] " + "Plugin Enabled. " + " Version " + pdfFile.getVersion());
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.pl, this);
            try
            {
                Metrics metrics = new Metrics(this);
                metrics.start();
                File file = new File("plugins/MoreThanFish/config.yml");
                FileConfiguration cfg = getConfig();
                if(!file.exists())
                {
                    this.logger.info("[" + pdfFile.getName() + "] " + "Config file not found, creating one for you.");
                    cfg.options().copyDefaults(true);
                    saveConfig();
                }
            }
            catch(Exception e){ }
        }
    And my config.yml:
    Code:
    options:
      rarecatches: 3
    '1':
      percentage: 2.5
      itemid: DIAMOND
      message: '&eYou''ve caught a &bdiamond&e, Lucky You!'
    '2':
      percentage: 2.5
      itemid: EMERALD
      message: '&eYou''ve caught a &aEmerald&e!'
    '3':
      percentage: 12.3
      itemid: LEATHER_BOOTS
      message: '&eBah... You''ve caught some old &cBoots&e, probably it was from a fisherman.'
     
  4. Offline

    Drkmaster83

    Well, for future help, you can contact me on Skype (Drkmaster83). But for now, Let's get to the command:

    So, how to create a command, exactly? Well, you always start with a public boolean onCommand method. Use the parameters "(CommandSender sender, Command cmd, String commandLabel, String[] args)."

    So far:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
     
    }
    After you have done this, we need to specify to which command this method will respond. To do this, use an 'if' statement to check if the 'commandLabel' (the part after a player uses /) is equal to your command (Oh, let's say, 'MTF' for example). We want to make sure that it doesn't matter about the case of the command, whether it be 'mTf' or 'MTF' or 'MtF'. So, to do this, we use the 'equalsIgnoreCase' check.

    Here's that explanation above put into context:
    Code:
    if(commandLabel.equalsIgnoreCase("MTF"){
     
    }
    After checking to see which command this onCommand method will respond to, we need to make sure that it will only reload if the player types /mtf reload. One thing you should know: Anything after the commandLabel (the word/acronym after the /) with a space between itself and the commandLabel is called an argument. To check if an argument is present at the time of the command, you have to formulate an 'if' statement checking for a specific argument. Now, you may be asking: How do I check for arguments? Well, it'd be simplest to say this: The count starts at 0. So, if you had 1 argument, its name would be args[0]. If you had two arguments, the name of the second argument would be args[1], and so on. So, if our first argument is, Oh, let's say, 'reload', then we're going to reload the config.yml file using the reloadConfig() method.

    Again, the explanation above put into context:
    Code:
    if(args[0].equalsIgnoreCase("reload"){
        reloadConfig();
    }
    So, the whole onCommand method should look like this:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        if(commandLabel.equalsIgnoreCase("MTF"){
            if(args[0].equalsIgnoreCase("reload"){
                reloadConfig();
            }
        }
    }
    Normally, we'd cast the 'sender' parameter to a Player object straightaway. However, we need to ask ourselves one important question: Are we going to use Player-oriented methods or are we just going to use CommandSender-oriented methods? If we are going to use Player-oriented methods, we need to check if the sender is an instanceof a Player object. If it is, then we cast the 'sender' parameter to a Player object. If we are going to use CommandSender-oriented methods, we don't have to perform a check at all. In the next example, we'll pretend that we'll only be using Player-oriented methods as a safety precaution. We're going to perform an if-statement checking to see if the sender is an instanceof a Player object, and if they aren't, we're going to send the 'sender' a message saying that they cannot use this command. You would likely want to put this right after your commandLabel check, so here's that put into context:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        if(commandLabel.equalsIgnoreCase("MTF"){
            if(sender instanceof Player){
                if(args[0].equalsIgnoreCase("reload"){
                    reloadConfig();
                }
            }
            else{
                sender.sendMessage("You are not able to perform this command, due to your not being a player.");
            }
        }
    }
    The 'else' statement is to do whatever is defined if the 'if' statement is NOT TRUE.

    Now, I know what you're thinking. How would the player know the config has actually been reloaded? Well, we'd have to send them a message. To do so, we'd have to identify the sender as a Player. To do this, we must cast Player to a sender.

    How to cast Player to 'sender':
    Code:
    Player <name of variable, could be 'player', so I will make it that> = (Player) sender;
    You're making the sender a Player in essence. Note: The angle braces (<>) are not meant to be used, they're there to show you what you're supposed to insert.

    So, you can put this after your check for if the sender is a Player.
    Code:
    if(sender instanceof Player){
        Player player = (Player) sender;
    }
    Now that we've confirmed the sender is a Player, we can send them a message when the config is reloaded... which would be under the reloadConfig() method.
    Code:
    reloadConfig();
    player.sendMessage("The config.yml file has been reloaded.");
    
    Remember THIS, though: It's important that any action you do to the player, that you're using the variable name.

    Our final code (I guess):
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        if(commandLabel.equalsIgnoreCase("MTF"){
            if(sender instanceof Player){
                Player player = (Player) sender;
                if(args[0].equalsIgnoreCase("reload"){
                    reloadConfig();
                    player.sendMessage("The config.yml file has been reloaded.");
                }
            }
            else{
            sender.sendMessage("You are not able to perform this command, due to your not being a player.");
            }
        }
        return false;
    }
    So, that's the completed command. However, there's one thing I haven't covered: Permissions. If you'd like to use the thread-unsafe method for checking if either the 'sender' or the 'player' has a certain permission, you use the player/sender.hasPermission(String permission) which will return true if they have the permission mentioned in the method's parameters. If not, you can create your own hasPermission method in your class.
     
    Benlewis9000 and blackwolf12333 like this.
  5. Offline

    CeramicTitan

    Code:JAVA
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. if(cmd.getName().equalsIgnoreCase("reload")){
    4. reloadConfig();
    5. sender.sendMessage(ChatColor.DARK_GREEN + "Config Reloaded");
    6. }
    7. }
     
  6. Offline

    Drkmaster83

    First of all, he learns nothing by you just giving him the code. Secondly, you didn't even give him the code he wanted.
     
    Benlewis9000 likes this.
  7. actually i don't copy and paste
    xD i just wanted to know the methods, thanks both ;)
    Basicaly i just needed the reloadConfig();
     
  8. Offline

    CeramicTitan

    Hey buddy, Firstly you don't know what type of learner he is, he could learn by seeing things. So in that case pasting code would be the right thing todo.
    Secondly, I quote, "I'm having a problem creating a command that when it's executed will reload the config.yml file." So I think code I posted was correct. If you disagree please tell me how I am wrong.

    I believe it should look something like this:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        if(commandLabel.equalsIgnoreCase("MTF"){
           if(args.length == 1){
            if(args[0].equalsIgnoreCase("reload"){
                reloadConfig();
            }
        }
    }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  9. actually it is, but i said before mtf reload, but that i can do by myself... i just needed the reloadConfig();

    I'm basicaly rewritting the plugin for better performance, add permissions, and more funcionalities.
     
  10. Offline

    Drkmaster83

    That it should, CeramicTitan. I left that part out because I assumed the sender wouldn't be doing anymore arguments than 1.
     
  11. Offline

    CeramicTitan

    meh, fair enough
     
Thread Status:
Not open for further replies.

Share This Page