Hello how do I make it so when they click on a certain item like a diamond sword here for instance it will open into another menu? Thanks! Code:java @EventHandler public void onPlayerInteractEntity1(PlayerInteractEntityEvent e) { Inventory potions = Bukkit.createInventory(null, 9, ChatColor.GREEN + "Potions"); if(e.getRightClicked() instanceof Zombie){ Zombie zombie = (Zombie) e.getRightClicked(); if (zombie.getCustomName().equals(ChatColor.GOLD + "Shop")) { Inventory kits = Bukkit.createInventory(null, 9, ChatColor.GOLD + "Shop"); ItemStack sword = new ItemStack(Material.DIAMOND_CHESTPLATE); ItemMeta meta = sword.getItemMeta(); meta.setDisplayName(ChatColor.DARK_RED + "Armour"); sword.setItemMeta(meta); kits.setItem(0, sword); ItemStack chestplate = new ItemStack(Material.DIAMOND_SWORD); ItemMeta meta1 = chestplate.getItemMeta(); meta1.setDisplayName(ChatColor.BLUE + "Enchants"); chestplate.setItemMeta(meta1); kits.setItem(4, chestplate); ItemStack bow = new ItemStack(Material.BLAZE_POWDER); ItemMeta meta2 = bow.getItemMeta(); meta2.setDisplayName(ChatColor.GREEN + "Potions"); bow.setItemMeta(meta2); kits.setItem(8, bow); e.getPlayer().openInventory(kits); }} }
KaitouKidFTW So I create a new inventory. Then under the potions part do something like this? Code:java e.getPlayer().closeInventory(); e.getPlayer().openInventory(potions); KaitouKidFTW Hm that seemed to make it only open the inventory "potions" Code:java @EventHandler public void onPlayerInteractEntity1(PlayerInteractEntityEvent e) { Inventory potions = Bukkit.createInventory(null, 9, ChatColor.GREEN + "Potions"); if(e.getRightClicked() instanceof Zombie){ Zombie zombie = (Zombie) e.getRightClicked(); if (zombie.getCustomName().equals(ChatColor.GOLD + "Shop")) { Inventory kits = Bukkit.createInventory(null, 9, ChatColor.GOLD + "Shop"); ItemStack sword = new ItemStack(Material.DIAMOND_CHESTPLATE); ItemMeta meta = sword.getItemMeta(); meta.setDisplayName(ChatColor.DARK_RED + "Armour"); sword.setItemMeta(meta); kits.setItem(0, sword); ItemStack chestplate = new ItemStack(Material.DIAMOND_SWORD); ItemMeta meta1 = chestplate.getItemMeta(); meta1.setDisplayName(ChatColor.BLUE + "Enchants"); chestplate.setItemMeta(meta1); kits.setItem(4, chestplate); ItemStack bow = new ItemStack(Material.BLAZE_POWDER); ItemMeta meta2 = bow.getItemMeta(); meta2.setDisplayName(ChatColor.GREEN + "Potions"); bow.setItemMeta(meta2); kits.setItem(8, bow); e.getPlayer().openInventory(kits); e.getPlayer(). e.getPlayer().openInventory(potions); } } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Sean0402 Listen to the InventoryClickEvent and get the current item. Then, to check whether that ItemStack is yours, check If the ItemStack is not null If the ItemStack hasItemMeta() -get the ItemMeta- If the ItemMeta hasDisplayName() -get the display name- If the display name matches one of the names you added to the inventory KaitouKidFTW also missed a pretty important detail. Due to the fact that you are in the middle of an inventory modification in the event, some methods are not safe to use (check the javadocs for a small list). The solution is still simple, lucky us. Simply use the following snippet to delay code by one tick Code: new BukkitRunnable() { @Override public void run() { // Your code } }.runTask(plugin instace) If you are not known to task scheduling, I recommend reading this wiki page. Also note that you need to delay opening an inventory by one tick after you closed it. You can use the same snippet mentioned above for that.
bwfcwalshy I have never done them and what do they do? fireblast709 I have this as for now Code:java @EventHandler public void onInventoryClick1(InventoryClickEvent e) { Player p = (Player)e.getWhoClicked(); if (e.getInventory().getName().equals(ChatColor.GOLD + "Shop")) { if (e.getCurrentItem() == null) return; if (e.getCurrentItem().getItemMeta().getDisplayName() .equals(ChatColor.DARK_RED + "Armour")) { e.setCancelled(true); return; } if (e.getCurrentItem().getItemMeta().getDisplayName() .equals(ChatColor.BLUE + "Enchants")) { e.setCancelled(true); } if (e.getCurrentItem().getItemMeta().getDisplayName() .equals(ChatColor.GREEN + "Potions")) { e.setCancelled(true); } } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.
hi this is the was of handling the inventory data what ppl click in the inventory Code:java public void oninvclick(InventoryClickEvent e){if ( ChatColor.stripColor(e.getInventory.getName()).equalsIgnoreCase("Your Inventory name")){Player p = (Player) e.getWhoClicked;e.setCancelled(true);//for canceling the item they click that they cannot have it in thier handsswitch(e.getCurrentItem.getType()){case your_Item: p.openInventory(Your inv in a seperate class); e.setCancelled(true); break;default:p.closeInventory():break;}}
Sean0402 it checks if you click that material then does something here is an example Code:java case DIAMOND_SWORD:player.openInventory(potions);break;
fireblast709 I'm trying but still noob at java. So I'm trying my best sorry for my problems and here is what I got so far: Code:java if (e.getCurrentItem().getItemMeta().getDisplayName() .equals(ChatColor.GREEN + "Potions")) { if(e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Potions")) e.setCancelled(true); }
Sean0402 I pretty much typed everything out for you... Unless you don't know how to convert "is not null" to Java or call methods, then I suggest you look more into the basics before continuing with Bukkit
fireblast709 is this any closer? :/ Code:java if (e.getCurrentItem().getItemMeta().getDisplayName() .equals(ChatColor.GREEN + "Potions")) { if(e.getCurrentItem() == null) e.getCurrentItem().hasItemMeta(); if(e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Potions")) e.setCancelled(true);
Sean0402 slightly. Code:java ItemStack is = e.getCurrentItem();if(is != null && is.hasItemMeta()){ if([exercise: fill in the rest by yourself :3]) { // By now you should be able to guarantee that "ItemStack is" is the ItemStack you are looking for }}