Menu in a menu?

Discussion in 'Plugin Development' started by Sean0402, Aug 12, 2014.

Thread Status:
Not open for further replies.
  1. 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
    1. @EventHandler
    2. public void onPlayerInteractEntity1(PlayerInteractEntityEvent e) {
    3. Inventory potions = Bukkit.createInventory(null, 9, ChatColor.GREEN + "Potions");
    4. if(e.getRightClicked() instanceof Zombie){
    5. Zombie zombie = (Zombie) e.getRightClicked();
    6. if (zombie.getCustomName().equals(ChatColor.GOLD + "Shop")) {
    7. Inventory kits = Bukkit.createInventory(null, 9, ChatColor.GOLD
    8. + "Shop");
    9. ItemStack sword = new ItemStack(Material.DIAMOND_CHESTPLATE);
    10. ItemMeta meta = sword.getItemMeta();
    11. meta.setDisplayName(ChatColor.DARK_RED + "Armour");
    12. sword.setItemMeta(meta);
    13. kits.setItem(0, sword);
    14. ItemStack chestplate = new ItemStack(Material.DIAMOND_SWORD);
    15. ItemMeta meta1 = chestplate.getItemMeta();
    16. meta1.setDisplayName(ChatColor.BLUE + "Enchants");
    17. chestplate.setItemMeta(meta1);
    18. kits.setItem(4, chestplate);
    19. ItemStack bow = new ItemStack(Material.BLAZE_POWDER);
    20. ItemMeta meta2 = bow.getItemMeta();
    21. meta2.setDisplayName(ChatColor.GREEN + "Potions");
    22. bow.setItemMeta(meta2);
    23. kits.setItem(8, bow);
    24. e.getPlayer().openInventory(kits);
    25. }
    26. }
    27. }
     
  2. Offline

    KaitouKidFTW

    Sean0402 Close the inventory and open the other inventory.
     
  3. KaitouKidFTW So I create a new inventory. Then under the potions part do something like this?

    Code:java
    1. e.getPlayer().closeInventory();
    2. e.getPlayer().openInventory(potions);


    KaitouKidFTW Hm that seemed to make it only open the inventory "potions"

    Code:java
    1. @EventHandler
    2. public void onPlayerInteractEntity1(PlayerInteractEntityEvent e) {
    3. Inventory potions = Bukkit.createInventory(null, 9, ChatColor.GREEN + "Potions");
    4. if(e.getRightClicked() instanceof Zombie){
    5. Zombie zombie = (Zombie) e.getRightClicked();
    6. if (zombie.getCustomName().equals(ChatColor.GOLD + "Shop")) {
    7. Inventory kits = Bukkit.createInventory(null, 9, ChatColor.GOLD
    8. + "Shop");
    9. ItemStack sword = new ItemStack(Material.DIAMOND_CHESTPLATE);
    10. ItemMeta meta = sword.getItemMeta();
    11. meta.setDisplayName(ChatColor.DARK_RED + "Armour");
    12. sword.setItemMeta(meta);
    13. kits.setItem(0, sword);
    14. ItemStack chestplate = new ItemStack(Material.DIAMOND_SWORD);
    15. ItemMeta meta1 = chestplate.getItemMeta();
    16. meta1.setDisplayName(ChatColor.BLUE + "Enchants");
    17. chestplate.setItemMeta(meta1);
    18. kits.setItem(4, chestplate);
    19. ItemStack bow = new ItemStack(Material.BLAZE_POWDER);
    20. ItemMeta meta2 = bow.getItemMeta();
    21. meta2.setDisplayName(ChatColor.GREEN + "Potions");
    22. bow.setItemMeta(meta2);
    23. kits.setItem(8, bow);
    24. e.getPlayer().openInventory(kits);
    25. e.getPlayer().
    26. e.getPlayer().openInventory(potions);
    27. }
    28. }
    29. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    KaitouKidFTW

    Sean0402 If the player clicks the diamond sword.. Open the inventory you want to open
     
  5. Offline

    fireblast709

    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.
     
  6. bwfcwalshy I have never done them and what do they do?

    fireblast709 I have this as for now

    Code:java
    1. @EventHandler
    2. public void onInventoryClick1(InventoryClickEvent e) {
    3. Player p = (Player)e.getWhoClicked();
    4. if (e.getInventory().getName().equals(ChatColor.GOLD + "Shop")) {
    5. if (e.getCurrentItem() == null)
    6. return;
    7. if (e.getCurrentItem().getItemMeta().getDisplayName()
    8. .equals(ChatColor.DARK_RED + "Armour")) {
    9. e.setCancelled(true);
    10. return;
    11. }
    12. if (e.getCurrentItem().getItemMeta().getDisplayName()
    13. .equals(ChatColor.BLUE + "Enchants")) {
    14. e.setCancelled(true);
    15. }
    16. if (e.getCurrentItem().getItemMeta().getDisplayName()
    17. .equals(ChatColor.GREEN + "Potions")) {
    18. e.setCancelled(true);
    19. }
    20. }
    21. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  7. Offline

    mechoriet

    hi this is the was of handling the inventory data what ppl click in the inventory


    Code:java
    1. public void oninvclick(InventoryClickEvent e){
    2. if ( ChatColor.stripColor(e.getInventory.getName()).equalsIgnoreCase("Your Inventory name")){
    3. Player p = (Player) e.getWhoClicked;
    4. e.setCancelled(true);//for canceling the item they click that they cannot have it in thier hands
    5. switch(e.getCurrentItem.getType()){
    6. case your_Item:
    7. p.openInventory(Your inv in a seperate class);
    8. e.setCancelled(true);
    9. break;
    10. default:
    11. p.closeInventory():
    12. break;
    13. }
    14. }
     
  8. Sean0402 it checks if you click that material then does something here is an example

    Code:java
    1. case DIAMOND_SWORD:
    2. player.openInventory(potions);
    3. break;
     
  9. Offline

    MCMatters


    Code:java
    1. switch(e.getCurrentItem().getType()){
    2. case (item):
    3. //Code
    4. break;
    5. case (item):
    6. //Code
    7. break;
    8. }
     
  10. Offline

    fireblast709

    Sean0402
     
  11. 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
    1. if (e.getCurrentItem().getItemMeta().getDisplayName()
    2. .equals(ChatColor.GREEN + "Potions")) {
    3. if(e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Potions"))
    4. e.setCancelled(true);
    5. }
     
  12. Offline

    fireblast709

    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
     
  13. fireblast709 is this any closer? :/

    Code:java
    1. if (e.getCurrentItem().getItemMeta().getDisplayName()
    2. .equals(ChatColor.GREEN + "Potions")) {
    3. if(e.getCurrentItem() == null)
    4. e.getCurrentItem().hasItemMeta();
    5. if(e.getCurrentItem().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Potions"))
    6. e.setCancelled(true);
     
  14. Offline

    fireblast709

    Sean0402 slightly.
    Code:java
    1. ItemStack is = e.getCurrentItem();
    2. if(is != null && is.hasItemMeta())
    3. {
    4. if([exercise: fill in the rest by yourself :3])
    5. {
    6. // By now you should be able to guarantee that "ItemStack is" is the ItemStack you are looking for
    7. }
    8. }
     
Thread Status:
Not open for further replies.

Share This Page