Commands not working when Events are being registered

Discussion in 'Spigot Plugin Development' started by zackatack108, Jun 19, 2016.

  1. Okay so I'm having an issue with my plugin where the commands won't work when I have the register events being implemented. When I Comment out the register events in the onEnable() the commands work just fine.

    Main Class
    Code (Text):
    public class Main extends JavaPlugin{
       
        private Main Main = this;

        public void onEnable(){
            getLogger().info(" has been Enabled.");
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
           
            //Listeners
            getServer().getPluginManager().registerEvents(new ShowEditorUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkEditorUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkTypeUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkBaseColorUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkFadeColorUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkEffectUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkDirectionUI(Main), this);
           
            //Commands
            this.getCommand("Show").setExecutor(new Commands(this));
        }
       
        public void onDisable(){
           
        }
    }
    Commands Class
    Code (Text):
    public class Commands implements CommandExecutor {

        public Main plugin;
        public Commands(Main main) {
            this.plugin = main;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {
           
            ShowEditor Editor = new ShowEditor();
            FileConfiguration config = plugin.getConfig();
            ConfigurationSection root = config.getRoot();
           
            if(cmd.getName().equalsIgnoreCase("Show")){
                if(sender instanceof Player){
                   
                    //Add a firework show to the list
                    if(args[0].equalsIgnoreCase("Create") || args[0].equalsIgnoreCase("Add")){
                        if(args.length==2){
                            if(config.contains("Shows." + args[1])){
                                sender.sendMessage(ChatColor.DARK_RED + "A show with that name already exist.");
                            } else {
                                root.set("Shows." + args[1], args[1]);
                                sender.sendMessage(ChatColor.GREEN + "Show " + args[1] + " created.");
                                plugin.saveConfig();
                            }
                        }
                    }
                   
                    //Remove a firework show from the list
                    else if(args[0].equalsIgnoreCase("Remove") || args[0].equalsIgnoreCase("Delete")){
                        if(args.length==2){
                            root.set("Shows." + args[1], null);
                            sender.sendMessage(ChatColor.DARK_RED + "Show " + args[1] + " removed.");
                            plugin.saveConfig();
                        }
                    }
                   
                    //List all firework shows
                    else if(args[0].equalsIgnoreCase("List")){
                        Set<String> showNames = plugin.getConfig().getConfigurationSection("Shows").getKeys(false);
                        String[] allShowNames = new String[showNames.size()];
                        showNames.toArray(allShowNames);
                       
                        String message = new String();
                        for(String shows : allShowNames){
                            message += shows + "\n";
                        }
                        sender.sendMessage(ChatColor.GOLD + "Shows that exist:");
                        sender.sendMessage(ChatColor.GOLD + message);
                    }
                   
                    //Edit a show that exist
                    else if(args[0].equalsIgnoreCase("Edit")){
                        if(args.length==2){
                            if(config.contains("Shows." + args[1])){
                                sender.sendMessage(ChatColor.GOLD + "You are now editing " + args[1]);
                                Editor.showEditorGUI(sender);
                            }
                        }
                    }
                   
                    //Help
                    else if(args[0].equalsIgnoreCase("Help")){
                        sender.sendMessage(ChatColor.GOLD + "/show help" + ChatColor.WHITE + ": Display all firework show commands.");
                        sender.sendMessage(ChatColor.GOLD + "/show create" + ChatColor.WHITE + ": Create a firework show.");
                        sender.sendMessage(ChatColor.GOLD + "/show remove" + ChatColor.WHITE + ": Remove a firework show.");
                        sender.sendMessage(ChatColor.GOLD + "/show edit" + ChatColor.WHITE + ": Edit a firework show.");
                        sender.sendMessage(ChatColor.GOLD + "/show list" + ChatColor.WHITE + ": Display all firework shows.");
                    }
                }
            }
           
            return false;
        }

    }
    One of the Listeners Classes
    Code (Text):
    public class ShowEditorUI implements Listener {
       
        private FireworkEditor Editor = new FireworkEditor();
        public Main plugin;

        public ShowEditorUI(Main plugin) {
            this.plugin = plugin;
        }
       
        FileConfiguration config = plugin.getConfig();
        ConfigurationSection root = config.getRoot();
       
        //Show Editor
        @EventHandler
        public void showEditor(InventoryClickEvent e){
           
            Player player = (Player) e.getWhoClicked();
           
            if(e.getInventory().getTitle().equals(ChatColor.DARK_PURPLE + "Show Editor")){
               
                switch(e.getSlot()){
                case 0:
                    e.setCancelled(true);
                    break;
                case 4:
                    e.setCancelled(true);
                    Editor.fwEditorGUI(player);
                    break;
                case 8:
                    e.setCancelled(true);
                    break;
                }
            }
        }

    }
     
  2. why are you making an instance object
    Code (Text):
    private Main Main = this;
    if you're just going to pass the class in constructors? you can just put "this".

    and what's the point of this
    Code (Text):
     ConfigurationSection root = config.getRoot();
    you're basically just creating a second object for the same thing and using them interchangeably..

    Code (Text):
            getServer().getPluginManager().registerEvents(new ShowEditorUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkEditorUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkTypeUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkBaseColorUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkFadeColorUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkEffectUI(Main), this);
            getServer().getPluginManager().registerEvents(new FireworkDirectionUI(Main), this);
    everyone loves listeners!

    So anyway, are you getting any errors or anything?? Did you try putting debug messages?
     
  3. So I tried putting player.sendMessaget("it works"); after the if statement for the command. However when I run the command in the game nothing appears in chat or console so I have no idea what's going on.
     
  4. Did you register the command in the plugin.yml? And try above the if statement. Maybe it is returning false?
     
  5. yes I registered the command in the plugin.yml. As I stated in the first post the commands work when the listeners are commented out. When I have the listeners being registered the commands don't work.
     
  6. Does anyone Know what's happening? I really need the commands and the listeners to work