Solved Inventory GUI?

Discussion in 'Plugin Development' started by ShadowLAX, Aug 5, 2013.

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

    ShadowLAX

    Hello bukkit, I am looking for a way to add a gui of sorts. Like when a player right clicks a book, it opens a chest gui of one row and has a bunch of Items that teleports you, gives you things, etc. I have no idea where to begin, and if someone could explain how to do this I would very grateful! :D
     
  2. Offline

    Pizza371

  3. Offline

    chris4315

    Here you go, feel free to use this:

    Code:java
    1. // Add this variable at the top of your class
    2. public static Inventory inv = null;
    3.  
    4. // Your onEnable()
    5. public void onEnable() {
    6. createInventoryGUI();
    7. // Other stuff that you'd put in an onEnable goes here...
    8. }
    9.  
    10. public void createInventoryGUI() {
    11. inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "MyServer Teleportation Menu"); // (owner, size (int), name)
    12.  
    13. // First item
    14. ItemStack chestplate = new ItemStack(Material.IRON_CHESTPLATE, 1);
    15. ItemMeta chestplatemeta = chestplate.getItemMeta();
    16. chestplatemeta.setDisplayName(ChatColor.BLUE + "Teleport to PvP zone.");
    17. chestplate.setItemMeta(chestplatemeta);
    18.  
    19. // Second item
    20. ItemStack blazerod = new ItemStack(Material.BLAZE_ROD, 1);
    21. ItemMeta blazerodmeta = blazerod.getItemMeta();
    22. blazerodmeta.setDisplayName(ChatColor.BLUE + "Teleport to Spawn.");
    23. blazerod.setItemMeta(blazerodmeta);
    24.  
    25. inv.addItem(blazerod);
    26. inv.addItem(chestplate);
    27. }
    28. // Method for listening to Interact Event
    29. @EventHandler
    30. public void interact(PlayerInteractEvent event) {
    31. Player p = event.getPlayer();
    32. if (p.getItemInHand().getType() == Material.FURNACE) { // For example purposes...
    33. event.setCancelled(true);
    34. p.openInventory(inv);
    35. }
    36. }
    37.  
    38. @EventHandler
    39. public void click(InventoryClickEvent event) {
    40. if (event.getInventory().getSize() == 9) {
    41. event.setCancelled(true);
    42. if (event.getCurrentItem().getType() == Material.BLAZE_ROD) {
    43. // Teleport to "spawn"
    44. }
    45. if (event.getCurrentItem().getType() == Material.IRON_CHESTPLATE) {
    46. // Teleport to "pvp zone"
    47. }
    48. else {
    49. return;
    50. }
    51. }
    52. }
    53. }
     
    GEOFBOT, shmkane and Philitup321 like this.
  4. Offline

    ShadowLAX

  5. Offline

    chris4315

    Updated it after testing in Eclipse. Should work and it's good to go! You're welcome and happy coding :)
     
  6. Offline

    ial1010

    How would you make it so if the player left clicked the item it would open?
     
  7. Offline

    chris4315

    ial1010
    You'd have to listen for that in InventoryClickEvent and open a new inventory GUI.
     
Thread Status:
Not open for further replies.

Share This Page