Solved Getting Items from a config

Discussion in 'Plugin Development' started by Lilret123, Feb 21, 2015.

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

    Lilret123

    Hey everyone,

    (I'm creating this plugin for a server network)

    The way im doing this is i have a command "/browser", that allows you to add items to a server browser
    (compass). when you do the command it writes the necessary data to a config file then when you use the browser (compass), it reads the items from the file and displays them in the inventory.

    This is how the items are stored in the config: {Slot}, {Type}, {Name}, {Lore}

    [​IMG]

    This is how im setting the data in the config
    Code:
        if(args.length == 4){
                        String arg0 = args[0];
                        String slot = args[1];
                        Material item = p.getItemInHand().getType();
                        String type = item.name().toLowerCase();
                        String name = args[2];
                        String lore = args[3];
    
                        if(arg0.equalsIgnoreCase("add")){
                            if(!(item == Material.AIR)){
                                if(isNumeric(slot)){
                                    int num = Integer.parseInt(slot);
                                    String path = "." + num;
    
                                    browser.set("Items." + ".Slot" + path, "");
                                    browser.set("Items." + ".Slot" + path + ".Type", type);
                                    browser.set("Items." + ".Slot" + path + ".Name", name);
                                    browser.set("Items." + ".Slot" + path + ".Lore", lore);
                                    browser.saveConfig();
    
                                    p.sendMessage(type + " With the name " + name + " has been added to slot " + num);
                                }
    This is a debug that i used to make sure that getting the values would work: It just outputs the keys in the config (inventoryslots), and the item data associated with it (Type, Name, Lore)

    Code:
        @EventHandler
        public void onCom(PlayerInteractEvent e){
            Player p = e.getPlayer();
            Action a = e.getAction();
           
            if(p.getItemInHand().getType() == Material.COMPASS){
                if(a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK || a == Action.LEFT_CLICK_AIR || a == Action.LEFT_CLICK_BLOCK){
                    e.setCancelled(true);
                    for(String key : browser.getConfigurationSection("Items.Slot").getKeys(false)){
                        p.sendMessage("Slot: " + key);
                        p.sendMessage("Type: " + Material.getMaterial(browser.getString("Items.Slot." + key + ".Type").toUpperCase()));
                        p.sendMessage("Name: " + browser.getString("Items.Slot." + key + ".Name"));
                        p.sendMessage("Lore: " + browser.getString("Items.Slot." + key + ".Lore"));
                    }
                   
                    //serversadd();
                    //p.openInventory(ServerNav.compass);
                }
            }
        }
    The omitted part is what doesnt work, its a method im using to actually get the keys and put the items in the inventory

    This is the method:

    Code:
    public static void serversadd(){
            if(!(browser.get("Items.Slot") == null)){
    
                for(String key : browser.getConfigurationSection("Items.Slot").getKeys(false)){
                    Material mat = Material.getMaterial(browser.getString("Items.Slot." + key + ".Type").toUpperCase());
                    Inventory nav = ServerNav.compass;
                    int slot = browser.getInt(key);
                    String name = browser.getString("Items.Slot." + key + ".Name");
                    String lore = browser.getString("Items.Slot." + key + ".Lore");
                    ServerNav.createServer(mat, nav, slot, name, lore);
                }
            }
        }
    When debugging it outputs all the blocks like this:
    [​IMG]

    but when i try the method it only inserts the leaves into the inventory :

    [​IMG]

    I should also mention that there is no error in console

    Thank you for whatever help you can offer

    ,Lilret123
     
  2. Offline

    Qwertyness_

    Its because you are saving the slot number as a String object in the config, so browser.getInt() always returns 0. You need to use Integer.parseInt() on the section name you retrieve from browser.getString(key).
     
  3. Offline

    Lilret123

    omg, genius
    Thank you do much
     
  4. Offline

    MCMatters

  5. Offline

    Lilret123

    True, thkx
     
Thread Status:
Not open for further replies.

Share This Page