[Loop] Loop for ItemStack?

Discussion in 'Spigot Plugin Development' started by relampagorojo93, Jun 25, 2015.

  1. I'm trying to do a loop to generate items with a specific slot in an inventory. The problem is that I want that the items get the information from files generated in a folder with different names. I don't know how to do a loop with some files and with her specific information. For example, in the folder are two warps, one called Survival and the other called Minigames, and I want that the ItemStack gets her information without mix all the information.
     
  2. Hex

    Hex
    IRC Staff

    Please restate your goal in clearer terms. I can't tell what you are trying to do with files and how it relates to itemstacks.
     
  3. I want to create a folder that will have all the warps generated. They will have the name, the slot, the id, the data value and other things of the warp. I created a menu that will have all warps generated in it. But I want to use a loop to get all the information of each of the warps. If a warp have slot 1, id 2 and the name Survival, in the menu, will be a warp in slot 1, with a grass block and a DisplayName called Survival. But I don't know how to generate this loop getting all the information from all the files, and every warp will get her information, without having a warp with a DisplayName that equals as the first warp and a id that equals as the second warp.
     
  4. You can get an array containing all the File s in a folder with File#listFiles (). If you call that on your folder you can iterate through the returned areay handling each File as warp data. Hope this helps a bit :)
     
  5. I used it to do that:

    public void openMenu(Player player){
    File warp = new File("plugins/FunnyWarps/Warps");
    File[] paths;
    paths = warp.listFiles();
    for (File warps : paths) {

    The problem is that I'm new and only I need one more thing. With that, if I'm not going wrong, I will get all the files of the directory Warps, but, with something can I read the Strings and Ints from all the files, for example, to replace the id and data-value of an item?
     
  6. You can read them as Configuration s. Take a look at bukkits FileConfiguration. Each file would be its own config and you can just work with each one as you normally would with the regular config.
     
  7. Another thing. I tryed something but it doesn't work for one thing. I want to try to create multiple ItemStacks, I wanted concretly to create about 54 slots. If it will take a lot of bytes, I will remove some slots. I could create the item, but only loads one item. But I only put one ItemStack.
     
  8. I did this code:

    public void openMenu(Player player){
    File warp = new File("plugins/FunnyWarps/Warps");
    File[] paths;
    paths = warp.listFiles();
    for (File warps : paths) {
    YamlConfiguration w = YamlConfiguration.loadConfiguration(warps);
    if (w.getInt("Slot") <= 9) Warps = Bukkit.createInventory(null, 9, "Warps");
    if (w.getInt("Slot") > 9) Warps = Bukkit.createInventory(null, 18, "Warps");
    if (w.getInt("Slot") > 18) Warps = Bukkit.createInventory(null, 27, "Warps");
    if (w.getInt("Slot") > 27) Warps = Bukkit.createInventory(null, 36, "Warps");
    if (w.getInt("Slot") > 36) Warps = Bukkit.createInventory(null, 45, "Warps");
    if (w.getInt("Slot") > 45) Warps = Bukkit.createInventory(null, 54, "Warps");
    saveDefaultConfig();
    loadYamls();
    int d = w.getInt(i);
    int v = w.getInt(a);
    String name = w.getString("Name");
    @SuppressWarnings("deprecation")
    ItemStack W = new ItemStack(d, 1, (short) v);
    ItemMeta WMeta = W.getItemMeta();
    WMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
    WMeta.setLore(getConfig().getStringList("Lore"));
    W.setItemMeta(WMeta);
    if (w.getInt("Slot") == 1) {
    Warps.setItem(0, W);
    }
    if (w.getInt("Slot") == 2) {
    Warps.setItem(1, W);
    }
    }

    I have two files, but only one is assigned, the warp with slot 2. The multipleFiles only use the last file. Example and Survival, it selects Survival.
     
  9. You are reinitializing everything in each iteration of the loop. Also use code tags to post code.

    Edit: I found out you are cheating with me on another thread. We are done here.
     
  10. Because the question in this post officialy was for do a loop with the Item Stack, but it can be only one item per ItemStack, and the other post is for how to import files and so a check to use in every method the specified file.