Solved GUI having issues

Discussion in 'Plugin Development' started by CodePlaysMC, May 17, 2014.

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

    CodePlaysMC

    So recently I've been coding a new GUI plugin for someone, and there's an issue that I just can't seem to get around... On the player join, they get the Watch, but when they right-click another item (ex. Diamond Sword) they get the GUI.

    Here's my Main class:
    Code:java
    1. package me.CodePlaysMC.TeamCubedGUI;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.inventory.InventoryClickEvent;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.event.player.PlayerJoinEvent;
    17. import org.bukkit.event.player.PlayerQuitEvent;
    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 Main extends JavaPlugin implements Listener {
    24.  
    25. public void onEnable() {
    26. getLogger().info("TeamCubedGUI has been enabled.");
    27. getServer().getPluginManager().registerEvents(this, this);
    28. getCommand("tcgui").setExecutor(new PlayerCommands());
    29. }
    30.  
    31. private void teleportInWorld(Player player, int x, int y, int z) {
    32. player.teleport(new Location(player.getWorld(), x, y, z));
    33. }
    34.  
    35. private void openGUI(Player player) {
    36. Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_GREEN
    37. + "TeamCubed MiniGames");
    38.  
    39. ItemStack factions = new ItemStack(Material.IRON_SWORD);
    40. ItemMeta factionsMeta = factions.getItemMeta();
    41. ItemStack survival = new ItemStack(Material.WORKBENCH);
    42. ItemMeta survivalMeta = factions.getItemMeta();
    43. ItemStack mineZ = new ItemStack(Material.SKULL_ITEM, 1, (short) 2);
    44. ItemMeta mineZMeta = mineZ.getItemMeta();
    45. ItemStack oitc = new ItemStack(Material.BOW);
    46. ItemMeta oitcMeta = oitc.getItemMeta();
    47. ItemStack spleef = new ItemStack(Material.DIAMOND_SPADE);
    48. ItemMeta spleefMeta = spleef.getItemMeta();
    49. ItemStack sg = new ItemStack(Material.DIAMOND_SWORD);
    50. ItemMeta sgMeta = sg.getItemMeta();
    51. ItemStack spawn = new ItemStack(Material.COMPASS);
    52. ItemMeta spawnMeta = spawn.getItemMeta();
    53.  
    54. survivalMeta.setDisplayName(ChatColor.GREEN + "Survival");
    55. survivalMeta.setLore(Arrays.asList(ChatColor.RED
    56. + "Survive In The Wilderness"));
    57. survival.setItemMeta(survivalMeta);
    58.  
    59. factionsMeta.setDisplayName(ChatColor.GREEN + "Factions");
    60. factionsMeta.setLore(Arrays.asList(ChatColor.DARK_AQUA
    61. + "Build Your Team In A Quest For Victory!"));
    62. factions.setItemMeta(factionsMeta);
    63.  
    64. mineZMeta.setDisplayName(ChatColor.GREEN + "Mine" + ChatColor.DARK_RED
    65. + "Z");
    66. mineZMeta.setLore(Arrays.asList(ChatColor.DARK_GREEN + "Coming Soon!"));
    67. mineZ.setItemMeta(mineZMeta);
    68.  
    69. oitcMeta.setDisplayName(ChatColor.GREEN + "One In The Chamber");
    70. oitcMeta.setLore(Arrays.asList(ChatColor.DARK_RED + "Kill Everyone!!!"));
    71. oitc.setItemMeta(oitcMeta);
    72.  
    73. spleefMeta.setDisplayName(ChatColor.GREEN + "Spleef");
    74. spleefMeta
    75. .setLore(Arrays.asList(ChatColor.DARK_GREEN + "Coming Soon!"));
    76. spleef.setItemMeta(spleefMeta);
    77.  
    78. sgMeta.setDisplayName(ChatColor.GREEN + "Survival Games");
    79. sgMeta.setLore(Arrays.asList(ChatColor.DARK_GREEN + "Coming Soon!"));
    80. sg.setItemMeta(sgMeta);
    81.  
    82. spawnMeta.setDisplayName(ChatColor.GREEN + "Hub");
    83. spawnMeta.setLore(Arrays.asList(ChatColor.AQUA + "Teleport To The Hub"));
    84. spawn.setItemMeta(spawnMeta);
    85.  
    86. inv.setItem(3, factions);
    87. inv.setItem(5, survival);
    88. inv.setItem(4, mineZ);
    89. inv.setItem(2, oitc);
    90. inv.setItem(6, spleef);
    91. inv.setItem(7, sg);
    92. inv.setItem(0, spawn);
    93.  
    94. player.openInventory(inv);
    95. }
    96.  
    97. @EventHandler
    98. public void onInventoryClick(InventoryClickEvent event) {
    99. if (!ChatColor.stripColor(event.getInventory().getName())
    100. .equalsIgnoreCase("TeamCubed MiniGames"))
    101. return;
    102.  
    103. Player player = (Player) event.getWhoClicked();
    104. event.setCancelled(true);
    105.  
    106. if (event.getCurrentItem() == null
    107. || event.getCurrentItem().getType() == Material.AIR
    108. || !event.getCurrentItem().hasItemMeta()) {
    109. player.closeInventory();
    110. return;
    111. }
    112.  
    113. switch (event.getCurrentItem().getType()) {
    114. case IRON_SWORD:
    115. teleportInWorld(player, 70, 50, 20);
    116. player.closeInventory();
    117. player.sendMessage(ChatColor.GREEN + "MiniGame"
    118. + ChatColor.DARK_GRAY + " >> " + ChatColor.GRAY
    119. + "You have been teleported to " + ChatColor.DARK_AQUA
    120. + "Factions" + ChatColor.GRAY + ".");
    121. break;
    122. case DIAMOND_SPADE:
    123. player.closeInventory();
    124. player.sendMessage(ChatColor.GREEN + "MiniGame"
    125. + ChatColor.DARK_GRAY + " >> " + ChatColor.GRAY
    126. + "This minigame is still in development.");
    127. break;
    128. case WORKBENCH:
    129. player.performCommand("warp Survival");
    130. player.closeInventory();
    131. player.sendMessage(ChatColor.GREEN + "MiniGame"
    132. + ChatColor.DARK_GRAY + " >> " + ChatColor.GRAY
    133. + "You have been teleported to " + ChatColor.DARK_AQUA
    134. + "Survival" + ChatColor.GRAY + ".");
    135. break;
    136. case SKULL_ITEM:
    137. player.closeInventory();
    138. player.sendMessage(ChatColor.GREEN + "MiniGame"
    139. + ChatColor.DARK_GRAY + " >> " + ChatColor.GRAY
    140. + "This minigame is still in development.");
    141. break;
    142. case BOW:
    143. player.performCommand("warp OITC");
    144. player.closeInventory();
    145. player.sendMessage(ChatColor.GREEN + "MiniGame"
    146. + ChatColor.DARK_GRAY + " >> " + ChatColor.GRAY
    147. + "You have been teleported to " + ChatColor.DARK_AQUA
    148. + "OITC" + ChatColor.GRAY + ".");
    149. break;
    150. case DIAMOND_SWORD:
    151. player.closeInventory();
    152. player.sendMessage(ChatColor.GREEN + "MiniGame"
    153. + ChatColor.DARK_GRAY + " >> " + ChatColor.GRAY
    154. + "This minigame is still in development.");
    155. break;
    156. case COMPASS:
    157. player.performCommand("spawn");
    158. player.closeInventory();
    159. player.sendMessage(ChatColor.GREEN + "Hub" + ChatColor.DARK_GRAY
    160. + " >> " + ChatColor.GRAY
    161. + "You have been teleported to the Hub.");
    162. break;
    163. default:
    164. player.closeInventory();
    165. break;
    166. }
    167. }
    168.  
    169. @EventHandler
    170. public void clearInventory(PlayerQuitEvent event) {
    171. event.getPlayer().getInventory().clear();
    172. }
    173.  
    174. @EventHandler
    175. public void onPlayerJoin(PlayerJoinEvent event) {
    176. ItemStack gui = new ItemStack(Material.WATCH);
    177. ItemMeta guiMeta = gui.getItemMeta();
    178. ArrayList<String> name = new ArrayList<String>();
    179. guiMeta.setDisplayName(ChatColor.DARK_GREEN + "MiniGame Selector");
    180. name.add(ChatColor.GREEN + "Select A MiniGame");
    181. guiMeta.setLore(name);
    182. gui.setItemMeta(guiMeta);
    183.  
    184. event.getPlayer().getInventory().addItem(new ItemStack(gui));
    185. }
    186.  
    187. @EventHandler
    188. public void onPlayerInteract(PlayerInteractEvent event) {
    189. Action a = event.getAction();
    190. ItemStack is = event.getItem();
    191.  
    192. if (is.getType() == Material.WATCH);
    193. openGUI(event.getPlayer());
    194.  
    195. if (a == Action.RIGHT_CLICK_BLOCK || is == null || is.getType() == Material.AIR);
    196. return;
    197. }
    198.  
    199. public void onDisable() {
    200. getLogger().info("TeamCubedGUI has been disabled.");
    201. }
    202. }
     
  2. Offline

    Xyplo

    Best way to do this is to give the Watch a ItemMeta and when they right click to check if that Watch has the same ItemMeta if it does to open the inventory, if it doesn't, then simply don't open it :)

    CodePlaysMC For example in the PlayerInteractEvent change it to this...
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. Action a = event.getAction();
    4. ItemStack is = event.getItem();
    5.  
    6. if (is.getType() == Material.WATCH);
    7. if(event.getPlayer().getItemInHand().hasItemMeta(guiMeta){
    8. openGUI(event.getPlayer());
    9. } else {//NOTHING IN HERE; you don't even need an else here...}
    10.  
    11. if (a == Action.RIGHT_CLICK_BLOCK || is == null || is.getType() == Material.AIR);
    12. return;
    13. }


    This seemed to work for me...

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

    Commander9292

    CodePlaysMC
    By the way your code could be so much more efficient and easier to look at.
    You could use:
    Code:
    public ItemStack createItem(Material material, int amount, short data, String name, List<String> lore) {
            ItemStack i = new ItemStack(material, amount, data);
            ItemMeta im = i.getItemMeta();
     
            if(name != null) {
                im.setDisplayName(name);
            }
     
            if(lore != null) {
                im.setLore(lore);
            }
     
            i.setItemMeta(im);
            return i;
        }
    Then you would use:
    Code:
    ItemStack itemStackName = createItem(Material, amount, data, name, lore);
    You can then add that item to the player's inventory like you normally would.
     
  4. Offline

    Azubuso

    CodePlaysMC Can entirely change your InteractEvent to something smaller:
    PHP:
    @EventHandler
        
    public void onPlayerInteract(PlayerInteractEvent event) {
            if (
    event.getPlayer().getItemInHand().getType() == Material.WATCH && event.getAction() == Action.RIGHT_CLICK_AIR) {
                
    openGUI(event.getPlayer());
            }
        }
     
  5. Offline

    CodePlaysMC

    Xyplo
    Your code seemed fine, but in
    Code:
    if(event.getPlayer().getItemInHand().hasItemMeta(guiMeta)) {
                openGUI(event.getPlayer());
            }
    The .hasItemMeta(guiMeta) has an error:
    "guiMeta cannot be resolved to a variable"
    So I'm guessing that the variable guiMeta is not reachable by the public void onPlayerJoin.

    What should I do to solve this?
     
  6. Offline

    Xyplo

    CodePlaysMC in the PlayerJoinEvent put this instead...
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent event) {
    3. ItemStack gui = new ItemStack(Material.WATCH);
    4. public ItemMeta guiMeta = gui.getItemMeta();
    5. ArrayList<String> name = new ArrayList<String>();
    6. guiMeta.setDisplayName(ChatColor.DARK_GREEN + "MiniGame Selector");
    7. name.add(ChatColor.GREEN + "Select A MiniGame");
    8. guiMeta.setLore(name);
    9. gui.setItemMeta(guiMeta);
    10.  
    11. event.getPlayer().getInventory().addItem(new ItemStack(gui));
    12. }


    I believe this should work. I don't know if it will as I've not tested it out.
     
  7. Offline

    DxDy

    This will not work. That'll just produce a compiler-error.

    For the actual solution see http://forums.bukkit.org/threads/itemmeta-problems.269072/
    That is an exact cross-post of his problem here.
     
  8. Offline

    CodePlaysMC

    Well there has to be a solution to this, for it has been done by other plugin developers.
     
  9. Offline

    xTigerRebornx

    CodePlaysMC You are never opening your if statement when checking for the ItemStack's Material in your PlayerInteractEvent, so the openGui function is always executed
    EDIT:
    Code:
     if (is.getType() == Material.WATCH); // HERE
            openGUI(event.getPlayer());
     
  10. Offline

    CodePlaysMC

    xTigerRebornx
    Oh my gosh, thanks so much. I hadn't realized I had put that there. Clumsy me.
     
  11. Offline

    Azubuso

    :'( Nobody even noticed my response...
     
  12. Offline

    Commander9292

  13. Offline

    CodePlaysMC

Thread Status:
Not open for further replies.

Share This Page