Solved Inventory not opening

Discussion in 'Plugin Development' started by PieMan456, Nov 17, 2013.

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

    PieMan456

    Hello Everyone,

    I made a custom inventory. But whenever I type my command it does not open. There is no error message. Thanks for any help!
    Code:
    Code:java
    1. package me.pieman.custominventory;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.inventory.InventoryClickEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.event.player.PlayerJoinEvent;
    18. import org.bukkit.inventory.Inventory;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.inventory.meta.ItemMeta;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class CustomInventory extends JavaPlugin implements Listener{
    24.  
    25. public static Inventory CustomInv;
    26.  
    27. public void onEnable(){
    28. CustomInv = Bukkit.createInventory(null, 9, "Worlds");
    29. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    30. }
    31.  
    32. public void onDisable(){
    33.  
    34. }
    35.  
    36. public boolean onCommand(CommandSender sender , Command cmd, String label, String[] args){
    37. if(!(sender instanceof Player)){
    38. sender.sendMessage(ChatColor.RED + "Only players can use that command!");
    39. return true;
    40. } else {
    41. if(label.equalsIgnoreCase("World")){
    42. Player p = (Player) sender;
    43. if(p.hasPermission("ci.open")){
    44. p.openInventory(CustomInv);
    45.  
    46. ItemStack pvp = new ItemStack(Material.DIAMOND_SWORD);
    47. ItemMeta pvpMeta = pvp.getItemMeta();
    48. ArrayList<String> pworld = new ArrayList<String>();
    49. pvpMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "PVPArena World");
    50. pworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the PVPArena World!");
    51. pvpMeta.setLore(pworld);
    52. pvp.setItemMeta(pvpMeta);
    53.  
    54. ItemStack survival = new ItemStack(Material.GRASS);
    55. ItemMeta survivalMeta = survival.getItemMeta();
    56. ArrayList<String> sworld = new ArrayList<String>();
    57. survivalMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Survival World");
    58. sworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the Survival World!");
    59. survivalMeta.setLore(sworld);
    60. survival.setItemMeta(survivalMeta);
    61.  
    62. ItemStack mg = new ItemStack(Material.REDSTONE_BLOCK);
    63. ItemMeta mgMeta = mg.getItemMeta();
    64. ArrayList<String> mgworld = new ArrayList<String>();
    65. mgMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Minigame World");
    66. mgworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the Minigame World!");
    67. mgMeta.setLore(mgworld);
    68. mg.setItemMeta(mgMeta);
    69.  
    70. CustomInv.clear();
    71. CustomInv.setItem(1, new ItemStack(pvp));
    72. CustomInv.setItem(4, new ItemStack(mg));
    73. CustomInv.setItem(7, new ItemStack(survival));
    74. } else {
    75. p.sendMessage(ChatColor.RED + "You do not have permission!");
    76. }
    77. }
    78. }
    79.  
    80. return true;
    81. }
    82.  
    83. @EventHandler
    84. public void onPlayerJoin(PlayerJoinEvent e){
    85. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    86. ItemMeta bookMeta = book.getItemMeta();
    87. bookMeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds");
    88. book.setItemMeta(bookMeta);
    89. ArrayList<String> book1 = new ArrayList<String>();
    90. book1.add(ChatColor.RED + "Click this to open the world teleporter!");
    91. bookMeta.setLore(book1);
    92. if(e.getPlayer().getInventory().firstEmpty() == -1){
    93. return;
    94. } else {
    95. Player p = (Player) e.getPlayer();
    96. p.getInventory().setItem(8, new ItemStack(book));
    97. }
    98. }
    99.  
    100. @EventHandler
    101. public void onPlayerInteract(PlayerInteractEvent e){
    102. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    103. ItemMeta bookMeta = book.getItemMeta();
    104. ArrayList<String> book1 = new ArrayList<String>();
    105. book1.add(ChatColor.RED + "Click this to open the world teleporter!");
    106. bookMeta.setLore(book1);
    107. bookMeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds");
    108. book.setItemMeta(bookMeta);
    109.  
    110. if(!(e.getAction() == Action.LEFT_CLICK_AIR) && !(e.getAction() == Action.LEFT_CLICK_BLOCK)) return;
    111. if(e.getClickedBlock() != new ItemStack(book)) return;
    112. if (e.getClickedBlock() == new ItemStack(book)){
    113. e.getPlayer().openInventory(CustomInv);
    114. }
    115. }
    116.  
    117. @EventHandler
    118. public void onInventoryClick(InventoryClickEvent e){
    119. if(!(e.getInventory().equals(CustomInv))) return;
    120. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("PVPArena World")){
    121. e.setCancelled(true);
    122. e.getWhoClicked().teleport(new Location(Bukkit.getWorld("world"), 49, 88, -206));
    123. }
    124. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival World")){
    125. e.setCancelled(true);
    126. e.getWhoClicked().teleport(new Location(Bukkit.getWorld("world"), 463, 88, -157));
    127. }
    128. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Minigame World")){
    129. e.setCancelled(true);
    130. e.getWhoClicked().teleport(new Location(Bukkit.getWorld("minigames"), -0.5, 66, -6.5));
    131. }
    132. }
    133. }
    134.  
     
  2. Offline

    RealDope

    Did you register your command in plugin.yml?
     
  3. Offline

    the_merciless

    Code:
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender , Command cmd, String label, String[] args){
            if(!(sender instanceof Player)){
                sender.sendMessage(ChatColor.RED + "Only players can use that command!");
                return true;
            } else {
                if(label.equalsIgnoreCase("World")){
                    Player p = (Player) sender;
                    if(p.hasPermission("ci.open")){
                        Inventory custom = Bukkit.getServer().createInventory(p, 9, "Worlds");
     
                        ItemStack pvp = new ItemStack(Material.DIAMOND_SWORD);
                        ItemMeta pvpMeta = pvp.getItemMeta();
                        ArrayList<String> pworld = new ArrayList<String>();
                        pvpMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "PVPArena World");
                        pworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the PVPArena World!");
                        pvpMeta.setLore(pworld);
                        pvp.setItemMeta(pvpMeta);
     
                        ItemStack survival = new ItemStack(Material.GRASS);
                        ItemMeta survivalMeta = survival.getItemMeta();
                        ArrayList<String> sworld = new ArrayList<String>();
                        survivalMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Survival World");
                        sworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the Survival World!");
                        survivalMeta.setLore(sworld);
                        survival.setItemMeta(survivalMeta);
     
                        ItemStack mg = new ItemStack(Material.REDSTONE_BLOCK);
                        ItemMeta mgMeta = mg.getItemMeta();
                        ArrayList<String> mgworld = new ArrayList<String>();
                        mgMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Minigame World");
                        mgworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the Minigame World!");
                        mgMeta.setLore(mgworld);
                        mg.setItemMeta(mgMeta);
     
                        custom.clear();
                        custom.setItem(1, new ItemStack(pvp));
                        custom.setItem(4, new ItemStack(mg));
                        custom.setItem(7, new ItemStack(survival));
                        p.updateInventory();
                    } else {
                        p.sendMessage(ChatColor.RED + "You do not have permission!");
                    }
                }
            }
     
            return true;
        }
     
  4. Offline

    PieMan456

    RealDope
    Oh my god I am so stupid. Thanks!

    RealDope the_merciless
    Ok I added the command to the plugin.yml but it still does not work.

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

    iWareWolf

    I think you should add the items into the inventory before you open the inventory.
     
    MayoDwarf likes this.
  6. Offline

    JRL1004

    PieMan456 iWareWolf Or perhaps creating the inventory would be a start?
    Code:java
    1. Inventory inventory = Bukkit.createInventory(null, size, title);

    Replace size with any number divisible by 9 (Eg. 90 = 10 rows since 90 / 9 = 10)
    Replace title with whatever you want the inventory name to be.
     
  7. Offline

    iWareWolf

    JRL1004
    He already created it onEnable()
     
  8. Offline

    jacklin213

    Try taking the static modifier off Custom Inventory
     
  9. Offline

    PieMan456

    jacklin213 iWareWolf JRL1004
    Ok so now I have this:
    Code:java
    1. package me.pieman.custominventory;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.inventory.InventoryClickEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.event.player.PlayerJoinEvent;
    18. import org.bukkit.inventory.Inventory;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.inventory.meta.ItemMeta;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class CustomInventory extends JavaPlugin implements Listener{
    24.  
    25. public Inventory CustomInv;
    26.  
    27. public void onEnable(){
    28. CustomInv = Bukkit.createInventory(null, 9, "Worlds");
    29. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    30. }
    31.  
    32. public void onDisable(){
    33.  
    34. }
    35.  
    36. public boolean onCommand(CommandSender sender , Command cmd, String label, String[] args){
    37. if(!(sender instanceof Player)){
    38. sender.sendMessage(ChatColor.RED + "Only players can use that command!");
    39. return true;
    40. } else {
    41. if(label.equalsIgnoreCase("World")){
    42. Player p = (Player) sender;
    43. if(p.hasPermission("ci.open")){
    44. ItemStack pvp = new ItemStack(Material.DIAMOND_SWORD);
    45. ItemMeta pvpMeta = pvp.getItemMeta();
    46. ArrayList<String> pworld = new ArrayList<String>();
    47. pvpMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "PVPArena World");
    48. pworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the PVPArena World!");
    49. pvpMeta.setLore(pworld);
    50. pvp.setItemMeta(pvpMeta);
    51.  
    52. ItemStack survival = new ItemStack(Material.GRASS);
    53. ItemMeta survivalMeta = survival.getItemMeta();
    54. ArrayList<String> sworld = new ArrayList<String>();
    55. survivalMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Survival World");
    56. sworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the Survival World!");
    57. survivalMeta.setLore(sworld);
    58. survival.setItemMeta(survivalMeta);
    59.  
    60. ItemStack mg = new ItemStack(Material.REDSTONE_BLOCK);
    61. ItemMeta mgMeta = mg.getItemMeta();
    62. ArrayList<String> mgworld = new ArrayList<String>();
    63. mgMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Minigame World");
    64. mgworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the Minigame World!");
    65. mgMeta.setLore(mgworld);
    66. mg.setItemMeta(mgMeta);
    67.  
    68. p.openInventory(CustomInv);
    69.  
    70. CustomInv.clear();
    71. CustomInv.setItem(1, new ItemStack(pvp));
    72. CustomInv.setItem(4, new ItemStack(mg));
    73. CustomInv.setItem(7, new ItemStack(survival));
    74. } else {
    75. p.sendMessage(ChatColor.RED + "You do not have permission!");
    76. }
    77. }
    78. }
    79.  
    80. return true;
    81. }
    82.  
    83. @EventHandler
    84. public void onPlayerJoin(PlayerJoinEvent e){
    85. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    86. ItemMeta bookMeta = book.getItemMeta();
    87. bookMeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds");
    88. book.setItemMeta(bookMeta);
    89. ArrayList<String> book1 = new ArrayList<String>();
    90. book1.add(ChatColor.RED + "Click this to open the world teleporter!");
    91. bookMeta.setLore(book1);
    92. if(e.getPlayer().getInventory().firstEmpty() == -1){
    93. return;
    94. } else {
    95. Player p = (Player) e.getPlayer();
    96. p.getInventory().setItem(8, new ItemStack(book));
    97. }
    98. }
    99.  
    100. @EventHandler
    101. public void onPlayerInteract(PlayerInteractEvent e){
    102. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    103. ItemMeta bookMeta = book.getItemMeta();
    104. ArrayList<String> book1 = new ArrayList<String>();
    105. book1.add(ChatColor.RED + "Click this to open the world teleporter!");
    106. bookMeta.setLore(book1);
    107. bookMeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds");
    108. book.setItemMeta(bookMeta);
    109.  
    110. if(!(e.getAction() == Action.LEFT_CLICK_AIR) && !(e.getAction() == Action.LEFT_CLICK_BLOCK)) return;
    111. if(e.getClickedBlock() != new ItemStack(book)) return;
    112. if (e.getClickedBlock() == new ItemStack(book)){
    113. e.getPlayer().openInventory(CustomInv);
    114. }
    115. }
    116.  
    117. @EventHandler
    118. public void onInventoryClick(InventoryClickEvent e){
    119. if(!(e.getInventory().equals(CustomInv))) return;
    120. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("PVPArena World")){
    121. e.setCancelled(true);
    122. e.getWhoClicked().teleport(new Location(Bukkit.getWorld("world"), 49, 88, -206));
    123. }
    124. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival World")){
    125. e.setCancelled(true);
    126. e.getWhoClicked().teleport(new Location(Bukkit.getWorld("Hub"), 0, 66, 0));
    127. }
    128. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Minigame World")){
    129. e.setCancelled(true);
    130. e.getWhoClicked().teleport(new Location(Bukkit.getWorld("minigames"), -0.5, 66, -6.5));
    131. }
    132. }
    133. }
    134.  


    But it still does not work.:((Sorry for not responding I have been having a hard time at school)
     
  10. Offline

    the_merciless

    Create a new inventory inside the command rather than onEnable. Also player.updateInventory() is required
     
  11. Offline

    PieMan456

    the_merciless
    Where do i put the player.updateInventory() and what is it used for?
     
  12. Offline

    the_merciless

    See my 1st post

    It updates what the player sees on screen
     
  13. Offline

    PieMan456

    the_merciless
    How would I check to see if the inventory that the player has open is the Custom in my events one if it is inside my command?
     
  14. Offline

    the_merciless

    Let me get on my pc and I will help more
     
  15. Offline

    PieMan456

  16. Offline

    the_merciless

    Here look over this

    Code:
    package me.pieman.custominventory;
     
    import java.util.ArrayList;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    //////////////////// REMOVED ////////////////////
     
    public class CustomInventory extends JavaPlugin implements Listener{
     
        public void onEnable(){
            //////////////////// REMOVED ////////////////////
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
     
        public void onDisable(){
     
        }
       
       
       
       
       
        public void openCustomInventory(Player p){
            ////////////////////CREATE NEW INVENTORY ////////////////////               
            Inventory CustomInv = Bukkit.getServer().createInventory(p, 9, "Worlds");
         
            //////////////////// CREATE NEW ITEMS ////////////////////
            ItemStack pvp = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta pvpMeta = pvp.getItemMeta();
            ArrayList<String> pworld = new ArrayList<String>();
            pvpMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "PVPArena World");
            pworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the PVPArena World!");
            pvpMeta.setLore(pworld);
            pvp.setItemMeta(pvpMeta);
     
            ItemStack survival = new ItemStack(Material.GRASS);
            ItemMeta survivalMeta = survival.getItemMeta();
            ArrayList<String> sworld = new ArrayList<String>();
            survivalMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Survival World");
            sworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the Survival World!");
            survivalMeta.setLore(sworld);
            survival.setItemMeta(survivalMeta);
     
            ItemStack mg = new ItemStack(Material.REDSTONE_BLOCK);
            ItemMeta mgMeta = mg.getItemMeta();
            ArrayList<String> mgworld = new ArrayList<String>();
            mgMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Minigame World");
            mgworld.add(ChatColor.DARK_PURPLE + "Click this to teleport to the Minigame World!");
            mgMeta.setLore(mgworld);
            mg.setItemMeta(mgMeta);
     
            ////////////////////ADD ITEMS TO INVENTORY ////////////////////
            CustomInv.setItem(1, new ItemStack(pvp));
            CustomInv.setItem(4, new ItemStack(mg));
            CustomInv.setItem(7, new ItemStack(survival));
     
            //////////////////// OPEN INVENTORY ////////////////////
            p.openInventory(CustomInv);
     
            //////////////////// NOT SURE IF THIS IS NEEDED BUT TRY WITH AND WITHOUT ////////////////////
            p.updateInventory();
     
        }
       
       
       
       
     
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender , Command cmd, String label, String[] args){
            if(!(sender instanceof Player)){
                sender.sendMessage(ChatColor.RED + "Only players can use that command!");
                return true;
            } else {
                if(label.equalsIgnoreCase("World")){
                    Player p = (Player) sender;
                    if(p.hasPermission("ci.open")){
                       
                        openCustomInventory(p);
                       
                    } else {
                        p.sendMessage(ChatColor.RED + "You do not have permission!");
                    }
                }
            }
     
            return true;
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
            ItemMeta bookMeta = book.getItemMeta();
            bookMeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds");
            book.setItemMeta(bookMeta);
            ArrayList<String> book1 = new ArrayList<String>();
            book1.add(ChatColor.RED + "Click this to open the world teleporter!");
            bookMeta.setLore(book1);
            if(e.getPlayer().getInventory().firstEmpty() == -1){
                return;
            } else {
                Player p = (Player) e.getPlayer();
                p.getInventory().setItem(8, new ItemStack(book));
            }
        }
     
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
            ItemMeta bookMeta = book.getItemMeta();
            ArrayList<String> book1 = new ArrayList<String>();
            book1.add(ChatColor.RED + "Click this to open the world teleporter!");
            bookMeta.setLore(book1);
            bookMeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds");
            book.setItemMeta(bookMeta);
     
            if(!(e.getAction() == Action.LEFT_CLICK_AIR) && !(e.getAction() == Action.LEFT_CLICK_BLOCK)) return;
            if(e.getClickedBlock() != new ItemStack(book)) return;
            if (e.getClickedBlock() == new ItemStack(book)){
                openCustomInventory(e.getPlayer());
            }
        }
     
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){
            if(!(e.getInventory().getName().equalsIgnoreCase("Worlds"))) return;
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("PVPArena World")){
                e.setCancelled(true);
                e.getWhoClicked().teleport(new Location(Bukkit.getWorld("world"), 49, 88, -206));
            }
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival World")){
                e.setCancelled(true);
                e.getWhoClicked().teleport(new Location(Bukkit.getWorld("Hub"), 0, 66, 0));
            }
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Minigame World")){
                e.setCancelled(true);
                e.getWhoClicked().teleport(new Location(Bukkit.getWorld("minigames"), -0.5, 66, -6.5));
            }
        }
    }
     
  17. Offline

    PieMan456

    the_merciless
    This is what I have now but the listeners still come up red.
     
  18. Offline

    the_merciless

    What do you mean, post a screenshot
     
  19. Offline

    PieMan456

  20. Offline

    the_merciless

    Updated last post
     
  21. Offline

    PieMan456

    the_merciless
    So if I wanted an onCommand() would I just call the method to do it? Beacuse I want to try both.
     
  22. Offline

    the_merciless

    Not sure i understand, i added a call in your command already so everything should work if you use my updated code
     
  23. Offline

    PieMan456

  24. Offline

    the_merciless

    Where are you using that?
     
  25. Offline

    PieMan456

    the_merciless
    This is what I have:
    Code:java
    1. package me.pieman.custominventory;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.inventory.InventoryClickEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.event.player.PlayerJoinEvent;
    18. import org.bukkit.inventory.Inventory;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.inventory.meta.ItemMeta;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class CustomInventory extends JavaPlugin implements Listener {
    24.  
    25. public void onEnable() {
    26. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    27. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    28. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    29. }
    30.  
    31. public void onDisable() {
    32.  
    33. }
    34.  
    35. public void openCustomInventory() {
    36. Inventory CustomInv = Bukkit.createInventory(null, 9, "Worlds");
    37.  
    38. ItemStack pvp = new ItemStack(Material.DIAMOND_SWORD);
    39. ItemMeta pvpMeta = pvp.getItemMeta();
    40. ArrayList<String> pworld = new ArrayList<String>();
    41. pvpMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD
    42. + "PVPArena World");
    43. pworld.add(ChatColor.DARK_PURPLE
    44. + "Click this to teleport to the PVPArena World!");
    45. pvpMeta.setLore(pworld);
    46. pvp.setItemMeta(pvpMeta);
    47.  
    48. ItemStack survival = new ItemStack(Material.GRASS);
    49. ItemMeta survivalMeta = survival.getItemMeta();
    50. ArrayList<String> sworld = new ArrayList<String>();
    51. survivalMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD
    52. + "Survival World");
    53. sworld.add(ChatColor.DARK_PURPLE
    54. + "Click this to teleport to the Survival World!");
    55. survivalMeta.setLore(sworld);
    56. survival.setItemMeta(survivalMeta);
    57.  
    58. ItemStack mg = new ItemStack(Material.REDSTONE_BLOCK);
    59. ItemMeta mgMeta = mg.getItemMeta();
    60. ArrayList<String> mgworld = new ArrayList<String>();
    61. mgMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD
    62. + "Minigame World");
    63. mgworld.add(ChatColor.DARK_PURPLE
    64. + "Click this to teleport to the Minigame World!");
    65. mgMeta.setLore(mgworld);
    66. mg.setItemMeta(mgMeta);
    67.  
    68. CustomInv.clear();
    69. CustomInv.setItem(1, new ItemStack(pvp));
    70. CustomInv.setItem(4, new ItemStack(mg));
    71. CustomInv.setItem(7, new ItemStack(survival));
    72.  
    73. p.openInventory(CustomInv);
    74.  
    75. p.updateInventory();
    76. }
    77.  
    78. @EventHandler
    79. public void onPlayerJoin(PlayerJoinEvent e) {
    80. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    81. ItemMeta bookMeta = book.getItemMeta();
    82. bookMeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds");
    83. book.setItemMeta(bookMeta);
    84. ArrayList<String> book1 = new ArrayList<String>();
    85. book1.add(ChatColor.RED + "Click this to open the world teleporter!");
    86. bookMeta.setLore(book1);
    87. if (e.getPlayer().getInventory().firstEmpty() == -1) {
    88. return;
    89. } else {
    90. Player p = (Player) e.getPlayer();
    91. p.getInventory().setItem(8, new ItemStack(book));
    92. }
    93. }
    94.  
    95. @EventHandler
    96. public void onPlayerInteract(PlayerInteractEvent e) {
    97. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    98. ItemMeta bookMeta = book.getItemMeta();
    99. ArrayList<String> book1 = new ArrayList<String>();
    100. book1.add(ChatColor.RED + "Click this to open the world teleporter!");
    101. bookMeta.setLore(book1);
    102. bookMeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds");
    103. book.setItemMeta(bookMeta);
    104.  
    105. if (!(e.getAction() == Action.LEFT_CLICK_AIR)
    106. && !(e.getAction() == Action.LEFT_CLICK_BLOCK))
    107. return;
    108. if (e.getClickedBlock() != new ItemStack(book))
    109. return;
    110. if (e.getClickedBlock() == new ItemStack(book)) {
    111. openCustomInventory();
    112. }
    113. }
    114.  
    115. @EventHandler
    116. public void onInventoryClick(InventoryClickEvent e) {
    117. if (!(e.getInventory().getName().equalsIgnoreCase("Worlds")))
    118. return;
    119. if (e.getCurrentItem().getItemMeta().getDisplayName()
    120. .contains("PVPArena World")) {
    121. e.setCancelled(true);
    122. e.getWhoClicked().teleport(
    123. new Location(Bukkit.getWorld("world"), 49, 88, -206));
    124. }
    125. if (e.getCurrentItem().getItemMeta().getDisplayName()
    126. .contains("Survival World")) {
    127. e.setCancelled(true);
    128. e.getWhoClicked().teleport(
    129. new Location(Bukkit.getWorld("Hub"), 0, 66, 0));
    130. }
    131. if (e.getCurrentItem().getItemMeta().getDisplayName()
    132. .contains("Minigame World")) {
    133. e.setCancelled(true);
    134. e.getWhoClicked().teleport(
    135. new Location(Bukkit.getWorld("minigames"), -0.5, 66, -6.5));
    136. }
    137. }
    138. }
    139.  


    When I do p.openInventory(CustomInv); and p.updateInventory(); p comes up red so what would I make p?
     
  26. Offline

    the_merciless

    Dont change anything, just copy and paste the code i provided you

    The p come up red because you changed:
    Code:
    public void openCustomInventory(Player p){
    to
    Code:
    public void openCustomInventory(){
    You have to leave the Player p in the brackets to show that you are specifying a player to use when you call the method.

    Notice in your onCommand it now shows:
    Code:
    if(p.hasPermission("ci.open")){
                       
                        openCustomInventory(p);
                       
                    } else 
    the p is the player that you will be specifying to use in the openCustomInventory()
     
  27. Offline

    PieMan456

    the_merciless
    Ok so I used what you have but it won't open on a command or the right click of a book. And also the book does not have the lore i set it to have. Also it will not give the custom book to the player when they join.
    Code:java
    1. package me.pieman.custominventory;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.inventory.InventoryClickEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.event.player.PlayerJoinEvent;
    18. import org.bukkit.inventory.Inventory;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.inventory.meta.ItemMeta;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class CustomInventory extends JavaPlugin implements Listener {
    24.  
    25. public void onEnable() {
    26. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    27. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    28. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    29. }
    30.  
    31. public void onDisable() {
    32.  
    33. }
    34.  
    35. @SuppressWarnings("deprecation")
    36. public void openCustomInventory(Player p) {
    37. Inventory CustomInv = Bukkit.createInventory(null, 9, "Worlds");
    38.  
    39. ItemStack pvp = new ItemStack(Material.DIAMOND_SWORD);
    40. ItemMeta pvpMeta = pvp.getItemMeta();
    41. ArrayList<String> pworld = new ArrayList<String>();
    42. pvpMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD
    43. + "PVPArena World");
    44. pworld.add(ChatColor.DARK_PURPLE
    45. + "Click this to teleport to the PVPArena World!");
    46. pvpMeta.setLore(pworld);
    47. pvp.setItemMeta(pvpMeta);
    48.  
    49. ItemStack survival = new ItemStack(Material.GRASS);
    50. ItemMeta survivalMeta = survival.getItemMeta();
    51. ArrayList<String> sworld = new ArrayList<String>();
    52. survivalMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD
    53. + "Survival World");
    54. sworld.add(ChatColor.DARK_PURPLE
    55. + "Click this to teleport to the Survival World!");
    56. survivalMeta.setLore(sworld);
    57. survival.setItemMeta(survivalMeta);
    58.  
    59. ItemStack mg = new ItemStack(Material.REDSTONE_BLOCK);
    60. ItemMeta mgMeta = mg.getItemMeta();
    61. ArrayList<String> mgworld = new ArrayList<String>();
    62. mgMeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD
    63. + "Minigame World");
    64. mgworld.add(ChatColor.DARK_PURPLE
    65. + "Click this to teleport to the Minigame World!");
    66. mgMeta.setLore(mgworld);
    67. mg.setItemMeta(mgMeta);
    68.  
    69. CustomInv.clear();
    70. CustomInv.setItem(1, new ItemStack(pvp));
    71. CustomInv.setItem(4, new ItemStack(mg));
    72. CustomInv.setItem(7, new ItemStack(survival));
    73.  
    74. p.openInventory(CustomInv);
    75.  
    76. p.updateInventory();
    77. }
    78.  
    79. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    80. if(!(sender instanceof Player)) return true;
    81. Player p = (Player) sender;
    82. if(label.equalsIgnoreCase("world")){
    83. if(p.hasPermission("ci.open")){
    84. openCustomInventory(p);
    85. }
    86. }
    87.  
    88. return true;
    89. }
    90.  
    91. @EventHandler
    92. public void onPlayerJoin(PlayerJoinEvent e) {
    93. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    94. ItemMeta bookMeta = book.getItemMeta();
    95. bookMeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds");
    96. book.setItemMeta(bookMeta);
    97. ArrayList<String> book1 = new ArrayList<String>();
    98. book1.add(ChatColor.RED + "Click this to open the world teleporter!");
    99. bookMeta.setLore(book1);
    100. if (e.getPlayer().getInventory().firstEmpty() == -1) {
    101. return;
    102. } else {
    103. Player p = (Player) e.getPlayer();
    104. p.getInventory().setItem(8, new ItemStack(book));
    105. }
    106. }
    107.  
    108. @EventHandler
    109. public void onPlayerInteract(PlayerInteractEvent e) {
    110. if (!(e.getAction() == Action.LEFT_CLICK_AIR)
    111. && !(e.getAction() == Action.LEFT_CLICK_BLOCK))
    112. return;
    113. if (e.getClickedBlock().hasMetadata("Worlds"))
    114. return;
    115. if (e.getClickedBlock().hasMetadata("Worlds")) {
    116. openCustomInventory(e.getPlayer());
    117. }
    118. }
    119.  
    120. @EventHandler
    121. public void onInventoryClick(InventoryClickEvent e) {
    122. if (!(e.getInventory().getName().equalsIgnoreCase("Worlds")))
    123. return;
    124. if (e.getCurrentItem().getItemMeta().getDisplayName()
    125. .contains("PVPArena World")) {
    126. e.setCancelled(true);
    127. e.getWhoClicked().teleport(
    128. new Location(Bukkit.getWorld("world"), 49, 88, -206));
    129. }
    130. if (e.getCurrentItem().getItemMeta().getDisplayName()
    131. .contains("Survival World")) {
    132. e.setCancelled(true);
    133. e.getWhoClicked().teleport(
    134. new Location(Bukkit.getWorld("Hub"), 0, 66, 0));
    135. }
    136. if (e.getCurrentItem().getItemMeta().getDisplayName()
    137. .contains("Minigame World")) {
    138. e.setCancelled(true);
    139. e.getWhoClicked().teleport(
    140. new Location(Bukkit.getWorld("minigames"), -0.5, 66, -6.5));
    141. }
    142. }
    143. }
    144.  
     
  28. Offline

    the_merciless

    to fix the book lore move book.setItemMeta(bookMeta); after you set the lore

    Code:
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
         
            ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
            ItemMeta bookMeta = book.getItemMeta();
            bookMeta.setDisplayName(ChatColor.BLUE + "" + ChatColor.BOLD + "Worlds");
            ArrayList<String> book1 = new ArrayList<String>();
            book1.add(ChatColor.RED + "Click this to open the world teleporter!");
            bookMeta.setLore(book1);
            book.setItemMeta(bookMeta);
            if(e.getPlayer().getInventory().firstEmpty() == -1){
                return;
            } else {
                Player p = (Player) e.getPlayer();
                p.getInventory().setItem(8, new ItemStack(book));
            }
         
        }
    Do you get any errors in the console after using the command?
     
  29. Offline

    PieMan456

    the_merciless
    Ok so the book now goes in to the peoples inv but the inventory still does not pop up when you right click it or do the command.

    Edit: Opens on command but not on right click.
     
  30. Offline

    the_merciless

    any errors in the console?

    Try changing
    Code:
    if(label.equalsIgnoreCase("World")){
    to
    Code:
    if(cmd.getName().equalsIgnoreCase("World")){
     
Thread Status:
Not open for further replies.

Share This Page