[Tutorial] Create a Inventory Menu!

Discussion in 'Resources' started by JPG2000, Sep 8, 2013.

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

    TekkitSense

    Code:java
    1. package me.marshall;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.inventory.InventoryClickEvent;
    11. import org.bukkit.inventory.Inventory;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin implements Listener{
    16.  
    17. public static Inventory mainMenu = Bukkit.createInventory(null, 9, "SenseRestrict");
    18.  
    19. static {
    20. mainMenu.setItem(0, new ItemStack(Material.DIRT, 1));
    21. }
    22.  
    23. @Override
    24. public void onEnable(){
    25. getLogger().info("SenseRestrict has been Enabled.");
    26. }
    27.  
    28. @Override
    29. public void onDisable(){
    30. getLogger().info("SenseRestrict has been Disabled.");
    31. }
    32.  
    33.  
    34. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    35. Player player = (Player)sender;
    36. if(cmd.getName().equalsIgnoreCase("restricted")){
    37. if(sender instanceof Player){
    38. player.openInventory(mainMenu);
    39. }
    40. }
    41. return false;
    42. }
    43.  
    44. @EventHandler
    45. public void onInventoryClick(InventoryClickEvent event) {
    46. Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    47. ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    48. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    49. if (inventory.getName().equals(mainMenu.getName())) { // The inventory is our custom Inventory
    50. if (clicked.getType() == Material.DIRT) { // The item that the player clicked it dirt
    51. event.setCancelled(true); // Make it so the dirt is back in its original spot
    52. player.closeInventory(); // Closes there inventory
    53. player.getInventory().addItem(new ItemStack(Material.DIRT, 1)); // Adds dirt
    54. }
    55. }
    56. }
    57.  
    58. }


    if you have skype you could add me, marshall.walker14
    i could screenshare or something

    JPG2000 this wont work :(

    Code:java
    1. package me.marshall;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.inventory.InventoryClickEvent;
    11. import org.bukkit.inventory.Inventory;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin implements Listener{
    16.  
    17. public static Inventory mainMenu = Bukkit.createInventory(null, 9, "SenseRestrict");
    18.  
    19. static {
    20. mainMenu.setItem(0, new ItemStack(Material.DIRT, 1));
    21. }
    22.  
    23. @Override
    24. public void onEnable(){
    25. getLogger().info("SenseRestrict has been Enabled.");
    26. }
    27.  
    28. @Override
    29. public void onDisable(){
    30. getLogger().info("SenseRestrict has been Disabled.");
    31. }
    32.  
    33.  
    34. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    35. Player player = (Player)sender;
    36. if(cmd.getName().equalsIgnoreCase("restricted")){
    37. if(sender instanceof Player){
    38. player.openInventory(mainMenu);
    39. }
    40. }
    41. return false;
    42. }
    43.  
    44. @EventHandler
    45. public void onInventoryClick(InventoryClickEvent event) {
    46. Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    47. ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    48. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    49. if (inventory.getName().equals(mainMenu.getName())) { // The inventory is our custom Inventory
    50. if (clicked.getType() == Material.DIRT) { // The item that the player clicked it dirt
    51. event.setCancelled(true); // Make it so the dirt is back in its original spot
    52. player.closeInventory(); // Closes there inventory
    53. player.getInventory().addItem(new ItemStack(Material.DIRT, 1)); // Adds dirt
    54. }
    55. }
    56. }
    57.  
    58. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 16, 2016
  2. Offline

    JPG2000

    TekkitSense Thats exactly what I said, you didn't register the event. Change your code to:

    Code:
    package me.marshall;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener{
     
    public static Inventory mainMenu = Bukkit.createInventory(null, 9, "SenseRestrict");
     
    static {
    mainMenu.setItem(0, new ItemStack(Material.DIRT, 1));
    }
     
    @Override
    public void onEnable(){
    getLogger().info("SenseRestrict has been Enabled.");
     
    Bukkit.getPluginManager().registerEvent(this, this);
    }
     
    @Override
    public void onDisable(){
    getLogger().info("SenseRestrict has been Disabled.");
    }
     
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    Player player = (Player)sender;
    if(cmd.getName().equalsIgnoreCase("restricted")){
    if(sender instanceof Player){
    player.openInventory(mainMenu);
    }
    }
    return false;
    }
     
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    Inventory inventory = event.getInventory(); // The inventory that was clicked in
    if (inventory.getName().equals(mainMenu.getName())) { // The inventory is our custom Inventory
    if (clicked.getType() == Material.DIRT) { // The item that the player clicked it dirt
    event.setCancelled(true); // Make it so the dirt is back in its original spot
    player.closeInventory(); // Closes there inventory
    player.getInventory().addItem(new ItemStack(Material.DIRT, 1)); // Adds dirt
    }
    }
    }
     
    }
    Implementing listener isn't enough, you need to register it. So all I did was add:
    Bukkit.getPluginManager().registerEvent(this, this);
     
  3. Offline

    TekkitSense

    It worked but I would like to show you my new code, I'm trying to make multiple menus. right now I have it working but when I'm in the bannedItems menu and click the bedrock it wont send me back the the mainMenu . can you help me again please JPG2000 . OHH and thank you so much for helping me earlier

    My code anything with // disreguard
    Code:java
    1. package me.marshall;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.inventory.InventoryClickEvent;
    11. import org.bukkit.inventory.Inventory;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin implements Listener{
    16.  
    17. public static Inventory mainMenu = Bukkit.createInventory(null, 9, "SenseRestrict");
    18. public static Inventory bannedMenu = Bukkit.createInventory(null, 18, "Banned-Items");
    19. public static Inventory noClickMenu = Bukkit.createInventory(null, 18, "Crafting Only");
    20. public static Inventory donatorMenu = Bukkit.createInventory(null, 18, "Donators Only");
    21.  
    22. static {
    23. mainMenu.setItem(2, new ItemStack(Material.TNT, 1));
    24. mainMenu.setItem(4, new ItemStack(Material.WORKBENCH, 1));
    25. mainMenu.setItem(6, new ItemStack(Material.FIRE, 1));
    26.  
    27.  
    28. bannedMenu.setItem(0, new ItemStack(Material.BEDROCK, 1));
    29. //bannedMenu.setItem(0, new ItemStack(Material.getMaterial(6362), 1));
    30. //bannedMenu.setItem(1, new ItemStack(Material.getMaterial(225), 1));
    31. //bannedMenu.setItem(2, new ItemStack(Material.getMaterial(30208), 1));
    32. //bannedMenu.setItem(3, new ItemStack(Material.getMaterial(30173), 1));
    33. //bannedMenu.setItem(4, new ItemStack(Material.getMaterial(6362), 1));
    34. //bannedMenu.setItem(5, new ItemStack(Material.getMaterial(25027), 1));
    35. //bannedMenu.setItem(6, new ItemStack(Material.getMaterial(11530), 1));
    36. //bannedMenu.setItem(7, new ItemStack(Material.getMaterial(11531), 1));
    37. //bannedMenu.setItem(8, new ItemStack(Material.getMaterial(11532), 1));
    38. //bannedMenu.setItem(9, new ItemStack(Material.getMaterial(11533), 1));
    39. //bannedMenu.setItem(10, new ItemStack(Material.getMaterial(11534), 1));
    40. //bannedMenu.setItem(11, new ItemStack(Material.getMaterial(11559), 1));
    41. //bannedMenu.setItem(12, new ItemStack(Material.getMaterial(30213), 1));
    42. //bannedMenu.setItem(13, new ItemStack(Material.getMaterial(30124), 1));
    43. //bannedMenu.setItem(14, new ItemStack(Material.getMaterial(30125), 1));
    44. //bannedMenu.setItem(15, new ItemStack(Material.getMaterial(11551), 1));
    45. //bannedMenu.setItem(16, new ItemStack(Material.getMaterial(11566), 1));
    46. //bannedMenu.setItem(17, new ItemStack(Material.getMaterial(30104), 1));
    47. //bannedMenu.setItem(18, new ItemStack(Material.getMaterial(19258), 1));
    48.  
    49.  
    50. noClickMenu.setItem(0, new ItemStack(Material.FIRE, 1));
    51.  
    52. donatorMenu.setItem(0, new ItemStack(Material.SNOW, 1));
    53. //donatorMenu.setItem(0, new ItemStack(Material.getMaterial(12264), 1));
    54. //donatorMenu.setItem(1, new ItemStack(Material.getMaterial(12260), 1));
    55. //donatorMenu.setItem(2, new ItemStack(Material.getMaterial(12268), 1));
    56. //donatorMenu.setItem(3, new ItemStack(Material.getMaterial(383*50), 1));
    57.  
    58.  
    59.  
    60. }
    61.  
    62. @Override
    63. public void onEnable(){
    64. getLogger().info("SenseRestrict has been Enabled.");
    65.  
    66. Bukkit.getPluginManager().registerEvents(this, this);
    67. }
    68.  
    69. @Override
    70. public void onDisable(){
    71. getLogger().info("SenseRestrict has been Disabled.");
    72. }
    73.  
    74.  
    75. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    76. Player player = (Player)sender;
    77. if(cmd.getName().equalsIgnoreCase("restricted")){
    78. if(sender instanceof Player){
    79. player.openInventory(mainMenu);
    80. }
    81. }
    82. return false;
    83. }
    84.  
    85. @EventHandler
    86. public void onInventoryClick(InventoryClickEvent event) {
    87. Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    88. ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    89. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    90.  
    91.  
    92. if (inventory.getName().equals(mainMenu.getName())) { // The Main Menu
    93. if (clicked.getType() == Material.TNT) { // The button for the BannedItems Menu
    94. event.setCancelled(true); // Make it so the dirt is back in its original spot
    95. player.closeInventory(); // Closes there inventory
    96. player.openInventory(bannedMenu); //opens the BannedItems Menu
    97. }
    98. if (clicked.getType() == Material.WORKBENCH) { // The item that the player clicked
    99. event.setCancelled(true); // Make it so the dirt is back in its original spot
    100. player.closeInventory(); // Closes there inventory
    101. player.openInventory(noClickMenu); //opens the noClickMenu
    102. }
    103. if (clicked.getType() == Material.FIRE) { // The item that the player clicked
    104. event.setCancelled(true); // Make it so the dirt is back in its original spot
    105. player.closeInventory(); // Closes there inventory
    106. player.openInventory(donatorMenu);
    107. }
    108.  
    109. if (inventory.equals(bannedMenu)) { // The inventory is our custom Inventory
    110. if (clicked.getType().equals(Material.BEDROCK)) { // The item that the player clicked it dirt
    111. event.setCancelled(true);
    112. player.closeInventory();
    113. player.openInventory(mainMenu);
    114. }
    115. }
    116. }
    117. }
    118. }


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

    JPG2000

    TekkitSense Because the beadwork is in a different menu, and firstly your checking if its your original menu, which will be false.

    Either make another event, or add a else if after checking the inventory and check the second inventory.
     
  5. Offline

    TekkitSense

    JPG2000 Ok, so i made another @EventHandler & put this code in but still nothing. Sorry for bugging you so much
    Code:java
    1. @EventHandler
    2. public void BansMenu(InventoryClickEvent event) {
    3. Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    4. ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    5. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    6. if (inventory.getName().equals(BansMenu.getName())) { // The inventory is our custom Inventory
    7. if (clicked.getType() == Material.SAND) { // The item that the player clicked it dirt
    8. event.setCancelled(true); // Make it so the dirt is back in its original spot
    9. }
    10.  
    11. }
    12. }


    JPG2000 All I'm trying to do is when someone in the BansMenu and clicks on a item it just cancels the event so they cant take the item

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 16, 2016
  6. Offline

    JPG2000

    TekkitSense Do the same thing with the first, then change the name to check for BansMenu.
     
  7. Offline

    TekkitSense

    JPG2000 I did and still nothing :(

    JPG2000 My 2 Events
    Code:java
    1. @EventHandler
    2. public void MainMenu(InventoryClickEvent event) {
    3. Player player = (Player) event.getWhoClicked();
    4. ItemStack clicked = event.getCurrentItem();
    5. Inventory inventory = event.getInventory();
    6.  
    7. if (inventory.getName().equals(MainMenu.getName())) {
    8. if (clicked.getType() == Material.TNT) {
    9. event.setCancelled(true);
    10. player.closeInventory();
    11. player.openInventory(BansMenu);
    12. }
    13.  
    14. if(clicked.getType() == Material.WORKBENCH) {
    15. event.setCancelled(true);
    16. player.closeInventory();
    17. player.openInventory(CraftingMenu);
    18. }
    19.  
    20. if(clicked.getType() == Material.PORTAL) {
    21. event.setCancelled(true);
    22. player.closeInventory();
    23. player.openInventory(DonatorMenu);
    24. }
    25.  
    26. if(clicked.getType() == Material.LAVA) {
    27. event.setCancelled(true);
    28. }
    29. }
    30. }
    31.  
    32.  
    33. @EventHandler
    34. public void BansMenu(InventoryClickEvent event) {
    35. Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    36. ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    37. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    38. if (inventory.getName().equals(BansMenu.getName())) { // The inventory is our custom Inventory
    39. if (clicked.getType() == Material.SAND) { // The item that the player clicked it dirt
    40. event.setCancelled(true); // Make it so the dirt is back in its original spot
    41. }
    42. }
    43. }
    44. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 16, 2016
  8. Offline

    JPG2000

  9. Offline

    TekkitSense

    JPG2000 I will, but for now could you fix up my code? Please

    JPG2000 Ok so when i go into the BansMenu and click the sand i can move it i also put stuff into that menu i dont want to be able to do that

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 16, 2016
  10. Offline

    michael566

    I want to get a value from the config and put it in an item's lore. How would I do that?
     
  11. Offline

    michael566

    Anyone?
     
  12. Offline

    L33m4n123

    Where are you stuggeling? Getting the String from the config or adding it to the Item as Lore?
     
  13. Offline

    michael566

    I'm trying to get the a string from the config and put it into the lore.
     
  14. Offline

    L33m4n123

    If you are using bukkits yml api
    Code:java
    1. String[] lore = config.getStringList("path.to.the.List.of.Lore");

    and then basicly

    Untested. Could be that I am missing a bit. It's been a while since I added custom lore to an Item
    Code:java
    1. Itemstack item = new ItemStack(/***/);
    2. ItemMeta meta = item.getMeta();
    3. meta.setLore(lore);
    4. item.setMeta(meta);
     
    JPG2000 likes this.
  15. Offline

    michael566

    Well this is what I am trying to do but it won't let me:

    Code:java
    1. static {
    2. ItemStack dblock = new ItemStack(Material.DIAMOND_BLOCK);
    3. ItemMeta dblockmeta = dblock.getItemMeta();
    4. dblockmeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Heal");
    5. dblockmeta.setLore(Arrays.asList(getConfig.getString("heal cost")));
    6. dblock.setItemMeta(dblockmeta);
     
  16. Offline

    L33m4n123

    Post your config file please. And make sure that "heal cost" is really the path to it And it should be .getStringList("path.to.the.value");
     
  17. Offline

    michael566

    Here is my config:
    Code:
    heal cost: 10
    feed cost: 10
    But it still says its wrong.
     
  18. Offline

    JPG2000

    michael566 Make sure the congih is working. Oh, and you need another } (you may of forgot this only here, not in the real code)
     
  19. Offline

    michael566

    the config is working and ther is a } in that section its just I did not include that here.
     
  20. Offline

    JPG2000

    michael566 Whats the issue then? Not working? Stacktrace? Whats exactly happening??
     
  21. Offline

    michael566

    It just says that you cant write getConfig.getString there.
     
  22. Offline

    michael566

    It says you can't put getconfig.getString there.
     
  23. Offline

    JPG2000

    michael566 because getConfig isn't a method, but getConfig() is...
     
  24. Offline

    michael566

    What do you mean?
     
  25. Offline

    JPG2000

    michael566 You using getConfig.getString but methods must have paremeters, (), so you should be using getConfig().getString
     
  26. Offline

    michael566

    It says non static method getConfig() cannot be referenced from a static context.
     
  27. Offline

    iiHeroo

    I'll be adding something soon to this ^-^
     
  28. Offline

    JPG2000

    michael566 Then do what it does, make it static...
     
  29. Offline

    michael566

    No, it tells me to remove the static but when I do I get an error in the console.
     
  30. Offline

    iiHeroo


    p.performCommand("your cmd"); is better for a player running a command he has permission to.

    Also if you want the menu to have a color and a special effect like this http://icap.me/i/L8Tgeh6aLN.png , here's what I did

    Code:java
    1. public static Inventory myInventory = Bukkit.createInventory(null, 9, ChatColor.DARK_RED +""+ ChatColor.BOLD + "Kit Selector");


    What you're seeing in the picture is this part

    Code:java
    1. ChatColor.DARK_RED +""+ ChatColor.BOLD + "Kit Selector";


    Pretty simple to do, but thought to share it for future reference :)
     
Thread Status:
Not open for further replies.

Share This Page