Solved get value from config through command

Discussion in 'Plugin Development' started by Ezrab_, Mar 10, 2018.

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

    Ezrab_

    Hi guys,
    So what I want is the following:

    I have a config.yml file with
    -------------------------
    kits:
    slayer:
    knight:
    -------------------------

    I have a command: /kitpvp create <kitname>
    Which creates a section in kits.

    Now I want to add stuff to the created <kitname> so what I'd like the command to have is

    /kitpvp add <kitname> <stuff for in the future>

    Now here's the question how do I access the created kit value through a command?

    I thought I had a solution but that doesn't work.. Here's my code:

    Code:
    package nl.ezrab.kitpvp.cmds;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import nl.ezrab.kitpvp.Main;
    import nl.ezrab.kitpvp.listeners.InteractListener;
    
    public class MainCommand implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("kitpvp")) {
                    if (args.length == 0) {
                        p.sendMessage(Main.msg + "Commands for /kitpvp");
                        p.sendMessage(Main.msg + "/kitpvp open");
                        p.sendMessage(Main.msg + "/kitpvp create <kit of your choise>");
                        p.sendMessage(Main.msg + "/kitpvp add <created kit>");
                        p.sendMessage(Main.msg + "/kitpvp reload");
                        return true;
                    }
                    if (args[0].equalsIgnoreCase("open")) {
                        if (args.length == 1) {
                            InteractListener.openGUI(p);
                            return true;
                        }
                    } else if (args[0].equalsIgnoreCase("create")) {
                        if (args.length == 1) {
                            p.sendMessage(
                                    Main.msg + ChatColor.RESET + ChatColor.RED + "You have to specify more arguments.");
                            return true;
                        }
                        String kits = "";
                        kits += args[1];
    
                        Main.plugin.getConfig().createSection("kits." + kits);
                        Main.plugin.getConfig().set("kits." + kits, "");
                        Main.plugin.saveConfig();
                        p.sendMessage(ChatColor.GREEN + "New kit: " + kits + " created!");
                        // TODO add stuff to created kits.
    
                      // HERE'S THE ADD COMMAND!!!
                    } else if (args[0].equalsIgnoreCase("add")) {
                        if (args.length == 1) {
                            p.sendMessage(
                                    Main.msg + ChatColor.RESET + ChatColor.RED + "You have to specify more arguments.");
                            return true;
                        }
                        // TODO add command config.
                        if (args[1].equalsIgnoreCase(Main.plugin.getConfig().getConfigurationSection(""))) {
                            p.sendMessage("works");
                        } else {
                            p.sendMessage("doens't work");
                        }
                    } else if (args[0].equalsIgnoreCase("reload")) {
                        p.sendMessage(Main.msg + "config reloaded.");
                        Main.plugin.reloadConfig();
                    } else {
                        p.sendMessage(Main.msg + ChatColor.RESET + ChatColor.RED + "That command doesn't exist.");
                    }
                }
            } else {
                sender.sendMessage("Only players can do this command.");
            }
            return true;
        }
    }
     
  2. Offline

    CommonSenze

    @Ezrab_
    What you are doing is not needed.

    You know the first thing on your config string line will be "kits". (ie. to access the slayer kit you would do "kits.slayer")

    What I recommend you do is to:
    1. go check if args.length >= 3
    2. Check if the kit exist with this check:
    Code:java
    1.  
    2. if (Main.plugin.getConfig().contains("kits."+args[1])){
    3.  

    3. make the user enter a number for args[2] representing the inventory number they want to set the item they are holding (The item they will be holding the while time during the command to sent that item to the slot specified)
    4. Create a Configuration section with the inventory number and in the section put the material, amount, and durability.
    Example:
    Code:java
    1.  
    2. Main.plugin.getConfig().set("kits."+args[1] + "."+slotNumber+ ".Material", player.getItemInHand().getType().toString())
    3. Main.plugin.getConfig().set("kits."+args[1] + "."+slotNumber+ ".Amount", player.getItemInHand().getAmount())
    4. Main.plugin.getConfig().set("kits."+args[1] + "."+slotNumber+ ".Durability", player.getItemInHand().getDurability())
    5.  


    I hope this was informative and helpful. If you have any questions feel free to ask. I also provided a png of the inventory slots
     

    Attached Files:

    Ezrab_ likes this.
  3. Offline

    Ezrab_

    You're the real mvp. <3

    Could you explain this further?
    3. make the user enter a number for args[2] representing the inventory number they want to set the item they are holding (The item they will be holding the while time during the command to sent that item to the slot specified)

    Because if I understand correctly, I'd make it like this
    Code:java
    1.  
    2. if (args.length >= 3) {
    3. if (Main.plugin.getConfig().contains("kits." + args[1])) {
    4. if (args[2].equalsIgnoreCase(slotNumber)) {
    5. }
    6. }
    7. }
    8.  


    So what do I do after the args[2] check? Hope you understand my question ;)
     
    Last edited: Mar 12, 2018
  4. Offline

    CommonSenze

    Sure thing.

    args[2] is going to be an integer. So basically you're going to convert that String input to an Integer and then make sure it's a valid number to use. I hate to spoon feed you code but its the best way to show you what I mean.

    Firstly, I made a slight error, you can just use:

    Code:java
    1.  
    2. if (args.length == 3) {
    3.  

    Instead of:
    Code:java
    1.  
    2. if (args.length >= 3) {
    3.  

    Since you only need 3 arguments, nothing more.

    Now here's how you would do this:
    Code:java
    1.  
    2. if (args.length == 3) {
    3. if (Main.plugin.getConfig().contains("kits." + args[1])) {
    4. int slotNumber = 0;
    5. try {
    6. slotNumber = Integer.parseInt(args[2]);
    7. } catch (Exception e){
    8. player.sendMessage(args[2] + " is not a valid number");
    9. return true;
    10. }
    11. if (slotNumber > 35 || slotNumber < 0){
    12. player.sendMessage("Inventory Number must be inbetween 0 and 35");
    13. return true;
    14. }
    15. Main.plugin.getConfig().set("kits."+args[1] + "."+slotNumber+ ".Material", player.getItemInHand().getType().toString());
    16. Main.plugin.getConfig().set("kits."+args[1] + "."+slotNumber+ ".Amount", player.getItemInHand().getAmount());
    17. Main.plugin.getConfig().set("kits."+args[1] + "."+slotNumber+ ".Durability", player.getItemInHand().getDurability());
    18. }
    19. }
    20.  

    Now let me explain this code so you can learn from this.

    1. I started by creating the integer that will be called slotNumber and equaling it to 0.

    2. I then proceed to parse (convert) the string of the slot number to an integer/number in the try/catch blocks you see above. When args[2] is not a number and can not be converted, an Exception is triggered and it tells the player that the argument provided isn't correct. If it is a number, I convert it to an Integer and make slotNumber equal that.

    3. Now that I have the number the player wants to put in, I check to see if the number provided is a valid inventory slot (As I discussed before in my previous post, it has to between 0 and 35). If it is out of that range, it tells the player the number has to be between, or equal to, those 2 numbers

    4. Finally getting all the checks out of the way and knowing that:
    (A) The argument the player put in is a valid number.
    AND
    (B) The number provided is within the range of the Inventory slots.

    I proceed to finish it up by saving the players item that he's holding in his hand by saving the 3 most important things in an ItemStack:
    (A) The Material
    (B) The Amount
    AND
    (C) The Durability

    EDIT:
    I for got to add in. Make sure you save the file after you finish setting everything. I figure you know how to do that since you are messing with configs, but for those of the people that don't know.
    Code:java
    1. Main.plugin.saveConfig();


    I hope this helped you further understand what I was trying to explain. I apologize if it was a little lengthy.
     
    Last edited: Mar 12, 2018
    Ezrab_ likes this.
  5. Offline

    Ezrab_

    Thank you very much!
     
Thread Status:
Not open for further replies.

Share This Page