Solved Trouble finding amount of a certain item on the ground

Discussion in 'Bukkit Help' started by ereilly89, Jun 15, 2017.

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

    ereilly89

    Code:
    public double getDiamondOnGround(){
            diamonds = 0;
            List<org.bukkit.entity.Entity> entities = world.getEntities();
           
            for(int i=0;i<entities.size();i++){
                if(entities.get(i).getName().equals("item.item.diamond")){
                      diamonds++;
                }
            }
            return diamonds;
    }
    My problem is that this method only finds one diamond when there are more than one dropped. For instance, if I drop 3 diamonds on the ground, this method only returns 1. I need a way to find the stack on the ground. Any help would be greatly appreciated, thanks.
     
  2. Offline

    Zombie_Striker

    @ereilly89
    You're not accounting for itemstack amounts. If the entity is an instanceof Item, add the itemstack's amount instead of just increasing by one.

    Also, it will never be a double. How can you have a fraction of a diamond? Change the double to ints.
     
  3. Offline

    ereilly89

    Thanks for the quick response. Could you go more into detail of how I am supposed to add the itemstack? I am fairly new to plugin development.
    I am using my diamonds variable as a double due to the method being a part of an equation that yields a double amount. It is easier to have it initially as so.
     
  4. Offline

    Zombie_Striker

    @ereilly89
    1. If the entity is an instanceof Item, cast it to that.
    2. Use Item#getItemstack() to get the itemstack of the Item.
    3. Use Itemstack#getAmount() to return the amount of diamonds in the itemstack. Add this to the diamonds.
     
  5. Offline

    ereilly89

    Thanks! It works great!
     
  6. Offline

    Zombie_Striker

    @ereilly89
    If your problem has been solved, mark this thread as solved.
     
  7. Offline

    ereilly89

Thread Status:
Not open for further replies.

Share This Page