Adding items to player inventory not working

Discussion in 'Spigot Plugin Development' started by DyrhoThomas, May 28, 2017.

  1. Hello,
    I'm working on a kitpvp plugin, and ran into a problem with this code:
    Code (Text):
                       ItemStack shop = new ItemStack(Material.EMERALD);
                        ItemMeta shopMeta = shop.getItemMeta();
                        shopMeta.setDisplayName("§c§lKit Shop");

                        ItemStack stats = new ItemStack(Material.ENDER_CHEST);
                        ItemMeta statsMeta = stats.getItemMeta();

                        ItemStack compass = new ItemStack(Material.COMPASS);
                        ItemMeta compassMeta = compass.getItemMeta();
                        compassMeta.setDisplayName("§c§lKit selector");

                        for(Player p : toernooiManager.getCurrentSpectators()) {
                            GameTypeManager.setGameTypeHashMap(p, GameTypeManager.GameType.SPAWN);



                            p.getInventory().setItem(2, shop);
                            p.getInventory().setItem(6, stats);
                            p.getInventory().setItem(4, compass);


    in this case toernooiManager.getCurrentSpectators() returns a List<Player>
    I'm looping this list, clearing their inventory, and adding the 3 items, the players get them, but when they right-click, they won't fire the PlayerInteractEvent, and sometimes the items will disappear... (for the last one in the list, it looks like it works)
    I've tried the Player#updateInventory() bit but it doesn't work...
    Hope you can help me, if you need more information or so from me, just ask it!

    Thomas Kuijs
    #1 DyrhoThomas, May 28, 2017
    Last edited: May 28, 2017
  2. As you mentioned, your "PlayerInteractEvent" is not beeing fired, could you provide that code too? as its not listed above in the code
    Also i wouldn't recommend to use "p.updateInventory();" as its a deprecated method
  3. You are setting the ArmorContents to null, and I'm pretty sure a Material can't be null.
  4. Here is my PlayerInteractEvent, you get the same items on join, then they do work, so that's the weird thing, it isn't in the interactevent...
    Code (Text):
        public void onInteract(PlayerInteractEvent e) {
            if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
            if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                if(e.getItem().getType().equals(Material.COMPASS)) {
                    if(e.getItem().getItemMeta().hasDisplayName()) {
                        if(e.getItem().getItemMeta().getDisplayName().equalsIgnoreCase("§c§lKit selector")) {
                }if(e.getItem().getType().equals(Material.ENDER_CHEST)) {
                    if(e.getItem().getItemMeta().hasDisplayName()) {
                        if (e.getItem().getItemMeta().getDisplayName().equalsIgnoreCase("§c§lStatistics")) {
                }if(e.getItem().getType().equals(Material.EMERALD)) {
                    if(e.getItem().getItemMeta().hasDisplayName()) {
                        if (e.getItem().getItemMeta().getDisplayName().equalsIgnoreCase("§c§lKit Shop")) {
                            e.getPlayer().sendMessage(Chat.formatMessage("&cDit komt bij de volgende update!"));

    Sorry for the dutch messages in my sendMessage... It's for a Dutch server :)

    And what should I use instead of p.updateInventory(); as it isn't deprecated in my spigot 1.8.8... Is that deprecated in a later version or...?
  5. What should I use than to remove all of the armor? Like this it is removing the armor... And p.getInventory().clear() isn't removing the armor as far as I know..
  6. Well, try p#getInventory()#setArmorContents(Materia#AIR) OR you can do p#getInventory()#setHelmet(Material#AIR) along with the 3 other types of armor.
  7. Tried this, still not working at all...
  8. YOu must be doing something wrong in your code then, since this very well should work. Show us some more of your code
  9. It's the exact same piece of code as in my joinevent, except for that it isn't looping a List<Player> there... there everything works fine...