Open GUI using specific named item

Discussion in 'Plugin Development' started by Skulled, Aug 10, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Skulled

    hi all,

    i successfully rewrote my plugin code to make it open when right clicking a specific item, now here's the problem, i want to item to be named, if you dont get it, basicly when a player right clicks an item named "Example" it will open the gui

    The main class
    Code:java
    1. package me.SkulledrMenu;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.Sound;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class MenuInv extends JavaPlugin implements Listener {
    14.  
    15. private Menu menu;
    16.  
    17. public void onEnable() {
    18. menu = new Menu(this);
    19. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    20. }
    21.  
    22. @EventHandler
    23. public void onPlayerInteract(PlayerInteractEvent e) {
    24. Player p = e.getPlayer();
    25.  
    26. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    27. if (!(e.getMaterial() == Material.NETHER_STAR)) return;
    28.  
    29. menu.show(e.getPlayer());
    30. p.playSound(p.getLocation(), Sound.LEVEL_UP, 1, 0);
    31.  
    32. if ((e.getMaterial() == Material.NETHER_STAR)) return;
    33. e.setCancelled(true);
    34. }
    35. }



    My Menu Class
    Code:java
    1. package me.SkulledrMenu;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.DyeColor;
    8. import org.bukkit.GameMode;
    9. import org.bukkit.Material;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.inventory.InventoryClickEvent;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.material.MaterialData;
    18. import org.bukkit.plugin.Plugin;
    19.  
    20. public class Menu implements Listener {
    21.  
    22. private Inventory inv;
    23. private ItemStack c, s, a;
    24.  
    25. public Menu(Plugin p) {
    26. inv = Bukkit.getServer().createInventory(null, 9, ChatColor.RED + "" + ChatColor.BOLD + "Gamemode Chooser");
    27.  
    28. c = createItem(DyeColor.GREEN, ChatColor.GREEN + "Creative");
    29. s = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "Survival");
    30. a = createItem(DyeColor.RED, ChatColor.RED + "Adventure");
    31.  
    32. inv.setItem(2, c);
    33. inv.setItem(4, s);
    34. inv.setItem(6, a);
    35.  
    36. Bukkit.getServer().getPluginManager().registerEvents(this, p);
    37. }
    38.  
    39. private ItemStack createItem(DyeColor dc, String name) {
    40. ItemStack i = new MaterialData(Material.WOOL).toItemStack(1);
    41. ItemMeta im = i.getItemMeta();
    42. im.setDisplayName(name);
    43. im.setLore(Arrays.asList("Set your gamemode", "to " + name.toLowerCase() + " mode"));
    44. i.setItemMeta(im);
    45. return i;
    46. }
    47.  
    48. public void show(Player p) {
    49. p.openInventory(inv);
    50. }
    51.  
    52. @EventHandler
    53. public void onInventoryClick(InventoryClickEvent e) {
    54. if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    55. if (e.getCurrentItem().getItemMeta() == null) return;
    56. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Creative")) {
    57. e.setCancelled(true);
    58. e.getWhoClicked().setGameMode(GameMode.CREATIVE);
    59. e.getWhoClicked().closeInventory();
    60. }
    61. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival")) {
    62. e.setCancelled(true);
    63. e.getWhoClicked().setGameMode(GameMode.SURVIVAL);
    64. e.getWhoClicked().closeInventory();
    65. }
    66. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Adventure")) {
    67. e.setCancelled(true);
    68. e.getWhoClicked().setGameMode(GameMode.ADVENTURE);
    69. e.getWhoClicked().closeInventory();
    70. }
    71. }
    72. }


    if you can tell me the code to make is open on a specific named item i would thankful
     
  2. Offline

    JustinsCool15

    Skulled
    No one here will spoon feed you code. But I'll help you out.

    When the player right clicks.
    Check if the item's not null.
    Check if it's the material you want.
    Check if it has item meta.
    Check if it has a display name.
    Check what the display name equals.
    If it's "Example"
    Open the inventory.
     
  3. Offline

    Skulled

    JustinsCool15
    i added what you told me and it worked, thanks a ton man

    btw nice Morgan free pic :p
     
    Developerjohn likes this.
  4. Offline

    Nonliker1000

    Simply add:
    Code:
                  if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("Your Item Name it should be ^^"))
     
  5. Offline

    FabeGabeMC

    Nonliker1000
    That wouldn't be efficient if the item doesn't have a custom item meta. However, it would still work.
     
  6. Offline

    Developerjohn

    Morgan Freeman, lel.
     
  7. Offline

    xepisolonxx

    Skulled How did you succesfully rewrote your plugin code it looks exactly like pogostick29dev I event found the class http://pastebin.com/jNWWdgma also if you want it name and stuff i recommend doing this
    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player player = (Player) e.getWhoClicked();
              if (e.getInventory().getName().equalsIgnoreCase(inv.getName())){
               
                  if (e.getCurrentItem() != null && !e.getCurrentItem().getType().equals(Material.AIR)){
               
            if(e.getCurrentItem().getItemMeta().hasDisplayName()){
                if (e.getSlotType() != SlotType.OUTSIDE) {
     
Thread Status:
Not open for further replies.

Share This Page