Solved Can't update inventory.

Discussion in 'Plugin Help/Development/Requests' started by matiss234, Mar 9, 2015.

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

    matiss234

    Hello! I'm trying to update player inventory after creating it.

    In this code "player.updateInventory();" throws NullPointerException even though inventory is created.
    Code:
    Bukkit.createInventory(player, InventoryType.PLAYER).setItem(2, pvpoff);
    player.updateInventory();
    Note: Inventory is created in another method when onPlayerRespawnEvent is triggered.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @matiss234 That is not how you set an item in the player inventory, use player.getInventory()
     
  3. Offline

    matiss234

    Well I have tried that but it still throwed NullPointerException since the inventory have no contents
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    matiss234

    Code:
     
    public class LPvP implements Listener{
    public void addItem(UUID playerid){
            Player player = Bukkit.getPlayer(playerid);
            ItemStack pvpoff = new ItemStack(Material.WOOL, 1);
            ItemMeta pvpoffmeta = pvpoff.getItemMeta();
            pvpoffmeta.setDisplayName(ChatColor.translateAlternateColorCodes('%', "%f%lPvP:%c OFF"));
            pvpoff.setItemMeta(pvpoffmeta);
            Bukkit.createInventory(player, InventoryType.PLAYER).setItem(2, pvpoff);
            player.updateInventory();
    }
        @EventHandler
        public void onRespawn(PlayerRespawnEvent event) {
            UUID playerid = event.getPlayer().getUniqueId();
            addItem(playerid);
        }
    }
    
    This is without all imports and Its not main class.

    Appearently there is difference from getting player from event and then adding contents to inventory than getting player UUID from event then getting player with Bukkit.getPlayer() and adding contents to inventory.

    Fixed by passing player got from event to method and then adding item with player.getInventory().setItem(2, pvpoff) and then player.updateInventory()

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Mar 9, 2015
Thread Status:
Not open for further replies.

Share This Page