Solved Not giving the player items

Discussion in 'Plugin Development' started by jimuskin, Dec 20, 2013.

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

    jimuskin

    Hello bukkit,
    I seem to be having a little bit of trouble giving a player an item. Half of the code executes but the other half doesn't. I have no errors in the console.

    Here is the code I use:
    (In my playerDeathListener. Before you ask, yes. I have registered the listener.)
    Code:java
    1. if(this.plugin.playerManager.killstreak.get(killerName) == 3) { //Hashmap for killstreaks
    2. this.plugin.getServer().broadcastMessage(ChatColor.BLUE + killerName + ChatColor.GRAY + " is on fire!"); // This executes
    3. killer.setFireTicks(100); // So does this
    4. killer.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 100, 0), true); //So does this
    5.  
    6. ItemStack stick1 = new ItemStack(Material.STICK, 1); // None of this executes.
    7. ItemMeta stick1Meta = stick1.getItemMeta();
    8. stick1Meta.setDisplayName(ChatColor.GREEN + "Jump Boost");
    9. stick1.setItemMeta(stick1Meta);
    10. stick1.setAmount(3);
    11.  
    12. player.getInventory().setItem(6, stick1);
    13.  
    14. }
     
  2. Offline

    NoLiver92

    which half executes and which half doesnt?
     
  3. Offline

    jimuskin

    NoLiver92 read what is inside the //
    lines 2 - 4 execute but nothing else
     
  4. ItemStack stick = new ItemStack(Material.STICK);{
    ItemMeta stickmeta = stick.getItemMeta();
    ArrayList<String> stickim = new ArrayList<String>();
    stickmeta.setDisplayName(ChatColor.GREEN + "Jump Boost");
    stickmeta.setLore(stickim);
    stick.setItemMeta(stickmeta);
    }
     
  5. Offline

    jimuskin

    KevyPorter Thanks, ill give it a try...

    KevyPorter

    Still doesn't seem to work. Unfortunately, I cannot post any console errors as there are none. Here is the code:

    Code:java
    1. ItemStack stick = new ItemStack(Material.STICK);
    2. ItemMeta stickmeta = stick.getItemMeta();
    3. ArrayList<String> stickim = new ArrayList<String>();
    4. stickmeta.setDisplayName(ChatColor.GREEN + "Jump Boost");
    5. stickmeta.setLore(stickim);
    6. stick.setItemMeta(stickmeta);
    7.  
    8. player.getInventory().setItem(6, stick);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  6. KevyPorter
    Why did you create an empty ArrayList, then set that as the item lore? He doesn't even have a lore in his code, which makes me think he doesn't want one.

    jimuskin
    Add debug lines in your code, and see where it stops working. Also, the setAmount() method isn't necessary, you can define the amount within creation of the ItemStack.
     
  7. Offline

    jimuskin

    Assist The debug shows no errors in the code. It apparently seems to work all alright...

    Console:
    HTML:
    [Server] INFO success!!!
    [Server] INFO Set the itemMeta to the item. Now giving it to the player
    [Server] INFO Item's displayname loaded. Now setting the itemMeta to the item
    [Server] INFO ItemMeta loaded. Now setting the display name
    [Server] INFO Stick loaded. Loading the itemMeta
    [Server] INFO Creating the item "stick". Making 3 of the item.
    [Server] INFO Giving the player a fire boost
    EDIT: This is in reverse...

    Debug code:

    Code:java
    1. System.out.println("Giving the player a fire boost");
    2. killer.setFireTicks(100);
    3. killer.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 100, 0), true);
    4. System.out.println("Creating the item \"stick\". Making 3 of the item.");
    5. ItemStack stick = new ItemStack(Material.STICK, 3);
    6. System.out.println("Stick loaded. Loading the itemMeta");
    7. ItemMeta stickmeta = stick.getItemMeta();
    8. System.out.println("ItemMeta loaded. Now setting the display name");
    9. stickmeta.setDisplayName(ChatColor.GREEN + "Jump Boost");
    10. System.out.println("Item's displayname loaded. Now setting the itemMeta to the item");
    11. stick.setItemMeta(stickmeta);
    12. System.out.println("Set the itemMeta to the item. Now giving it to the player");
    13. player.getInventory().setItem(6, stick);
    14. System.out.println("success!!!");
     
  8. jimuskin
    That's interesting.. Maybe the setItem() method doesn't update the player inventory? Try using addItem() instead (just to test), and if it works, switch back to setItem(), and use player.updateInventory() after that.
     
  9. Offline

    jimuskin

    Assist Hmm... I tried using addItem aswell, but the item was not added to my inventory.
     
  10. jimuskin
    What version of Bukkit are you using? If 1.7, then that may be the source of the problem.
     
  11. Offline

    jimuskin

  12. jimuskin
    Well the only thing I can think of anymore is that you're probably giving the stick to a wrong player. Is it supposed to be given to the killer? Right now you're giving it to some 'player'.
     
    jimuskin likes this.
  13. Offline

    jimuskin

    Assist Well, I'm a huge dope....

    I was giving the stick to the wrong player like you had said. This isn't the first time I stare at a piece of code for like an hour thinking what the issue is when it is right there :p

    Thanks for your help! +1

    solved.
     
    Assist likes this.
Thread Status:
Not open for further replies.

Share This Page