2015-06-21 35 views
-3

我需要寫一張支票。它的本質是:應該返回true或者false,如果玩家擁有一個存在於配置庫中的東西(只有他),否則返回false。如何從config.yml加載id列表?

+0

在你存儲什麼樣的格式在您的配置文件中的項目?我們可以看到一些代碼保存/加載的內容和一個示例配置文件? –

+0

可能的重複[如何從配置中加載播放器清單的ID列表? \ [Bukkit \]](http://stackoverflow.com/questions/30965375/how-to-load-id-list-from-config-for-player-inventory-bukkit) –

回答

0

因此,這裏是你的問題的解決方案:

@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; 
} 

而且配置應顯示這樣的:

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

是的,但你需要對庫存中的其他物品的處理被忽略,並且該檢查僅在witch config項目上完成。 – GodmanT

+0

如果玩家有其他物品!=物品巫婆配置然後返回false。 – GodmanT

+0

我edidet我的答案 –