Trying to create a Shop GUI

Discussion in 'Spigot Plugin Development' started by Th3Wh1teG0d, Jan 7, 2020.

  1. Hello, I am trying to create a GUI shop for my plugin but I get an error when I am trying to check if a player has 5 Emeralds to purchase a Wooden Hoe:

    Error: The method getItem(int) in the type Inventory is not applicable for the arguments (ItemStack)

    Code (Java):

    public class ShopCommand implements CommandExecutor, Listener {
       
        private final String shopname = "┬žbShop";
           
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (sender instanceof Player) {
                Player p = (Player)sender;
                Inventory shop = Bukkit.createInventory(null, 5*9, shopname);
                shop.setItem(22, new ItemStack(Material.WOOD_HOE));
                p.openInventory(shop);          
            }
            return false;
        }
       
        @EventHandler
        public void handleShopClick(InventoryClickEvent e) {
            if(!(e.getWhoClicked() instanceof Player)) return;
            Player p = (Player) e.getWhoClicked();
            if(e.getClickedInventory().getTitle().equals(shopname)) {
                e.setCancelled(true);
                switch(e.getCurrentItem().getType()) {
                case WOOD_HOE:
                    if(p.getInventory().getItem(new ItemStack(Material.EMERALD, 5))) {
                       
                    }
                   
                default:
                    break;
                }          
            }      
        }  
    }
     
    • Optimistic Optimistic x 1
  2. Hello,

    The error is here: if(p.getInventory().getItem(new ItemStack(Material.EMERALD, 5)))
    The method Inventory#getItem(ItemStack) does not exists.

    To check if the player has 5 emeralds in their Inventory, you could use Inventory#contains(Material material, int amount):
    Code (Java):
    if (p.getInventory().contains(Material.EMERALD, 5)) {
        // Player has 5 emeralds
    }
    I highly recommand you to read the javadoc: Inventory
     
    • Agree Agree x 1
  3. Thanks, it works but when I have more than 5 emeralds in one slot it not works.
     
  4. Please, make sure you have read carefully and used the correct method: Inventory#contains(Material material, int amount).
    Else, please post your code.
     
  5. Code (Java):


        @EventHandler
        public void handleShopClick(InventoryClickEvent e) {
            if(!(e.getWhoClicked() instanceof Player)) return;
            Player p = (Player) e.getWhoClicked();
            if(e.getClickedInventory().getTitle().equals(shopname)) {
                e.setCancelled(true);
                switch(e.getCurrentItem().getType()) {
                case WOOD_HOE:
                    if(p.getInventory().contains(new ItemStack(Material.EMERALD, 5))) {
                        p.sendMessage("You have 5 Emeralds!");
                        break;
                    } else
                        p.sendMessage("You dont have 5 Emeralds!");
                    break;
                default:
                    break;
                }          
            }      
        }  
    }
     
  6. Please, make sure you have read carefully and used the correct method:
    Inventory#contains(Material material, int amount) not Inventory#contains(ItemStack item).

    Code (Java):
    if(p.getInventory().contains(Material.EMERALD, 5)) {
        p.sendMessage("You have 5 Emeralds!");
        break;
    }
     
    • Agree Agree x 1
    • Funny Funny x 1