[Tutorial] Create a Inventory Menu!

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

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

    michael566

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

    JPG2000

  3. Offline

    samosaara

    michael566 Instead of making all the getConfig() thing static (what I don't even know if it's possible) Make a static array of Strings. and use it in your code...
    Code:java
    1. static String[] array = getConfig().getStringList("path.to.it");
    2. static {
    3. //Your thing.
    4. }



    My time to ask. This line don't work:
    Code:java
    1. event.setCancelled(true);

    It's just replace the item back at my custom inventory. But I still Have the item at my mouse. to I do whatever I want with. I want, a method that, replace the item back AND take back from the player. Help.

    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

    samosaara It does do that. But its a bukkit glitch. If you open an inventory and call the cancel after the next time you click in the inventory it won't call the item.
     
  5. Offline

    samosaara

    Nah, manage to do it, When the player doesn't got the points necessary to buy the sword from the "shop"(my custom inv) I close it and open it again.Works Perfect.
     
    JPG2000 likes this.
  6. Offline

    michael566

    Where would I put that? Here is my code
    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);
    7.  
    8.  
    9. ItemStack steak = new ItemStack(Material.COOKED_BEEF);
    10. ItemMeta steakmeta = steak.getItemMeta();
    11. steakmeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Feed");
    12. steak.setItemMeta(steakmeta);
    13.  
    14.  
    15.  
    16. myInventory.setItem(3, dblock);
    17. myInventory.setItem(5, steak);
    18.  
    19.  
    20. }
     
  7. Offline

    samosaara

    Out of any method and inside the class, (i recommend at the begging)
    Code:java
    1. public class apples{
    2. static String[] array;
    3. public void someMethod(){
    4. //...
    5. }
    6. }
     
  8. Offline

    JPG2000

  9. Offline

    michael566

    For your information I do I was just asking for help geez don't have to be so rude.

    Ok what do I put in //your thing

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

    JPG2000

    michael566 I wasn't being rude. All I said was "don't". I wan't him to do it him self so he actually learns.
     
  11. Offline

    samosaara

    Lols. When i say
    Code:java
    1. //you thing

    I mean your code :S
     
  12. Offline

    hamzaxx

    Ultimate_n00b
    How would I only use one row right now if you use anything less than 2 rows it throws an out of bounds exception.
     
  13. Offline

    JPG2000

    hamzaxx I'm pretty sure you can do 9. If not try and Hopper inventory (Docs can help you with that one)
     
  14. Offline

    sgavster

    Error in the first line of code:
    Code:java
    1. Bxukkit
    :p
     
  15. Offline

    hamzaxx

    Also String should be capitalized.
     
  16. Offline

    HeavyMine13


    Where can I put this??
     
  17. Offline

    JPG2000

    HeavyMine13 Outside of a method in any class you wish
     
    HeavyMine13 likes this.
  18. Offline

    HeavyMine13

    Ok mine is working, but I cannot rename the items inside D:

    Here is my only class code:
    Code:java
    1. package me.HeavyMine13.potjoin;
    2.  
    3. import java.util.List;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.EventPriority;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.inventory.InventoryClickEvent;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.inventory.Inventory;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.inventory.meta.ItemMeta;
    18. import org.bukkit.plugin.Plugin;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class Potjoin extends JavaPlugin implements Listener {
    22. public void onEnable() {
    23. Bukkit.getServer().getPluginManager().registerEvents(this, (Plugin) this);
    24. }
    25.  
    26. public void onDisable() {
    27. }
    28.  
    29. public static Inventory myInventory = Bukkit.createInventory(null, 9, ChatColor.DARK_AQUA + "Join Menu:");
    30. static {
    31. myInventory.setItem(0, new ItemStack(Material.IRON_CHESTPLATE, 1));
    32. }
    33.  
    34. @SuppressWarnings("unused")
    35. private ItemStack setName(ItemStack is, String name, List<String> lore) {
    36. ItemMeta im = is.getItemMeta();
    37. if (name != null)
    38. im.setDisplayName("&cJuggernaut");
    39. if (name != null)
    40. im.setLore(lore);
    41. is.setItemMeta(im);
    42.  
    43. return is;
    44.  
    45. }
    46.  
    47. @EventHandler(priority=EventPriority.HIGH)
    48. public void onPlayerUse(PlayerInteractEvent event){
    49. Player p = event.getPlayer();
    50.  
    51. if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
    52. if(p.getItemInHand().getType() == Material.DIAMOND){
    53. p.openInventory(myInventory);
    54. }
    55. }
    56. }
    57.  
    58. @EventHandler
    59. public void onInventoryClick(InventoryClickEvent event) {
    60. Player player = (Player) event.getWhoClicked();
    61. ItemStack clicked = event.getCurrentItem();
    62. Inventory inventory = event.getInventory();
    63. if (inventory.getName().equals(myInventory.getName())) {
    64. if (clicked.getType() == Material.IRON_CHESTPLATE) {
    65. event.setCancelled(true);
    66. player.closeInventory();
    67. Bukkit.dispatchCommand(player, "jug join arena1");
    68.  
    69. }
    70. }
    71. }
    72.  
    73.  
    74.  
    75. }
    76.  
     
  19. Offline

    FissionTech

    @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(myInventory.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
    }
    }
    }
    Code:java
    1.  
    2. @EventHandler
    3. public void onInventoryClick(InventoryClickEvent event) {
    4. Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    5. ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    6. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    7. if (inventory.getName().equals(myInventory.getName())) { // The inventory is our custom Inventory
    8. if (clicked.getType() == Material.DIRT) { // The item that the player clicked it dirt
    9. event.setCancelled(true); // Make it so the dirt is back in its original spot
    10. player.closeInventory(); // Closes there inventory
    11. player.getInventory().addItem(new ItemStack(Material.DIRT, 1)); // Adds dirt
    12. }
    13. }
    14. }
    15.  


    What if someone wanted to have multiple items in the inventory? They would be stuck (This is thanks to lukeer31, btw).
    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onInventoryClick(InventoryClickEvent event){
    3. Player player = (Player) event.getWhoClicked();
    4. Inventory inventory = event.getInventory();
    5. if(inventory.getName().equals(menuInventory.getName())){
    6. //Slot ID based clicking
    7. if(event.getSlot() == 1){
    8. event.setCancelled(true);
    9. player.closeInventory();
    10. player.performCommand("creative");
    11. }
    12. }
    13. }
     
  20. Offline

    Wizehh

    Thanks, great tutorial!
     
  21. Offline

    MonkeyPlays

    I tried this but the inventory doesn't pop up when I right click the emerald, what did I do wrong?
    Here is the code
    Code:
    package me.BuissnessMonkey.main;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import me.BuissnessMonkey.kits.ManiacKit;
    import me.BuissnessMonkey.kits.PvPKit;
    import me.BuissnessMonkey.kits.VipKit;
    import me.BuissnessMonkey.kits.VipplusKit;
    import me.BuissnessMonkey.kits.WarlordKit;
    import me.BuissnessMonkey.kits.WarriorKit;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener {
       
        public List<String> used = new ArrayList<String>();
       
       
        public void onEnable(){
            getLogger().info("NoobKits has started up");
            getServer().getPluginManager().registerEvents(this, this);
           
            getCommand("pvp").setExecutor(new PvPKit(this));
            getCommand("warrior").setExecutor(new WarriorKit(this));
            getCommand("vip").setExecutor(new VipKit(this));
            getCommand("maniac").setExecutor(new ManiacKit(this));
            getCommand("vipplus").setExecutor(new VipplusKit(this));
            getCommand("kitlist").setExecutor(new KitList(this));
            getCommand("warlord").setExecutor(new WarlordKit(this));
        }
       
       
        @EventHandler
        public void onPlayerDeathKits(PlayerDeathEvent e){
            Player p = (Player)e.getEntity();
            if(used.contains(p.getName())){
                used.remove(p.getName());
            }
        }
        public static Inventory myInventory = Bukkit.createInventory(null, 9, ChatColor.RED + "Kits");
        static{
            myInventory.setItem(1, new ItemStack(Material.STONE_SWORD, 1 ));
            myInventory.setItem(1, new ItemStack(Material.GOLD_INGOT, 1));
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player p = (Player) event.getPlayer();
            if (p.getItemInHand() !=null) {
                ItemStack item = p.getItemInHand();
                if(item.getType() == Material.EMERALD);
                p.openInventory(myInventory);
            }
               
               
            }
        @EventHandler(priority = EventPriority.MONITOR)
        public void onInventoryClick(InventoryClickEvent event) {
            Player p = (Player) event.getWhoClicked();
            if(myInventory.getName().equals(myInventory.getName()));
            if(event.getSlot() == 1){
                event.setCancelled(true);
                p.performCommand("pvp");
            }
        }
       
        }
       
     
     
     
     
       
     
    
     
  22. Offline

    samosaara

    The inventory is not initialized. This is never called:
    Code:java
    1. public static Inventory myInventory = Bukkit.createInventory(null, 9, ChatColor.RED + "Kits");
    2. static{
    3. myInventory.setItem(1, new ItemStack(Material.STONE_SWORD, 1 ));
    4. myInventory.setItem(1, new ItemStack(Material.GOLD_INGOT, 1));
    5. }
     
  23. Offline

    MonkeyPlays

    samosaara I'm a noob with bukkit so what do I have to do to call it?

    Never mind samosaara I re made the plugin because of recompile errors and it now seems to work :)

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

    97WaterPolo

    JPG2000 When I open the inventory, I am able to take the item out and place it in my inventory. If I then re-open the inventory there are no items in it. This is the code

    Code:
    public class IconMenu {
        public static Inventory myInventory = Bukkit.createInventory(null, 9, "My custom Inventory!");
        // The first parameter, is the inventory owner. I make it null to let everyone use it.
        //The second parameter, is the slots in a inventory. Must be a multiple of 9. Can be up to 54.
        //The third parameter, is the inventory name. This will accept chat colors
        static {
            myInventory.setItem(0, new ItemStack(Material.DIRT, 1));
            myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
        }
        @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(myInventory.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
                }
            }
        }
    }
    and I am opening it using
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            final Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("pinv") || cmd.getName().equalsIgnoreCase("ppinv"))
            {
                player.openInventory(IconMenu.myInventory);
     
            }
    Any ideas on how to cancel it? Seems event.setCancelled isn't working to well.
     
  25. Offline

    WeeziMonkey

    http://jd.bukkit.org/rb/apidocs/org/bukkit/event/inventory/InventoryClickEvent.html

    Aren't you supposed to use a BukkitScheduler task to close the inventory 1 tick later?
     
  26. Offline

    McKiller5252

    Lets say i have a menu plugin and i have the multiworld plugin and i want to warp to that world using the menu plugin. How would the code look like for that?
     
  27. Offline

    MunchMallow

    Can you make a tutorial about quests and saving hashsets?
     
  28. Offline

    valon750

    I assume this would only allow me to add a single line of lore, I guess I could add more arguments, assuming I knew I would be using multiple lines, but how about when I use say, 2 on one, 3 on another? perhaps I could just add "" to the lore.
     
  29. Offline

    HariHD

  30. Offline

    TheLexoPlexx

Thread Status:
Not open for further replies.

Share This Page