Chest GUI Help

Discussion in 'Plugin Development' started by guitarian, Sep 7, 2013.

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

    guitarian

    I am creating a plugin for my server where you click an emerald to open up a GUI where you can view the ranks. If you click one it closes the chest GUI and tells you the donate link. I can't find errors with the code itself, and yet nothing happens when I click an emerald in-game (The chest GUI fails to show up). The plugin does enable correctly.
    Source (I realize only part of this is relevant but I want to check for all possible errors):
    Code:java
    1. package com.rnmc.chestdonate;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    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.PluginDescriptionFile;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class chestdonate extends JavaPlugin{
    22. public final Logger logger = Logger.getLogger("Minecraft");
    23. public static chestdonate plugin;
    24.  
    25. @Override
    26. public void onDisable() {
    27. PluginDescriptionFile pdfFile = this.getDescription();
    28. this.logger.info(pdfFile.getName() + " has been disabled!");
    29. }
    30.  
    31. @Override
    32. public void onEnable() {
    33. PluginDescriptionFile pdfFile = this.getDescription();
    34. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been enabled!");
    35. }
    36.  
    37. @EventHandler
    38. public void onInteract(PlayerInteractEvent event){
    39. Player p = event.getPlayer();
    40. if(p.hasPermission("cd.open")){
    41. if (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR){
    42. if(p.getItemInHand().getType() == Material.EMERALD) {
    43. Inventory inv = Bukkit.getServer().createInventory(p, 9, "Donate! rn-mc.com/donate");
    44. ItemStack slot0 = new ItemStack(Material.GOLD_NUGGET, 1);
    45. ItemStack slot1 = new ItemStack(Material.GOLD_INGOT, 1);
    46. ItemStack slot2 = new ItemStack(Material.GOLD_BLOCK, 1);
    47. ItemStack slot3 = new ItemStack(Material.BLAZE_ROD, 1);
    48.  
    49. ItemMeta meta0 = slot0.getItemMeta();
    50. ItemMeta meta1 = slot1.getItemMeta();
    51. ItemMeta meta2 = slot2.getItemMeta();
    52. ItemMeta meta3 = slot3.getItemMeta();
    53.  
    54.  
    55. meta0.setDisplayName(ChatColor.GREEN + "VIP");
    56. meta1.setDisplayName(ChatColor.GREEN + "VIP+");
    57. meta2.setDisplayName(ChatColor.DARK_GREEN + "VIP++");
    58. meta3.setDisplayName(ChatColor.DARK_PURPLE + "Premium");
    59.  
    60. List<String> lore1 = new ArrayList<String>();
    61. lore1.add(ChatColor.WHITE + "Perks:");
    62. lore1.add(ChatColor.WHITE + "A colored prefix");
    63. lore1.add(ChatColor.WHITE + "VIP Classes in games");
    64. lore1.add(ChatColor.WHITE + "/msg");
    65. meta0.setLore(lore1);
    66.  
    67. List<String> lore2 = new ArrayList<String>();
    68. lore2.add(ChatColor.WHITE + "Perks:");
    69. lore2.add(ChatColor.WHITE + "All VIP Perks");
    70. lore2.add(ChatColor.WHITE + "/hat");
    71. lore2.add(ChatColor.WHITE + "/enderchest");
    72. lore2.add(ChatColor.WHITE + "/workbench");
    73. meta1.setLore(lore2);
    74.  
    75. List<String> lore3 = new ArrayList<String>();
    76. lore3.add(ChatColor.GREEN + "Perks:");
    77. lore3.add(ChatColor.GREEN + "All VIP+ Perks");
    78. lore3.add(ChatColor.GREEN + "/repair");
    79. lore3.add(ChatColor.GREEN + "/enchant");
    80. meta2.setLore(lore3);
    81.  
    82. List<String> lore4 = new ArrayList<String>();
    83. lore4.add(ChatColor.LIGHT_PURPLE + "Perks:");
    84. lore4.add(ChatColor.LIGHT_PURPLE + "All VIP++ Perks");
    85. lore4.add(ChatColor.LIGHT_PURPLE + "White Chat Color");
    86. lore4.add(ChatColor.LIGHT_PURPLE + "/fly");
    87. lore4.add(ChatColor.LIGHT_PURPLE + "/afk");
    88. lore4.add(ChatColor.LIGHT_PURPLE + "/tptoggle");
    89. lore4.add(ChatColor.LIGHT_PURPLE + "/speed");
    90. lore4.add(ChatColor.LIGHT_PURPLE + "/kit premium");
    91. lore4.add(ChatColor.LIGHT_PURPLE + "/heal");
    92. meta3.setLore(lore4);
    93.  
    94. slot0.setItemMeta(meta0);
    95. slot1.setItemMeta(meta1);
    96. slot2.setItemMeta(meta2);
    97. slot3.setItemMeta(meta3);
    98.  
    99. inv.setItem(1, slot0);
    100. inv.setItem(3, slot1);
    101. inv.setItem(5, slot2);
    102. inv.setItem(7, slot3);
    103. }
    104. }
    105. }
    106. }
    107. public void inventoryclick(InventoryClickEvent e){
    108. Player p = (Player) e.getWhoClicked();
    109. if (e.getInventory().getTitle().equals("Donate! rn-mc.com/donate")) {
    110. e.setCancelled(true);
    111. p.sendMessage(ChatColor.GREEN + "Donate at rn-mc.com/donate!");
    112. p.closeInventory();
    113.  
    114. }
    115. }
    116. }
     
  2. Code:java
    1. public class Chestdonate extends JavaPlugin implements Listener {
    2. @Override
    3. public void onEnable() {
    4. ...
    5. getServer().getPluginManager().registerEvents(this, this);
    6. }
    7.  
    8. ...
    9.  
    10. }

    You need to register your listener class (in this case your main class - must implement the Listener interface).

    Also you forgot
    Code:java
    1. p.sendInventory(inv);

    By setting the inventory holder to player you don't open that inventory to him I don't think, and actualy I would recommend setting the inventory holder to null since it might have some unexpected results.

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

    guitarian

    CraftedWarrior59 I did what you said and the menu now comes up, but the plugin is not doing what I want when I click on an item. Should I maybe substitute InventoryClickEvent for something else?
     
  4. Offline

    Conlexio



    Excellent tutorial above where i learned how to do this.
     
    PogoStick29 likes this.
  5. InventoryClickEvent is fine, but you forgot to tag it with @EventHandler.
     
  6. Offline

    Mother__

    Code:java
    1.  
    2.  
    3. Change : public class Chestdonate extends JavaPlugin {
    4. to
    5. public class Chestdonate extends JavaPlugin implements Listener {
    6. Interact : p.openInventory(inv);
    7.  
    8. public void onEnable() {
    9. getServer().getPluginManager().registerEvents(this, this);
    10. }
    11.  
    12.  
     
  7. Offline

    guitarian

Thread Status:
Not open for further replies.

Share This Page