1.14.4 The method getName() is undefined for the type Inventory

Discussion in 'Spigot Plugin Development' started by Reddington_, Jan 19, 2020.

  1. Hey Guys. Im having the next issue and im wondering with I'm doing wrong? Could anyone help me. Code below

    The problem is with the next lines of code
    Code (Text):

    (p.getOpenInventory().getTopInventory().getName().equalsIgnoreCase(msg.getCMessage(Message.CHAT_MENU_TITLE))){
                    if (e.getClickedInventory().getName().equalsIgnoreCase(msg.getCMessage(Message.CHAT_MENU_TITLE))){
     
    Full code
    Code (Text):

    package me.Mystic_Mark_.Morgathe.Events;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.command.Command;

    import me.Mystic_Mark_.Morgathe.MorgatheCore;
    import me.Mystic_Mark_.Morgathe.Util.MessageUtil;
    import me.Mystic_Mark_.Morgathe.Util.MessageUtil.Message;
    import me.Mystic_Mark_.Morgathe.Util.MorgathePlayer;

    public class InventoryEvent implements Listener{

        MorgatheCore core = MorgatheCore.get();
        MessageUtil msg = core.getMessageUtil();

        @EventHandler
        public void onClick(InventoryClickEvent e){
            Player p = (Player) e.getWhoClicked();
            try {
                if (p.getOpenInventory().getTopInventory().getName().equalsIgnoreCase(msg.getCMessage(Message.CHAT_MENU_TITLE))){
                    if (e.getClickedInventory().getName().equalsIgnoreCase(msg.getCMessage(Message.CHAT_MENU_TITLE))){
                        MorgathePlayer mp = core.getPlayerManager().getPlayer(p.getUniqueId());
                        switch (e.getSlot()){
                        case 0:
                            if (mp.hasEnabledLChat()){
                                mp.setEnabledLChat(false);
                                msg.sendMessage(p, Message.CHAT_NORECIEVE_LAND);
                            } else {
                                mp.setEnabledLChat(true);
                                msg.sendMessage(p, Message.CHAT_RECIEVE_LAND);
                            }
                            break;
                        case 1:
                            if (mp.hasEnabledGChat()){
                                mp.setEnabledGChat(false);
                                msg.sendMessage(p, Message.CHAT_NORECIEVE_ROLEPLAY);
                            } else {
                                mp.setEnabledGChat(true);
                                msg.sendMessage(p, Message.CHAT_RECIEVE_ROLEPLAY);
                            }
                            break;
                        case 2:
                            if (mp.hasGChat()){
                                mp.setGChat(false);
                                msg.sendMessage(p, Message.CHAT_NORECIEVE_GLOBAL);
                            } else {
                                mp.setGChat(true);
                                msg.sendMessage(p, Message.CHAT_RECIEVE_GLOBAL);
                            }
                            break;
                        case 4:
                            if (mp.hasGlobalChat() == 0){
                                mp.setGlobalChat(1);
                                msg.sendMessage(p, Message.DEFAULT_CHAT_SET, msg.getCMessage(Message.GLOBAL_CHAT_NAME));
                            } else if (mp.hasGlobalChat() == 1){
                                mp.setGlobalChat(2);
                                msg.sendMessage(p, Message.DEFAULT_CHAT_SET, msg.getCMessage(Message.ROLEPLAY_CHAT_NAME));
                            } else {
                                mp.setGlobalChat(0);
                                msg.sendMessage(p, Message.DEFAULT_CHAT_SET, msg.getCMessage(Message.LAND_CHAT_NAME));
                            }
                            break;
                        default:
                            break;
                        }
                        core.getInventoryUtil().setupChatInventory(p, mp.hasEnabledLChat().booleanValue(), mp.hasGlobalChat(), mp.hasEnabledGChat().booleanValue(), mp.hasGChat().booleanValue());
                    }
                    e.setCancelled(true);
                }
            } catch(Exception exc){}
        }

    }
     
     
    #1 Reddington_, Jan 19, 2020
    Last edited: Jan 20, 2020
  2. md_5

    Administrator Developer

    Literally 100 threads about this, try using google
     
    • Winner Winner x 8
    • Agree Agree x 3

  3. I did, and the solutions I found didn't work on this code. Adding and removing certain imports, changing things in the code. None of it worked. Thats why I've requested help on this forum.
     
  4. Have you tried getTitle instead of getName? - Not sure if this will help though.

    p.getOpenInventory().getTitle()
     
  5. To sum up the 100 threads:
    1. use the InventoryView, not the Inventory itself. The view has the getName() getTitle()-method that you are looking for
    2. don‘t make your inventory dependent on the name, that‘s no good practice.
     
    #5 Schottky, Jan 20, 2020
    Last edited: Jan 20, 2020
  6. Personally, when I'm doing inventories I have a list which contains all players with the GUI open. Although I'm sure there're much better ways of doing it.


    Code (Java):


        public static List<Player> open = new ArrayList<Player>();

        public static Inventory menu(Player p) {
            Inventory inv = Bukkit.createInventory(null, 9, Core.color("&aInventory"));
            //Setup Inventory
            open.add(p);
            return inv;
        }

        @EventHandler
        public void onClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            if (open.contains(p)) {
                //Player is clicking in this gui
            }
        }

        @EventHandler
        public void onClose(InventoryCloseEvent e) {
            Player p = (Player) e.getPlayer();
            if (open.contains(p)) {
                new BukkitRunnable() {
                    public void run() {
                        open.remove(p);
                    }
                }.runTaskLaterAsynchronously(NickRequest.inst, 3);
            }
        }