How can I make a chest GUI and open it with a certain item

Discussion in 'Plugin Development' started by JellyYods, Apr 5, 2014.

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

    JellyYods

    Okay this is my very first plugin and I want a chest gui with items in it, but it not opening can you help me and what should the plugin.yml be I need help with the main thats all i tried everything and i keep getting an error
    Code:java
    1. package me.JellyYods.JKits;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    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.event.player.PlayerJoinEvent;
    16. import org.bukkit.inventory.Inventory;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.ItemMeta;
    19. import org.bukkit.plugin.PluginDescriptionFile;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class JKits extends JavaPlugin implements Listener {
    23. public final Logger logger = Logger.getLogger("Minecraft");
    24. public static JKits plugin;
    25.  
    26. public void onDisable() {
    27. PluginDescriptionFile pdfFile = this.getDescription();
    28. this.logger.info(pdfFile.getName() + " Has Been Disabled");
    29. }
    30.  
    31. public void onEnable() {
    32. PluginDescriptionFile pdfFile = this.getDescription();
    33. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
    34.  
    35. }
    36.  
    37. private void openGUI(Player player) {
    38. Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GRAY
    39. + "Kits");
    40.  
    41. ItemStack pvp = new ItemStack(Material.DIAMOND_SWORD);
    42. ItemMeta pvpMeta = pvp.getItemMeta();
    43. ItemStack archer = new ItemStack(Material.BOW);
    44. ItemMeta archerMeta = archer.getItemMeta();
    45. ItemStack viper = new ItemStack(Material.SPIDER_EYE);
    46. ItemMeta viperMeta = viper.getItemMeta();
    47.  
    48. pvpMeta.setDisplayName(ChatColor.WHITE + "PvP Kit");
    49. pvp.setItemMeta(pvpMeta);
    50.  
    51. archerMeta.setDisplayName(ChatColor.WHITE + "Archer Kit");
    52. archer.setItemMeta(archerMeta);
    53.  
    54. viperMeta.setDisplayName(ChatColor.WHITE + "Viper Kit");
    55. viper.setItemMeta(viperMeta);
    56.  
    57. inv.setItem(0, pvp);
    58. inv.setItem(1, archer);
    59. inv.setItem(2, viper);
    60.  
    61. player.openInventory(inv);
    62. }
    63.  
    64. public void onPlayerJoin(PlayerJoinEvent event) {
    65. event.getPlayer().getInventory().addItem(new ItemStack(Material.ENCHANTED_BOOK));
    66.  
    67. }
    68.  
    69. public void onPlayerInteract(PlayerInteractEvent event) {
    70. Action a = event.getAction();
    71. ItemStack is = event.getItem();
    72.  
    73. if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
    74. return;
    75.  
    76. if (is.getType() == Material.ENCHANTED_BOOK)
    77. openGUI(event.getPlayer());
    78. }
    79.  
    80. }
     
  2. Offline

    Opacification

  3. Offline

    xepisolonxx

    @JellyYods
    Code:
    static Inventory inv;   
        static{
              inv =  Bukkit.getServer().createInventory(null, (mutiple of 9), ChatColor.DARK_BLUE + "Select your kit");
           
     
             
              inv.setItem(0, item);
              inv.setItem(1, item);
              inv.setItem(2, item);
              inv.setItem(3, item);
     
        }
    
     
  4. Offline

    JellyYods

    Opacification sorry if I spelled your name wrong posted a video and I did and entered the things that it told me too do and I get nothing but errors Can you tell me what to fix please or at least whats wrong thanks

    Nothing happened

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

    murded1

    You need to add player.openInventory(inv);
     
  6. Offline

    amhokies

    Oh, thanks for telling us specifically what errors you're getting. /sarcasm
     
  7. Offline

    DrTURTLE2

    With your first plugin, you should start with something much simpler, such as a basic command.
     
  8. Offline

    coasterman10

    Looking at your code, I notice a number of issues. Fortunately they're easy to fix.
    • You're using Logger.getLogger("Minecraft"). Not critical, but you should be using this.getLogger().
    • There is no need to keep a static instance of a plugin in this case. Again, not critical, but worth noting.
    • You forgot to register the events.
    • The events don't have the @EventHandler annotation.
    After fixing these errors the plugin worked perfectly for me. Here's a quick overview:

    First, you need to register your events. The plugin class is the listener, so both the plugin and listener are "this". The following code in your onEnable tells Bukkit to call your event handler methods on their respective events:
    Code:java
    1. @Override
    2. public void onEnable() {
    3. getServer().getPluginManager().registerEvents(this, this);
    4. // ...
    5. }


    Now, you have your events typed out correctly, except that you don't have the @EventHandler annotation. I'm not an expert on how annotations actually work, but if I remember correctly, when you register events, Bukkit checks for the annotation and reads any parameters from it, as well as register the event:
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent event) {
    3. // ...
    4. }


    Once you make these changes, your plugin should work.
     
Thread Status:
Not open for further replies.

Share This Page