Match String to Inventory Name

Discussion in 'Spigot Plugin Development' started by Arman, Jun 6, 2015.

  1. Hello all,

    I would like to match a specific string to the name of an inventory, but I can't seem to find a way to do so.

    Code (Java):
                        for (String cmd : plugin.getConfig().getStringList(Integer.toString(fn) + ".commands")) {
                            if (cmd.toLowerCase().contains("opengui:")) {
                                String[] split = cmd.split(":");
                                p.closeInventory();
                                p.openInventory(somehow get inventory from split);
                            }
                            else {
                             // If the line is not an opengui line, i'd like to dispatch the command via console instead of opening an inventory.
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%player%", p.getName()));
    }
                    }
    So, as you can see, i'm checking if a line in the StringList contains opengui:, and if so, I would like to open an inventory with the name (any characters after : ).

    Thanks for any help!
     
  2. Get the index of the "opengui" by calling this:

    Code (Text):
    int i = 0;
    for(String s : split){
       if(s.toLowerCase().endsWith("opengui"))break;
       i++;
    }
    Then open your inventory:

    Code (Text):
    p.openInventory(split[i+1]);
     
    #2 Friwi, Jun 6, 2015
    Last edited: Jun 6, 2015
    • Agree Agree x 1
    • Useful Useful x 1
  3. Instead of contains, use startsWith. Then simply substring it.
     
    • Useful Useful x 1