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; } } } }
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
Please, make sure you have read carefully and used the correct method: Inventory#contains(Material material, int amount). Else, please post your code.
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; } } } }
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; }