addItem doesn't work

Discussion in 'Plugin Development' started by Ninjasturm, Apr 13, 2011.

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

    Ninjasturm

    Hello everbody,

    why does this line of code not work:
    Code:
    this.Plugin.getServer().getPlayer(split[0]).getInventory().addItem(new ItemStack(Integer.parseInt(split[1]), Integer.parseInt(split[2])));
     
  2. Offline

    Relentless_x

    Code:
            ItemStack itemstack = new ItemStack(Id);
            itemstack.setAmount(Amount);
            player.getInventory().addItem(itemstack);
    thats what i do to give items
     
  3. Offline

    Ninjasturm

    It works but i need to relog to see changes. How can i reload the inventory with the bukkit API?
     
  4. Offline

    Plague

    The relog problem was always only temporary for me. And I add those items the same way.
     
  5. Offline

    Relentless_x

    you could try player.loadData()
     
  6. Offline

    Ninjasturm

    Doesn't work. How is it managed by the give Command?
     
  7. Offline

    Relentless_x

    you could force them to issue the command /give blah blah blah by using player.performCommand() or player.chat()
     
  8. Offline

    Ninjasturm

    Hmm now im not sure if the give command does that what i want.
    I want to add a item to a players inventory. This works but only if i relog.
     
  9. Offline

    Relentless_x

    if its a private mod install commandbook and make the use issue the command /item but the way i first said should work :confused:
     
  10. Offline

    Edward Hand

    What's wrong with using:
    Code:
    thePlayer.updateInventory();
    after adding the item?
     
  11. Offline

    Binaryclock

    There could be so many things wrong with that code, you wouldn't have a clue. Try this:

    Code:
    if ( split.length < 3 || split[0] == null || split[1]  == null || split[2] == null )
    {
      System.out.println("one argument was null");
      return;
    }
    
    String strPlayerName = split[0];
    String strItem = split[1];
    String strItemAmount = split[2];
    
    Player player;
    if ( (player = this.Plugin.getServer().getPlayer(strPlayerName)) == null )
    {
      System.out.println("player object was null (player not found: " + strPlayerName + ")");
      return;
    }
    
    int item = -1;
    int amount = 1;
    Material m;
    
    try
    {
      item = Integer.parseInt(  strItem );
      m = Material.getMaterial( item );
    }
    catch ( Exception e )
    {
      m = Material.getMaterial( strItem.toUpperCase() );
    }
    finally 
    {
      if ( m == null )
      {
        System.out.println( "Invalid item type!" );
        return;
      }
    }
    
    try
    {
      amount = Integer.parseInt( strItemAmount ); 
    }
    catch ( Exception e )
    {
      System.out.println( "Invalid amount!" );
      return;
    }
    
    PlayerInventory pi = player.getInventory();
    ItemStack is = new ItemStack( m, amount );
    pi.addItem( is );
    
    

    There may be errors, I just typed this in quickly in the forum editor, but this is what you need to do.

    Upon further consideration, you may be using an old version of bukkit in the 640 or 650s. As I remember correctly, there were some messed up versions of bukkit that would not add a new ItemStack to a player inventory. EvilSeph corrected this in the recommended build.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
Thread Status:
Not open for further replies.

Share This Page