My Custom Plugin GUI not opening

Discussion in 'Plugin Development' started by LAEliteYT, Feb 28, 2016.

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

    LAEliteYT

    Hi, I trying to get it to open with the command of /ce (/CE). But it does nothing I dont get any errors in console. Here are my three Classes of Code. Btw Atm what you click in the gui will do nothing but close and player a sound and message you. Nothing else.


    Code:
    package planetenchantments;
    
    import java.util.logging.Logger;
    
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import planetenchantments.commands.Gui;
    import planetenchantments.commands.inventory.InventoryClick;
    
    public class main extends JavaPlugin implements  Listener {
      
    
        public void onEnable() {
            PluginDescriptionFile pdf = getDescription();
            Logger logger = getLogger();
            logger.info(pdf.getName() + " has enabled");
          
        }
      
        public void registerCommands() {
            getCommand("ce").setExecutor(new Gui());
        }
      
        public void registerEvents() {
            PluginManager pm = getServer().getPluginManager();
          
            pm.registerEvents(new InventoryClick(), this);
        }
      
        public void onDisable() {
            PluginDescriptionFile pdf = getDescription();
            Logger logger = getLogger();
            logger.info(pdf.getName() + " has disabled");
        }
      
        }
    

    Code:
    package planetenchantments.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Gui implements CommandExecutor {
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] arg3) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Only players can run this command");
                return false;
        }
                Player player = (Player) sender;
              
                Inventory inv = Bukkit.createInventory(null, 9, ChatColor.RED + "Custom Enchants");
              
                ItemStack spawnItem = nameItem(Material.ENCHANTED_BOOK, ChatColor.YELLOW + "Custom Enchant Key");
                inv.setItem(4, spawnItem);
              
                player.openInventory(inv);
              
                return true;
        }
              
                private ItemStack nameItem(ItemStack item, String name) {
                    ItemMeta meta = item.getItemMeta();
                    meta.setDisplayName(name);
                    item.setItemMeta(meta);
                    return item;
        }
                private ItemStack nameItem(Material item, String name) {
                    return nameItem(new ItemStack(item), name);
    
    }
    }
    


    Code:
    package planetenchantments.commands.inventory;
    
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class InventoryClick implements Listener {
      
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Inventory inv = event.getInventory();
            if(inv.getTitle().equals("Custom Enchants"))
                return;
          
            if(!(event.getWhoClicked() instanceof Player))
                return;
    
            Player player = (Player) event.getWhoClicked();
            ItemStack item =  event.getCurrentItem();
            if (item.getType()==Material.ENCHANTED_BOOK) {
            player.giveExpLevels(-30);
            player.sendMessage("It has worked!");
    
            player.getWorld().playEffect(player.getLocation(), Effect.FLAME, 1);
              
            }
          
            event.setCancelled(true);
            player.closeInventory();
        }
    }

    And yes, I Know the yml is correct!
    Please tell whats incorrect so I can fix <3.
     
  2. Offline

    SkyleTyler1337

    @LAEliteYT You forgot to put your register commands method in the onEnable(). :p
     
  3. Offline

    LAEliteYT

    Can you please show me, I'm a bit new to coding sorry, and thanks.
     
  4. Offline

    SkyleTyler1337

    @LAEliteYT
    Code:
        public void onEnable() { 
             // register your commands
             registerCommands();
             // then your listeners the same way
        }
        public void registerCommands() {
            getCommand("ce").setExecutor(new Gui());
        }
     
    Last edited: Feb 28, 2016
  5. Offline

    Zombie_Striker

    @SkyleTyler1337
    Thanks captain spoonfeeder! BTW: You made some mistakes (which is why we don't like spoonfeeding on the forums):
    • You do not need to reference objects you only use once
    • You do not need to log your own plugin. Bukkit does this for you. Now you have two messages saying your plugin is enabled.
    • You are referencing another method that only has one line in it. I shouldn't have to tell you why this is bad.
     
  6. Offline

    SkyleTyler1337

    @Zombie_Striker I'm pretty sure i didn't.

    Honestly for one, if they are new. They wouldn't know how to do anything. How would they know what that meant?...
     
  7. Offline

    Zombie_Striker

    @SkyleTyler1337
    Oh my gosh, I almost forgot about the edit button! Thankfully, you reminded me it still exists.

    If you are going to edit your post, you should add the link to the Bukkit Wiki page which explains how and why you have to register commands. Whenever you can, refer to people to resources or tutorials instead of giving them code without much (if any) information.
     
  8. Offline

    PresentingTulip

    He's basically saying don't give them the code, tell them what to do and let them figure it out.
     
  9. Offline

    teej107

    That's why they should learn Java first and/or then you can link them to the wiki. It gets quite annoying when people use the Bukkit Forums create thread button as their only resource.
     
  10. Offline

    AppleBabies

    @LAEliteYT I'm not going to spoon-feed, but this is formatted in a pretty bad way. A GUI should be given it's own class for organization and maintenance, and inside that class should be your listener and also a show(); method.

    I would try doing that, just look up a tutorial. Formatting is important.
     
    Lightspeed likes this.
Thread Status:
Not open for further replies.

Share This Page