Solved Inventory trouble

Discussion in 'Plugin Development' started by timtower, Jun 4, 2013.

Thread Status:
Not open for further replies.
  1. Online

    timtower Administrator Administrator Moderator

    I am having trouble with adding items to an inventory, the items become unusable. When you click the items they disappear,
    This is my code:
    Code:java
    1. public void refillAll() {
    2. for(Integer chestint : config.getIntegerList("Chests")){
    3. String chestID = chestint+"";
    4. int x = config.getInt("Chest."+chestID+".x");
    5. int y = config.getInt("Chest."+chestID+".y");
    6. int z = config.getInt("Chest."+chestID+".z");
    7. String world = config.getString("Chest."+chestID+".world");
    8. Location loc = new Location(this.getServer().getWorld(world),0,0,0);
    9. loc.setX(x);
    10. loc.setY(y);
    11. loc.setZ(z);
    12. Block block = loc.getBlock();
    13. block.setType(Material.CHEST);
    14. Chest chest = (Chest) block.getState();
    15. Inventory inv = chest.getInventory();
    16. //List<Integer> items = getItems(chestID);
    17. String loot = config.getString("Chest."+chestID+".loot");
    18. if(loot==null){
    19. log("No items found for chest "+chestID);
    20. continue;
    21. }
    22. inv.clear();
    23. /*List<String> listruw = config.getStringList("Loot."+loot);
    24. //List<Integer> items = new ArrayList<Integer>();
    25. for(String item:listruw){
    26. int itemID;
    27. int aantal;
    28. itemID = Double.valueOf(item.split("%")[0]).intValue();
    29. aantal = Double.valueOf(item.split("%")[1]).intValue();
    30. int randomint = random.nextInt(100);
    31. log("Randomint = "+randomint+" Aantal = "+aantal);
    32. if(randomint<aantal){
    33. inv.addItem(new ItemStack(itemID,0,(short) 1));
    34. }
    35. }*/
    36. List<String> listruw = config.getStringList("Loot."+loot);
    37. List<Integer> items = new ArrayList<Integer>();
    38. for(String item:listruw){
    39. int itemID;
    40. int aantal;
    41. itemID = Double.valueOf(item.split("%")[0]).intValue();
    42. aantal = Double.valueOf(item.split("%")[1]).intValue();
    43. for(int i = 0;i<aantal;i++){
    44. items.add(itemID);
    45. }
    46.  
    47. }
    48. if(items.isEmpty()){
    49. continue;
    50. }
    51.  
    52. //log("Adding items");
    53. for(int i = 0;i<config.getInt("Chest."+chestID+".amount");i++){
    54. int nummer = random.nextInt(items.size());
    55. //log("Random: "+random.toString());
    56. if(nummer>=items.size()){
    57. continue;
    58. }
    59. ItemStack itemtoadd = new ItemStack(items.get(nummer),0,(short) 1);
    60. inv.addItem(itemtoadd);
    61. }
    62. chest.getInventory().setContents(inv.getContents());
    63. }
    64. }

    Anybody knows what is wrong?
     
  2. Offline

    bennie3211

    any errors? and what do you mean with unusable? Are there any items from your config in the chest? Or if you click it they disappear?
     
  3. Online

    timtower Administrator Administrator Moderator

  4. Offline

    imotionzz

    Nobody?
     
  5. Online

    timtower Administrator Administrator Moderator

  6. Offline

    catageek

    I think you should finish with chest.update(true) to update the block with the new type and data.
     
  7. Online

    timtower Administrator Administrator Moderator

    Will give it an try, 5 sec
    EDIT : Nope, not working

    catageek, Not working, items still disapear when I click them

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  8. Offline

    _Waffles_

    Code:
    ItemStack itemtoadd = new ItemStack(items.get(nummer),0,(short) 1);
    You're setting the size of the item stack to 0, when it should be 1 or more.
     
  9. Online

    timtower Administrator Administrator Moderator

    Damned, always those small stuff that are causing the big issues, ty dude ;)
     
Thread Status:
Not open for further replies.

Share This Page