Inventory select

Discussion in 'Spigot Plugin Development' started by Ziomkeee, Jun 6, 2018.

Thread Status:
Not open for further replies.
  1. Hi, I have a code that complements the GUI depending on what the previous player chooses. The problem is that if player A chooses something other than player B then the one with open GUI changes items. Please help :)

    Code (Java):
        public static Inventory nextGui = Bukkit.createInventory(null, cfg.getLast().getInt("ALL.size"),
                cfg.getConfig().getString("GUI.name").replace("&", "§"));

        @Deprecated
        public static void setNextGUI(ConfigurationSection config) {
            ItemStack items = new ItemStack(Material.getMaterial(config.getInt("item")), config.getInt("amount"),
                    (byte) config.getInt("data"));
            ItemMeta meta = items.getItemMeta();
            meta.setDisplayName(config.getString("name").replace("&", "§"));
            List<String> desc = config.getStringList("lore");
            ArrayList<String> lore = new ArrayList<String>();
            for(String d : desc) {
                String color = d.replace("&", "§");
                lore.add(color);
            }
            meta.setLore(lore);
            if(config.getBoolean("enchant") == true) {
                meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1, true);
                meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                items.setItemMeta(meta);
            }
            items.setItemMeta(meta);
            nextGui.setItem(config.getInt("slot"), items);
            selectBack.setBack();
        }

        @Deprecated
        public static void setSelectNext(Player p, String select) {
            nextGui.clear();
            for (String configList : cfg.getLast().getConfigurationSection( "ListGUI." + select + ".items").getKeys(false)) {
                ConfigurationSection config = cfg.getLast().getConfigurationSection("ListGUI." + select + ".items." + configList);
                setNextGUI(config);
            }
            p.openInventory(nextGui);
        }
     
  2. Have you tried caching inventories per player?
    Make a map with UUID and Inventory and if player chooses some option just put player's unique id to the map with the new inventory and then check for this inventory instead of a default one.
     
  3. Ehh .. Could you show somehow what it would look like? Because I do not understand too much.
     
  4. Code (Text):

    // Default inventory.
    private final Inventory defaultInventory = /*some default inventory*/;
    // Cache where you can store players' inventories.
    private final Map<UUID, Inventory> inventories = new WeakHashMap<>(16);
     
    Then in the setNextGUI method you want to create a new inventory based on the default one and then use:
    Code (Text):

    this.inventories.put(player.getUniqueId(), inventory);
     
    then you can get current player's inventory by using:
    Code (Text):

    this.inventories.getOrDefault(player.getUniqueId(), this.defaultInventory);
     
    If the map doesn't find the key with the given unique id the default inventory will be returned instead.
     
  5. Thanks, but it only loads the last item in EQ. How to solve it?
     
  6. This doesn't save item but the whole inventory, so you are probably creating new inventory instance with new item instead of new item AND old items from the previous inventory.
     
  7. Code (Java):
        @Deprecated
        public static void getNextGUI() {
            for(String keys1 : cfg.getNext().getConfigurationSection("ListGUI").getKeys(false)) {
                ConfigurationSection config1 = cfg.getNext().getConfigurationSection("ListGUI." + keys1);
                for(String keys : config1.getConfigurationSection("items").getKeys(false)) {
                    ConfigurationSection config = config1.getConfigurationSection("items." + keys);

                    nextGUI = Bukkit.createInventory(null, cfg.getNext().getInt("GUI.size"),
                            cfg.getNext().getString("GUI.name").replace("&", "§"));

                    ItemStack item = new ItemStack(Material.getMaterial(config.getInt("item")), config.getInt("amount"),
                            (byte) config.getInt("data"));
                    ItemMeta itemMeta = item.getItemMeta();
                    itemMeta.setDisplayName(config.getString("name").replace("&", "§"));
                    List<String> desc = config.getStringList("lore");
                    ArrayList<String> lore = new ArrayList<>();
                    for(String c:desc) {
                        String color = c.replace("&", "§");
                        lore.add(color);
                    }
                    itemMeta.setLore(lore);
                    if(config.getBoolean("enchant") == true) {
                        itemMeta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL,1,true);
                        itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
                        item.setItemMeta(itemMeta);
                    }
                    item.setItemMeta(itemMeta);
                    nextGUI.setItem(config.getInt("slot"), item);
                }
                invSelect.put(keys1, nextGUI);
                System.out.println(invSelect.get(keys1)); //info
                System.out.println(keys1); //info
            }
        }
    In that case, what do I have to change here to read everything from the selected section in the config and not just the last item?

    Code (YAML):
    ListGUI:
      regulamin1
    :
        items
    :
          1
    :
            item
    : 166
            data
    : 0
            amount
    : 1
            name
    : "&c&lTest"
            lore
    :
             - '&7test'
              - '&7333!'
            slot
    : 0
            enchant
    : false
            actions
    :
              open
    :
             send:
     
Thread Status:
Not open for further replies.