How to load id list from config.yml?

1.6k Views Asked by At

I need to write a check. Its essence is: should return true or false, true if the player has a thing that in the inventory of the config (and only he) if it is not, then return false.

1

There are 1 best solutions below

6
On BEST ANSWER

So here is the solution for your problem:

@SuppressWarnings("deprecation")
public boolean hasInInv(Player p) {
    List<String> list = getConfig().getStringList("List");
    for(ItemStack i : p.getInventory().getContents()) {
        if(i != null) {
            boolean found = false;
            for(String st : list) {
                if(st.startsWith("" + i.getTypeId())) {
                    if(st.startsWith(i.getTypeId() + ":")) {
                        try {
                            byte data = Byte.parseByte(st.replaceFirst(i.getTypeId() + ":", ""));
                            if(data != i.getDurability()) {
                                continue;
                            } else {
                                found = true;
                                break;
                            }
                        } catch(Exception ex) {
                            System.out.println("Error in Config: " + st + " is not a valid Item");
                            return false;
                        }
                    }
                    found = true;
                    break;
                }
            }
            if(!found) {
                return false;
            }
        }
    }
    return true;
}

And the config should show like that:

List:
  - 1
  - 2
  - 3
  - 4
  - '17:3'
  - '17:1'