Solved Adding 0 number of items in inventory somehow

Discussion in 'Plugin Development' started by TheDiamond06, Mar 1, 2015.

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

    TheDiamond06

    So here is my code upon breaking a glowstone block with a wooden pickaxe
    Code:java
    1.  
    2. if(b.getType() == Material.GLOWSTONE)
    3. {
    4. Random rand = new Random();
    5.  
    6. int amount = rand.nextInt(3);
    7.  
    8. ItemStack one = new ItemStack(Material.GLOWSTONE_DUST);
    9.  
    10. b.setType(Material.AIR);
    11.  
    12. int a = one.getAmount();
    13. p.getInventory().remove(one);
    14. ItemStack two = new ItemStack(Material.GLOWSTONE_DUST, a + amount + 1);
    15. p.getInventory().addItem(two);
    16. p.updateInventory();
    17. }
    18.  

    Now I had this problem before, before I added the + 1 in ItemStack two. It still sometimes thinks it is giving me a zero amount of glowstone dust in my inventory. Is there another way I can fix this. When it gives me 0 glowstone dust it just appears in my inventory but not registered as a real item.
     
  2. Offline

    Zombie_Striker

    When you say it gives you zero amount, do you mean that it actually gives you zero amount(Not giving you the items) or do you mean that a number doesn't show (the item is one). The +1 should be directly added to amount.
     
  3. Offline

    TheMintyMate

    @TheDiamond06

    My advise is to use something like this instead:
    Code:
    package -------;
    public class RandInt {
        public RandInt(){}
        public int randIntInRange(int min, int max) {
            int z = min + (int)(Math.random() * ((max - min) + 1));
           return z;
        }
    }
    
    This class can then be referenced like this:
    Code:
    int randNum = new RandInt().randIntInRange(1,3); /* Will either be 1, 2 or 3 */
    
    ItemStack item = new ItemStack(Material.GLOWSTONE_DUST, randNum);
    player.getInventory().addItem(item);
     player.updateInventory();
    
    Also, why are you creating ItemStack "one", to then just remove it?

    Hope this helped,
    - Minty
     
  4. Offline

    TheDiamond06

    @TheMintyMate Thanks, that fixed it.

    I am creating ItemStack one because if they already have glowstone dust in their inventory, int a will get the amount. After that I remove one so they don't have anymore glowstone dust in their inventory. After that I make ItemStack two with glowstone dust that has the int a (previous amount of glowstone they had) + the random amount. If I don't do this and the random int is different it will create a new slot to put it in to.
     
    TheMintyMate likes this.
Thread Status:
Not open for further replies.

Share This Page