Can't add items to player inventory [SOLVED]

Discussion in 'Plugin Development' started by SenojLuap, Oct 3, 2011.

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

    SenojLuap

    I can't figure out what's wrong with this. It builds correctly, and when I test it out in game, it prints the 'bukkit' message correctly. However, it doesn't add the item.
    Code:
      public void onBlockPlace(BlockPlaceEvent bpe) {
        PlayerInventory inv = bpe.getPlayer().getInventory();
        ItemStack bucket = new ItemStack(Material.ENDER_PEARL, 1);
        HashMap<Integer, ItemStack> leftovers = inv.addItem(bucket);
        bpe.getPlayer().sendMessage(ChatColor.DARK_PURPLE +
                "Haz you seen mah bukkit?");
        if(!leftovers.isEmpty())
          bpe.getPlayer().sendMessage(ChatColor.DARK_RED + "Oh noes!");
      }
    
     
  2. Offline

    Pasukaru

    add this:
    Code:
            bpe.getPlayer().updateInventory();
    
    It's deprecated for quite some time now, but I don't know any other way.
     
  3. Offline

    SenojLuap

    I swear I'd tried that before and it didn't work. But for the sake of eliminating possibilities I tried again and it worked. Very weird. But in running a few tests, I came to another "problem" (not exactly a problem).
    When I try to add a bucket to the players inventory using
    Code:
    new ItemStack(Material.BUCKET);
    some very funny stuff happens in game. But if I use
    Code:
    new ItemStack(Material.BUCKET, 1);
    it works just fine. Is this a known problem; is this expected behavior?
     
  4. Offline

    Acrobot

    @SenojLuap
    Giving no amount means both 0 and infinite - it causes "funny stuff".
    Always remember to check if the quantity is greater than 0, or bad things happen.
     
  5. Offline

    SenojLuap

    Aight. That makes sense.
    Getting back to the updateInventory() thing: they show it as being deprecated, and yet it seems to be the only way to get this simple command to work. Does anyone know if they plan on making it so that addItem() will force it's own update? Subquestion, if it's deprecated, is there some other method I should be calling instead?
     
  6. Offline

    RPGWorld

    @SenojLuap
    @Pasukaru
    player.updateInventory() is deprecated because it's a minecraft bug...it doesn't refresh inventories automatically :D
    So they created this,and it's a hacky way of doing it,they're searching for a better way :]
     
  7. Offline

    Pasukaru

    @RPGWorld
    Thanks for clarification, I didn't know that. :)
     
Thread Status:
Not open for further replies.

Share This Page