Solved Error with inventory

Discussion in 'Plugin Development' started by rykker_, Jun 23, 2020.

Thread Status:
Not open for further replies.
  1. Hi, I don't know why, but the GUI doesn't work, more precisely the getName() method. I've tried many things but I don't know.

    ERROR:
    Code:
    The method getName() is undefined for the type Inventory
    CODE:
    Code:
    package GUI;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.HumanEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import GUI.GUICommand;
    import Core.Main;
    
    public class GUIMoveItem extends JavaPlugin implements Listener{
        @EventHandler
        public void clickEvent(InventoryClickEvent e){
            //Check to see if its the GUI menu
            if(e.getInventory().getName().equals(ChatColor.AQUA + "Custom GUI")){
                Player player = (Player) e.getWhoClicked();
                switch(e.getCurrentItem().getType()){
                    case TNT:
                        player.closeInventory();
                        player.setHealth(0.0);
                        player.sendMessage("1");
                        break;
                    case BREAD:
                        player.closeInventory();
                        player.setFoodLevel(20);
                        player.sendMessage("2");
                        break;
                    case DIAMOND_SWORD:
                        player.closeInventory();
                        player.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD));
                        player.sendMessage("");
                        break;
                }
                e.setCancelled(true);
            }
        }
    
       
    }
     
  2. Offline

    Plootonix

    Looks like you're missing the actual inventory itself.
    I'd start by declaring a global "Inventory" variable:
    Code:
    public Inventory inv = Bukkit.createInventory(null, [num slots], ["inv name"]);
    Now, you have to assign the items to slots:
    Code:
    ItemStack item = //blah blah
    ItemMeta meta = item.getItemMeta();
    //after creating item, setting meta and lore:
    Code:
    inv.setItem([slot num, item])
    Next, make sure to differentiate between the custom inv and items as opposed to the same inv and items that you could find in a chest/inv. Items you put in should have a custom meta/lore:
    Code:
            if (!event.getInventory().equals(inv))
                return;
            if (event.getCurrentItem() == null)
                return;
            if (event.getCurrentItem().getItemMeta() == null)
                return;
            if (event.getCurrentItem().getItemMeta().getDisplayName() == null)
                return;
         
            event.setCancelled(true); //makes it so that the player doesn't take the item out of the inv
    Then you can get who clicked and use a listener to note which slot they clicked.
    Code:
            if (event.getSlot() == 0) {
                         //code
                    }
    Finally, to open the inventory, you use:
    Code:
            player.openInventory(inv);
    If any part is unclear, let me know. I glanced over a few things such as the more in depth details.
     
    Last edited: Jun 23, 2020
  3. Offline

    Machine Maker

    @Plootonix I'm not sure what you are answering? I believe he is talking about the line
    Code:java
    1. e.getInventory().getName().equals(ChatColor.AQUA + "Custom GUI")


    @rykker_ Inventories do not have names, InventoryView's do. Get the InventoryView from the event, and then use getTitle() to get the title of that inventory view.
     
  4. Offline

    Plootonix

    Maybe I misunderstood the question :p. The purpose of my response was to show OP that there's a different (and probably cleaner) way to make a custom inventory with clickable items that do some action.
     
  5. Offline

    Machine Maker

    Yeah, it looks like he is just trying to detect when an item is clicked in his already created custom inventory.
     
  6. I changed to this code. There are no errors. I can still take out items.

    CODE:

    Code:
    package GUI;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class GUIMoveItem implements Listener{
        @EventHandler
        public void clickEvent(InventoryClickEvent e){
            //Check to see if its the GUI menu
            if(e.getView().getTitle().equals(ChatColor.AQUA + "Custom GUI")){
                Player player = (Player) e.getWhoClicked();
                Material item = e.getCurrentItem().getType();
                if (item == Material.BREAD) {
                    player.closeInventory();
                    player.setFoodLevel(20);
                    player.sendMessage("2");
                    e.setCancelled(true);
                }
                if (item == Material.TNT) {
                    player.closeInventory();
                    player.setHealth(0.0);
                    player.sendMessage("1");
                    e.setCancelled(true);
                }
                if (item == Material.DIAMOND_SWORD) {
                    player.closeInventory();
                    player.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD));
                    player.sendMessage("3");
                    e.setCancelled(true);
                }
            }
        }
    
       
    }
    @Plootonix
    I have a different class with this

    CODE:
    Code:
    package GUI;
    
    import org.bukkit.Bukkit;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import java.util.ArrayList;
    public class GUICommand implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player){
                Player player = (Player) sender;
                Inventory gui = Bukkit.createInventory(player, 9, ChatColor.AQUA + "Custom GUI");
                //Menu Options(Items)
                ItemStack suicide = new ItemStack(Material.TNT);
                ItemStack feed =  new ItemStack(Material.BREAD);
                ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
                //Edit the items
                ItemMeta suicide_meta = suicide.getItemMeta();
                suicide_meta.setDisplayName(ChatColor.RED + "Suicide");
                ArrayList<String> suicide_lore = new ArrayList<>();
                suicide_lore.add(ChatColor.GOLD + "Kill yourself. ;)");
                suicide_meta.setLore(suicide_lore);
                suicide.setItemMeta(suicide_meta);
                ItemMeta feed_meta = feed.getItemMeta();
                feed_meta.setDisplayName(ChatColor.DARK_GREEN + "Feed");
                ArrayList<String> feed_lore = new ArrayList<>();
                feed_lore.add(ChatColor.GOLD + "Hunger no more.");
                feed_meta.setLore(feed_lore);
                feed.setItemMeta(feed_meta);
                ItemMeta sword_meta = sword.getItemMeta();
                sword_meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Sword");
                ArrayList<String> sword_lore = new ArrayList<>();
                sword_lore.add(ChatColor.GOLD + "Get a sword.");
                sword_meta.setLore(sword_lore);
                sword.setItemMeta(sword_meta);
                //Put the items in the inventory
                ItemStack[] menu_items = {suicide, feed, sword};
                gui.setContents(menu_items);
                player.openInventory(gui);
            }
            return true;
        }
    }
    OK. I fixed. I only changed places:
    Code:
    if (item == Material.TNT) {
                    player.closeInventory();
                    player.setHealth(0.0);
                    player.sendMessage("1");
                    e.setCancelled(true);
                }

    Thank you all for your help :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 24, 2020
Thread Status:
Not open for further replies.

Share This Page