Core Issues

Discussion in 'Plugin Development' started by GodzillaFlame42, Jul 13, 2016.

Thread Status:
Not open for further replies.
  1. Hi again, i am having errors in my Core.java and i cant figure out why. There is an error over all the .setExecutor highlighted in red.
    getCommand("Clear").setExecutor(new Inventory.Clear());
    getCommand("CI").setExecutor(new Inventory.Clear());
    getCommand("ClearInventory").setExecutor(new Inventory.Clear());
    Code:
    package me.godzilla.Main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin {
        public void onEnable() {
            new Plugin(this);
          
            getCommand("Sethome").setExecutor(new Home.Sethome());
            getCommand("Delhome").setExecutor(new Home.Delhome());
            getCommand("Home").setExecutor(new Home.Home());
          
            getCommand("Clear").setExecutor(new Inventory.Clear());
            getCommand("CI").setExecutor(new Inventory.Clear());
            getCommand("ClearInventory").setExecutor(new Inventory.Clear());
          
            saveConfig();
          
            Bukkit.getConsoleSender().sendMessage(ChatColor.GRAY + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "             Core Enabled            ");
            Bukkit.getConsoleSender().sendMessage(ChatColor.GRAY + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
          
          
        }
    
    }
     
  2. Alright, let's get started here. First of all, you don't need to register multiple commands that will run the same thing. You can put "aliases: [ci, clearinventory]". Second, the setExecutor method takes a class as it's parameters. So if Inventory.Clear() doesn't return a Class object that's wrong. Also, follow convention please. That means using camelCase for methods variables etc. Also, use Bukkit's project format, which is me.<authorname>.<projectname>. Lastly, you don't need to log the enable of your plugin. Bukkit already does this for you.
    EDIT: Woops posted too fast, give me a second to edit this.
     
    Last edited: Jul 13, 2016
  3. Offline

    tylersyme

    So uhh. In all of those .setExecutor() methods, what are you putting in them? They look sort of like nested inner classes or something but it's hard to tell without some context.

    If you are trying to use inner classes you will need to do this
    .setExecutor(new Home().new Sethome()); // Bizarre syntax stuff

    However I suspect that you may instead just be confused as to what you are supposed to do. So with that in mind, let me try to explain (I apologize if you already know this).

    The purpose of the "setExecutor" method is to tell your plugin "Where am I supposed to handle this command?".
    All it needs to be given is the class which will handle its functionality.
    However, not just any old class will be able to do this. In order for a class be acceptable, it has to implement "CommandExecutor". If the class you are trying to use does not implement CommandExecutor, then it will be an invalid class to use.

    For more information, you should watch a good tutorial :)

    Here, let me give you a small example.

    Code:
    public class Core extends JavaPlugin
    {
       
        @Override
        public void onEnable()
        {
            // This isn't entirely perfect because it literally creates a brand new ExecutorClass instance
            // for every single new command, but this is the easiest way to go and probably won't matter anyways.
            getCommand("Sethome").setExececutor(new ExecutorClass());
            getCommand("Delhome").setExececutor(new ExecutorClass());
            getCommand("Home").setExececutor(new ExecutorClass());
        }
       
    }
    
    public class ExecutorClass implement CommandExecutor
    {
        // This method will handle ALL of your commands at once!
    
        @Override
        public boolean onCommand(CommandSender sender1, 
                                Command cmd, 
                                String label, 
                                String[] args) 
        {
            // Check to see what the command's name is
            if (cmd.getName().equalsIgnoreCase("Sethome")) // If the command's name is "Sethome"
            {
                // Code here
            } else if (cmd.getName().equalsIgnoreCase("Delhome")) // Otherwise if the name is "Delhome"
            {
                // Code here
            } else if (cmd.getName().equalsIgnoreCase("Home")) // Otherwise if the name is "Home" etc.
            {
                // Code here
            }
        }
    }
    
     
    Last edited by a moderator: Jul 13, 2016
Thread Status:
Not open for further replies.

Share This Page