Inventory Iteration Fix... (Glitch, error, bug)

Discussion in 'Plugin Development' started by kev3200, Apr 28, 2015.

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

    kev3200

    Hi there, having trouble squashing this bug and can't figure out why.
    I have this code:
    makeItem() is a custom method I made that takes in information and returns an ItemStack with given information.
    My trouble is that when a player clicks in their inventory on an iron ingot, I receive the "Iron Bar" but the previous ingredients (1 iron ore) are not removed. It seems like the program is not iterating through all the inventory items. It stops at an empty space. Can anyone think of a solution to this?

    Code:
    public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            ItemStack clicked = e.getCurrentItem();
            Inventory inv = p.getInventory();
            World w = p.getWorld();
                   
             // If Iron Bar clicked
              if (clicked.equals(makeItem(Material.IRON_INGOT, "Iron",   "Smelt an iron bar "              + RED + "Required: " + WHITE + "1 Iron Ore, 1 Coal", 1))) {
                    if (inv.containsAtLeast(makeItem(Material.IRON_ORE, WHITE + "Iron Ore"), 1) && inv.containsAtLeast(makeItem(Material.COAL, WHITE + "Coal"), 1)) {
                        // Adding items
                        p.sendMessage(RED + "You smelted an " + GOLD + "Iron" + RED + " bar.");
                        inv.addItem(makeItem(Material.IRON_INGOT, WHITE + "Iron Bar", "A bar of iron", 1));
                        p.updateInventory();
                        w.playSound(p.getLocation(), Sound.FIZZ, 10, 1);
                        // Remove items
                        for (int i = 0; i <= inv.getSize(); i++) {
                            ItemStack is  = inv.getItem(i),
                                      rep = new ItemStack(is);
                            int am = is.getAmount();
                           
                            if (is != null) {
                                if (is == (makeItem(Material.IRON_ORE, WHITE + "Iron Ore", am))) {
                                    p.sendMessage("Inventory contains Iron Ore");
                                    if (am - 1 == 0) {
                                        inv.remove(is);
                                    }
                                    else if (am - 1 >= 1) {
                                       
                                        rep.setAmount(am -= 1);
                                        //inv.setItem(i, replace);
                                        p.updateInventory();
                                    }
                                }
                            }
                        }
                    }
                    else {
                        p.closeInventory();
                        p.sendMessage(GOLD + "You don't have enough materials to make that!\n" + RED + "Required: " + WHITE + "1 Iron Ore, 1 Coal");
                    }
     
  2. Offline

    mine-care

    You compare objects with ==?
     
  3. Offline

    kev3200

    I was using .equals() before but.. I don't remember why I switched :p. I'll try changing that and see if it helps.
     
  4. Offline

    kev3200

    Ok now using .equals() for object comparison... Problem still exists. If my inventory hotbar contains (iron bar, empty space, coal) only the iron bar will be removed and the coal will be skipped. It seems that the iteration stops at a null location for some reason...
     
  5. Offline

    HenkDeKipGaming

    I'm just learning to code, but isn't is possible to just do:
    p.getInventory()
    and then use Material.iron.remove to remove it or someting?
    i know this won't work, but maybe you get an idea from this?
    I don't know xD
     
  6. Offline

    kev3200

    So normally yes that would work, except that would remove the entire itemstack. I only want to remove 1 of the bars. It's still a good suggestion though. I'll tweak it and see what I come up with. Thanks and welcome to Bukkit coding! :D
     
  7. Offline

    HenkDeKipGaming

    thanks :) this community makes me happy :D i'm glad i started coding plugins :)
     
Thread Status:
Not open for further replies.

Share This Page