e.getInventory().getName()

Discussion in 'Spigot Plugin Development' started by Christophe6, Jul 1, 2016.

  1. Hello,
    e.getInventory().getName() is returning the name of a previous opened inventory.
    First I fire an event (InventoryClickEvent) in the class: "PlayerInfo", later I fire the same event in the class XPStore. But the e.getInventory().getName() returns the name of the "PlayerInfo"-inventory. The XPStore-inventory is openend by the inventory click event of the "PlayerInfo"-class. I hope I explained it well enough!
     
  2. use the InventoryOpenEvent#getInventory()#getName()
     
  3. @MadMaxCookie
    I use it for executing a command while clicking on a speciefied item...
     
  4. Can you send the code of the classes?
     
  5. @shawn_ian
    XPStore:
    Code (Java):
    @EventHandler
        public void onInventoryClickEvent(InventoryClickEvent e) {
            if(!(e.getWhoClicked() instanceof Player)) {
                return;
            }

            Player player = (Player) e.getWhoClicked();
            System.out.println("1");
            System.out.println(ChatColor.stripColor(e.getInventory().getName()));
            System.out.println(ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', SettingsManager.getInstance().getXPStore().getString("xpstore.title"))));
            if(!(ChatColor.stripColor(e.getInventory().getName()).equals(ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', SettingsManager.getInstance().getXPStore().getString("xpstore.title")))))) {
                return;
            }
            System.out.println("2");
            for(String item : SettingsManager.getInstance().getXPStore().getStringList("xpstore.itemsinxpstore")) {
                System.out.println("3");
                if(e.getSlot() == SettingsManager.getInstance().getXPStore().getInt("xpstore.itemsinxpstore." + item + ".slot")) {
                    System.out.println("4");
                    if(XPHandler.getInstance().getXP(player) >= SettingsManager.getInstance().getXPStore().getDouble("xpstore.itemsinxpstore." + item + ".xpprice")) {
                        System.out.println("5");
                        for(String playercommand : SettingsManager.getInstance().getXPStore().getStringList("xpstore.itemsinxpstore." + item + ".playercommands")) {
                            if(playercommand.contains("*player*")) {
                                playercommand = playercommand.replace("*player*", player.getName());
                            }
                         
                            player.performCommand(playercommand);
                        }
                     
                        for(String consolecommand : SettingsManager.getInstance().getXPStore().getStringList("xpstore.itemsinxpstore." + item + ".consolecommands")) {
                            if(consolecommand.contains("*player*")) {
                                consolecommand = consolecommand.replace("*player*", player.getName());
                            }
                         
                            Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), consolecommand);
                        }
                     
                        XPHandler.getInstance().removeXP(player, SettingsManager.getInstance().getXPStore().getDouble("xpstore.itemsinxpstore." + item + ".xpprice"));
                        MessageManager.getInstance().severe(player, SettingsManager.getInstance().getMessages().getString("buy_xpstore"));
                    } else {
                        MessageManager.getInstance().severe(player, SettingsManager.getInstance().getMessages().getString("not_enough_xp"));
                    }
                }
            }
         
            player.closeInventory();
        }
    PLAYERINFO:
    Code (Java):
    @EventHandler
        public void onInventoryClickEvent(InventoryClickEvent e) {
            if(!(e.getWhoClicked() instanceof Player)) {
                return;
            }
         
            Player player = (Player) e.getWhoClicked();

            if(!(ChatColor.stripColor(e.getInventory().getName()).equals(ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', SettingsManager.getInstance().getMainConfig().getString("playerinfo.displayname")))))) {
                return;
            }
         
            if(e.getSlot() == 13) {
                String your_xp = SettingsManager.getInstance().getMessages().getString("your_xp");
                if(your_xp.contains("*xp*")) {
                    your_xp = your_xp.replace("*xp*", String.valueOf(XPHandler.getInstance().getXP(player)));
                }
             
                if(your_xp.contains("*player*")) {
                    your_xp = your_xp.replace("*player*", player.getName());
                }
             
                if(your_xp.contains("*playerdisplayname*")) {
                    your_xp = your_xp.replace("*playerdisplayname*", player.getDisplayName());
                }
             
             
                e.getInventory().clear();
                player.closeInventory();
                MessageManager.getInstance().good(player, your_xp);
             
            } else if(e.getSlot() == SettingsManager.getInstance().getXPStore().getInt("xpstore.slotinprofile")) {
                if(SettingsManager.getInstance().getXPStore().getBoolean("xpstore.enabled")) {
                    player.closeInventory();
                    player.openInventory(XPStore.getInstance().getInventory());
                }
            }
         
            if(e.getSlot() == 0 || e.getSlot() == 8 || e.getSlot() == 18 || e.getSlot() == 26) {
                e.getInventory().clear();
                player.closeInventory();
            }
        }
     
  6. @Christophe6 to know what is the name of the Inventory use the InventoryOpenEvent

    Code (Java):
    // to get the name
    @EventHandler
    public void onOpen(InventoryOpenEvent e) {
       e.getInventory().getName(); // returns to the inventory name that is opened
    }
     
  7. I agree with @MadMaxCookie.