Solved Opening a custom inventory doesn't work

Discussion in 'Plugin Development' started by JjPwN1, May 19, 2013.

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

    JjPwN1

    When trying to make an inventory in which a player can type a command to access, the player can't even access the inventory. This inventory is used so a player clicks on a skull item, and teleports that player to the other player the skull represents.

    OnEnable():
    Code:
    specinv = Bukkit.createInventory(null, 27, ChatColor.RED + "Who do you want to spectate?");
    refreshSpectatorInventory():
    Code:
        public void refreshSpectatorInventory(){
            pg.specinv.clear();
            for(Player p : Bukkit.getOnlinePlayers()){
                if(pg.tributes.containsKey(p.getName())){
                    ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                    ItemMeta meta = item.getItemMeta();
                    meta.setDisplayName(ChatColor.DARK_GREEN + p.getName());
                    item.setItemMeta(meta);
                    pg.specinv.addItem(item);
                }
            }
        }
    onCommand():
    Code:
                if(cmd.getName().equalsIgnoreCase("spectate") || cmd.getName().equalsIgnoreCase("spec")){
                    if(args.length == 0){
                        if(!tributes.containsKey(player.getName())){
                            pgplayer.refreshSpectatorInventory();
                            player.openInventory(specinv);
                        }else if(tributes.containsKey(player.getName())){
                            pgplayer.sendError("You're not a spectator!", player);
                        }
                    }else{
                        pgplayer.sendError("Usage: /spectate", player);
                    }
                }
    I've tried everything I thought possible, but using player.openInventory() doesn't open the inventory. In the onCommand() method, the variable player is "Player player = (Player) sender;" and I did do a check to see if they are a player "if(sender instanceof Player){"
     
  2. Offline

    Xx_LeetGamer_xX

    Maybe the Inventory Owner (first argument, you set it to null) cannot be null. Try making a new inventory every time the player sends the command and use p as the Inventory Owner.
     
  3. Offline

    JjPwN1

    I thought that as well, yet that worked to no avail.

    Any other ideas

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  4. Like Xx_LeetGamer_xX said it's really because you set the first arg to null...You can do something like this:
    Code:
    public void openInv(Player player){
    Inventory inv = Bukkit.createInventory(player, 27, "name");
    player.openInventory(inv);
    }
     
  5. Offline

    Xx_LeetGamer_xX

    I already suggested that.

    @JjPwN1
    This is the code that I'm using:

    Code:
    Inventory inv = Bukkit.createInventory(p, 9, "Warps");
    p.openInventory(inv);
    
    That should work, let me know.
     
  6. Thats why I said "it's really". I'll change my post.
     
  7. Offline

    JjPwN1

    I made a very foolish mistake here xD.

    To prevent "spectators" from opening chests, I made an event
    Code:
        @EventHandler
        public void openChest(InventoryOpenEvent event){
            if(!tributes.containsKey(event.getPlayer().getName())){
                event.setCancelled(true);
            }
        }
    without thinking of checking if the inventory was a chest's, so it blocked all inventory opening...
     
Thread Status:
Not open for further replies.

Share This Page