InventoryGUI Issue. :/

Discussion in 'Plugin Development' started by coolguy4744, Apr 22, 2014.

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

    coolguy4744

    I am making a GUI for my KitPvP plugin. It loads up the GUI, but I can move things in it around freely and it does not execute the command or close the inventory when I do.
    Here is the code for it.

    Code:java
    1. public class SimpleKitPvP extends JavaPlugin implements Listener
    2. {
    3. public void onEnable()
    4. {
    5. getServer().getPluginManager().registerEvents(this, this);
    6. getLogger().info("SimpleKitPvP has been enabled. - created by coolguy4744");
    7. }
    8.  
    9. ArrayList<String> kits = new ArrayList();
    10. ArrayList<String> NinjaCooldown = new ArrayList();
    11. ArrayList<String> Ninja = new ArrayList();
    12.  
    13. public void openGUI(Player player) {
    14. Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN
    15. + "Member Kits" );
    16.  
    17. ItemStack swordsman = new ItemStack(Material.DIAMOND_SWORD);
    18. ItemMeta swordsmanMeta = swordsman.getItemMeta();
    19. ItemStack archer = new ItemStack(Material.BOW);
    20. ItemMeta archerMeta = archer.getItemMeta();
    21.  
    22. swordsmanMeta.setDisplayName(ChatColor.BLUE
    23. + "Swordsman");
    24. swordsman.setItemMeta(swordsmanMeta);
    25.  
    26. archerMeta.setDisplayName(ChatColor.BLUE
    27. + "Archer");
    28. archer.setItemMeta(archerMeta);
    29.  
    30. inv.setItem(1, swordsman);
    31. inv.setItem(2, archer);
    32.  
    33. player.openInventory(inv);
    34. }
    35.  
    36.  
    37. @EventHandler
    38. public void onPlayerJoin(PlayerJoinEvent event) {
    39. event.getPlayer().getInventory()
    40. .addItem(new ItemStack(Material.BEDROCK));
    41. }
    42. @EventHandler
    43. public void onInventoryClick(InventoryClickEvent event) {
    44. if(ChatColor.stripColor(event.getInventory().getName())
    45. .equalsIgnoreCase("Member Kits"))
    46. return;
    47. Player player = (Player) event.getWhoClicked();
    48. event.setCancelled(true);
    49.  
    50. if (event.getCurrentItem() == null
    51. || event.getCurrentItem().getType() == Material.AIR
    52. || !event.getCurrentItem().hasItemMeta()) {
    53. player.closeInventory();
    54. return;
    55. }
    56. switch (event.getCurrentItem().getType()) {
    57. case DIAMOND_SWORD:
    58. player.performCommand("Swordsman");
    59. player.closeInventory();
    60. break;
    61. case BOW:
    62. player.performCommand("Archer");
    63. player.closeInventory();
    64. break;
    65. default:
    66. player.closeInventory();
    67. break;
    68. }
    69. }
    70.  
    71. @EventHandler
    72. public void onPlayerInteract(PlayerInteractEvent event) {
    73. Action a = event.getAction();
    74. ItemStack is = event.getItem();
    75.  
    76. if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
    77. return;
    78.  
    79. if (is.getType() == Material.BEDROCK)
    80. openGUI(event.getPlayer());
    81. }


    Please help, also I get errors and it does not work when I add 2 ChatColors. I would like to do
    Code:java
    1. ChatColor.BLUE + ChatColor.BOLD + "Text");
     
  2. Sorry on phone but in inventory click event you wrote if the inventory name was the name you wanted to return... Remove that portion of the code
     
  3. Offline

    michael566

    For that chatcolor thing I would do:
    Code:java
    1. p.sendmessage(Chatcolor.BOLD + "" + Chatcolor.BLUE + "Text")
     
  4. Offline

    coolguy4744

  5. Offline

    chikenlitle99

  6. Now remove event.setCanceled(true);
    And place it before each break;
    Because you are canceling the event before the code checks what item you clicked

    Never do chat color like that...

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

    coolguy4744

  8. Paste your class here because from what I see you didn't even make the commands yet so obviously it won't do anything, although it should close the inventory. Paste class please
     
  9. Offline

    coolguy4744

    WD_MUFFINFLUFFER
    I did make the commands, I only showed the GUI. And even I didn't then it would say unknown command but this one doesn't even close the inventory after.

    Hmmm, after testing it is not reading this.
    Code:java
    1. }
    2. switch (event.getCurrentItem().getType()) {
    3. case Swordsman:
    4. player.performCommand("Swordsman");
    5. player.closeInventory();
    6. event.setCancelled(true);
    7. break;
    8. case BOW:
    9. player.performCommand("Archer");
    10. player.closeInventory();
    11. event.setCancelled(true);
    12. break;
    13. default:
    14. player.closeInventory();
    15. event.setCancelled(true);
    16. break;
    17. }
    18. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Please paste your class
     
  11. Offline

    coolguy4744

    WD_MUFFINFLUFFER
    Oh right sorry. :p
    Code:java
    1. package com.coolguy4744.SimpleKitPvP;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    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.inventory.InventoryClickEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.event.player.PlayerJoinEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class InvGUI extends JavaPlugin implements Listener
    19. {
    20. public void onEnable()
    21. {
    22. getServer().getPluginManager().registerEvents(this, this);
    23. }
    24.  
    25. public void openGUI(Player player) {
    26. Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "" + ChatColor.BOLD
    27. + "Member Kits" );
    28.  
    29. ItemStack swordsman = new ItemStack(Material.DIAMOND_SWORD);
    30. ItemMeta swordsmanMeta = swordsman.getItemMeta();
    31. ItemStack archer = new ItemStack(Material.BOW);
    32. ItemMeta archerMeta = archer.getItemMeta();
    33.  
    34. swordsmanMeta.setDisplayName(ChatColor.BLUE
    35. + "Swordsman");
    36. swordsman.setItemMeta(swordsmanMeta);
    37.  
    38. archerMeta.setDisplayName(ChatColor.BLUE
    39. + "Archer");
    40. archer.setItemMeta(archerMeta);
    41.  
    42. inv.setItem(1, swordsman);
    43. inv.setItem(2, archer);
    44.  
    45. player.openInventory(inv);
    46. }
    47.  
    48.  
    49. @EventHandler
    50. public void onPlayerJoin(PlayerJoinEvent event) {
    51. event.getPlayer().getInventory()
    52. .addItem(new ItemStack(Material.BEDROCK));
    53. }
    54. @EventHandler
    55. public void onInventoryClick(InventoryClickEvent event) {
    56. if(ChatColor.stripColor(event.getInventory().getName())
    57. .equalsIgnoreCase("Member Kits"))
    58. return;
    59. Player player = (Player) event.getWhoClicked();
    60.  
    61.  
    62. if (event.getCurrentItem() == null
    63. || event.getCurrentItem().getType() == Material.AIR
    64. || !event.getCurrentItem().hasItemMeta()) {
    65. player.closeInventory();
    66. return;
    67. }
    68. switch (event.getCurrentItem().getType()) {
    69. case DIAMOND_SWORD:
    70. player.performCommand("Swordsman");
    71. player.closeInventory();
    72. event.setCancelled(true);
    73. break;
    74. case BOW:
    75. player.performCommand("Archer");
    76. player.closeInventory();
    77. event.setCancelled(true);
    78. break;
    79. default:
    80. player.closeInventory();
    81. event.setCancelled(true);
    82. break;
    83. }
    84. }
    85.  
    86. @EventHandler
    87. public void onPlayerInteract(PlayerInteractEvent event) {
    88. Action a = event.getAction();
    89. ItemStack is = event.getItem();
    90.  
    91. if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
    92. return;
    93.  
    94. if (is.getType() == Material.BEDROCK)
    95. openGUI(event.getPlayer());
    96. }
    97. }
    98.  
    99.  
     
  12. Offline

    chikenlitle99

    @WD_MUFFINFLUFFER
    You've never used?, Or should I not use the?, Work! and reduces your code much
     
  13. Offline

    coolguy4744

    TigerHix likes this.
  14. Offline

    xTigerRebornx

    coolguy4744
    Code:
    if(ChatColor.stripColor(event.getInventory().getName())
                  .equalsIgnoreCase("Member Kits"))
              return;
    You are returning if the inventory is "Member Kits", You should be returning if it is not "Member Kits"
     
    TigerHix likes this.
  15. Offline

    chikenlitle99

    I mean about the color code
     
  16. Offline

    coolguy4744

    Oh right. :p

    xTigerRebornx
    What do you mean? Like should I just remove it?

    xTigerRebornx
    Yes it worked! Thank you so much. ^.^ <3

    Thanks everyone! :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  17. Dude I told you that like 5 posts ago..

    coolguy4744

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

    coolguy4744

    WD_MUFFINFLUFFER
    Ah sorry, I got a bit confused. :p I know that I am very slow so I thank you for your patience. The person who knows this the most is, WeeziMonkey. XD Thanks guys!
     
Thread Status:
Not open for further replies.

Share This Page