Getting multiple items from config

Discussion in 'Spigot Plugin Development' started by Multimap, Apr 30, 2017.

  1. I'm wondering how to check if a players inventory contains multiple items.

    Here is my config:

    Code (Text):
      1:
        name: "Miner Quest"
        item: DIAMOND_PICKAXE
        lore:
        - "&7To complete this quest you must"
        - "&7obtain the following items:"
        - "&a8x Iron ore"
        - "&a32x Stone"
        items:
        - IRON_ORE,8
        - STONE,32
        reward-commands:
        - "eco give {player} 1000"
    I've worked out how to get the item and the amount but I dont know how to check if they are both in a players inventory. Any help is appreciated.
     
  2. 1. Get inventory
    2. Check if inventory contains
    new ItemStack(Material.block, amount)
    && the other blocks
    3. Execute ur code?
     
  3. I don't think that would work because of renamed items. What I think would work is iterating through the players inventory and checking that they have the minimum amount of what is required as well as confirming that the block type is the same.
     
  4. As in two for statements?
     
  5. yes, something like this:
    Code (Java):
    boolean correct = true;
    for(itemarr : getConfig("1.items")){
        int req = itemarr[1];
        String matString = itemarr[0];
        Material mat = Material.getMaterial(matString);
        int num = 0;
        for(stack : player.getInventory()){
            if (stack.getType()==mat){
                num+=stack.getAmount();
            }
        }
        if(num<req){
            correct = false;
            break;
        }
    }
     
  6. If anyone else is wondering how I did this, heres how.
    Code (Text):

        protected ArrayList<ItemStack> items = new ArrayList<ItemStack>();

        public void rewardPlayer(Player player, String reward) {

            int complete = 0;

            items.clear();

            for (int i = 0; i < dr.getConfig().getStringList("quests." + reward + ".items")
                    .size(); i++) {

                for (ItemStack size : player.getInventory().getContents()) {

                    String[] params = dr.getConfig().getStringList("quests." + reward + ".items").get(i)
                            .split(",");

                    Material mat = Material.valueOf(params[0]);

                    int amount = Integer.parseInt(params[1]);

                    if (size != null && size.getType() != Material.AIR) {

                        if (size.getType() == mat) {

                            if (size.getAmount() >= amount) {

                                if (!items.contains(new ItemStack(mat, amount))) {

                                    items.add(new ItemStack(mat, amount));

                                    complete++;
                                }
                            }
                        }

                    }

                }

            }
     
    dr being the instance of my main class. splitting the parameters as its set as "IRON_ORE,32" in the config.

    then to give them there rewards I did:

    Code (Text):
            if (complete >= dr.getConfig().getStringList("quests." + reward + ".items").size()) {

                for (ItemStack item : items) {

                    player.getInventory().removeItem(item);

                }

                for (String s : dr.getConfig().getStringList("quests." + reward + ".reward-message")) {
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', s.replace("{quest}",
                            dr.getConfig().getString("quests." + reward + ".name"))));
                }
                for (String s : dr.getConfig().getStringList("quests." + reward + ".reward-commands")) {
                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                            s.replace("{player}", player.getName()));
                }
               
                // ADD PLAYER TO COOLDOWN
               
               
               
            } else {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&',
                        "&aTo complete the " + dr.getConfig().getString("quests." + reward + ".name")
                                + " quest you need: "));
                for (int i = 0; i < dr.getConfig().getStringList("quests." + reward + ".items")
                        .size(); i++) {

                    String[] params = dr.getConfig().getStringList("quests." + reward + ".items").get(i)
                            .split(",");

                    String mat = params[0];

                    int amount = Integer.parseInt(params[1]);

                    player.sendMessage(ChatColor.translateAlternateColorCodes('&',
                            "&8&l* &a" + amount + "x " + mat));

                }
            }